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

Quick Search    Search Deep

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


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/util/TestEncodingUtils.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 junit.framework.Test;
33  import junit.framework.TestCase;
34  import junit.framework.TestSuite;
35  
36  /**
37   * Unit tests for {@link TestEncodingUtils}.
38   *
39   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
40   */
41  public class TestEncodingUtils extends TestCase {
42  
43      public TestEncodingUtils(String testName) {
44          super(testName);
45      }
46  
47      public static void main(String args[]) {
48          String[] testCaseName = { TestEncodingUtils.class.getName() };
49          junit.textui.TestRunner.main(testCaseName);
50      }
51  
52      public static Test suite() {
53          return new TestSuite(TestEncodingUtils.class);
54      }
55  
56      private static String constructString(int [] unicodeChars) {
57          StringBuffer buffer = new StringBuffer();
58          if (unicodeChars != null) {
59              for (int i = 0; i < unicodeChars.length; i++) {
60                  buffer.append((char)unicodeChars[i]); 
61              }
62          }
63          return buffer.toString();
64      }
65  
66      static final int SWISS_GERMAN_HELLO [] = {
67              0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
68          };
69                  
70      public void testBytesToString() throws Exception {
71          String s = constructString(SWISS_GERMAN_HELLO);
72          byte[] utf = s.getBytes("UTF-8");
73          byte[] latin1 = s.getBytes("ISO-8859-1");
74          
75          String s1 = EncodingUtils.getString(utf, "UTF-8");
76          String s2 = EncodingUtils.getString(latin1, "ISO-8859-1");
77          
78          assertEquals(s, s1);
79          assertEquals(s, s2);
80          
81          try {
82              EncodingUtils.getString(null, 0, 0, "UTF-8");
83              fail("IllegalArgumentException should have been thrown");
84          } catch (IllegalArgumentException ex) {
85              // expected
86          }
87          try {
88              EncodingUtils.getString(null, "UTF-8");
89              fail("IllegalArgumentException should have been thrown");
90          } catch (IllegalArgumentException ex) {
91              // expected
92          }
93          try {
94              EncodingUtils.getString(new byte[] {}, null);
95              fail("IllegalArgumentException should have been thrown");
96          } catch (IllegalArgumentException ex) {
97              // expected
98          }
99          try {
100             EncodingUtils.getString(new byte[] {}, "");
101             fail("IllegalArgumentException should have been thrown");
102         } catch (IllegalArgumentException ex) {
103             // expected
104         }
105     }
106     
107     public void testStringToBytesToString() throws Exception {
108         String s = constructString(SWISS_GERMAN_HELLO);
109         byte[] utf = s.getBytes("UTF-8");
110         byte[] latin1 = s.getBytes("ISO-8859-1");
111         
112         byte[] data1 = EncodingUtils.getBytes(s, "UTF-8");
113         byte[] data2 = EncodingUtils.getBytes(s, "ISO-8859-1");
114         
115         assertNotNull(data1);
116         assertEquals(utf.length, data1.length);
117         for (int i = 0; i < utf.length; i++) {
118             assertEquals(utf[i], data1[i]);
119         }
120         assertNotNull(data2);
121         assertEquals(latin1.length, data2.length);
122         for (int i = 0; i < latin1.length; i++) {
123             assertEquals(latin1[i], data2[i]);
124         }
125         
126         try {
127             EncodingUtils.getBytes(null, "UTF-8");
128             fail("IllegalArgumentException should have been thrown");
129         } catch (IllegalArgumentException ex) {
130             // expected
131         }
132         try {
133             EncodingUtils.getBytes("what not", null);
134             fail("IllegalArgumentException should have been thrown");
135         } catch (IllegalArgumentException ex) {
136             // expected
137         }
138         try {
139             EncodingUtils.getBytes("what not", "");
140             fail("IllegalArgumentException should have been thrown");
141         } catch (IllegalArgumentException ex) {
142             // expected
143         }
144     }
145 
146     public void testAsciiBytesToString() throws Exception {
147         String s = "ascii only, I mean it!";
148         assertEquals(s, EncodingUtils.getAsciiString(s.getBytes("US-ASCII")));
149         try {
150             EncodingUtils.getAsciiString(null);
151             fail("IllegalArgumentException should have been thrown");
152         } catch (IllegalArgumentException ex) {
153             // expected
154         }
155         try {
156             EncodingUtils.getAsciiString(null, 0, 0);
157             fail("IllegalArgumentException should have been thrown");
158         } catch (IllegalArgumentException ex) {
159             // expected
160         }
161     }
162     
163     public void testAsciiStringToBytes() throws Exception {
164         String s = "ascii only, I mean it!";
165         byte[] ascii = s.getBytes("US-ASCII");
166         byte[] data = EncodingUtils.getAsciiBytes(s);
167 
168         assertNotNull(data);
169         assertEquals(ascii.length, data.length);
170         for (int i = 0; i < ascii.length; i++) {
171             assertEquals(ascii[i], data[i]);
172         }
173         try {
174             EncodingUtils.getAsciiBytes(null);
175             fail("IllegalArgumentException should have been thrown");
176         } catch (IllegalArgumentException ex) {
177             // expected
178         }
179     }
180     
181     public void testUnsupportedEncoding() {
182         String s = constructString(SWISS_GERMAN_HELLO);
183         byte[] b1 = s.getBytes();
184         byte[] b2 = EncodingUtils.getBytes(s, "ThisJustAintRight");
185         assertEquals(b1.length, b2.length);
186         for (int i = 0; i < b1.length; i++) {
187             assertEquals(b1[i], b2[i]);
188         }
189         String s1 = new String(b1);
190         String s2 = EncodingUtils.getString(b1, "ThisJustAintRight");
191         assertEquals(s1, s2);        
192     }
193     
194 }