View Javadoc

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 static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.fail;
23  
24  import java.nio.charset.UnsupportedCharsetException;
25  
26  import org.apache.commons.codec.CharEncoding;
27  import org.apache.commons.codec.DecoderException;
28  import org.apache.commons.codec.EncoderException;
29  import org.junit.Test;
30  
31  /**
32   * Quoted-printable codec test cases
33   *
34   * @version $Id: BCodecTest.java 1429868 2013-01-07 16:08:05Z ggregory $
35   */
36  public class BCodecTest {
37  
38      static final int SWISS_GERMAN_STUFF_UNICODE[] =
39          { 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
40  
41      static final int RUSSIAN_STUFF_UNICODE[] =
42          { 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
43  
44      private String constructString(final int[] unicodeChars) {
45          final StringBuilder buffer = new StringBuilder();
46          if (unicodeChars != null) {
47              for (final int unicodeChar : unicodeChars) {
48                  buffer.append((char) unicodeChar);
49              }
50          }
51          return buffer.toString();
52      }
53  
54      @Test
55      public void testNullInput() throws Exception {
56          final BCodec bcodec = new BCodec();
57          assertNull(bcodec.doDecoding(null));
58          assertNull(bcodec.doEncoding(null));
59      }
60  
61      @Test
62      public void testUTF8RoundTrip() throws Exception {
63  
64          final String ru_msg = constructString(RUSSIAN_STUFF_UNICODE);
65          final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE);
66  
67          final BCodec bcodec = new BCodec(CharEncoding.UTF_8);
68  
69          assertEquals("=?UTF-8?B?0JLRgdC10Lxf0L/RgNC40LLQtdGC?=", bcodec.encode(ru_msg));
70          assertEquals("=?UTF-8?B?R3LDvGV6aV96w6Rtw6Q=?=", bcodec.encode(ch_msg));
71  
72          assertEquals(ru_msg, bcodec.decode(bcodec.encode(ru_msg)));
73          assertEquals(ch_msg, bcodec.decode(bcodec.encode(ch_msg)));
74      }
75  
76      @Test
77      public void testBasicEncodeDecode() throws Exception {
78          final BCodec bcodec = new BCodec();
79          final String plain = "Hello there";
80          final String encoded = bcodec.encode(plain);
81          assertEquals("Basic B encoding test", "=?UTF-8?B?SGVsbG8gdGhlcmU=?=", encoded);
82          assertEquals("Basic B decoding test", plain, bcodec.decode(encoded));
83      }
84  
85      @Test
86      public void testEncodeDecodeNull() throws Exception {
87          final BCodec bcodec = new BCodec();
88          assertNull("Null string B encoding test", bcodec.encode((String) null));
89          assertNull("Null string B decoding test", bcodec.decode((String) null));
90      }
91  
92      @Test
93      public void testEncodeStringWithNull() throws Exception {
94          final BCodec bcodec = new BCodec();
95          final String test = null;
96          final String result = bcodec.encode(test, "charset");
97          assertEquals("Result should be null", null, result);
98      }
99  
100     @Test
101     public void testDecodeStringWithNull() throws Exception {
102         final BCodec bcodec = new BCodec();
103         final String test = null;
104         final String result = bcodec.decode(test);
105         assertEquals("Result should be null", null, result);
106     }
107 
108     @Test
109     public void testEncodeObjects() throws Exception {
110         final BCodec bcodec = new BCodec();
111         final String plain = "what not";
112         final String encoded = (String) bcodec.encode((Object) plain);
113 
114         assertEquals("Basic B encoding test", "=?UTF-8?B?d2hhdCBub3Q=?=", encoded);
115 
116         final Object result = bcodec.encode((Object) null);
117         assertEquals("Encoding a null Object should return null", null, result);
118 
119         try {
120             final Object dObj = new Double(3.0);
121             bcodec.encode(dObj);
122             fail("Trying to url encode a Double object should cause an exception.");
123         } catch (final EncoderException ee) {
124             // Exception expected, test segment passes.
125         }
126     }
127 
128     @Test(expected=UnsupportedCharsetException.class)
129     public void testInvalidEncoding() {
130         new BCodec("NONSENSE");
131     }
132 
133     @Test
134     public void testDecodeObjects() throws Exception {
135         final BCodec bcodec = new BCodec();
136         final String decoded = "=?UTF-8?B?d2hhdCBub3Q=?=";
137         final String plain = (String) bcodec.decode((Object) decoded);
138         assertEquals("Basic B decoding test", "what not", plain);
139 
140         final Object result = bcodec.decode((Object) null);
141         assertEquals("Decoding a null Object should return null", null, result);
142 
143         try {
144             final Object dObj = new Double(3.0);
145             bcodec.decode(dObj);
146             fail("Trying to url encode a Double object should cause an exception.");
147         } catch (final DecoderException ee) {
148             // Exception expected, test segment passes.
149         }
150     }
151 }