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.hc.core5.http.io.entity;
29  
30  import java.io.ByteArrayOutputStream;
31  import java.nio.charset.Charset;
32  import java.nio.charset.StandardCharsets;
33  
34  import org.apache.hc.core5.http.ContentType;
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(StandardCharsets.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 testNullConstructor() throws Exception {
57          try {
58              new StringEntity(null);
59              Assert.fail("NullPointerException should have been thrown");
60          } catch (final NullPointerException 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", httpentity.getContentType());
70          httpentity = new StringEntity(s, StandardCharsets.US_ASCII);
71          Assert.assertEquals("text/plain; charset=US-ASCII", httpentity.getContentType());
72          httpentity = new StringEntity(s);
73          Assert.assertEquals("text/plain; charset=ISO-8859-1", httpentity.getContentType());
74      }
75  
76      private static String constructString(final int [] unicodeChars) {
77          final StringBuilder buffer = new StringBuilder();
78          if (unicodeChars != null) {
79              for (final int unicodeChar : unicodeChars) {
80                  buffer.append((char)unicodeChar);
81              }
82          }
83          return buffer.toString();
84      }
85  
86      static final int SWISS_GERMAN_HELLO [] = {
87              0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
88          };
89  
90      @Test
91      public void testNullCharset() throws Exception {
92          final String s = constructString(SWISS_GERMAN_HELLO);
93          StringEntity httpentity = new StringEntity(s, ContentType.create("text/plain", (Charset) null));
94          Assert.assertNotNull(httpentity.getContentType());
95          Assert.assertEquals("text/plain", httpentity.getContentType());
96          Assert.assertEquals(s, EntityUtils.toString(httpentity));
97          httpentity = new StringEntity(s, (Charset) null);
98          Assert.assertNotNull(httpentity.getContentType());
99          Assert.assertEquals("text/plain", httpentity.getContentType());
100         Assert.assertEquals(s, EntityUtils.toString(httpentity));
101     }
102 
103     @Test
104     public void testWriteTo() throws Exception {
105         final String s = "Message content";
106         final byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
107         final StringEntity httpentity = new StringEntity(s);
108 
109         ByteArrayOutputStream out = new ByteArrayOutputStream();
110         httpentity.writeTo(out);
111         byte[] bytes2 = out.toByteArray();
112         Assert.assertNotNull(bytes2);
113         Assert.assertEquals(bytes.length, bytes2.length);
114         for (int i = 0; i < bytes.length; i++) {
115             Assert.assertEquals(bytes[i], bytes2[i]);
116         }
117 
118         out = new ByteArrayOutputStream();
119         httpentity.writeTo(out);
120         bytes2 = out.toByteArray();
121         Assert.assertNotNull(bytes2);
122         Assert.assertEquals(bytes.length, bytes2.length);
123         for (int i = 0; i < bytes.length; i++) {
124             Assert.assertEquals(bytes[i], bytes2[i]);
125         }
126 
127         try {
128             httpentity.writeTo(null);
129             Assert.fail("NullPointerException should have been thrown");
130         } catch (final NullPointerException ex) {
131             // expected
132         }
133     }
134 
135 }