001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.codec.net;
018:
019: import org.apache.commons.codec.DecoderException;
020: import org.apache.commons.codec.EncoderException;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * Quoted-printable codec test cases
026: *
027: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
028: */
029: public class QuotedPrintableCodecTest extends TestCase {
030:
031: static final int SWISS_GERMAN_STUFF_UNICODE[] = { 0x47, 0x72, 0xFC,
032: 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
033:
034: static final int RUSSIAN_STUFF_UNICODE[] = { 0x412, 0x441, 0x435,
035: 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
036:
037: public QuotedPrintableCodecTest(String name) {
038: super (name);
039: }
040:
041: private String constructString(int[] unicodeChars) {
042: StringBuffer buffer = new StringBuffer();
043: if (unicodeChars != null) {
044: for (int i = 0; i < unicodeChars.length; i++) {
045: buffer.append((char) unicodeChars[i]);
046: }
047: }
048: return buffer.toString();
049: }
050:
051: public void testUTF8RoundTrip() throws Exception {
052:
053: String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
054: String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
055:
056: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
057:
058: assertEquals(
059: "=D0=92=D1=81=D0=B5=D0=BC_=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82",
060: qpcodec.encode(ru_msg, "UTF-8"));
061: assertEquals("Gr=C3=BCezi_z=C3=A4m=C3=A4", qpcodec.encode(
062: ch_msg, "UTF-8"));
063:
064: assertEquals(ru_msg, qpcodec.decode(qpcodec.encode(ru_msg,
065: "UTF-8"), "UTF-8"));
066: assertEquals(ch_msg, qpcodec.decode(qpcodec.encode(ch_msg,
067: "UTF-8"), "UTF-8"));
068: }
069:
070: public void testBasicEncodeDecode() throws Exception {
071: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
072: String plain = "= Hello there =\r\n";
073: String encoded = qpcodec.encode(plain);
074: assertEquals("Basic quoted-printable encoding test",
075: "=3D Hello there =3D=0D=0A", encoded);
076: assertEquals("Basic quoted-printable decoding test", plain,
077: qpcodec.decode(encoded));
078: }
079:
080: public void testSafeCharEncodeDecode() throws Exception {
081: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
082: String plain = "abc123_-.*~!@#$%^&()+{}\"\\;:`,/[]";
083: String encoded = qpcodec.encode(plain);
084: assertEquals("Safe chars quoted-printable encoding test",
085: plain, encoded);
086: assertEquals("Safe chars quoted-printable decoding test",
087: plain, qpcodec.decode(encoded));
088: }
089:
090: public void testUnsafeEncodeDecode() throws Exception {
091: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
092: String plain = "=\r\n";
093: String encoded = qpcodec.encode(plain);
094: assertEquals("Unsafe chars quoted-printable encoding test",
095: "=3D=0D=0A", encoded);
096: assertEquals("Unsafe chars quoted-printable decoding test",
097: plain, qpcodec.decode(encoded));
098: }
099:
100: public void testEncodeDecodeNull() throws Exception {
101: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
102: assertNull("Null string quoted-printable encoding test",
103: qpcodec.encode((String) null));
104: assertNull("Null string quoted-printable decoding test",
105: qpcodec.decode((String) null));
106: }
107:
108: public void testDecodeInvalid() throws Exception {
109: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
110: try {
111: qpcodec.decode("=");
112: fail("DecoderException should have been thrown");
113: } catch (DecoderException e) {
114: // Expected. Move on
115: }
116: try {
117: qpcodec.decode("=A");
118: fail("DecoderException should have been thrown");
119: } catch (DecoderException e) {
120: // Expected. Move on
121: }
122: try {
123: qpcodec.decode("=WW");
124: fail("DecoderException should have been thrown");
125: } catch (DecoderException e) {
126: // Expected. Move on
127: }
128: }
129:
130: public void testEncodeNull() throws Exception {
131: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
132: byte[] plain = null;
133: byte[] encoded = qpcodec.encode(plain);
134: assertEquals("Encoding a null string should return null", null,
135: encoded);
136: }
137:
138: public void testEncodeUrlWithNullBitSet() throws Exception {
139: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
140: String plain = "1+1 = 2";
141: String encoded = new String(QuotedPrintableCodec
142: .encodeQuotedPrintable(null, plain.getBytes()));
143: assertEquals("Basic quoted-printable encoding test",
144: "1+1 =3D 2", encoded);
145: assertEquals("Basic quoted-printable decoding test", plain,
146: qpcodec.decode(encoded));
147:
148: }
149:
150: public void testDecodeWithNullArray() throws Exception {
151: byte[] plain = null;
152: byte[] result = QuotedPrintableCodec
153: .decodeQuotedPrintable(plain);
154: assertEquals("Result should be null", null, result);
155: }
156:
157: public void testEncodeStringWithNull() throws Exception {
158: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
159: String test = null;
160: String result = qpcodec.encode(test, "charset");
161: assertEquals("Result should be null", null, result);
162: }
163:
164: public void testDecodeStringWithNull() throws Exception {
165: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
166: String test = null;
167: String result = qpcodec.decode(test, "charset");
168: assertEquals("Result should be null", null, result);
169: }
170:
171: public void testEncodeObjects() throws Exception {
172: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
173: String plain = "1+1 = 2";
174: String encoded = (String) qpcodec.encode((Object) plain);
175: assertEquals("Basic quoted-printable encoding test",
176: "1+1 =3D 2", encoded);
177:
178: byte[] plainBA = plain.getBytes();
179: byte[] encodedBA = (byte[]) qpcodec.encode((Object) plainBA);
180: encoded = new String(encodedBA);
181: assertEquals("Basic quoted-printable encoding test",
182: "1+1 =3D 2", encoded);
183:
184: Object result = qpcodec.encode((Object) null);
185: assertEquals("Encoding a null Object should return null", null,
186: result);
187:
188: try {
189: Object dObj = new Double(3.0);
190: qpcodec.encode(dObj);
191: fail("Trying to url encode a Double object should cause an exception.");
192: } catch (EncoderException ee) {
193: // Exception expected, test segment passes.
194: }
195: }
196:
197: public void testInvalidEncoding() {
198: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(
199: "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: // Exception expected, test segment passes.
206: }
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: // Exception expected, test segment passes.
212: }
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", "1+1 = 2",
220: 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", "1+1 = 2",
226: decoded);
227:
228: Object result = qpcodec.decode((Object) null);
229: assertEquals("Decoding a null Object should return null", null,
230: result);
231:
232: try {
233: Object dObj = new Double(3.0);
234: qpcodec.decode(dObj);
235: fail("Trying to url encode a Double object should cause an exception.");
236: } catch (DecoderException ee) {
237: // Exception expected, test segment passes.
238: }
239: }
240:
241: public void testDefaultEncoding() throws Exception {
242: String plain = "Hello there!";
243: QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(
244: "UnicodeBig");
245: qpcodec.encode(plain); // To work around a weird quirk in Java 1.2.2
246: String encoded1 = qpcodec.encode(plain, "UnicodeBig");
247: String encoded2 = qpcodec.encode(plain);
248: assertEquals(encoded1, encoded2);
249: }
250: }
|