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.assertEquals; 021 import static org.junit.Assert.assertNull; 022 import static org.junit.Assert.fail; 023 024 import java.nio.charset.UnsupportedCharsetException; 025 026 import org.apache.commons.codec.CharEncoding; 027 import org.apache.commons.codec.DecoderException; 028 import org.apache.commons.codec.EncoderException; 029 import org.junit.Test; 030 031 /** 032 * Quoted-printable codec test cases 033 * 034 * @version $Id: BCodecTest.html 889935 2013-12-11 05:05:13Z ggregory $ 035 */ 036 public class BCodecTest { 037 038 static final int SWISS_GERMAN_STUFF_UNICODE[] = 039 { 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 }; 040 041 static final int RUSSIAN_STUFF_UNICODE[] = 042 { 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 }; 043 044 private String constructString(int[] unicodeChars) { 045 StringBuilder buffer = new StringBuilder(); 046 if (unicodeChars != null) { 047 for (int unicodeChar : unicodeChars) { 048 buffer.append((char) unicodeChar); 049 } 050 } 051 return buffer.toString(); 052 } 053 054 @Test 055 public void testNullInput() throws Exception { 056 BCodec bcodec = new BCodec(); 057 assertNull(bcodec.doDecoding(null)); 058 assertNull(bcodec.doEncoding(null)); 059 } 060 061 @Test 062 public void testUTF8RoundTrip() throws Exception { 063 064 String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); 065 String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); 066 067 BCodec bcodec = new BCodec(CharEncoding.UTF_8); 068 069 assertEquals("=?UTF-8?B?0JLRgdC10Lxf0L/RgNC40LLQtdGC?=", bcodec.encode(ru_msg)); 070 assertEquals("=?UTF-8?B?R3LDvGV6aV96w6Rtw6Q=?=", bcodec.encode(ch_msg)); 071 072 assertEquals(ru_msg, bcodec.decode(bcodec.encode(ru_msg))); 073 assertEquals(ch_msg, bcodec.decode(bcodec.encode(ch_msg))); 074 } 075 076 @Test 077 public void testBasicEncodeDecode() throws Exception { 078 BCodec bcodec = new BCodec(); 079 String plain = "Hello there"; 080 String encoded = bcodec.encode(plain); 081 assertEquals("Basic B encoding test", "=?UTF-8?B?SGVsbG8gdGhlcmU=?=", encoded); 082 assertEquals("Basic B decoding test", plain, bcodec.decode(encoded)); 083 } 084 085 @Test 086 public void testEncodeDecodeNull() throws Exception { 087 BCodec bcodec = new BCodec(); 088 assertNull("Null string B encoding test", bcodec.encode((String) null)); 089 assertNull("Null string B decoding test", bcodec.decode((String) null)); 090 } 091 092 @Test 093 public void testEncodeStringWithNull() throws Exception { 094 BCodec bcodec = new BCodec(); 095 String test = null; 096 String result = bcodec.encode(test, "charset"); 097 assertEquals("Result should be null", null, result); 098 } 099 100 @Test 101 public void testDecodeStringWithNull() throws Exception { 102 BCodec bcodec = new BCodec(); 103 String test = null; 104 String result = bcodec.decode(test); 105 assertEquals("Result should be null", null, result); 106 } 107 108 @Test 109 public void testEncodeObjects() throws Exception { 110 BCodec bcodec = new BCodec(); 111 String plain = "what not"; 112 String encoded = (String) bcodec.encode((Object) plain); 113 114 assertEquals("Basic B encoding test", "=?UTF-8?B?d2hhdCBub3Q=?=", encoded); 115 116 Object result = bcodec.encode((Object) null); 117 assertEquals("Encoding a null Object should return null", null, result); 118 119 try { 120 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 (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 BCodec bcodec = new BCodec(); 136 String decoded = "=?UTF-8?B?d2hhdCBub3Q=?="; 137 String plain = (String) bcodec.decode((Object) decoded); 138 assertEquals("Basic B decoding test", "what not", plain); 139 140 Object result = bcodec.decode((Object) null); 141 assertEquals("Decoding a null Object should return null", null, result); 142 143 try { 144 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 (DecoderException ee) { 148 // Exception expected, test segment passes. 149 } 150 } 151 }