|
58 | 58 | package org.apache.commons.codec.net; |
59 | 59 |
|
60 | 60 | import org.apache.commons.codec.DecoderException; |
| 61 | +import org.apache.commons.codec.EncoderException; |
61 | 62 |
|
62 | 63 | import junit.framework.TestCase; |
63 | 64 |
|
@@ -172,4 +173,106 @@ public void testDecodeInvalid() throws Exception { |
172 | 173 | // Expected. Move on |
173 | 174 | } |
174 | 175 | } |
| 176 | + |
| 177 | + public void testEncodeNull() throws Exception { |
| 178 | + URLCodec urlcodec = new URLCodec(); |
| 179 | + byte[] plain = null; |
| 180 | + byte[] encoded = urlcodec.encode(plain); |
| 181 | + assertEquals("Encoding a null string should return null", |
| 182 | + null, encoded); |
| 183 | + } |
| 184 | + |
| 185 | + public void testEncodeUrlWithNullBitSet() throws Exception { |
| 186 | + URLCodec urlcodec = new URLCodec(); |
| 187 | + String plain = "Hello there!"; |
| 188 | + String encoded = new String( URLCodec.encodeUrl(null, plain.getBytes())); |
| 189 | + assertEquals("Basic URL encoding test", |
| 190 | + "Hello+there%21", encoded); |
| 191 | + assertEquals("Basic URL decoding test", |
| 192 | + plain, urlcodec.decode(encoded)); |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | + public void testDecodeWithNullArray() throws Exception { |
| 197 | + byte[] plain = null; |
| 198 | + byte[] result = URLCodec.decodeUrl( plain ); |
| 199 | + assertEquals("Result should be null", null, result); |
| 200 | + } |
| 201 | + |
| 202 | + public void testEncodeStringWithNull() throws Exception { |
| 203 | + URLCodec urlcodec = new URLCodec(); |
| 204 | + String test = null; |
| 205 | + String result = urlcodec.encode( test, "charset" ); |
| 206 | + assertEquals("Result should be null", null, result); |
| 207 | + } |
| 208 | + |
| 209 | + public void testDecodeStringWithNull() throws Exception { |
| 210 | + URLCodec urlcodec = new URLCodec(); |
| 211 | + String test = null; |
| 212 | + String result = urlcodec.decode( test, "charset" ); |
| 213 | + assertEquals("Result should be null", null, result); |
| 214 | + } |
| 215 | + |
| 216 | + public void testEncodeObjects() throws Exception { |
| 217 | + URLCodec urlcodec = new URLCodec(); |
| 218 | + String plain = "Hello there!"; |
| 219 | + String encoded = (String) urlcodec.encode((Object) plain); |
| 220 | + assertEquals("Basic URL encoding test", |
| 221 | + "Hello+there%21", encoded); |
| 222 | + |
| 223 | + byte[] plainBA = plain.getBytes(); |
| 224 | + byte[] encodedBA = (byte[]) urlcodec.encode((Object) plainBA); |
| 225 | + encoded = new String(encodedBA); |
| 226 | + assertEquals("Basic URL encoding test", |
| 227 | + "Hello+there%21", encoded); |
| 228 | + |
| 229 | + Object result = urlcodec.encode((Object) null); |
| 230 | + assertEquals( "Encoding a null Object should return null", null, result); |
| 231 | + |
| 232 | + try { |
| 233 | + Object dObj = new Double(3.0); |
| 234 | + urlcodec.encode( dObj ); |
| 235 | + fail( "Trying to url encode a Double object should cause an exception."); |
| 236 | + } catch( EncoderException ee ) { |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + public void testInvalidEncoding() { |
| 241 | + URLCodec urlcodec = new URLCodec("NONSENSE"); |
| 242 | + String plain = "Hello there!"; |
| 243 | + try { |
| 244 | + String encoded = urlcodec.encode(plain); |
| 245 | + fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked."); |
| 246 | + } catch( EncoderException ee ) { |
| 247 | + } |
| 248 | + try { |
| 249 | + String decoded = urlcodec.decode(plain); |
| 250 | + fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked."); |
| 251 | + } catch( DecoderException ee ) { |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + public void testDecodeObjects() throws Exception { |
| 256 | + URLCodec urlcodec = new URLCodec(); |
| 257 | + String plain = "Hello+there%21"; |
| 258 | + String decoded = (String) urlcodec.decode((Object) plain); |
| 259 | + assertEquals("Basic URL decoding test", |
| 260 | + "Hello there!", decoded); |
| 261 | + |
| 262 | + byte[] plainBA = plain.getBytes(); |
| 263 | + byte[] decodedBA = (byte[]) urlcodec.decode((Object) plainBA); |
| 264 | + decoded = new String(decodedBA); |
| 265 | + assertEquals("Basic URL decoding test", |
| 266 | + "Hello there!", decoded); |
| 267 | + |
| 268 | + Object result = urlcodec.decode((Object) null); |
| 269 | + assertEquals( "Decoding a null Object should return null", null, result); |
| 270 | + |
| 271 | + try { |
| 272 | + Object dObj = new Double(3.0); |
| 273 | + urlcodec.decode( dObj ); |
| 274 | + fail( "Trying to url encode a Double object should cause an exception."); |
| 275 | + } catch( DecoderException ee ) { |
| 276 | + } |
| 277 | + } |
175 | 278 | } |
0 commit comments