View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.io;
29  
30  import org.apache.http.impl.SessionInputBufferMock;
31  import org.apache.http.io.SessionInputBuffer;
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  /**
36   * Simple tests for {@link IdentityInputStream}.
37   *
38   */
39  public class TestIdentityInputStream {
40  
41      @Test
42      public void testConstructor() throws Exception {
43          final SessionInputBuffer receiver = new SessionInputBufferMock(new byte[] {});
44          final IdentityInputStream in = new IdentityInputStream(receiver);
45          in.close();
46          try {
47              new IdentityInputStream(null);
48              Assert.fail("IllegalArgumentException should have been thrown");
49          } catch (final IllegalArgumentException ex) {
50              //expected
51          }
52      }
53  
54      @Test
55      public void testBasicRead() throws Exception {
56          final byte[] input = new byte[] {'a', 'b', 'c'};
57          final SessionInputBufferMock receiver = new SessionInputBufferMock(input);
58          final IdentityInputStream instream = new IdentityInputStream(receiver);
59          final byte[] tmp = new byte[2];
60          Assert.assertEquals(2, instream.read(tmp, 0, tmp.length));
61          Assert.assertEquals('a', tmp[0]);
62          Assert.assertEquals('b', tmp[1]);
63          Assert.assertEquals('c', instream.read());
64          Assert.assertEquals(-1, instream.read(tmp, 0, tmp.length));
65          Assert.assertEquals(-1, instream.read());
66          Assert.assertEquals(-1, instream.read(tmp, 0, tmp.length));
67          Assert.assertEquals(-1, instream.read());
68          instream.close();
69      }
70  
71      @Test
72      public void testClosedCondition() throws Exception {
73          final byte[] input = new byte[] {'a', 'b', 'c'};
74          final SessionInputBufferMock receiver = new SessionInputBufferMock(input);
75          final IdentityInputStream instream = new IdentityInputStream(receiver);
76  
77          instream.close();
78          instream.close();
79  
80          Assert.assertEquals(0, instream.available());
81          final byte[] tmp = new byte[2];
82          Assert.assertEquals(-1, instream.read(tmp, 0, tmp.length));
83          Assert.assertEquals(-1, instream.read());
84          Assert.assertEquals(-1, instream.read(tmp, 0, tmp.length));
85          Assert.assertEquals(-1, instream.read());
86      }
87  
88      @Test
89      public void testAvailable() throws Exception {
90          final byte[] input = new byte[] {'a', 'b', 'c'};
91          final SessionInputBufferMock receiver = new SessionInputBufferMock(input);
92          final IdentityInputStream instream = new IdentityInputStream(receiver);
93          instream.read();
94          Assert.assertEquals(2, instream.available());
95          instream.close();
96      }
97  
98  }