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.nio.charset.Charset;
32
33 import org.apache.http.Consts;
34 import org.apache.http.util.EntityUtils;
35 import org.junit.Assert;
36 import org.junit.Test;
37
38
39
40
41 public class TestStringEntity {
42
43 @Test
44 public void testBasics() throws Exception {
45 final String s = "Message content";
46 final StringEntity httpentity = new StringEntity(s, ContentType.TEXT_PLAIN);
47
48 final byte[] bytes = s.getBytes(Consts.ISO_8859_1);
49 Assert.assertEquals(bytes.length, httpentity.getContentLength());
50 Assert.assertNotNull(httpentity.getContent());
51 Assert.assertTrue(httpentity.isRepeatable());
52 Assert.assertFalse(httpentity.isStreaming());
53 }
54
55 @Test
56 public void testIllegalConstructor() throws Exception {
57 try {
58 new StringEntity(null);
59 Assert.fail("IllegalArgumentException should have been thrown");
60 } catch (final IllegalArgumentException ex) {
61
62 }
63 }
64
65 @Test
66 public void testDefaultContent() throws Exception {
67 final String s = "Message content";
68 StringEntity httpentity = new StringEntity(s, ContentType.create("text/csv", "ANSI_X3.4-1968"));
69 Assert.assertEquals("text/csv; charset=US-ASCII",
70 httpentity.getContentType().getValue());
71 httpentity = new StringEntity(s, Consts.ASCII.name());
72 Assert.assertEquals("text/plain; charset=US-ASCII",
73 httpentity.getContentType().getValue());
74 httpentity = new StringEntity(s, Consts.ASCII);
75 Assert.assertEquals("text/plain; charset=US-ASCII",
76 httpentity.getContentType().getValue());
77 httpentity = new StringEntity(s);
78 Assert.assertEquals("text/plain; charset=ISO-8859-1",
79 httpentity.getContentType().getValue());
80 }
81
82 private static String constructString(final int [] unicodeChars) {
83 final StringBuilder buffer = new StringBuilder();
84 if (unicodeChars != null) {
85 for (final int unicodeChar : unicodeChars) {
86 buffer.append((char)unicodeChar);
87 }
88 }
89 return buffer.toString();
90 }
91
92 static final int SWISS_GERMAN_HELLO [] = {
93 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
94 };
95
96 @Test
97 public void testNullCharset() throws Exception {
98 final String s = constructString(SWISS_GERMAN_HELLO);
99 StringEntity httpentity = new StringEntity(s, ContentType.create("text/plain", (Charset) null));
100 Assert.assertNotNull(httpentity.getContentType());
101 Assert.assertEquals("text/plain", httpentity.getContentType().getValue());
102 Assert.assertEquals(s, EntityUtils.toString(httpentity));
103 httpentity = new StringEntity(s, (Charset) null);
104 Assert.assertNotNull(httpentity.getContentType());
105 Assert.assertEquals("text/plain", httpentity.getContentType().getValue());
106 Assert.assertEquals(s, EntityUtils.toString(httpentity));
107 httpentity = new StringEntity(s, (String) null);
108 Assert.assertNotNull(httpentity.getContentType());
109 Assert.assertEquals("text/plain", httpentity.getContentType().getValue());
110 Assert.assertEquals(s, EntityUtils.toString(httpentity));
111 }
112
113 @Test
114 public void testWriteTo() throws Exception {
115 final String s = "Message content";
116 final byte[] bytes = s.getBytes(Consts.ISO_8859_1);
117 final StringEntity httpentity = new StringEntity(s);
118
119 ByteArrayOutputStream out = new ByteArrayOutputStream();
120 httpentity.writeTo(out);
121 byte[] bytes2 = out.toByteArray();
122 Assert.assertNotNull(bytes2);
123 Assert.assertEquals(bytes.length, bytes2.length);
124 for (int i = 0; i < bytes.length; i++) {
125 Assert.assertEquals(bytes[i], bytes2[i]);
126 }
127
128 out = new ByteArrayOutputStream();
129 httpentity.writeTo(out);
130 bytes2 = out.toByteArray();
131 Assert.assertNotNull(bytes2);
132 Assert.assertEquals(bytes.length, bytes2.length);
133 for (int i = 0; i < bytes.length; i++) {
134 Assert.assertEquals(bytes[i], bytes2[i]);
135 }
136
137 try {
138 httpentity.writeTo(null);
139 Assert.fail("IllegalArgumentException should have been thrown");
140 } catch (final IllegalArgumentException ex) {
141
142 }
143 }
144
145 }