1 16 17 18 package org.apache.commons.codec.net; 19 20 import org.apache.commons.codec.DecoderException; 21 import org.apache.commons.codec.EncoderException; 22 23 import junit.framework.TestCase; 24 25 30 public class QCodecTest extends TestCase { 31 32 static final int SWISS_GERMAN_STUFF_UNICODE [] = { 33 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 34 }; 35 36 static final int RUSSIAN_STUFF_UNICODE [] = { 37 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 38 0x432, 0x435, 0x442 39 }; 40 41 public QCodecTest(String name) { 42 super(name); 43 } 44 45 private String constructString(int [] unicodeChars) { 46 StringBuffer buffer = new StringBuffer (); 47 if (unicodeChars != null) { 48 for (int i = 0; i < unicodeChars.length; i++) { 49 buffer.append((char)unicodeChars[i]); 50 } 51 } 52 return buffer.toString(); 53 } 54 55 public void testNullInput() throws Exception { 56 QCodec qcodec = new QCodec(); 57 assertNull(qcodec.doDecoding(null)); 58 assertNull(qcodec.doEncoding(null)); 59 } 60 61 public void testUTF8RoundTrip() throws Exception { 62 63 String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); 64 String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); 65 66 QCodec qcodec = new QCodec("UTF-8"); 67 68 assertEquals( 69 "=?UTF-8?Q?=D0=92=D1=81=D0=B5=D0=BC=5F=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82?=", 70 qcodec.encode(ru_msg) 71 ); 72 assertEquals("=?UTF-8?Q?Gr=C3=BCezi=5Fz=C3=A4m=C3=A4?=", qcodec.encode(ch_msg)); 73 74 assertEquals(ru_msg, qcodec.decode(qcodec.encode(ru_msg))); 75 assertEquals(ch_msg, qcodec.decode(qcodec.encode(ch_msg))); 76 } 77 78 79 public void testBasicEncodeDecode() throws Exception { 80 QCodec qcodec = new QCodec(); 81 String plain = "= Hello there =\r\n"; 82 String encoded = qcodec.encode(plain); 83 assertEquals("Basic Q encoding test", 84 "=?UTF-8?Q?=3D Hello there =3D=0D=0A?=", encoded); 85 assertEquals("Basic Q decoding test", 86 plain, qcodec.decode(encoded)); 87 } 88 89 public void testUnsafeEncodeDecode() throws Exception { 90 QCodec qcodec = new QCodec(); 91 String plain = "?_=\r\n"; 92 String encoded = qcodec.encode(plain); 93 assertEquals("Unsafe chars Q encoding test", 94 "=?UTF-8?Q?=3F=5F=3D=0D=0A?=", encoded); 95 assertEquals("Unsafe chars Q decoding test", 96 plain, qcodec.decode(encoded)); 97 } 98 99 public void testEncodeDecodeNull() throws Exception { 100 QCodec qcodec = new QCodec(); 101 assertNull("Null string Q encoding test", 102 qcodec.encode((String )null)); 103 assertNull("Null string Q decoding test", 104 qcodec.decode((String )null)); 105 } 106 107 public void testEncodeStringWithNull() throws Exception { 108 QCodec qcodec = new QCodec(); 109 String test = null; 110 String result = qcodec.encode( test, "charset" ); 111 assertEquals("Result should be null", null, result); 112 } 113 114 public void testDecodeStringWithNull() throws Exception { 115 QCodec qcodec = new QCodec(); 116 String test = null; 117 String result = qcodec.decode( test ); 118 assertEquals("Result should be null", null, result); 119 } 120 121 122 public void testEncodeObjects() throws Exception { 123 QCodec qcodec = new QCodec(); 124 String plain = "1+1 = 2"; 125 String encoded = (String ) qcodec.encode((Object ) plain); 126 assertEquals("Basic Q encoding test", 127 "=?UTF-8?Q?1+1 =3D 2?=", encoded); 128 129 Object result = qcodec.encode((Object ) null); 130 assertEquals( "Encoding a null Object should return null", null, result); 131 132 try { 133 Object dObj = new Double (3.0); 134 qcodec.encode( dObj ); 135 fail( "Trying to url encode a Double object should cause an exception."); 136 } catch( EncoderException ee ) { 137 } 139 } 140 141 142 public void testInvalidEncoding() { 143 QCodec qcodec = new QCodec("NONSENSE"); 144 try { 145 qcodec.encode("Hello there!"); 146 fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked."); 147 } catch( EncoderException ee ) { 148 } 150 try { 151 qcodec.decode("=?NONSENSE?Q?Hello there!?="); 152 fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked."); 153 } catch( DecoderException ee ) { 154 } 156 } 157 158 public void testDecodeObjects() throws Exception { 159 QCodec qcodec = new QCodec(); 160 String decoded = "=?UTF-8?Q?1+1 =3D 2?="; 161 String plain = (String ) qcodec.decode((Object ) decoded); 162 assertEquals("Basic Q decoding test", 163 "1+1 = 2", plain); 164 165 Object result = qcodec.decode((Object ) null); 166 assertEquals( "Decoding a null Object should return null", null, result); 167 168 try { 169 Object dObj = new Double (3.0); 170 qcodec.decode( dObj ); 171 fail( "Trying to url encode a Double object should cause an exception."); 172 } catch( DecoderException ee ) { 173 } 175 } 176 177 178 public void testEncodeDecodeBlanks() throws Exception { 179 String plain = "Mind those pesky blanks"; 180 String encoded1 = "=?UTF-8?Q?Mind those pesky blanks?="; 181 String encoded2 = "=?UTF-8?Q?Mind_those_pesky_blanks?="; 182 QCodec qcodec = new QCodec(); 183 qcodec.setEncodeBlanks(false); 184 String s = qcodec.encode(plain); 185 assertEquals("Blanks encoding with the Q codec test", encoded1, s); 186 qcodec.setEncodeBlanks(true); 187 s = qcodec.encode(plain); 188 assertEquals("Blanks encoding with the Q codec test", encoded2, s); 189 s = qcodec.decode(encoded1); 190 assertEquals("Blanks decoding with the Q codec test", plain, s); 191 s = qcodec.decode(encoded2); 192 assertEquals("Blanks decoding with the Q codec test", plain, s); 193 } 194 195 196 public void testLetUsMakeCloverHappy() throws Exception { 197 QCodec qcodec = new QCodec(); 198 qcodec.setEncodeBlanks(true); 199 assertTrue(qcodec.isEncodeBlanks()); 200 qcodec.setEncodeBlanks(false); 201 assertFalse(qcodec.isEncodeBlanks()); 202 } 203 204 } 205 | Popular Tags |