1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.codec.net;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.commons.codec.DecoderException;
23  import org.apache.commons.codec.EncoderException;
24  
25  /**
26   * Quoted-printable codec test cases
27   * 
28   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
29   * @version $Id: BCodecTest.java 480406 2006-11-29 04:56:58Z bayard $
30   */
31  public class BCodecTest extends TestCase {
32  
33      static final int SWISS_GERMAN_STUFF_UNICODE[] =
34          { 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
35  
36      static final int RUSSIAN_STUFF_UNICODE[] =
37          { 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
38  
39      public BCodecTest(String name) {
40          super(name);
41      }
42  
43      private String constructString(int[] unicodeChars) {
44          StringBuffer buffer = new StringBuffer();
45          if (unicodeChars != null) {
46              for (int i = 0; i < unicodeChars.length; i++) {
47                  buffer.append((char) unicodeChars[i]);
48              }
49          }
50          return buffer.toString();
51      }
52  
53      public void testNullInput() throws Exception {
54          BCodec bcodec = new BCodec();
55          assertNull(bcodec.doDecoding(null));
56          assertNull(bcodec.doEncoding(null));
57      }
58  
59      public void testUTF8RoundTrip() throws Exception {
60  
61          String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
62          String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
63  
64          BCodec bcodec = new BCodec("UTF-8");
65  
66          assertEquals("=?UTF-8?B?0JLRgdC10Lxf0L/RgNC40LLQtdGC?=", bcodec.encode(ru_msg));
67          assertEquals("=?UTF-8?B?R3LDvGV6aV96w6Rtw6Q=?=", bcodec.encode(ch_msg));
68  
69          assertEquals(ru_msg, bcodec.decode(bcodec.encode(ru_msg)));
70          assertEquals(ch_msg, bcodec.decode(bcodec.encode(ch_msg)));
71      }
72  
73      public void testBasicEncodeDecode() throws Exception {
74          BCodec bcodec = new BCodec();
75          String plain = "Hello there";
76          String encoded = bcodec.encode(plain);
77          assertEquals("Basic B encoding test", "=?UTF-8?B?SGVsbG8gdGhlcmU=?=", encoded);
78          assertEquals("Basic B decoding test", plain, bcodec.decode(encoded));
79      }
80  
81      public void testEncodeDecodeNull() throws Exception {
82          BCodec bcodec = new BCodec();
83          assertNull("Null string B encoding test", bcodec.encode((String) null));
84          assertNull("Null string B decoding test", bcodec.decode((String) null));
85      }
86  
87      public void testEncodeStringWithNull() throws Exception {
88          BCodec bcodec = new BCodec();
89          String test = null;
90          String result = bcodec.encode(test, "charset");
91          assertEquals("Result should be null", null, result);
92      }
93  
94      public void testDecodeStringWithNull() throws Exception {
95          BCodec bcodec = new BCodec();
96          String test = null;
97          String result = bcodec.decode(test);
98          assertEquals("Result should be null", null, result);
99      }
100 
101     public void testEncodeObjects() throws Exception {
102         BCodec bcodec = new BCodec();
103         String plain = "what not";
104         String encoded = (String) bcodec.encode((Object) plain);
105 
106         assertEquals("Basic B encoding test", "=?UTF-8?B?d2hhdCBub3Q=?=", encoded);
107 
108         Object result = bcodec.encode((Object) null);
109         assertEquals("Encoding a null Object should return null", null, result);
110 
111         try {
112             Object dObj = new Double(3.0);
113             bcodec.encode(dObj);
114             fail("Trying to url encode a Double object should cause an exception.");
115         } catch (EncoderException ee) {
116             // Exception expected, test segment passes.
117         }
118     }
119 
120     public void testInvalidEncoding() {
121         BCodec bcodec = new BCodec("NONSENSE");
122         try {
123             bcodec.encode("Hello there!");
124             fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked.");
125         } catch (EncoderException ee) {
126             // Exception expected, test segment passes.
127         }
128         try {
129             bcodec.decode("=?NONSENSE?B?Hello there!?=");
130             fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked.");
131         } catch (DecoderException ee) {
132             // Exception expected, test segment passes.
133         }
134     }
135 
136     public void testDecodeObjects() throws Exception {
137         BCodec bcodec = new BCodec();
138         String decoded = "=?UTF-8?B?d2hhdCBub3Q=?=";
139         String plain = (String) bcodec.decode((Object) decoded);
140         assertEquals("Basic B decoding test", "what not", plain);
141 
142         Object result = bcodec.decode((Object) null);
143         assertEquals("Decoding a null Object should return null", null, result);
144 
145         try {
146             Object dObj = new Double(3.0);
147             bcodec.decode(dObj);
148             fail("Trying to url encode a Double object should cause an exception.");
149         } catch (DecoderException ee) {
150             // Exception expected, test segment passes.
151         }
152     }
153 }