1 16 17 package org.apache.commons.codec.net; 18 19 import org.apache.commons.codec.DecoderException; 20 import org.apache.commons.codec.EncoderException; 21 22 import junit.framework.TestCase; 23 24 29 public class BCodecTest extends TestCase { 30 31 static final int SWISS_GERMAN_STUFF_UNICODE[] = 32 { 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 }; 33 34 static final int RUSSIAN_STUFF_UNICODE[] = 35 { 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 }; 36 37 public BCodecTest(String name) { 38 super(name); 39 } 40 41 private String constructString(int[] unicodeChars) { 42 StringBuffer buffer = new StringBuffer (); 43 if (unicodeChars != null) { 44 for (int i = 0; i < unicodeChars.length; i++) { 45 buffer.append((char) unicodeChars[i]); 46 } 47 } 48 return buffer.toString(); 49 } 50 51 public void testNullInput() throws Exception { 52 BCodec bcodec = new BCodec(); 53 assertNull(bcodec.doDecoding(null)); 54 assertNull(bcodec.doEncoding(null)); 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 BCodec bcodec = new BCodec("UTF-8"); 63 64 assertEquals("=?UTF-8?B?0JLRgdC10Lxf0L/RgNC40LLQtdGC?=", bcodec.encode(ru_msg)); 65 assertEquals("=?UTF-8?B?R3LDvGV6aV96w6Rtw6Q=?=", bcodec.encode(ch_msg)); 66 67 assertEquals(ru_msg, bcodec.decode(bcodec.encode(ru_msg))); 68 assertEquals(ch_msg, bcodec.decode(bcodec.encode(ch_msg))); 69 } 70 71 public void testBasicEncodeDecode() throws Exception { 72 BCodec bcodec = new BCodec(); 73 String plain = "Hello there"; 74 String encoded = bcodec.encode(plain); 75 assertEquals("Basic B encoding test", "=?UTF-8?B?SGVsbG8gdGhlcmU=?=", encoded); 76 assertEquals("Basic B decoding test", plain, bcodec.decode(encoded)); 77 } 78 79 public void testEncodeDecodeNull() throws Exception { 80 BCodec bcodec = new BCodec(); 81 assertNull("Null string B encoding test", bcodec.encode((String ) null)); 82 assertNull("Null string B decoding test", bcodec.decode((String ) null)); 83 } 84 85 public void testEncodeStringWithNull() throws Exception { 86 BCodec bcodec = new BCodec(); 87 String test = null; 88 String result = bcodec.encode(test, "charset"); 89 assertEquals("Result should be null", null, result); 90 } 91 92 public void testDecodeStringWithNull() throws Exception { 93 BCodec bcodec = new BCodec(); 94 String test = null; 95 String result = bcodec.decode(test); 96 assertEquals("Result should be null", null, result); 97 } 98 99 public void testEncodeObjects() throws Exception { 100 BCodec bcodec = new BCodec(); 101 String plain = "what not"; 102 String encoded = (String ) bcodec.encode((Object ) plain); 103 104 assertEquals("Basic B encoding test", "=?UTF-8?B?d2hhdCBub3Q=?=", encoded); 105 106 Object result = bcodec.encode((Object ) null); 107 assertEquals("Encoding a null Object should return null", null, result); 108 109 try { 110 Object dObj = new Double (3.0); 111 bcodec.encode(dObj); 112 fail("Trying to url encode a Double object should cause an exception."); 113 } catch (EncoderException ee) { 114 } 116 } 117 118 public void testInvalidEncoding() { 119 BCodec bcodec = new BCodec("NONSENSE"); 120 try { 121 bcodec.encode("Hello there!"); 122 fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked."); 123 } catch (EncoderException ee) { 124 } 126 try { 127 bcodec.decode("=?NONSENSE?B?Hello there!?="); 128 fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked."); 129 } catch (DecoderException ee) { 130 } 132 } 133 134 public void testDecodeObjects() throws Exception { 135 BCodec bcodec = new BCodec(); 136 String decoded = "=?UTF-8?B?d2hhdCBub3Q=?="; 137 String plain = (String ) bcodec.decode((Object ) decoded); 138 assertEquals("Basic B decoding test", "what not", plain); 139 140 Object result = bcodec.decode((Object ) null); 141 assertEquals("Decoding a null Object should return null", null, result); 142 143 try { 144 Object dObj = new Double (3.0); 145 bcodec.decode(dObj); 146 fail("Trying to url encode a Double object should cause an exception."); 147 } catch (DecoderException ee) { 148 } 150 } 151 } 152 | Popular Tags |