1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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 }