001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/util/TestEncodingUtils.java $
003: * $Revision: 496069 $
004: * $Date: 2007-01-14 13:03:05 +0100 (Sun, 14 Jan 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.util;
033:
034: import junit.framework.Test;
035: import junit.framework.TestCase;
036: import junit.framework.TestSuite;
037:
038: /**
039: * Unit tests for {@link TestEncodingUtils}.
040: *
041: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
042: */
043: public class TestEncodingUtils extends TestCase {
044:
045: public TestEncodingUtils(String testName) {
046: super (testName);
047: }
048:
049: public static void main(String args[]) {
050: String[] testCaseName = { TestEncodingUtils.class.getName() };
051: junit.textui.TestRunner.main(testCaseName);
052: }
053:
054: public static Test suite() {
055: return new TestSuite(TestEncodingUtils.class);
056: }
057:
058: private static String constructString(int[] unicodeChars) {
059: StringBuffer buffer = new StringBuffer();
060: if (unicodeChars != null) {
061: for (int i = 0; i < unicodeChars.length; i++) {
062: buffer.append((char) unicodeChars[i]);
063: }
064: }
065: return buffer.toString();
066: }
067:
068: static final int SWISS_GERMAN_HELLO[] = { 0x47, 0x72, 0xFC, 0x65,
069: 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
070:
071: public void testBytesToString() throws Exception {
072: String s = constructString(SWISS_GERMAN_HELLO);
073: byte[] utf = s.getBytes("UTF-8");
074: byte[] latin1 = s.getBytes("ISO-8859-1");
075:
076: String s1 = EncodingUtils.getString(utf, "UTF-8");
077: String s2 = EncodingUtils.getString(latin1, "ISO-8859-1");
078:
079: assertEquals(s, s1);
080: assertEquals(s, s2);
081:
082: try {
083: EncodingUtils.getString(null, 0, 0, "UTF-8");
084: fail("IllegalArgumentException should have been thrown");
085: } catch (IllegalArgumentException ex) {
086: // expected
087: }
088: try {
089: EncodingUtils.getString(null, "UTF-8");
090: fail("IllegalArgumentException should have been thrown");
091: } catch (IllegalArgumentException ex) {
092: // expected
093: }
094: try {
095: EncodingUtils.getString(new byte[] {}, null);
096: fail("IllegalArgumentException should have been thrown");
097: } catch (IllegalArgumentException ex) {
098: // expected
099: }
100: try {
101: EncodingUtils.getString(new byte[] {}, "");
102: fail("IllegalArgumentException should have been thrown");
103: } catch (IllegalArgumentException ex) {
104: // expected
105: }
106: }
107:
108: public void testStringToBytesToString() throws Exception {
109: String s = constructString(SWISS_GERMAN_HELLO);
110: byte[] utf = s.getBytes("UTF-8");
111: byte[] latin1 = s.getBytes("ISO-8859-1");
112:
113: byte[] data1 = EncodingUtils.getBytes(s, "UTF-8");
114: byte[] data2 = EncodingUtils.getBytes(s, "ISO-8859-1");
115:
116: assertNotNull(data1);
117: assertEquals(utf.length, data1.length);
118: for (int i = 0; i < utf.length; i++) {
119: assertEquals(utf[i], data1[i]);
120: }
121: assertNotNull(data2);
122: assertEquals(latin1.length, data2.length);
123: for (int i = 0; i < latin1.length; i++) {
124: assertEquals(latin1[i], data2[i]);
125: }
126:
127: try {
128: EncodingUtils.getBytes(null, "UTF-8");
129: fail("IllegalArgumentException should have been thrown");
130: } catch (IllegalArgumentException ex) {
131: // expected
132: }
133: try {
134: EncodingUtils.getBytes("what not", null);
135: fail("IllegalArgumentException should have been thrown");
136: } catch (IllegalArgumentException ex) {
137: // expected
138: }
139: try {
140: EncodingUtils.getBytes("what not", "");
141: fail("IllegalArgumentException should have been thrown");
142: } catch (IllegalArgumentException ex) {
143: // expected
144: }
145: }
146:
147: public void testAsciiBytesToString() throws Exception {
148: String s = "ascii only, I mean it!";
149: assertEquals(s, EncodingUtils.getAsciiString(s
150: .getBytes("US-ASCII")));
151: try {
152: EncodingUtils.getAsciiString(null);
153: fail("IllegalArgumentException should have been thrown");
154: } catch (IllegalArgumentException ex) {
155: // expected
156: }
157: try {
158: EncodingUtils.getAsciiString(null, 0, 0);
159: fail("IllegalArgumentException should have been thrown");
160: } catch (IllegalArgumentException ex) {
161: // expected
162: }
163: }
164:
165: public void testAsciiStringToBytes() throws Exception {
166: String s = "ascii only, I mean it!";
167: byte[] ascii = s.getBytes("US-ASCII");
168: byte[] data = EncodingUtils.getAsciiBytes(s);
169:
170: assertNotNull(data);
171: assertEquals(ascii.length, data.length);
172: for (int i = 0; i < ascii.length; i++) {
173: assertEquals(ascii[i], data[i]);
174: }
175: try {
176: EncodingUtils.getAsciiBytes(null);
177: fail("IllegalArgumentException should have been thrown");
178: } catch (IllegalArgumentException ex) {
179: // expected
180: }
181: }
182:
183: public void testUnsupportedEncoding() {
184: String s = constructString(SWISS_GERMAN_HELLO);
185: byte[] b1 = s.getBytes();
186: byte[] b2 = EncodingUtils.getBytes(s, "ThisJustAintRight");
187: assertEquals(b1.length, b2.length);
188: for (int i = 0; i < b1.length; i++) {
189: assertEquals(b1[i], b2[i]);
190: }
191: String s1 = new String(b1);
192: String s2 = EncodingUtils.getString(b1, "ThisJustAintRight");
193: assertEquals(s1, s2);
194: }
195:
196: }
|