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.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34
35 import org.apache.http.HttpEntity;
36 import org.apache.http.util.EntityUtils;
37 import org.junit.Assert;
38 import org.junit.Test;
39
40
41
42
43 public class TestEntityTemplate {
44
45 @Test
46 public void testBasics() throws Exception {
47
48 final HttpEntity httpentity = new EntityTemplate(new ContentProducer() {
49
50 @Override
51 public void writeTo(final OutputStream outStream) throws IOException {
52 outStream.write('a');
53 }
54
55 });
56
57 Assert.assertEquals(-1, httpentity.getContentLength());
58 Assert.assertTrue(httpentity.isRepeatable());
59 Assert.assertFalse(httpentity.isStreaming());
60 }
61
62 @Test
63 public void testIllegalConstructor() throws Exception {
64 try {
65 new EntityTemplate(null);
66 Assert.fail("IllegalArgumentException should have been thrown");
67 } catch (final IllegalArgumentException ex) {
68
69 }
70 }
71
72 @Test
73 public void testWriteTo() throws Exception {
74 final HttpEntity httpentity = new EntityTemplate(new ContentProducer() {
75
76 @Override
77 public void writeTo(final OutputStream outStream) throws IOException {
78 outStream.write('a');
79 }
80
81 });
82
83 final ByteArrayOutputStream out = new ByteArrayOutputStream();
84 httpentity.writeTo(out);
85 final byte[] bytes2 = out.toByteArray();
86 Assert.assertNotNull(bytes2);
87 Assert.assertEquals(1, bytes2.length);
88
89 try {
90 httpentity.writeTo(null);
91 Assert.fail("IllegalArgumentException should have been thrown");
92 } catch (final IllegalArgumentException ex) {
93
94 }
95 }
96
97 @Test
98 public void testgetContent() throws Exception {
99 final HttpEntity httpentity = new EntityTemplate(new ContentProducer() {
100
101 @Override
102 public void writeTo(final OutputStream outStream) throws IOException {
103 outStream.write('a');
104 }
105
106 });
107 final InputStream inStream = httpentity.getContent();
108 Assert.assertNotNull(inStream);
109 final String s = EntityUtils.toString(httpentity);
110 Assert.assertEquals("a", s);
111 }
112
113 }