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