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.jupiter.api.Assertions;
36  import org.junit.jupiter.api.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          Assertions.assertEquals(2, in.read(tmp, 0, tmp.length));
51          Assertions.assertEquals('a', tmp[0]);
52          Assertions.assertEquals('b', tmp[1]);
53          Assertions.assertEquals('c', in.read());
54          Assertions.assertEquals(-1, in.read(tmp, 0, tmp.length));
55          Assertions.assertEquals(-1, in.read());
56          Assertions.assertEquals(-1, in.read(tmp, 0, tmp.length));
57          Assertions.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          Assertions.assertEquals(0, in.available());
72          final byte[] tmp = new byte[2];
73          Assertions.assertThrows(StreamClosedException.class, () -> in.read(tmp, 0, tmp.length));
74          Assertions.assertThrows(StreamClosedException.class, () -> in.read());
75      }
76  
77      @Test
78      public void testAvailable() throws Exception {
79          final byte[] input = new byte[] {'a', 'b', 'c'};
80          final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
81          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(new BasicHttpTransportMetrics(), 16, 16, 1024, null);
82          final IdentityInputStream in = new IdentityInputStream(inBuffer, inputStream);
83          in.read();
84          Assertions.assertEquals(2, in.available());
85          in.close();
86      }
87  
88      @Test
89      public void testAvailableInStream() throws Exception {
90          final byte[] input = new byte[] {'a', 'b', 'c', 'd', 'e', 'f'};
91          final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
92          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(new BasicHttpTransportMetrics(), 16, 0, 1024, null);
93          final IdentityInputStream in = new IdentityInputStream(inBuffer, inputStream);
94          final byte[] tmp = new byte[3];
95          Assertions.assertEquals(3, in.read(tmp));
96          Assertions.assertEquals(3, in.available());
97          in.close();
98      }
99  
100 }