View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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   * Unit tests for {@link StringEntity}.
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              // expected
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             // expected
142         }
143     }
144 
145 }