| /* |
| * Copyright 2001-2005 The Apache Software Foundation. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.commons.codec.net; |
| |
| import junit.framework.TestCase; |
| |
| import org.apache.commons.codec.DecoderException; |
| import org.apache.commons.codec.EncoderException; |
| |
| /** |
| * Quoted-printable codec test cases |
| * |
| * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> |
| * @version $Id$ |
| */ |
| public class QuotedPrintableCodecTest extends TestCase { |
| |
| static final int SWISS_GERMAN_STUFF_UNICODE [] = { |
| 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 |
| }; |
| |
| static final int RUSSIAN_STUFF_UNICODE [] = { |
| 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, |
| 0x432, 0x435, 0x442 |
| }; |
| |
| public QuotedPrintableCodecTest(String name) { |
| super(name); |
| } |
| |
| private String constructString(int [] unicodeChars) { |
| StringBuffer buffer = new StringBuffer(); |
| if (unicodeChars != null) { |
| for (int i = 0; i < unicodeChars.length; i++) { |
| buffer.append((char)unicodeChars[i]); |
| } |
| } |
| return buffer.toString(); |
| } |
| |
| public void testUTF8RoundTrip() throws Exception { |
| |
| String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); |
| String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); |
| |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| |
| assertEquals( |
| "=D0=92=D1=81=D0=B5=D0=BC_=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82", |
| qpcodec.encode(ru_msg, "UTF-8") |
| ); |
| assertEquals("Gr=C3=BCezi_z=C3=A4m=C3=A4", qpcodec.encode(ch_msg, "UTF-8")); |
| |
| assertEquals(ru_msg, qpcodec.decode(qpcodec.encode(ru_msg, "UTF-8"), "UTF-8")); |
| assertEquals(ch_msg, qpcodec.decode(qpcodec.encode(ch_msg, "UTF-8"), "UTF-8")); |
| } |
| |
| public void testBasicEncodeDecode() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| String plain = "= Hello there =\r\n"; |
| String encoded = qpcodec.encode(plain); |
| assertEquals("Basic quoted-printable encoding test", |
| "=3D Hello there =3D=0D=0A", encoded); |
| assertEquals("Basic quoted-printable decoding test", |
| plain, qpcodec.decode(encoded)); |
| } |
| |
| public void testSafeCharEncodeDecode() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| String plain = "abc123_-.*~!@#$%^&()+{}\"\\;:`,/[]"; |
| String encoded = qpcodec.encode(plain); |
| assertEquals("Safe chars quoted-printable encoding test", |
| plain, encoded); |
| assertEquals("Safe chars quoted-printable decoding test", |
| plain, qpcodec.decode(encoded)); |
| } |
| |
| |
| public void testUnsafeEncodeDecode() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| String plain = "=\r\n"; |
| String encoded = qpcodec.encode(plain); |
| assertEquals("Unsafe chars quoted-printable encoding test", |
| "=3D=0D=0A", encoded); |
| assertEquals("Unsafe chars quoted-printable decoding test", |
| plain, qpcodec.decode(encoded)); |
| } |
| |
| public void testEncodeDecodeNull() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| assertNull("Null string quoted-printable encoding test", |
| qpcodec.encode((String)null)); |
| assertNull("Null string quoted-printable decoding test", |
| qpcodec.decode((String)null)); |
| } |
| |
| |
| public void testDecodeInvalid() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| try { |
| qpcodec.decode("="); |
| fail("DecoderException should have been thrown"); |
| } catch(DecoderException e) { |
| // Expected. Move on |
| } |
| try { |
| qpcodec.decode("=A"); |
| fail("DecoderException should have been thrown"); |
| } catch(DecoderException e) { |
| // Expected. Move on |
| } |
| try { |
| qpcodec.decode("=WW"); |
| fail("DecoderException should have been thrown"); |
| } catch(DecoderException e) { |
| // Expected. Move on |
| } |
| } |
| |
| public void testEncodeNull() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| byte[] plain = null; |
| byte[] encoded = qpcodec.encode(plain); |
| assertEquals("Encoding a null string should return null", |
| null, encoded); |
| } |
| |
| public void testEncodeUrlWithNullBitSet() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| String plain = "1+1 = 2"; |
| String encoded = new String(QuotedPrintableCodec. |
| encodeQuotedPrintable(null, plain.getBytes())); |
| assertEquals("Basic quoted-printable encoding test", |
| "1+1 =3D 2", encoded); |
| assertEquals("Basic quoted-printable decoding test", |
| plain, qpcodec.decode(encoded)); |
| |
| } |
| |
| public void testDecodeWithNullArray() throws Exception { |
| byte[] plain = null; |
| byte[] result = QuotedPrintableCodec.decodeQuotedPrintable( plain ); |
| assertEquals("Result should be null", null, result); |
| } |
| |
| public void testEncodeStringWithNull() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| String test = null; |
| String result = qpcodec.encode( test, "charset" ); |
| assertEquals("Result should be null", null, result); |
| } |
| |
| public void testDecodeStringWithNull() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| String test = null; |
| String result = qpcodec.decode( test, "charset" ); |
| assertEquals("Result should be null", null, result); |
| } |
| |
| public void testEncodeObjects() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| String plain = "1+1 = 2"; |
| String encoded = (String) qpcodec.encode((Object) plain); |
| assertEquals("Basic quoted-printable encoding test", |
| "1+1 =3D 2", encoded); |
| |
| byte[] plainBA = plain.getBytes(); |
| byte[] encodedBA = (byte[]) qpcodec.encode((Object) plainBA); |
| encoded = new String(encodedBA); |
| assertEquals("Basic quoted-printable encoding test", |
| "1+1 =3D 2", encoded); |
| |
| Object result = qpcodec.encode((Object) null); |
| assertEquals( "Encoding a null Object should return null", null, result); |
| |
| try { |
| Object dObj = new Double(3.0); |
| qpcodec.encode( dObj ); |
| fail( "Trying to url encode a Double object should cause an exception."); |
| } catch( EncoderException ee ) { |
| // Exception expected, test segment passes. |
| } |
| } |
| |
| public void testInvalidEncoding() { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec("NONSENSE"); |
| String plain = "Hello there!"; |
| try { |
| qpcodec.encode(plain); |
| fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked."); |
| } catch( EncoderException ee ) { |
| // Exception expected, test segment passes. |
| } |
| try { |
| qpcodec.decode(plain); |
| fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked."); |
| } catch( DecoderException ee ) { |
| // Exception expected, test segment passes. |
| } |
| } |
| |
| public void testDecodeObjects() throws Exception { |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); |
| String plain = "1+1 =3D 2"; |
| String decoded = (String) qpcodec.decode((Object) plain); |
| assertEquals("Basic quoted-printable decoding test", |
| "1+1 = 2", decoded); |
| |
| byte[] plainBA = plain.getBytes(); |
| byte[] decodedBA = (byte[]) qpcodec.decode((Object) plainBA); |
| decoded = new String(decodedBA); |
| assertEquals("Basic quoted-printable decoding test", |
| "1+1 = 2", decoded); |
| |
| Object result = qpcodec.decode((Object) null); |
| assertEquals( "Decoding a null Object should return null", null, result); |
| |
| try { |
| Object dObj = new Double(3.0); |
| qpcodec.decode( dObj ); |
| fail( "Trying to url encode a Double object should cause an exception."); |
| } catch( DecoderException ee ) { |
| // Exception expected, test segment passes. |
| } |
| } |
| |
| public void testDefaultEncoding() throws Exception { |
| String plain = "Hello there!"; |
| QuotedPrintableCodec qpcodec = new QuotedPrintableCodec("UnicodeBig"); |
| qpcodec.encode(plain); // To work around a weird quirk in Java 1.2.2 |
| String encoded1 = qpcodec.encode(plain, "UnicodeBig"); |
| String encoded2 = qpcodec.encode(plain); |
| assertEquals(encoded1, encoded2); |
| } |
| } |