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 java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.OutputStream;
33
34 import org.apache.http.impl.SessionOutputBufferMock;
35 import org.junit.Assert;
36 import org.junit.Test;
37
38 public class TestIdentityOutputStream {
39
40 @Test
41 public void testConstructors() throws Exception {
42 new IdentityOutputStream(new SessionOutputBufferMock()).close();
43 try {
44 new IdentityOutputStream(null);
45 Assert.fail("IllegalArgumentException should have been thrown");
46 } catch (final IllegalArgumentException ex) {
47
48 }
49 }
50
51 @Test
52 public void testBasics() throws Exception {
53 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
54 final SessionOutputBufferMockessionOutputBufferMock">SessionOutputBufferMock datatransmitter = new SessionOutputBufferMock(buffer);
55 final OutputStream out = new IdentityOutputStream(datatransmitter);
56
57 final byte[] tmp = new byte[10];
58 out.write(tmp, 0, 10);
59 out.write(tmp);
60 out.write(1);
61 out.flush();
62 out.close();
63 final byte[] data = datatransmitter.getData();
64 Assert.assertEquals(21, data.length);
65 }
66
67 @Test
68 public void testClose() throws Exception {
69 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
70 final SessionOutputBufferMockessionOutputBufferMock">SessionOutputBufferMock datatransmitter = new SessionOutputBufferMock(buffer);
71 final OutputStream out = new IdentityOutputStream(datatransmitter);
72 out.close();
73 out.close();
74 final byte[] tmp = new byte[10];
75 try {
76 out.write(tmp);
77 Assert.fail("IOException should have been thrown");
78 } catch (final IOException ex) {
79
80 }
81 try {
82 out.write(1);
83 Assert.fail("IOException should have been thrown");
84 } catch (final IOException ex) {
85
86 }
87 }
88
89 @Test
90 public void testConstructor() throws Exception {
91 final SessionOutputBufferMockml#SessionOutputBufferMock">SessionOutputBufferMock transmitter = new SessionOutputBufferMock();
92 new IdentityOutputStream(transmitter).close();
93 try {
94 new IdentityOutputStream(null);
95 Assert.fail("IllegalArgumentException should have been thrown");
96 } catch (final IllegalArgumentException ex) {
97
98 }
99 }
100
101 @Test
102 public void testBasicWrite() throws Exception {
103 final SessionOutputBufferMockml#SessionOutputBufferMock">SessionOutputBufferMock transmitter = new SessionOutputBufferMock();
104 final IdentityOutputStream outStream = new IdentityOutputStream(transmitter);
105 outStream.write(new byte[] {'a', 'b'}, 0, 2);
106 outStream.write('c');
107 outStream.flush();
108
109 final byte[] input = transmitter.getData();
110
111 Assert.assertNotNull(input);
112 final byte[] expected = new byte[] {'a', 'b', 'c'};
113 Assert.assertEquals(expected.length, input.length);
114 for (int i = 0; i < expected.length; i++) {
115 Assert.assertEquals(expected[i], input[i]);
116 }
117 outStream.close();
118 }
119
120 @Test
121 public void testClosedCondition() throws Exception {
122 final SessionOutputBufferMockml#SessionOutputBufferMock">SessionOutputBufferMock transmitter = new SessionOutputBufferMock();
123 final IdentityOutputStream outStream = new IdentityOutputStream(transmitter);
124 outStream.close();
125 outStream.close();
126
127 try {
128 final byte[] tmp = new byte[2];
129 outStream.write(tmp, 0, tmp.length);
130 Assert.fail("IOException should have been thrown");
131 } catch (final IOException e) {
132
133 }
134 try {
135 outStream.write('a');
136 Assert.fail("IOException should have been thrown");
137 } catch (final IOException e) {
138
139 }
140 }
141
142
143 }
144