Added: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/StringBytesUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/StringBytesUtilsTest.java?rev=796172&view=auto ============================================================================== --- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/StringBytesUtilsTest.java (added) +++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/StringBytesUtilsTest.java Tue Jul 21 08:00:52 2009 @@ -0,0 +1,169 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.codec.binary; + +import java.io.UnsupportedEncodingException; +import java.util.Arrays; + +import junit.framework.Assert; +import junit.framework.TestCase; + +/** + * @author <a href="mailto:ggreg...@seagullsw.com">Gary Gregory</a> + * @version $Id: $ + */ +public class StringBytesUtilsTest extends TestCase { + + private static byte[] BYTES_FIXTURE; + + private static final String STRING_FIXTURE = "ABC"; + { + try { + BYTES_FIXTURE = "abc".getBytes("US-ASCII"); + } catch (UnsupportedEncodingException e) { + throw new IllegalArgumentException(e.toString()); + } + } + + public void testGetBytesIso8859_1() throws UnsupportedEncodingException { + String charsetName = "ISO-8859-1"; + testGetSupportedBytes(charsetName); + byte[] expected = STRING_FIXTURE.getBytes(charsetName); + byte[] actual = StringBytesUtils.getBytesIso8859_1(STRING_FIXTURE); + Assert.assertTrue(Arrays.equals(expected, actual)); + } + + public void testGetBytesUsAscii() throws UnsupportedEncodingException { + String charsetName = "US-ASCII"; + testGetSupportedBytes(charsetName); + byte[] expected = STRING_FIXTURE.getBytes(charsetName); + byte[] actual = StringBytesUtils.getBytesUsAscii(STRING_FIXTURE); + Assert.assertTrue(Arrays.equals(expected, actual)); + } + + public void testGetBytesUtf16() throws UnsupportedEncodingException { + String charsetName = "UTF-16"; + testGetSupportedBytes(charsetName); + byte[] expected = STRING_FIXTURE.getBytes(charsetName); + byte[] actual = StringBytesUtils.getBytesUtf16(STRING_FIXTURE); + Assert.assertTrue(Arrays.equals(expected, actual)); + } + + public void testGetBytesUtf16Be() throws UnsupportedEncodingException { + String charsetName = "UTF-16BE"; + testGetSupportedBytes(charsetName); + byte[] expected = STRING_FIXTURE.getBytes(charsetName); + byte[] actual = StringBytesUtils.getBytesUtf16Be(STRING_FIXTURE); + Assert.assertTrue(Arrays.equals(expected, actual)); + } + + public void testGetBytesUtf16Le() throws UnsupportedEncodingException { + String charsetName = "UTF-16LE"; + testGetSupportedBytes(charsetName); + byte[] expected = STRING_FIXTURE.getBytes(charsetName); + byte[] actual = StringBytesUtils.getBytesUtf16Le(STRING_FIXTURE); + Assert.assertTrue(Arrays.equals(expected, actual)); + } + + public void testGetBytesUtf8() throws UnsupportedEncodingException { + String charsetName = "UTF-8"; + testGetSupportedBytes(charsetName); + byte[] expected = STRING_FIXTURE.getBytes(charsetName); + byte[] actual = StringBytesUtils.getBytesUtf8(STRING_FIXTURE); + Assert.assertTrue(Arrays.equals(expected, actual)); + } + + private void testGetSupportedBytes(String charsetName) throws UnsupportedEncodingException { + byte[] expected = STRING_FIXTURE.getBytes(charsetName); + byte[] actual = StringBytesUtils.getSupportedBytes(STRING_FIXTURE, charsetName); + Assert.assertTrue(Arrays.equals(expected, actual)); + } + + public void testGetSupportedBytesBadEnc() { + try { + StringBytesUtils.getSupportedBytes(STRING_FIXTURE, "UNKNOWN"); + Assert.fail("Expected " + IllegalStateException.class.getName()); + } catch (IllegalStateException e) { + // Expected + } + } + + private void testNewString(String charsetName) throws UnsupportedEncodingException { + String expected = new String(BYTES_FIXTURE, charsetName); + String actual = StringBytesUtils.newString(BYTES_FIXTURE, charsetName); + Assert.assertEquals(expected, actual); + } + + public void testNewStringBadEnc() { + try { + StringBytesUtils.newString(BYTES_FIXTURE, "UNKNOWN"); + Assert.fail("Expected " + IllegalStateException.class.getName()); + } catch (IllegalStateException e) { + // Expected + } + } + + public void testNewStringIso8859_1() throws UnsupportedEncodingException { + String charsetName = "ISO-8859-1"; + testNewString(charsetName); + String expected = new String(BYTES_FIXTURE, charsetName); + String actual = StringBytesUtils.newStringIso8859_1(BYTES_FIXTURE); + Assert.assertEquals(expected, actual); + } + + public void testNewStringUsAscii() throws UnsupportedEncodingException { + String charsetName = "US-ASCII"; + testNewString(charsetName); + String expected = new String(BYTES_FIXTURE, charsetName); + String actual = StringBytesUtils.newStringUsAscii(BYTES_FIXTURE); + Assert.assertEquals(expected, actual); + } + + public void testNewStringUtf16() throws UnsupportedEncodingException { + String charsetName = "UTF-16"; + testNewString(charsetName); + String expected = new String(BYTES_FIXTURE, charsetName); + String actual = StringBytesUtils.newStringUtf16(BYTES_FIXTURE); + Assert.assertEquals(expected, actual); + } + + public void testNewStringUtf16Be() throws UnsupportedEncodingException { + String charsetName = "UTF-16BE"; + testNewString(charsetName); + String expected = new String(BYTES_FIXTURE, charsetName); + String actual = StringBytesUtils.newStringUtf16Be(BYTES_FIXTURE); + Assert.assertEquals(expected, actual); + } + + public void testNewStringUtf16Le() throws UnsupportedEncodingException { + String charsetName = "UTF-16LE"; + testNewString(charsetName); + String expected = new String(BYTES_FIXTURE, charsetName); + String actual = StringBytesUtils.newStringUtf16Le(BYTES_FIXTURE); + Assert.assertEquals(expected, actual); + } + + public void testNewStringUtf8() throws UnsupportedEncodingException { + String charsetName = "UTF-8"; + testNewString(charsetName); + String expected = new String(BYTES_FIXTURE, charsetName); + String actual = StringBytesUtils.newStringUtf8(BYTES_FIXTURE); + Assert.assertEquals(expected, actual); + } + +}
Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/BCodecTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/BCodecTest.java?rev=796172&r1=796171&r2=796172&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/BCodecTest.java (original) +++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/BCodecTest.java Tue Jul 21 08:00:52 2009 @@ -21,6 +21,7 @@ import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; +import org.apache.commons.codec.RequiredCharsetNames; /** * Quoted-printable codec test cases @@ -61,7 +62,7 @@ String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); - BCodec bcodec = new BCodec("UTF-8"); + BCodec bcodec = new BCodec(RequiredCharsetNames.UTF_8); assertEquals("=?UTF-8?B?0JLRgdC10Lxf0L/RgNC40LLQtdGC?=", bcodec.encode(ru_msg)); assertEquals("=?UTF-8?B?R3LDvGV6aV96w6Rtw6Q=?=", bcodec.encode(ch_msg)); Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/QCodecTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/QCodecTest.java?rev=796172&r1=796171&r2=796172&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/QCodecTest.java (original) +++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/QCodecTest.java Tue Jul 21 08:00:52 2009 @@ -22,6 +22,7 @@ import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; +import org.apache.commons.codec.RequiredCharsetNames; /** * Quoted-printable codec test cases @@ -65,7 +66,7 @@ String ru_msg = constructString(RUSSIAN_STUFF_UNICODE); String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); - QCodec qcodec = new QCodec("UTF-8"); + QCodec qcodec = new QCodec(RequiredCharsetNames.UTF_8); assertEquals( "=?UTF-8?Q?=D0=92=D1=81=D0=B5=D0=BC=5F=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82?=", Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/QuotedPrintableCodecTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/QuotedPrintableCodecTest.java?rev=796172&r1=796171&r2=796172&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/QuotedPrintableCodecTest.java (original) +++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/QuotedPrintableCodecTest.java Tue Jul 21 08:00:52 2009 @@ -21,6 +21,7 @@ import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; +import org.apache.commons.codec.RequiredCharsetNames; /** * Quoted-printable codec test cases @@ -62,12 +63,12 @@ assertEquals( "=D0=92=D1=81=D0=B5=D0=BC_=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82", - qpcodec.encode(ru_msg, "UTF-8") + qpcodec.encode(ru_msg, RequiredCharsetNames.UTF_8) ); - assertEquals("Gr=C3=BCezi_z=C3=A4m=C3=A4", qpcodec.encode(ch_msg, "UTF-8")); + assertEquals("Gr=C3=BCezi_z=C3=A4m=C3=A4", qpcodec.encode(ch_msg, RequiredCharsetNames.UTF_8)); - assertEquals(ru_msg, qpcodec.decode(qpcodec.encode(ru_msg, "UTF-8"), "UTF-8")); - assertEquals(ch_msg, qpcodec.decode(qpcodec.encode(ch_msg, "UTF-8"), "UTF-8")); + assertEquals(ru_msg, qpcodec.decode(qpcodec.encode(ru_msg, RequiredCharsetNames.UTF_8), RequiredCharsetNames.UTF_8)); + assertEquals(ch_msg, qpcodec.decode(qpcodec.encode(ch_msg, RequiredCharsetNames.UTF_8), RequiredCharsetNames.UTF_8)); } public void testBasicEncodeDecode() throws Exception { Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/RFC1522CodecTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/RFC1522CodecTest.java?rev=796172&r1=796171&r2=796172&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/RFC1522CodecTest.java (original) +++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/RFC1522CodecTest.java Tue Jul 21 08:00:52 2009 @@ -20,6 +20,7 @@ import junit.framework.TestCase; import org.apache.commons.codec.DecoderException; +import org.apache.commons.codec.RequiredCharsetNames; /** * RFC 1522 compliant codec test cases @@ -52,7 +53,7 @@ public void testNullInput() throws Exception { RFC1522TestCodec testcodec = new RFC1522TestCodec(); assertNull(testcodec.decodeText(null)); - assertNull(testcodec.encodeText(null, "UTF-8")); + assertNull(testcodec.encodeText(null, RequiredCharsetNames.UTF_8)); } public void testDecodeInvalid() throws Exception { Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/URLCodecTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/URLCodecTest.java?rev=796172&r1=796171&r2=796172&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/URLCodecTest.java (original) +++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/URLCodecTest.java Tue Jul 21 08:00:52 2009 @@ -23,6 +23,7 @@ import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; +import org.apache.commons.codec.RequiredCharsetNames; /** * URL codec test cases @@ -69,12 +70,12 @@ assertEquals( "%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82", - urlCodec.encode(ru_msg, "UTF-8") + urlCodec.encode(ru_msg, RequiredCharsetNames.UTF_8) ); - assertEquals("Gr%C3%BCezi_z%C3%A4m%C3%A4", urlCodec.encode(ch_msg, "UTF-8")); + assertEquals("Gr%C3%BCezi_z%C3%A4m%C3%A4", urlCodec.encode(ch_msg, RequiredCharsetNames.UTF_8)); - assertEquals(ru_msg, urlCodec.decode(urlCodec.encode(ru_msg, "UTF-8"), "UTF-8")); - assertEquals(ch_msg, urlCodec.decode(urlCodec.encode(ch_msg, "UTF-8"), "UTF-8")); + assertEquals(ru_msg, urlCodec.decode(urlCodec.encode(ru_msg, RequiredCharsetNames.UTF_8), RequiredCharsetNames.UTF_8)); + assertEquals(ch_msg, urlCodec.decode(urlCodec.encode(ch_msg, RequiredCharsetNames.UTF_8), RequiredCharsetNames.UTF_8)); this.validateState(urlCodec); }