1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.util;
29
30 import org.junit.Assert;
31 import org.junit.Test;
32
33
34
35
36
37 public class TestEncodingUtils {
38
39 private static String constructString(final int [] unicodeChars) {
40 final StringBuilder buffer = new StringBuilder();
41 if (unicodeChars != null) {
42 for (final int unicodeChar : unicodeChars) {
43 buffer.append((char)unicodeChar);
44 }
45 }
46 return buffer.toString();
47 }
48
49 static final int SWISS_GERMAN_HELLO [] = {
50 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
51 };
52
53 @Test
54 public void testBytesToString() throws Exception {
55 final String s = constructString(SWISS_GERMAN_HELLO);
56 final byte[] utf = s.getBytes("UTF-8");
57 final byte[] latin1 = s.getBytes("ISO-8859-1");
58
59 final String s1 = EncodingUtils.getString(utf, "UTF-8");
60 final String s2 = EncodingUtils.getString(latin1, "ISO-8859-1");
61
62 Assert.assertEquals(s, s1);
63 Assert.assertEquals(s, s2);
64
65 try {
66 EncodingUtils.getString(null, 0, 0, "UTF-8");
67 Assert.fail("IllegalArgumentException should have been thrown");
68 } catch (final IllegalArgumentException ex) {
69
70 }
71 try {
72 EncodingUtils.getString(null, "UTF-8");
73 Assert.fail("IllegalArgumentException should have been thrown");
74 } catch (final IllegalArgumentException ex) {
75
76 }
77 try {
78 EncodingUtils.getString(new byte[] {}, null);
79 Assert.fail("IllegalArgumentException should have been thrown");
80 } catch (final IllegalArgumentException ex) {
81
82 }
83 try {
84 EncodingUtils.getString(new byte[] {}, "");
85 Assert.fail("IllegalArgumentException should have been thrown");
86 } catch (final IllegalArgumentException ex) {
87
88 }
89 }
90
91 @Test
92 public void testStringToBytesToString() throws Exception {
93 final String s = constructString(SWISS_GERMAN_HELLO);
94 final byte[] utf = s.getBytes("UTF-8");
95 final byte[] latin1 = s.getBytes("ISO-8859-1");
96
97 final byte[] data1 = EncodingUtils.getBytes(s, "UTF-8");
98 final byte[] data2 = EncodingUtils.getBytes(s, "ISO-8859-1");
99
100 Assert.assertNotNull(data1);
101 Assert.assertEquals(utf.length, data1.length);
102 for (int i = 0; i < utf.length; i++) {
103 Assert.assertEquals(utf[i], data1[i]);
104 }
105 Assert.assertNotNull(data2);
106 Assert.assertEquals(latin1.length, data2.length);
107 for (int i = 0; i < latin1.length; i++) {
108 Assert.assertEquals(latin1[i], data2[i]);
109 }
110
111 try {
112 EncodingUtils.getBytes(null, "UTF-8");
113 Assert.fail("IllegalArgumentException should have been thrown");
114 } catch (final IllegalArgumentException ex) {
115
116 }
117 try {
118 EncodingUtils.getBytes("what not", null);
119 Assert.fail("IllegalArgumentException should have been thrown");
120 } catch (final IllegalArgumentException ex) {
121
122 }
123 try {
124 EncodingUtils.getBytes("what not", "");
125 Assert.fail("IllegalArgumentException should have been thrown");
126 } catch (final IllegalArgumentException ex) {
127
128 }
129 }
130
131 @Test
132 public void testAsciiBytesToString() throws Exception {
133 final String s = "ascii only, I mean it!";
134 Assert.assertEquals(s, EncodingUtils.getAsciiString(s.getBytes("US-ASCII")));
135 try {
136 EncodingUtils.getAsciiString(null);
137 Assert.fail("IllegalArgumentException should have been thrown");
138 } catch (final IllegalArgumentException ex) {
139
140 }
141 try {
142 EncodingUtils.getAsciiString(null, 0, 0);
143 Assert.fail("IllegalArgumentException should have been thrown");
144 } catch (final IllegalArgumentException ex) {
145
146 }
147 }
148
149 @Test
150 public void testAsciiStringToBytes() throws Exception {
151 final String s = "ascii only, I mean it!";
152 final byte[] ascii = s.getBytes("US-ASCII");
153 final byte[] data = EncodingUtils.getAsciiBytes(s);
154
155 Assert.assertNotNull(data);
156 Assert.assertEquals(ascii.length, data.length);
157 for (int i = 0; i < ascii.length; i++) {
158 Assert.assertEquals(ascii[i], data[i]);
159 }
160 try {
161 EncodingUtils.getAsciiBytes(null);
162 Assert.fail("IllegalArgumentException should have been thrown");
163 } catch (final IllegalArgumentException ex) {
164
165 }
166 }
167
168 @Test
169 public void testUnsupportedEncoding() {
170 final String s = constructString(SWISS_GERMAN_HELLO);
171 final byte[] b1 = s.getBytes();
172 final byte[] b2 = EncodingUtils.getBytes(s, "ThisJustAintRight");
173 Assert.assertEquals(b1.length, b2.length);
174 for (int i = 0; i < b1.length; i++) {
175 Assert.assertEquals(b1[i], b2[i]);
176 }
177 final String s1 = new String(b1);
178 final String s2 = EncodingUtils.getString(b1, "ThisJustAintRight");
179 Assert.assertEquals(s1, s2);
180 }
181
182 }