001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.codec.net;
019
020 import static org.junit.Assert.assertNull;
021 import static org.junit.Assert.fail;
022
023 import org.apache.commons.codec.CharEncoding;
024 import org.apache.commons.codec.DecoderException;
025 import org.junit.Test;
026
027 /**
028 * RFC 1522 compliant codec test cases
029 *
030 * @version $Id: RFC1522CodecTest.html 889935 2013-12-11 05:05:13Z ggregory $
031 */
032 public class RFC1522CodecTest {
033
034 static class RFC1522TestCodec extends RFC1522Codec {
035
036 @Override
037 protected byte[] doDecoding(final byte[] bytes) {
038 return bytes;
039 }
040
041 @Override
042 protected byte[] doEncoding(final byte[] bytes) {
043 return bytes;
044 }
045
046 @Override
047 protected String getEncoding() {
048 return "T";
049 }
050
051 }
052
053 @Test
054 public void testNullInput() throws Exception {
055 final RFC1522TestCodec testcodec = new RFC1522TestCodec();
056 assertNull(testcodec.decodeText(null));
057 assertNull(testcodec.encodeText(null, CharEncoding.UTF_8));
058 }
059
060 private void assertExpectedDecoderException(final String s) throws Exception {
061 final RFC1522TestCodec testcodec = new RFC1522TestCodec();
062 try {
063 testcodec.decodeText(s);
064 fail("DecoderException should have been thrown");
065 } catch (final DecoderException e) {
066 // Expected.
067 }
068 }
069
070 @Test
071 public void testDecodeInvalid() throws Exception {
072 assertExpectedDecoderException("whatever");
073 assertExpectedDecoderException("=?");
074 assertExpectedDecoderException("?=");
075 assertExpectedDecoderException("==");
076 assertExpectedDecoderException("=??=");
077 assertExpectedDecoderException("=?stuff?=");
078 assertExpectedDecoderException("=?UTF-8??=");
079 assertExpectedDecoderException("=?UTF-8?stuff?=");
080 assertExpectedDecoderException("=?UTF-8?T?stuff");
081 assertExpectedDecoderException("=??T?stuff?=");
082 assertExpectedDecoderException("=?UTF-8??stuff?=");
083 assertExpectedDecoderException("=?UTF-8?W?stuff?=");
084 }
085
086 }