Source code: org/apache/http/entity/TestStringEntity.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/entity/TestStringEntity.java $
3 * $Revision: 385310 $
4 * $Date: 2006-03-12 17:28:43 +0100 (Sun, 12 Mar 2006) $
5 *
6 * ====================================================================
7 *
8 * Copyright 1999-2006 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ====================================================================
22 *
23 * This software consists of voluntary contributions made by many
24 * individuals on behalf of the Apache Software Foundation. For more
25 * information on the Apache Software Foundation, please see
26 * <http://www.apache.org/>.
27 *
28 */
29
30 package org.apache.http.entity;
31
32 import java.io.ByteArrayOutputStream;
33
34 import junit.framework.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37
38 import org.apache.http.protocol.HTTP;
39
40 /**
41 * Unit tests for {@link StringEntity}.
42 *
43 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
44 */
45 public class TestStringEntity extends TestCase {
46
47 public TestStringEntity(String testName) {
48 super(testName);
49 }
50
51 public static void main(String args[]) {
52 String[] testCaseName = { TestStringEntity.class.getName() };
53 junit.textui.TestRunner.main(testCaseName);
54 }
55
56 public static Test suite() {
57 return new TestSuite(TestStringEntity.class);
58 }
59
60 public void testBasics() throws Exception {
61 String s = "Message content";
62 StringEntity httpentity = new StringEntity(s, HTTP.ISO_8859_1);
63
64 byte[] bytes = s.getBytes(HTTP.ISO_8859_1);
65 assertEquals(bytes.length, httpentity.getContentLength());
66 assertNotNull(httpentity.getContent());
67 assertTrue(httpentity.isRepeatable());
68 assertFalse(httpentity.isStreaming());
69 }
70
71 public void testIllegalConstructor() throws Exception {
72 try {
73 new StringEntity(null);
74 fail("IllegalArgumentException should have been thrown");
75 } catch (IllegalArgumentException ex) {
76 // expected
77 }
78 }
79
80 public void testDefaultContent() throws Exception {
81 String s = "Message content";
82 StringEntity httpentity = new StringEntity(s, HTTP.US_ASCII);
83 assertEquals("text/plain; charset=US-ASCII",
84 httpentity.getContentType().getValue());
85 httpentity = new StringEntity(s);
86 assertEquals("text/plain; charset=ISO-8859-1",
87 httpentity.getContentType().getValue());
88 }
89
90 public void testWriteTo() throws Exception {
91 String s = "Message content";
92 byte[] bytes = s.getBytes(HTTP.ISO_8859_1);
93 StringEntity httpentity = new StringEntity(s);
94
95 ByteArrayOutputStream out = new ByteArrayOutputStream();
96 httpentity.writeTo(out);
97 byte[] bytes2 = out.toByteArray();
98 assertNotNull(bytes2);
99 assertEquals(bytes.length, bytes2.length);
100 for (int i = 0; i < bytes.length; i++) {
101 assertEquals(bytes[i], bytes2[i]);
102 }
103
104 out = new ByteArrayOutputStream();
105 httpentity.writeTo(out);
106 bytes2 = out.toByteArray();
107 assertNotNull(bytes2);
108 assertEquals(bytes.length, bytes2.length);
109 for (int i = 0; i < bytes.length; i++) {
110 assertEquals(bytes[i], bytes2[i]);
111 }
112
113 try {
114 httpentity.writeTo(null);
115 fail("IllegalArgumentException should have been thrown");
116 } catch (IllegalArgumentException ex) {
117 // expected
118 }
119 }
120
121 }