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.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
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
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 }