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.Assert;
36 import org.junit.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 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 }