1 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 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 name) { 40 super(name); 41 } 42 43 private void validateState(URLCodec urlCodec) { 44 assertEquals(urlCodec.getEncoding(), urlCodec.getDefaultCharset()); 45 } 46 47 private String constructString(int [] unicodeChars) { 48 StringBuffer buffer = new StringBuffer (); 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 { 58 59 String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); 60 String 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 { 77 URLCodec urlCodec = new URLCodec(); 78 String plain = "Hello there!"; 79 String 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 { 89 URLCodec urlCodec = new URLCodec(); 90 String plain = "abc123_-.*"; 91 String 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 { 101 URLCodec urlCodec = new URLCodec(); 102 String plain = "~!@#$%^&()+{}\"\\;:`,/[]"; 103 String 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 { 113 URLCodec urlCodec = new URLCodec(); 114 assertNull("Null string URL encoding test", 115 urlCodec.encode((String )null)); 116 assertNull("Null string URL decoding test", 117 urlCodec.decode((String )null)); 118 this.validateState(urlCodec); 119 } 120 121 122 public void testDecodeInvalid() throws Exception { 123 URLCodec urlCodec = new URLCodec(); 124 try { 125 urlCodec.decode("%"); 126 fail("DecoderException should have been thrown"); 127 } catch(DecoderException e) { 128 } 130 try { 131 urlCodec.decode("%A"); 132 fail("DecoderException should have been thrown"); 133 } catch(DecoderException e) { 134 } 136 try { 137 urlCodec.decode("%WW"); 138 fail("DecoderException should have been thrown"); 139 } catch(DecoderException e) { 140 } 142 this.validateState(urlCodec); 143 } 144 145 public void testEncodeNull() throws Exception { 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 { 155 URLCodec urlCodec = new URLCodec(); 156 String plain = "Hello there!"; 157 String encoded = new String ( 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 { 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 { 172 URLCodec urlCodec = new URLCodec(); 173 String test = null; 174 String result = urlCodec.encode( test, "charset" ); 175 assertEquals("Result should be null", null, result); 176 } 177 178 public void testDecodeStringWithNull() throws Exception { 179 URLCodec urlCodec = new URLCodec(); 180 String test = null; 181 String result = urlCodec.decode( test, "charset" ); 182 assertEquals("Result should be null", null, result); 183 } 184 185 public void testEncodeObjects() throws Exception { 186 URLCodec urlCodec = new URLCodec(); 187 String plain = "Hello there!"; 188 String encoded = (String ) urlCodec.encode((Object ) 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 ) plainBA); 194 encoded = new String (encodedBA); 195 assertEquals("Basic URL encoding test", 196 "Hello+there%21", encoded); 197 198 Object result = urlCodec.encode((Object ) null); 199 assertEquals( "Encoding a null Object should return null", null, result); 200 201 try { 202 Object dObj = new Double (3.0); 203 urlCodec.encode( dObj ); 204 fail( "Trying to url encode a Double object should cause an exception."); 205 } catch( EncoderException ee ) { 206 } 208 this.validateState(urlCodec); 209 } 210 211 public void testInvalidEncoding() { 212 URLCodec urlCodec = new URLCodec("NONSENSE"); 213 String 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 } 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 } 226 this.validateState(urlCodec); 227 } 228 229 public void testDecodeObjects() throws Exception { 230 URLCodec urlCodec = new URLCodec(); 231 String plain = "Hello+there%21"; 232 String decoded = (String ) urlCodec.decode((Object ) plain); 233 assertEquals("Basic URL decoding test", 234 "Hello there!", decoded); 235 236 byte[] plainBA = plain.getBytes(); 237 byte[] decodedBA = (byte[]) urlCodec.decode((Object ) plainBA); 238 decoded = new String (decodedBA); 239 assertEquals("Basic URL decoding test", 240 "Hello there!", decoded); 241 242 Object result = urlCodec.decode((Object ) null); 243 assertEquals( "Decoding a null Object should return null", null, result); 244 245 try { 246 Object dObj = new Double (3.0); 247 urlCodec.decode( dObj ); 248 fail( "Trying to url encode a Double object should cause an exception."); 249 } catch( DecoderException ee ) { 250 } 252 this.validateState(urlCodec); 253 } 254 255 public void testDefaultEncoding() throws Exception { 256 String plain = "Hello there!"; 257 URLCodec urlCodec = new URLCodec("UnicodeBig"); 258 urlCodec.encode(plain); String encoded1 = urlCodec.encode(plain, "UnicodeBig"); 260 String encoded2 = urlCodec.encode(plain); 261 assertEquals(encoded1, encoded2); 262 this.validateState(urlCodec); 263 } 264 } 265 | Popular Tags |