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 QCodecTest 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 QCodecTest(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 testNullInput() throws Exception {
052: QCodec qcodec = new QCodec();
053: assertNull(qcodec.doDecoding(null));
054: assertNull(qcodec.doEncoding(null));
055: }
056:
057: public void testUTF8RoundTrip() throws Exception {
058:
059: String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
060: String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
061:
062: QCodec qcodec = new QCodec("UTF-8");
063:
064: assertEquals(
065: "=?UTF-8?Q?=D0=92=D1=81=D0=B5=D0=BC=5F=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82?=",
066: qcodec.encode(ru_msg));
067: assertEquals("=?UTF-8?Q?Gr=C3=BCezi=5Fz=C3=A4m=C3=A4?=", qcodec
068: .encode(ch_msg));
069:
070: assertEquals(ru_msg, qcodec.decode(qcodec.encode(ru_msg)));
071: assertEquals(ch_msg, qcodec.decode(qcodec.encode(ch_msg)));
072: }
073:
074: public void testBasicEncodeDecode() throws Exception {
075: QCodec qcodec = new QCodec();
076: String plain = "= Hello there =\r\n";
077: String encoded = qcodec.encode(plain);
078: assertEquals("Basic Q encoding test",
079: "=?UTF-8?Q?=3D Hello there =3D=0D=0A?=", encoded);
080: assertEquals("Basic Q decoding test", plain, qcodec
081: .decode(encoded));
082: }
083:
084: public void testUnsafeEncodeDecode() throws Exception {
085: QCodec qcodec = new QCodec();
086: String plain = "?_=\r\n";
087: String encoded = qcodec.encode(plain);
088: assertEquals("Unsafe chars Q encoding test",
089: "=?UTF-8?Q?=3F=5F=3D=0D=0A?=", encoded);
090: assertEquals("Unsafe chars Q decoding test", plain, qcodec
091: .decode(encoded));
092: }
093:
094: public void testEncodeDecodeNull() throws Exception {
095: QCodec qcodec = new QCodec();
096: assertNull("Null string Q encoding test", qcodec
097: .encode((String) null));
098: assertNull("Null string Q decoding test", qcodec
099: .decode((String) null));
100: }
101:
102: public void testEncodeStringWithNull() throws Exception {
103: QCodec qcodec = new QCodec();
104: String test = null;
105: String result = qcodec.encode(test, "charset");
106: assertEquals("Result should be null", null, result);
107: }
108:
109: public void testDecodeStringWithNull() throws Exception {
110: QCodec qcodec = new QCodec();
111: String test = null;
112: String result = qcodec.decode(test);
113: assertEquals("Result should be null", null, result);
114: }
115:
116: public void testEncodeObjects() throws Exception {
117: QCodec qcodec = new QCodec();
118: String plain = "1+1 = 2";
119: String encoded = (String) qcodec.encode((Object) plain);
120: assertEquals("Basic Q encoding test", "=?UTF-8?Q?1+1 =3D 2?=",
121: encoded);
122:
123: Object result = qcodec.encode((Object) null);
124: assertEquals("Encoding a null Object should return null", null,
125: result);
126:
127: try {
128: Object dObj = new Double(3.0);
129: qcodec.encode(dObj);
130: fail("Trying to url encode a Double object should cause an exception.");
131: } catch (EncoderException ee) {
132: // Exception expected, test segment passes.
133: }
134: }
135:
136: public void testInvalidEncoding() {
137: QCodec qcodec = new QCodec("NONSENSE");
138: try {
139: qcodec.encode("Hello there!");
140: fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
141: } catch (EncoderException ee) {
142: // Exception expected, test segment passes.
143: }
144: try {
145: qcodec.decode("=?NONSENSE?Q?Hello there!?=");
146: fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
147: } catch (DecoderException ee) {
148: // Exception expected, test segment passes.
149: }
150: }
151:
152: public void testDecodeObjects() throws Exception {
153: QCodec qcodec = new QCodec();
154: String decoded = "=?UTF-8?Q?1+1 =3D 2?=";
155: String plain = (String) qcodec.decode((Object) decoded);
156: assertEquals("Basic Q decoding test", "1+1 = 2", plain);
157:
158: Object result = qcodec.decode((Object) null);
159: assertEquals("Decoding a null Object should return null", null,
160: result);
161:
162: try {
163: Object dObj = new Double(3.0);
164: qcodec.decode(dObj);
165: fail("Trying to url encode a Double object should cause an exception.");
166: } catch (DecoderException ee) {
167: // Exception expected, test segment passes.
168: }
169: }
170:
171: public void testEncodeDecodeBlanks() throws Exception {
172: String plain = "Mind those pesky blanks";
173: String encoded1 = "=?UTF-8?Q?Mind those pesky blanks?=";
174: String encoded2 = "=?UTF-8?Q?Mind_those_pesky_blanks?=";
175: QCodec qcodec = new QCodec();
176: qcodec.setEncodeBlanks(false);
177: String s = qcodec.encode(plain);
178: assertEquals("Blanks encoding with the Q codec test", encoded1,
179: s);
180: qcodec.setEncodeBlanks(true);
181: s = qcodec.encode(plain);
182: assertEquals("Blanks encoding with the Q codec test", encoded2,
183: s);
184: s = qcodec.decode(encoded1);
185: assertEquals("Blanks decoding with the Q codec test", plain, s);
186: s = qcodec.decode(encoded2);
187: assertEquals("Blanks decoding with the Q codec test", plain, s);
188: }
189:
190: public void testLetUsMakeCloverHappy() throws Exception {
191: QCodec qcodec = new QCodec();
192: qcodec.setEncodeBlanks(true);
193: assertTrue(qcodec.isEncodeBlanks());
194: qcodec.setEncodeBlanks(false);
195: assertFalse(qcodec.isEncodeBlanks());
196: }
197:
198: }
|