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 TestContentLengthOutputStream {
39
40 @Test
41 public void testConstructors() throws Exception {
42 final ContentLengthOutputStream in = new ContentLengthOutputStream(
43 new SessionOutputBufferMock(), 10L);
44 in.close();
45 try {
46 new ContentLengthOutputStream(null, 10L);
47 Assert.fail("IllegalArgumentException should have been thrown");
48 } catch (final IllegalArgumentException ex) {
49
50 }
51 try {
52 new ContentLengthOutputStream(new SessionOutputBufferMock(), -10);
53 Assert.fail("IllegalArgumentException should have been thrown");
54 } catch (final IllegalArgumentException ex) {
55
56 }
57 }
58
59 @Test
60 public void testBasics() throws Exception {
61 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
62 final SessionOutputBufferMockessionOutputBufferMock">SessionOutputBufferMock datatransmitter = new SessionOutputBufferMock(buffer);
63 final OutputStream out = new ContentLengthOutputStream(datatransmitter, 15L);
64
65 final byte[] tmp = new byte[10];
66 out.write(tmp, 0, 10);
67 out.write(1);
68 out.write(tmp, 0, 10);
69 out.write(tmp, 0, 10);
70 out.write(tmp);
71 out.write(1);
72 out.write(2);
73 out.flush();
74 out.close();
75 final byte[] data = datatransmitter.getData();
76 Assert.assertEquals(15, data.length);
77 }
78
79 @Test
80 public void testClose() throws Exception {
81 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
82 final SessionOutputBufferMockessionOutputBufferMock">SessionOutputBufferMock datatransmitter = new SessionOutputBufferMock(buffer);
83 final OutputStream out = new ContentLengthOutputStream(datatransmitter, 15L);
84 out.close();
85 out.close();
86 final byte[] tmp = new byte[10];
87 try {
88 out.write(tmp);
89 Assert.fail("IOException should have been thrown");
90 } catch (final IOException ex) {
91
92 }
93 try {
94 out.write(1);
95 Assert.fail("IOException should have been thrown");
96 } catch (final IOException ex) {
97
98 }
99 }
100
101 }
102