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.assertNull;
21  import static org.junit.Assert.fail;
22  
23  import org.apache.commons.codec.CharEncoding;
24  import org.apache.commons.codec.DecoderException;
25  import org.junit.Test;
26  
27  /**
28   * RFC 1522 compliant codec test cases
29   *
30   * @version $Id: RFC1522CodecTest.java 1429868 2013-01-07 16:08:05Z ggregory $
31   */
32  public class RFC1522CodecTest {
33  
34      static class RFC1522TestCodec extends RFC1522Codec {
35  
36          @Override
37          protected byte[] doDecoding(final byte[] bytes) {
38              return bytes;
39          }
40  
41          @Override
42          protected byte[] doEncoding(final byte[] bytes) {
43              return bytes;
44          }
45  
46          @Override
47          protected String getEncoding() {
48              return "T";
49          }
50  
51      }
52  
53      @Test
54      public void testNullInput() throws Exception {
55          final RFC1522TestCodec testcodec = new RFC1522TestCodec();
56          assertNull(testcodec.decodeText(null));
57          assertNull(testcodec.encodeText(null, CharEncoding.UTF_8));
58      }
59  
60      private void assertExpectedDecoderException(final String s) throws Exception {
61          final RFC1522TestCodec testcodec = new RFC1522TestCodec();
62          try {
63              testcodec.decodeText(s);
64              fail("DecoderException should have been thrown");
65          } catch (final DecoderException e) {
66              // Expected.
67          }
68      }
69  
70      @Test
71      public void testDecodeInvalid() throws Exception {
72          assertExpectedDecoderException("whatever");
73          assertExpectedDecoderException("=?");
74          assertExpectedDecoderException("?=");
75          assertExpectedDecoderException("==");
76          assertExpectedDecoderException("=??=");
77          assertExpectedDecoderException("=?stuff?=");
78          assertExpectedDecoderException("=?UTF-8??=");
79          assertExpectedDecoderException("=?UTF-8?stuff?=");
80          assertExpectedDecoderException("=?UTF-8?T?stuff");
81          assertExpectedDecoderException("=??T?stuff?=");
82          assertExpectedDecoderException("=?UTF-8??stuff?=");
83          assertExpectedDecoderException("=?UTF-8?W?stuff?=");
84      }
85  
86  }