Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/util/TestEntityUtils.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/util/TestEntityUtils.java $
3    * $Revision: 376458 $
4    * $Date: 2006-02-09 23:22:06 +0100 (Thu, 09 Feb 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.util;
31  
32  import java.io.InputStream;
33  import java.io.ByteArrayInputStream;
34  
35  import org.apache.http.Header;
36  import org.apache.http.entity.BasicHttpEntity;
37  
38  import junit.framework.Test;
39  import junit.framework.TestCase;
40  import junit.framework.TestSuite;
41  
42  /**
43   * Unit tests for {@link EntityUtils}.
44   *
45   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
46   */
47  public class TestEntityUtils extends TestCase {
48  
49      public TestEntityUtils(String testName) {
50          super(testName);
51      }
52  
53      public static void main(String args[]) {
54          String[] testCaseName = { TestEntityUtils.class.getName() };
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      public static Test suite() {
59          return new TestSuite(TestEntityUtils.class);
60      }
61  
62      public void testNullEntityToByteArray() throws Exception {
63          try {
64              EntityUtils.toByteArray(null);
65              fail("IllegalArgumentException should have been thrown");
66          } catch (IllegalArgumentException ex) {
67              // expected
68          }
69      }
70      
71      public void testEmptyContentToByteArray() throws Exception {
72          NullHttpEntity httpentity = new NullHttpEntity();
73          byte[] bytes = EntityUtils.toByteArray(httpentity);
74          assertNotNull(bytes);
75          assertEquals(0, bytes.length);
76      }
77      
78      public void testMaxIntContentToByteArray() throws Exception {
79          byte[] content = "Message content".getBytes("ISO-8859-1");
80          BasicHttpEntity httpentity = new BasicHttpEntity();
81          httpentity.setContent(new ByteArrayInputStream(content));
82          httpentity.setContentLength(Integer.MAX_VALUE + 100L);
83          try {
84              EntityUtils.toByteArray(httpentity);
85              fail("IllegalArgumentException should have been thrown");
86          } catch (IllegalArgumentException ex) {
87              // expected
88          }
89      }
90      
91      public void testUnknownLengthContentToByteArray() throws Exception {
92          byte[] bytes = "Message content".getBytes("ISO-8859-1");
93          BasicHttpEntity httpentity = new BasicHttpEntity();
94          httpentity.setContent(new ByteArrayInputStream(bytes));
95          httpentity.setContentLength(-1L);
96          byte[] bytes2 = EntityUtils.toByteArray(httpentity);
97          assertNotNull(bytes2);
98          assertEquals(bytes.length, bytes2.length);
99          for (int i = 0; i < bytes.length; i++) {
100             assertEquals(bytes[i], bytes2[i]);
101         }
102     }
103     
104     public void testKnownLengthContentToByteArray() throws Exception {
105         byte[] bytes = "Message content".getBytes("ISO-8859-1");
106         BasicHttpEntity httpentity = new BasicHttpEntity();
107         httpentity.setContent(new ByteArrayInputStream(bytes));
108         httpentity.setContentLength(bytes.length);
109         byte[] bytes2 = EntityUtils.toByteArray(httpentity);
110         assertNotNull(bytes2);
111         assertEquals(bytes.length, bytes2.length);
112         for (int i = 0; i < bytes.length; i++) {
113             assertEquals(bytes[i], bytes2[i]);
114         }
115     }
116 
117     public void testNullEntityGetContentCharset() throws Exception {
118         try {
119             EntityUtils.getContentCharSet(null);
120             fail("IllegalArgumentException should have been thrown");
121         } catch (IllegalArgumentException ex) {
122             // expected
123         }
124     }
125     
126     public void testNullContentTypeGetContentCharset() throws Exception {
127         BasicHttpEntity httpentity = new BasicHttpEntity();
128         httpentity.setContentType((Header)null);
129         assertNull(EntityUtils.getContentCharSet(httpentity));
130     }
131     
132     public void testNoCharsetGetContentCharset() throws Exception {
133         BasicHttpEntity httpentity = new BasicHttpEntity();
134         httpentity.setContentType(new Header("Content-Type", "text/plain; param=yadayada"));
135         assertNull(EntityUtils.getContentCharSet(httpentity));
136     }
137     
138     public void testGetContentCharset() throws Exception {
139         BasicHttpEntity httpentity = new BasicHttpEntity();
140         httpentity.setContentType(new Header("Content-Type", "text/plain; charset = UTF-8"));
141         assertEquals("UTF-8", EntityUtils.getContentCharSet(httpentity));
142     }
143     
144     public void testNullEntityToString() throws Exception {
145         try {
146             EntityUtils.toString(null);
147             fail("IllegalArgumentException should have been thrown");
148         } catch (IllegalArgumentException ex) {
149             // expected
150         }
151     }
152     
153     public void testEmptyContentToString() throws Exception {
154         NullHttpEntity httpentity = new NullHttpEntity();
155         String s = EntityUtils.toString(httpentity);
156         assertNotNull(s);
157         assertEquals("", s);
158     }
159         
160     public void testMaxIntContentToString() throws Exception {
161         byte[] content = "Message content".getBytes("ISO-8859-1");
162         BasicHttpEntity httpentity = new BasicHttpEntity();
163         httpentity.setContent(new ByteArrayInputStream(content));
164         httpentity.setContentLength(Integer.MAX_VALUE + 100L);
165         try {
166             EntityUtils.toString(httpentity);
167             fail("IllegalArgumentException should have been thrown");
168         } catch (IllegalArgumentException ex) {
169             // expected
170         }
171     }
172     
173     public void testUnknownLengthContentToString() throws Exception {
174         byte[] bytes = "Message content".getBytes("ISO-8859-1");
175         BasicHttpEntity httpentity = new BasicHttpEntity();
176         httpentity.setContent(new ByteArrayInputStream(bytes));
177         httpentity.setContentLength(-1L);
178         String s = EntityUtils.toString(httpentity, "ISO-8859-1");
179         assertEquals("Message content", s);
180     }
181 
182     public void testKnownLengthContentToString() throws Exception {
183         byte[] bytes = "Message content".getBytes("ISO-8859-1");
184         BasicHttpEntity httpentity = new BasicHttpEntity();
185         httpentity.setContent(new ByteArrayInputStream(bytes));
186         httpentity.setContentLength(bytes.length);
187         String s = EntityUtils.toString(httpentity, "ISO-8859-1");
188         assertEquals("Message content", s);
189     }
190 
191     static final int SWISS_GERMAN_HELLO [] = {
192         0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
193     };
194         
195     static final int RUSSIAN_HELLO [] = {
196         0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 
197         0x432, 0x435, 0x442 
198     }; 
199     
200     private static String constructString(int [] unicodeChars) {
201         StringBuffer buffer = new StringBuffer();
202         if (unicodeChars != null) {
203             for (int i = 0; i < unicodeChars.length; i++) {
204                 buffer.append((char)unicodeChars[i]); 
205             }
206         }
207         return buffer.toString();
208     }
209 
210     public void testNoCharsetContentToString() throws Exception {
211         String content = constructString(SWISS_GERMAN_HELLO);
212         byte[] bytes = content.getBytes("ISO-8859-1");
213         BasicHttpEntity httpentity = new BasicHttpEntity();
214         httpentity.setContent(new ByteArrayInputStream(bytes));
215         httpentity.setContentType(new Header("Content-Type", "text/plain"));
216         String s = EntityUtils.toString(httpentity);
217         assertEquals(content, s);
218     }
219     
220     public void testDefaultCharsetContentToString() throws Exception {
221         String content = constructString(RUSSIAN_HELLO);
222         byte[] bytes = content.getBytes("KOI8-R");
223         BasicHttpEntity httpentity = new BasicHttpEntity();
224         httpentity.setContent(new ByteArrayInputStream(bytes));
225         httpentity.setContentType(new Header("Content-Type", "text/plain"));
226         String s = EntityUtils.toString(httpentity, "KOI8-R");
227         assertEquals(content, s);
228     }
229     
230     public void testContentWithContentTypeToString() throws Exception {
231         String content = constructString(RUSSIAN_HELLO);
232         byte[] bytes = content.getBytes("UTF-8");
233         BasicHttpEntity httpentity = new BasicHttpEntity();
234         httpentity.setContent(new ByteArrayInputStream(bytes));
235         httpentity.setContentType(new Header("Content-Type", "text/plain; charset=UTF-8"));
236         String s = EntityUtils.toString(httpentity, "ISO-8859-1");
237         assertEquals(content, s);
238     }
239 
240     /**
241      * Helper class that returns <code>null</code> as the content.
242      */
243     public static class NullHttpEntity extends BasicHttpEntity {
244 
245         // default constructor
246         /**
247          * Obtains no content.
248          * This method disables the state checks in the base class.
249          *
250          * @return <code>null</code>
251          */
252         public InputStream getContent() {
253             return null;
254         }
255     } // class NullEntity
256 
257 } // class TestEntityUtils