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.html#SessionInputBufferMock">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.html#SessionInputBufferMock">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.html#SessionInputBufferMock">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  }