~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/test/java/org/apache/http/impl/io/TestIdentitynputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/test/java/org/apache/http/impl/io/TestIdentitynputStream.java $
 
3
 * $Revision: 744517 $
 
4
 * $Date: 2009-02-14 17:39:33 +0100 (Sat, 14 Feb 2009) $
 
5
 * ====================================================================
 
6
 * Licensed to the Apache Software Foundation (ASF) under one
 
7
 * or more contributor license agreements.  See the NOTICE file
 
8
 * distributed with this work for additional information
 
9
 * regarding copyright ownership.  The ASF licenses this file
 
10
 * to you under the Apache License, Version 2.0 (the
 
11
 * "License"); you may not use this file except in compliance
 
12
 * with the License.  You may obtain a copy of the License at
 
13
 *
 
14
 *   http://www.apache.org/licenses/LICENSE-2.0
 
15
 *
 
16
 * Unless required by applicable law or agreed to in writing,
 
17
 * software distributed under the License is distributed on an
 
18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
19
 * KIND, either express or implied.  See the License for the
 
20
 * specific language governing permissions and limitations
 
21
 * under the License.
 
22
 * ====================================================================
 
23
 *
 
24
 * This software consists of voluntary contributions made by many
 
25
 * individuals on behalf of the Apache Software Foundation.  For more
 
26
 * information on the Apache Software Foundation, please see
 
27
 * <http://www.apache.org/>.
 
28
 *
 
29
 */
 
30
 
 
31
package org.apache.http.impl.io;
 
32
 
 
33
import org.apache.http.impl.io.IdentityInputStream;
 
34
import org.apache.http.io.SessionInputBuffer;
 
35
import org.apache.http.mockup.SessionInputBufferMockup;
 
36
 
 
37
import junit.framework.Test;
 
38
import junit.framework.TestCase;
 
39
import junit.framework.TestSuite;
 
40
 
 
41
/**
 
42
 * Simple tests for {@link IdentityInputStream}.
 
43
 *
 
44
 */
 
45
public class TestIdentitynputStream extends TestCase {
 
46
 
 
47
    // ------------------------------------------------------------ Constructor
 
48
    public TestIdentitynputStream(String testName) {
 
49
        super(testName);
 
50
    }
 
51
 
 
52
    // ------------------------------------------------------------------- Main
 
53
    public static void main(String args[]) {
 
54
        String[] testCaseName = { TestIdentitynputStream.class.getName() };
 
55
        junit.textui.TestRunner.main(testCaseName);
 
56
    }
 
57
 
 
58
    // ------------------------------------------------------- TestCase Methods
 
59
 
 
60
    public static Test suite() {
 
61
        return new TestSuite(TestIdentitynputStream.class);
 
62
    }
 
63
 
 
64
    public void testConstructor() throws Exception {
 
65
        SessionInputBuffer receiver = new SessionInputBufferMockup(new byte[] {});
 
66
        new IdentityInputStream(receiver);
 
67
        try {
 
68
            new IdentityInputStream(null);
 
69
            fail("IllegalArgumentException should have been thrown");
 
70
        } catch (IllegalArgumentException ex) {
 
71
            //expected
 
72
        }
 
73
    }
 
74
    
 
75
    public void testBasicRead() throws Exception {
 
76
        byte[] input = new byte[] {'a', 'b', 'c'};
 
77
        SessionInputBufferMockup receiver = new SessionInputBufferMockup(input);
 
78
        IdentityInputStream instream = new IdentityInputStream(receiver);
 
79
        byte[] tmp = new byte[2];
 
80
        assertEquals(2, instream.read(tmp, 0, tmp.length));
 
81
        assertEquals('a', tmp[0]);
 
82
        assertEquals('b', tmp[1]);
 
83
        assertEquals('c', instream.read());
 
84
        assertEquals(-1, instream.read(tmp, 0, tmp.length));
 
85
        assertEquals(-1, instream.read());
 
86
        assertEquals(-1, instream.read(tmp, 0, tmp.length));
 
87
        assertEquals(-1, instream.read());        
 
88
    }
 
89
    
 
90
    public void testClosedCondition() throws Exception {
 
91
        byte[] input = new byte[] {'a', 'b', 'c'};
 
92
        SessionInputBufferMockup receiver = new SessionInputBufferMockup(input);
 
93
        IdentityInputStream instream = new IdentityInputStream(receiver);
 
94
 
 
95
        instream.close();
 
96
        instream.close();
 
97
        
 
98
        assertTrue(instream.available() == 0);
 
99
        byte[] tmp = new byte[2];
 
100
        assertEquals(-1, instream.read(tmp, 0, tmp.length));
 
101
        assertEquals(-1, instream.read());
 
102
        assertEquals(-1, instream.read(tmp, 0, tmp.length));
 
103
        assertEquals(-1, instream.read());        
 
104
    }
 
105
 
 
106
    public void testAvailable() throws Exception {
 
107
        byte[] input = new byte[] {'a', 'b', 'c'};
 
108
        SessionInputBufferMockup receiver = new SessionInputBufferMockup(input);
 
109
        IdentityInputStream instream = new IdentityInputStream(receiver);
 
110
        assertTrue(instream.available() > 0);        
 
111
    }
 
112
    
 
113
}