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