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.entity;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32
33 import org.apache.http.Consts;
34 import org.junit.Assert;
35 import org.junit.Test;
36
37
38
39
40
41 public class TestBufferedHttpEntity {
42
43 @Test
44 public void testBufferingEntity() throws Exception {
45 final byte[] bytes = "Message content".getBytes(Consts.ASCII);
46 final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1);
47 final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
48 Assert.assertEquals(bytes.length, bufentity.getContentLength());
49 Assert.assertTrue(bufentity.isRepeatable());
50 Assert.assertFalse(bufentity.isChunked());
51 Assert.assertFalse(bufentity.isStreaming());
52
53
54 Assert.assertNotNull(bufentity.getContent ());
55 Assert.assertNotNull(bufentity.getContent ());
56 }
57
58 @Test
59 public void testWrappingEntity() throws Exception {
60 final byte[] bytes = "Message content".getBytes(Consts.ASCII);
61 final ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
62 httpentity.setChunked(true);
63 final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
64 Assert.assertEquals(bytes.length, bufentity.getContentLength());
65 Assert.assertTrue(bufentity.isRepeatable());
66 Assert.assertTrue(bufentity.isChunked());
67 Assert.assertFalse(bufentity.isStreaming());
68
69
70 Assert.assertNotNull(bufentity.getContent ());
71 Assert.assertNotNull(bufentity.getContent ());
72 }
73
74 @Test
75 public void testIllegalConstructor() throws Exception {
76 try {
77 new BufferedHttpEntity(null);
78 Assert.fail("IllegalArgumentException should have been thrown");
79 } catch (final IllegalArgumentException ex) {
80
81 }
82 }
83
84 @Test
85 public void testWriteToBuffered() throws Exception {
86 final byte[] bytes = "Message content".getBytes(Consts.ASCII);
87 final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1);
88 final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
89
90 ByteArrayOutputStream out = new ByteArrayOutputStream();
91 bufentity.writeTo(out);
92 byte[] bytes2 = out.toByteArray();
93 Assert.assertNotNull(bytes2);
94 Assert.assertEquals(bytes.length, bytes2.length);
95 for (int i = 0; i < bytes.length; i++) {
96 Assert.assertEquals(bytes[i], bytes2[i]);
97 }
98
99 out = new ByteArrayOutputStream();
100 bufentity.writeTo(out);
101 bytes2 = out.toByteArray();
102 Assert.assertNotNull(bytes2);
103 Assert.assertEquals(bytes.length, bytes2.length);
104 for (int i = 0; i < bytes.length; i++) {
105 Assert.assertEquals(bytes[i], bytes2[i]);
106 }
107
108 try {
109 bufentity.writeTo(null);
110 Assert.fail("IllegalArgumentException should have been thrown");
111 } catch (final IllegalArgumentException ex) {
112
113 }
114 }
115
116 @Test
117 public void testWriteToWrapped() throws Exception {
118 final byte[] bytes = "Message content".getBytes(Consts.ASCII);
119 final ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
120 final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
121
122 ByteArrayOutputStream out = new ByteArrayOutputStream();
123 bufentity.writeTo(out);
124 byte[] bytes2 = out.toByteArray();
125 Assert.assertNotNull(bytes2);
126 Assert.assertEquals(bytes.length, bytes2.length);
127 for (int i = 0; i < bytes.length; i++) {
128 Assert.assertEquals(bytes[i], bytes2[i]);
129 }
130
131 out = new ByteArrayOutputStream();
132 bufentity.writeTo(out);
133 bytes2 = out.toByteArray();
134 Assert.assertNotNull(bytes2);
135 Assert.assertEquals(bytes.length, bytes2.length);
136 for (int i = 0; i < bytes.length; i++) {
137 Assert.assertEquals(bytes[i], bytes2[i]);
138 }
139
140 try {
141 bufentity.writeTo(null);
142 Assert.fail("IllegalArgumentException should have been thrown");
143 } catch (final IllegalArgumentException ex) {
144
145 }
146 }
147
148 }