KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > net > URLCodecTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.codec.net;
18
19 import junit.framework.TestCase;
20 import org.apache.commons.codec.DecoderException;
21 import org.apache.commons.codec.EncoderException;
22
23 /**
24  * URL codec test cases
25  *
26  * @author Apache Software Foundation
27  */

28 public class URLCodecTest extends TestCase {
29     
30     static final int SWISS_GERMAN_STUFF_UNICODE [] = {
31         0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
32     };
33     
34     static final int RUSSIAN_STUFF_UNICODE [] = {
35         0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
36         0x432, 0x435, 0x442
37     };
38
39     public URLCodecTest(String JavaDoc name) {
40         super(name);
41     }
42
43     private void validateState(URLCodec urlCodec) {
44         assertEquals(urlCodec.getEncoding(), urlCodec.getDefaultCharset());
45     }
46     
47     private String JavaDoc constructString(int [] unicodeChars) {
48         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
49         if (unicodeChars != null) {
50             for (int i = 0; i < unicodeChars.length; i++) {
51                 buffer.append((char)unicodeChars[i]);
52             }
53         }
54         return buffer.toString();
55     }
56     
57     public void testUTF8RoundTrip() throws Exception JavaDoc {
58
59         String JavaDoc ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
60         String JavaDoc ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
61         
62         URLCodec urlCodec = new URLCodec();
63         this.validateState(urlCodec);
64         
65         assertEquals(
66             "%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82",
67             urlCodec.encode(ru_msg, "UTF-8")
68         );
69         assertEquals("Gr%C3%BCezi_z%C3%A4m%C3%A4", urlCodec.encode(ch_msg, "UTF-8"));
70         
71         assertEquals(ru_msg, urlCodec.decode(urlCodec.encode(ru_msg, "UTF-8"), "UTF-8"));
72         assertEquals(ch_msg, urlCodec.decode(urlCodec.encode(ch_msg, "UTF-8"), "UTF-8"));
73         this.validateState(urlCodec);
74     }
75
76     public void testBasicEncodeDecode() throws Exception JavaDoc {
77         URLCodec urlCodec = new URLCodec();
78         String JavaDoc plain = "Hello there!";
79         String JavaDoc encoded = urlCodec.encode(plain);
80         assertEquals("Basic URL encoding test",
81             "Hello+there%21", encoded);
82         assertEquals("Basic URL decoding test",
83             plain, urlCodec.decode(encoded));
84         this.validateState(urlCodec);
85     }
86
87
88     public void testSafeCharEncodeDecode() throws Exception JavaDoc {
89         URLCodec urlCodec = new URLCodec();
90         String JavaDoc plain = "abc123_-.*";
91         String JavaDoc encoded = urlCodec.encode(plain);
92         assertEquals("Safe chars URL encoding test",
93             plain, encoded);
94         assertEquals("Safe chars URL decoding test",
95             plain, urlCodec.decode(encoded));
96         this.validateState(urlCodec);
97     }
98
99
100     public void testUnsafeEncodeDecode() throws Exception JavaDoc {
101         URLCodec urlCodec = new URLCodec();
102         String JavaDoc plain = "~!@#$%^&()+{}\"\\;:`,/[]";
103         String JavaDoc encoded = urlCodec.encode(plain);
104         assertEquals("Unsafe chars URL encoding test",
105             "%7E%21%40%23%24%25%5E%26%28%29%2B%7B%7D%22%5C%3B%3A%60%2C%2F%5B%5D", encoded);
106         assertEquals("Unsafe chars URL decoding test",
107             plain, urlCodec.decode(encoded));
108         this.validateState(urlCodec);
109     }
110
111
112     public void testEncodeDecodeNull() throws Exception JavaDoc {
113         URLCodec urlCodec = new URLCodec();
114         assertNull("Null string URL encoding test",
115             urlCodec.encode((String JavaDoc)null));
116         assertNull("Null string URL decoding test",
117             urlCodec.decode((String JavaDoc)null));
118         this.validateState(urlCodec);
119     }
120
121
122     public void testDecodeInvalid() throws Exception JavaDoc {
123         URLCodec urlCodec = new URLCodec();
124         try {
125             urlCodec.decode("%");
126             fail("DecoderException should have been thrown");
127         } catch(DecoderException e) {
128             // Expected. Move on
129
}
130         try {
131             urlCodec.decode("%A");
132             fail("DecoderException should have been thrown");
133         } catch(DecoderException e) {
134             // Expected. Move on
135
}
136         try {
137             urlCodec.decode("%WW");
138             fail("DecoderException should have been thrown");
139         } catch(DecoderException e) {
140             // Expected. Move on
141
}
142         this.validateState(urlCodec);
143     }
144
145     public void testEncodeNull() throws Exception JavaDoc {
146         URLCodec urlCodec = new URLCodec();
147         byte[] plain = null;
148         byte[] encoded = urlCodec.encode(plain);
149         assertEquals("Encoding a null string should return null",
150             null, encoded);
151         this.validateState(urlCodec);
152     }
153     
154     public void testEncodeUrlWithNullBitSet() throws Exception JavaDoc {
155         URLCodec urlCodec = new URLCodec();
156         String JavaDoc plain = "Hello there!";
157         String JavaDoc encoded = new String JavaDoc( URLCodec.encodeUrl(null, plain.getBytes()));
158         assertEquals("Basic URL encoding test",
159             "Hello+there%21", encoded);
160         assertEquals("Basic URL decoding test",
161             plain, urlCodec.decode(encoded));
162         this.validateState(urlCodec);
163     }
164
165     public void testDecodeWithNullArray() throws Exception JavaDoc {
166         byte[] plain = null;
167         byte[] result = URLCodec.decodeUrl( plain );
168         assertEquals("Result should be null", null, result);
169     }
170
171     public void testEncodeStringWithNull() throws Exception JavaDoc {
172         URLCodec urlCodec = new URLCodec();
173         String JavaDoc test = null;
174         String JavaDoc result = urlCodec.encode( test, "charset" );
175         assertEquals("Result should be null", null, result);
176     }
177
178     public void testDecodeStringWithNull() throws Exception JavaDoc {
179         URLCodec urlCodec = new URLCodec();
180         String JavaDoc test = null;
181         String JavaDoc result = urlCodec.decode( test, "charset" );
182         assertEquals("Result should be null", null, result);
183     }
184     
185     public void testEncodeObjects() throws Exception JavaDoc {
186         URLCodec urlCodec = new URLCodec();
187         String JavaDoc plain = "Hello there!";
188         String JavaDoc encoded = (String JavaDoc) urlCodec.encode((Object JavaDoc) plain);
189         assertEquals("Basic URL encoding test",
190             "Hello+there%21", encoded);
191
192         byte[] plainBA = plain.getBytes();
193         byte[] encodedBA = (byte[]) urlCodec.encode((Object JavaDoc) plainBA);
194         encoded = new String JavaDoc(encodedBA);
195         assertEquals("Basic URL encoding test",
196             "Hello+there%21", encoded);
197             
198         Object JavaDoc result = urlCodec.encode((Object JavaDoc) null);
199         assertEquals( "Encoding a null Object should return null", null, result);
200         
201         try {
202             Object JavaDoc dObj = new Double JavaDoc(3.0);
203             urlCodec.encode( dObj );
204             fail( "Trying to url encode a Double object should cause an exception.");
205         } catch( EncoderException ee ) {
206             // Exception expected, test segment passes.
207
}
208         this.validateState(urlCodec);
209     }
210     
211     public void testInvalidEncoding() {
212         URLCodec urlCodec = new URLCodec("NONSENSE");
213         String JavaDoc plain = "Hello there!";
214         try {
215             urlCodec.encode(plain);
216             fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
217         } catch (EncoderException ee) {
218             // Exception expected, test segment passes.
219
}
220         try {
221             urlCodec.decode(plain);
222             fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
223         } catch (DecoderException ee) {
224             // Exception expected, test segment passes.
225
}
226         this.validateState(urlCodec);
227     }
228
229     public void testDecodeObjects() throws Exception JavaDoc {
230         URLCodec urlCodec = new URLCodec();
231         String JavaDoc plain = "Hello+there%21";
232         String JavaDoc decoded = (String JavaDoc) urlCodec.decode((Object JavaDoc) plain);
233         assertEquals("Basic URL decoding test",
234             "Hello there!", decoded);
235
236         byte[] plainBA = plain.getBytes();
237         byte[] decodedBA = (byte[]) urlCodec.decode((Object JavaDoc) plainBA);
238         decoded = new String JavaDoc(decodedBA);
239         assertEquals("Basic URL decoding test",
240             "Hello there!", decoded);
241             
242         Object JavaDoc result = urlCodec.decode((Object JavaDoc) null);
243         assertEquals( "Decoding a null Object should return null", null, result);
244         
245         try {
246             Object JavaDoc dObj = new Double JavaDoc(3.0);
247             urlCodec.decode( dObj );
248             fail( "Trying to url encode a Double object should cause an exception.");
249         } catch( DecoderException ee ) {
250             // Exception expected, test segment passes.
251
}
252         this.validateState(urlCodec);
253     }
254
255     public void testDefaultEncoding() throws Exception JavaDoc {
256         String JavaDoc plain = "Hello there!";
257         URLCodec urlCodec = new URLCodec("UnicodeBig");
258         urlCodec.encode(plain); // To work around a weird quirk in Java 1.2.2
259
String JavaDoc encoded1 = urlCodec.encode(plain, "UnicodeBig");
260         String JavaDoc encoded2 = urlCodec.encode(plain);
261         assertEquals(encoded1, encoded2);
262         this.validateState(urlCodec);
263     }
264 }
265
Popular Tags