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.hc.core5.http.impl.io;
29  
30  import java.io.ByteArrayInputStream;
31  
32  import org.apache.hc.core5.http.StreamClosedException;
33  import org.apache.hc.core5.http.impl.BasicHttpTransportMetrics;
34  import org.apache.hc.core5.http.io.SessionInputBuffer;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  /**
39   * Unit tests for {@link IdentityInputStream}.
40   */
41  public class TestIdentityInputStream {
42  
43      @Test
44      public void testBasicRead() throws Exception {
45          final byte[] input = new byte[] {'a', 'b', 'c'};
46          final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
47          final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16);
48          final IdentityInputStream in = new IdentityInputStream(inbuffer, inputStream);
49          final byte[] tmp = new byte[2];
50          Assert.assertEquals(2, in.read(tmp, 0, tmp.length));
51          Assert.assertEquals('a', tmp[0]);
52          Assert.assertEquals('b', tmp[1]);
53          Assert.assertEquals('c', in.read());
54          Assert.assertEquals(-1, in.read(tmp, 0, tmp.length));
55          Assert.assertEquals(-1, in.read());
56          Assert.assertEquals(-1, in.read(tmp, 0, tmp.length));
57          Assert.assertEquals(-1, in.read());
58          in.close();
59      }
60  
61      @Test
62      public void testClosedCondition() throws Exception {
63          final byte[] input = new byte[] {'a', 'b', 'c'};
64          final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
65          final SessionInputBuffer inbuffer = new SessionInputBufferImpl(16);
66          final IdentityInputStream in = new IdentityInputStream(inbuffer, inputStream);
67  
68          in.close();
69          in.close();
70  
71          Assert.assertEquals(0, in.available());
72          final byte[] tmp = new byte[2];
73          try {
74              in.read(tmp, 0, tmp.length);
75              Assert.fail("StreamClosedException expected");
76          } catch (final StreamClosedException expected) {
77          }
78          try {
79              in.read();
80              Assert.fail("StreamClosedException expected");
81          } catch (final StreamClosedException expected) {
82          }
83      }
84  
85      @Test
86      public void testAvailable() throws Exception {
87          final byte[] input = new byte[] {'a', 'b', 'c'};
88          final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
89          final SessionInputBuffer inbuffer = new SessionInputBufferImpl(new BasicHttpTransportMetrics(), 16, 16, 1024, null);
90          final IdentityInputStream in = new IdentityInputStream(inbuffer, inputStream);
91          in.read();
92          Assert.assertEquals(2, in.available());
93          in.close();
94      }
95  
96      @Test
97      public void testAvailableInStream() throws Exception {
98          final byte[] input = new byte[] {'a', 'b', 'c', 'd', 'e', 'f'};
99          final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
100         final SessionInputBuffer inbuffer = new SessionInputBufferImpl(new BasicHttpTransportMetrics(), 16, 0, 1024, null);
101         final IdentityInputStream in = new IdentityInputStream(inbuffer, inputStream);
102         final byte[] tmp = new byte[3];
103         Assert.assertEquals(3, in.read(tmp));
104         Assert.assertEquals(3, in.available());
105         in.close();
106     }
107 
108 }