You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2011/08/12 21:50:57 UTC
svn commit: r1157231 [4/5] - in /commons/proper/codec/branches/generics: ./
src/java/org/apache/commons/codec/ src/java/org/apache/commons/codec/binary/
src/java/org/apache/commons/codec/language/
src/java/org/apache/commons/codec/language/bm/ src/java...
Copied: commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/BinaryCharCodecTest.java (from r1157197, commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/BinaryCodecTest.java)
URL: http://svn.apache.org/viewvc/commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/BinaryCharCodecTest.java?p2=commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/BinaryCharCodecTest.java&p1=commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/BinaryCodecTest.java&r1=1157197&r2=1157231&rev=1157231&view=diff
==============================================================================
--- commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/BinaryCodecTest.java (original)
+++ commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/BinaryCharCodecTest.java Fri Aug 12 19:50:56 2011
@@ -18,23 +18,21 @@
package org.apache.commons.codec.binary;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.EncoderException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
- * TestCase for BinaryCodec class.
+ * TestCase for BinaryCharCodec class.
*
* @author Apache Software Foundation
* @version $Id$
*/
-public class BinaryCodecTest {
+public class BinaryCharCodecTest {
/** mask with bit zero based index 0 raised */
private static final int BIT_0 = 0x01;
@@ -60,11 +58,11 @@ public class BinaryCodecTest {
private static final int BIT_7 = 0x80;
/** an instance of the binary codec */
- BinaryCodec instance = null;
+ BinaryCharCodec instance = null;
@Before
public void setUp() throws Exception {
- this.instance = new BinaryCodec();
+ this.instance = new BinaryCharCodec();
}
@After
@@ -72,25 +70,6 @@ public class BinaryCodecTest {
this.instance = null;
}
- // ------------------------------------------------------------------------
- //
- // Test decode(Object)
- //
- // ------------------------------------------------------------------------
- /**
- * Tests for Object decode(Object)
- */
- @Test
- public void testDecodeObjectException() {
- try {
- this.instance.decode(new Object());
- } catch (DecoderException e) {
- // all is well.
- return;
- }
- fail("Expected DecoderException");
- }
-
/**
* Tests for Object decode(Object)
*/
@@ -183,13 +162,13 @@ public class BinaryCodecTest {
if (encodeMe == null) {
decoded = instance.decode((byte[]) null);
} else {
- decoded = (byte[]) instance.decode((Object) encodeMe.getBytes("UTF-8"));
+ decoded = instance.decode(encodeMe.getBytes("UTF-8"));
}
assertEquals(new String(bits), new String(decoded));
if (encodeMe == null) {
- decoded = (byte[]) instance.decode((char[]) null);
+ decoded = instance.decode((char[]) null);
} else {
- decoded = (byte[]) instance.decode(encodeMe.toCharArray());
+ decoded = instance.decode(encodeMe.toCharArray());
}
assertEquals(new String(bits), new String(decoded));
}
@@ -386,90 +365,90 @@ public class BinaryCodecTest {
*/
@Test
public void testFromAsciiCharArray() {
- assertEquals(0, BinaryCodec.fromAscii((char[]) null).length);
- assertEquals(0, BinaryCodec.fromAscii(new char[0]).length);
+ assertEquals(0, BinaryCharCodec.fromAscii((char[]) null).length);
+ assertEquals(0, BinaryCharCodec.fromAscii(new char[0]).length);
// With a single raw binary
byte[] bits = new byte[1];
- byte[] decoded = BinaryCodec.fromAscii("00000000".toCharArray());
+ byte[] decoded = BinaryCharCodec.fromAscii("00000000".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0;
- decoded = BinaryCodec.fromAscii("00000001".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("00000001".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1;
- decoded = BinaryCodec.fromAscii("00000011".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("00000011".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2;
- decoded = BinaryCodec.fromAscii("00000111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("00000111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
- decoded = BinaryCodec.fromAscii("00001111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("00001111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
- decoded = BinaryCodec.fromAscii("00011111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("00011111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
- decoded = BinaryCodec.fromAscii("00111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("00111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
- decoded = BinaryCodec.fromAscii("01111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("01111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("11111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("11111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
// With a two raw binaries
bits = new byte[2];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0000000011111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("0000000011111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0000000111111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("0000000111111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0000001111111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("0000001111111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0000011111111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("0000011111111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0000111111111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("0000111111111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0001111111111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("0001111111111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0011111111111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("0011111111111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0111111111111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("0111111111111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("1111111111111111".toCharArray());
+ decoded = BinaryCharCodec.fromAscii("1111111111111111".toCharArray());
assertEquals(new String(bits), new String(decoded));
- assertEquals(0, BinaryCodec.fromAscii((char[]) null).length);
+ assertEquals(0, BinaryCharCodec.fromAscii((char[]) null).length);
}
// ------------------------------------------------------------------------
@@ -482,90 +461,90 @@ public class BinaryCodecTest {
*/
@Test
public void testFromAsciiByteArray() throws UnsupportedEncodingException {
- assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length);
- assertEquals(0, BinaryCodec.fromAscii(new byte[0]).length);
+ assertEquals(0, BinaryCharCodec.fromAscii((byte[]) null).length);
+ assertEquals(0, BinaryCharCodec.fromAscii(new byte[0]).length);
// With a single raw binary
byte[] bits = new byte[1];
- byte[] decoded = BinaryCodec.fromAscii("00000000".getBytes("UTF-8"));
+ byte[] decoded = BinaryCharCodec.fromAscii("00000000".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0;
- decoded = BinaryCodec.fromAscii("00000001".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("00000001".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1;
- decoded = BinaryCodec.fromAscii("00000011".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("00000011".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2;
- decoded = BinaryCodec.fromAscii("00000111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("00000111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
- decoded = BinaryCodec.fromAscii("00001111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("00001111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
- decoded = BinaryCodec.fromAscii("00011111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("00011111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
- decoded = BinaryCodec.fromAscii("00111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("00111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
- decoded = BinaryCodec.fromAscii("01111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("01111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[1];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("11111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("11111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
// With a two raw binaries
bits = new byte[2];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0000000011111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("0000000011111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0000000111111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("0000000111111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0000001111111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("0000001111111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0000011111111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("0000011111111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0000111111111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("0000111111111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0001111111111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("0001111111111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0011111111111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("0011111111111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("0111111111111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("0111111111111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
bits = new byte[2];
bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- decoded = BinaryCodec.fromAscii("1111111111111111".getBytes("UTF-8"));
+ decoded = BinaryCharCodec.fromAscii("1111111111111111".getBytes("UTF-8"));
assertEquals(new String(bits), new String(decoded));
- assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length);
+ assertEquals(0, BinaryCharCodec.fromAscii((byte[]) null).length);
}
// ------------------------------------------------------------------------
@@ -703,118 +682,118 @@ public class BinaryCodecTest {
public void testToAsciiBytes() {
// With a single raw binary
byte[] bits = new byte[1];
- String l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ String l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("00000000", l_encoded);
bits = new byte[1];
bits[0] = BIT_0;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("00000001", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("00000011", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("00000111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("00001111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("00011111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("00111111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("01111111", l_encoded);
bits = new byte[1];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("11111111", l_encoded);
// With a two raw binaries
bits = new byte[2];
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000000000000000", l_encoded);
bits = new byte[2];
bits[0] = BIT_0;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000000000000001", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000000000000011", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000000000000111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000000000001111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000000000011111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000000000111111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000000001111111", l_encoded);
bits = new byte[2];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000000011111111", l_encoded);
// work on the other byte now
bits = new byte[2];
bits[1] = BIT_0;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000000111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000001111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000011111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0000111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0001111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0011111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("0111111111111111", l_encoded);
bits = new byte[2];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiBytes(bits));
assertEquals("1111111111111111", l_encoded);
- assertEquals(0, BinaryCodec.toAsciiBytes((byte[]) null).length);
+ assertEquals(0, BinaryCharCodec.toAsciiBytes((byte[]) null).length);
}
// ------------------------------------------------------------------------
@@ -826,118 +805,118 @@ public class BinaryCodecTest {
public void testToAsciiChars() {
// With a single raw binary
byte[] bits = new byte[1];
- String l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ String l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("00000000", l_encoded);
bits = new byte[1];
bits[0] = BIT_0;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("00000001", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("00000011", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("00000111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("00001111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("00011111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("00111111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("01111111", l_encoded);
bits = new byte[1];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("11111111", l_encoded);
// With a two raw binaries
bits = new byte[2];
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000000000000000", l_encoded);
bits = new byte[2];
bits[0] = BIT_0;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000000000000001", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000000000000011", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000000000000111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000000000001111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000000000011111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000000000111111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000000001111111", l_encoded);
bits = new byte[2];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000000011111111", l_encoded);
// work on the other byte now
bits = new byte[2];
bits[1] = BIT_0;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000000111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000001111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000011111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0000111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0001111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0011111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("0111111111111111", l_encoded);
bits = new byte[2];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String(BinaryCodec.toAsciiChars(bits));
+ l_encoded = new String(BinaryCharCodec.toAsciiChars(bits));
assertEquals("1111111111111111", l_encoded);
- assertEquals(0, BinaryCodec.toAsciiChars((byte[]) null).length);
+ assertEquals(0, BinaryCharCodec.toAsciiChars((byte[]) null).length);
}
// ------------------------------------------------------------------------
@@ -952,116 +931,116 @@ public class BinaryCodecTest {
public void testToAsciiString() {
// With a single raw binary
byte[] bits = new byte[1];
- String l_encoded = BinaryCodec.toAsciiString(bits);
+ String l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("00000000", l_encoded);
bits = new byte[1];
bits[0] = BIT_0;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("00000001", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("00000011", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("00000111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("00001111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("00011111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("00111111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("01111111", l_encoded);
bits = new byte[1];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("11111111", l_encoded);
// With a two raw binaries
bits = new byte[2];
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000000000000000", l_encoded);
bits = new byte[2];
bits[0] = BIT_0;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000000000000001", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000000000000011", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000000000000111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000000000001111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000000000011111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000000000111111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000000001111111", l_encoded);
bits = new byte[2];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000000011111111", l_encoded);
// work on the other byte now
bits = new byte[2];
bits[1] = BIT_0;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000000111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000001111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000011111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0000111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0001111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0011111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("0111111111111111", l_encoded);
bits = new byte[2];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = BinaryCodec.toAsciiString(bits);
+ l_encoded = BinaryCharCodec.toAsciiString(bits);
assertEquals("1111111111111111", l_encoded);
}
@@ -1075,22 +1054,8 @@ public class BinaryCodecTest {
*/
@Test
public void testEncodeObjectNull() throws Exception {
- Object obj = new byte[0];
- assertEquals(0, ((char[]) instance.encode(obj)).length);
- }
-
- /*
- * Tests for Object encode(Object)
- */
- @Test
- public void testEncodeObjectException() {
- try {
- instance.encode("");
- } catch (EncoderException e) {
- // all is well.
- return;
- }
- fail("Expected EncoderException");
+ byte[] obj = new byte[0];
+ assertEquals(0, instance.encode(obj).length);
}
/*
@@ -1100,116 +1065,116 @@ public class BinaryCodecTest {
public void testEncodeObject() throws Exception {
// With a single raw binary
byte[] bits = new byte[1];
- String l_encoded = new String((char[]) instance.encode((Object) bits));
+ String l_encoded = new String(instance.encode(bits));
assertEquals("00000000", l_encoded);
bits = new byte[1];
bits[0] = BIT_0;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("00000001", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("00000011", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("00000111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("00001111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("00011111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("00111111", l_encoded);
bits = new byte[1];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("01111111", l_encoded);
bits = new byte[1];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("11111111", l_encoded);
// With a two raw binaries
bits = new byte[2];
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000000000000000", l_encoded);
bits = new byte[2];
bits[0] = BIT_0;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000000000000001", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000000000000011", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000000000000111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000000000001111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000000000011111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000000000111111", l_encoded);
bits = new byte[2];
bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000000001111111", l_encoded);
bits = new byte[2];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000000011111111", l_encoded);
// work on the other byte now
bits = new byte[2];
bits[1] = BIT_0;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000000111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000001111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000011111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0000111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0001111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0011111111111111", l_encoded);
bits = new byte[2];
bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("0111111111111111", l_encoded);
bits = new byte[2];
bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
- l_encoded = new String((char[]) instance.encode((Object) bits));
+ l_encoded = new String(instance.encode(bits));
assertEquals("1111111111111111", l_encoded);
}
}
Copied: commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexByteTest.java (from r1157197, commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexTest.java)
URL: http://svn.apache.org/viewvc/commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexByteTest.java?p2=commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexByteTest.java&p1=commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexTest.java&r1=1157197&r2=1157231&rev=1157231&view=diff
==============================================================================
--- commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexTest.java (original)
+++ commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexByteTest.java Fri Aug 12 19:50:56 2011
@@ -34,12 +34,12 @@ import org.apache.commons.codec.EncoderE
import org.junit.Test;
/**
- * Tests {@link org.apache.commons.codec.binary.Hex}.
+ * Tests {@link org.apache.commons.codec.binary.HexByteCodec}.
*
* @author Apache Software Foundation
* @version $Id$
*/
-public class HexTest {
+public class HexByteTest {
private static final String BAD_ENCODING_NAME = "UNKNOWN";
@@ -100,7 +100,7 @@ public class HexTest {
*/
private void checkDecodeHexOddCharacters(char[] data) {
try {
- Hex.decodeHex(data);
+ HexByteCodec.decodeHex(data);
fail("An exception wasn't thrown when trying to decode an odd number of characters");
} catch (DecoderException e) {
// Expected exception
@@ -140,7 +140,7 @@ public class HexTest {
return;
}
log(parent + "=" + name);
- Hex customCodec = new Hex(name);
+ HexByteCodec customCodec = new HexByteCodec(name);
// source data
String sourceString = "Hello World";
byte[] sourceBytes = sourceString.getBytes(name);
@@ -148,7 +148,7 @@ public class HexTest {
// encode source to hex string to bytes with charset
byte[] actualEncodedBytes = customCodec.encode(sourceBytes);
// encode source to hex string...
- String expectedHexString = Hex.encodeHexString(sourceBytes);
+ String expectedHexString = HexByteCodec.encodeHexString(sourceBytes);
// ... and get the bytes in the expected charset
byte[] expectedHexStringBytes = expectedHexString.getBytes(name);
Assert.assertTrue(Arrays.equals(expectedHexStringBytes, actualEncodedBytes));
@@ -157,7 +157,7 @@ public class HexTest {
assertEquals(name + ", expectedHexString=" + expectedHexString + ", actualStringFromBytes=" + actualStringFromBytes,
expectedHexString, actualStringFromBytes);
// second test:
- Hex utf8Codec = new Hex();
+ HexByteCodec utf8Codec = new HexByteCodec();
expectedHexString = "48656c6c6f20576f726c64";
byte[] decodedUtf8Bytes = (byte[]) utf8Codec.decode(expectedHexString);
actualStringFromBytes = new String(decodedUtf8Bytes, utf8Codec.getCharsetName());
@@ -172,7 +172,7 @@ public class HexTest {
@Test
public void testCustomCharsetBadNameEncodeByteArray() throws UnsupportedEncodingException {
try {
- new Hex(BAD_ENCODING_NAME).encode("Hello World".getBytes("UTF-8"));
+ new HexByteCodec(BAD_ENCODING_NAME).encode("Hello World".getBytes("UTF-8"));
fail("Expected " + IllegalStateException.class.getName());
} catch (IllegalStateException e) {
// Expected
@@ -182,7 +182,7 @@ public class HexTest {
@Test
public void testCustomCharsetBadNameEncodeObject() {
try {
- new Hex(BAD_ENCODING_NAME).encode("Hello World");
+ new HexByteCodec(BAD_ENCODING_NAME).encode("Hello World");
fail("Expected " + EncoderException.class.getName());
} catch (EncoderException e) {
// Expected
@@ -192,7 +192,7 @@ public class HexTest {
@Test
public void testCustomCharsetBadNameDecodeObject() throws UnsupportedEncodingException {
try {
- new Hex(BAD_ENCODING_NAME).decode("Hello World".getBytes("UTF-8"));
+ new HexByteCodec(BAD_ENCODING_NAME).decode("Hello World".getBytes("UTF-8"));
fail("Expected " + DecoderException.class.getName());
} catch (DecoderException e) {
// Expected
@@ -201,13 +201,13 @@ public class HexTest {
@Test
public void testCustomCharsetToString() {
- assertTrue(new Hex().toString().indexOf(Hex.DEFAULT_CHARSET_NAME) >= 0);
+ assertTrue(new HexByteCodec().toString().indexOf(HexByteCodec.DEFAULT_CHARSET_NAME) >= 0);
}
@Test
public void testDecodeArrayOddCharacters() {
try {
- new Hex().decode(new byte[]{65});
+ new HexByteCodec().decode(new byte[]{65});
fail("An exception wasn't thrown when trying to decode an odd number of characters");
} catch (DecoderException e) {
// Expected exception
@@ -217,7 +217,7 @@ public class HexTest {
@Test
public void testDecodeBadCharacterPos0() {
try {
- new Hex().decode("q0");
+ new HexByteCodec().decode("q0");
fail("An exception wasn't thrown when trying to decode an illegal character");
} catch (DecoderException e) {
// Expected exception
@@ -227,7 +227,7 @@ public class HexTest {
@Test
public void testDecodeBadCharacterPos1() {
try {
- new Hex().decode("0q");
+ new HexByteCodec().decode("0q");
fail("An exception wasn't thrown when trying to decode an illegal character");
} catch (DecoderException e) {
// Expected exception
@@ -235,16 +235,6 @@ public class HexTest {
}
@Test
- public void testDecodeClassCastException() {
- try {
- new Hex().decode(new int[]{65});
- fail("An exception wasn't thrown when trying to decode.");
- } catch (DecoderException e) {
- // Expected exception
- }
- }
-
- @Test
public void testDecodeHexOddCharacters1() {
checkDecodeHexOddCharacters(new char[]{'A'});
}
@@ -262,7 +252,7 @@ public class HexTest {
@Test
public void testDecodeStringOddCharacters() {
try {
- new Hex().decode("6");
+ new HexByteCodec().decode("6");
fail("An exception wasn't thrown when trying to decode an odd number of characters");
} catch (DecoderException e) {
// Expected exception
@@ -271,33 +261,23 @@ public class HexTest {
@Test
public void testDencodeEmpty() throws DecoderException {
- assertTrue(Arrays.equals(new byte[0], Hex.decodeHex(new char[0])));
- assertTrue(Arrays.equals(new byte[0], new Hex().decode(new byte[0])));
- assertTrue(Arrays.equals(new byte[0], (byte[]) new Hex().decode("")));
- }
-
- @Test
- public void testEncodeClassCastException() {
- try {
- new Hex().encode(new int[]{65});
- fail("An exception wasn't thrown when trying to encode.");
- } catch (EncoderException e) {
- // Expected exception
- }
+ assertTrue(Arrays.equals(new byte[0], HexByteCodec.decodeHex(new char[0])));
+ assertTrue(Arrays.equals(new byte[0], new HexByteCodec().decode(new byte[0])));
+ assertTrue(Arrays.equals(new byte[0], (byte[]) new HexByteCodec().decode("")));
}
@Test
public void testEncodeDecodeRandom() throws DecoderException, EncoderException {
Random random = new Random();
- Hex hex = new Hex();
+ HexByteCodec hex = new HexByteCodec();
for (int i = 5; i > 0; i--) {
byte[] data = new byte[random.nextInt(10000) + 1];
random.nextBytes(data);
// static API
- char[] encodedChars = Hex.encodeHex(data);
- byte[] decodedBytes = Hex.decodeHex(encodedChars);
+ char[] encodedChars = HexByteCodec.encodeHex(data);
+ byte[] decodedBytes = HexByteCodec.decodeHex(encodedChars);
assertTrue(Arrays.equals(data, decodedBytes));
// instance API with array parameter
@@ -321,14 +301,14 @@ public class HexTest {
@Test
public void testEncodeEmpty() throws EncoderException {
- assertTrue(Arrays.equals(new char[0], Hex.encodeHex(new byte[0])));
- assertTrue(Arrays.equals(new byte[0], new Hex().encode(new byte[0])));
- assertTrue(Arrays.equals(new char[0], (char[]) new Hex().encode("")));
+ assertTrue(Arrays.equals(new char[0], HexByteCodec.encodeHex(new byte[0])));
+ assertTrue(Arrays.equals(new byte[0], new HexByteCodec().encode(new byte[0])));
+ assertTrue(Arrays.equals(new char[0], (char[]) new HexByteCodec().encode("")));
}
@Test
public void testEncodeZeroes() {
- char[] c = Hex.encodeHex(new byte[36]);
+ char[] c = HexByteCodec.encodeHex(new byte[36]);
assertEquals("000000000000000000000000000000000000000000000000000000000000000000000000", new String(c));
}
@@ -337,11 +317,11 @@ public class HexTest {
byte[] b = StringUtils.getBytesUtf8("Hello World");
final String expected = "48656c6c6f20576f726c64";
char[] actual;
- actual = Hex.encodeHex(b);
+ actual = HexByteCodec.encodeHex(b);
assertTrue(expected.equals(new String(actual)));
- actual = Hex.encodeHex(b, true);
+ actual = HexByteCodec.encodeHex(b, true);
assertTrue(expected.equals(new String(actual)));
- actual = Hex.encodeHex(b, false);
+ actual = HexByteCodec.encodeHex(b, false);
assertFalse(expected.equals(new String(actual)));
}
@@ -350,11 +330,11 @@ public class HexTest {
byte[] b = StringUtils.getBytesUtf8("Hello World");
final String expected = "48656C6C6F20576F726C64";
char[] actual;
- actual = Hex.encodeHex(b);
+ actual = HexByteCodec.encodeHex(b);
assertFalse(expected.equals(new String(actual)));
- actual = Hex.encodeHex(b, true);
+ actual = HexByteCodec.encodeHex(b, true);
assertFalse(expected.equals(new String(actual)));
- actual = Hex.encodeHex(b, false);
+ actual = HexByteCodec.encodeHex(b, false);
assertTrue(expected.equals(new String(actual)));
}
Added: commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexCharTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexCharTest.java?rev=1157231&view=auto
==============================================================================
--- commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexCharTest.java (added)
+++ commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/binary/HexCharTest.java Fri Aug 12 19:50:56 2011
@@ -0,0 +1,347 @@
+/*
+ * 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 static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.util.Arrays;
+import java.util.Random;
+
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.EncoderException;
+import org.junit.Test;
+
+/**
+ * Tests {@link org.apache.commons.codec.binary.HexCharCodec}.
+ *
+ * @author Apache Software Foundation
+ * @version $Id: HexByteTest.java 1156841 2011-08-11 21:33:42Z ggregory $
+ */
+public class HexCharTest {
+
+ private static final String BAD_ENCODING_NAME = "UNKNOWN";
+
+ private final static boolean LOG = false;
+
+ private boolean charsetSanityCheck(String name) {
+ final String source = "the quick brown dog jumped over the lazy fox";
+ try {
+ byte[] bytes = source.getBytes(name);
+ String str = new String(bytes, name);
+ boolean equals = source.equals(str);
+ if (equals == false) {
+ // Here with:
+ //
+ // Java Sun 1.4.2_19 x86 32-bits on Windows XP
+ // JIS_X0212-1990
+ // x-JIS0208
+ //
+ // Java Sun 1.5.0_17 x86 32-bits on Windows XP
+ // JIS_X0212-1990
+ // x-IBM834
+ // x-JIS0208
+ // x-MacDingbat
+ // x-MacSymbol
+ //
+ // Java Sun 1.6.0_14 x86 32-bits
+ // JIS_X0212-1990
+ // x-IBM834
+ // x-JIS0208
+ // x-MacDingbat
+ // x-MacSymbol
+ //
+ log("FAILED charsetSanityCheck=Interesting Java charset oddity: Roundtrip failed for " + name);
+ }
+ return equals;
+ } catch (UnsupportedEncodingException e) {
+ // Should NEVER happen since we are getting the name from the Charset class.
+ if (LOG) {
+ log("FAILED charsetSanityCheck=" + name + ", e=" + e);
+ log(e);
+ }
+ return false;
+ } catch (UnsupportedOperationException e) {
+ // Caught here with:
+ // x-JISAutoDetect on Windows XP and Java Sun 1.4.2_19 x86 32-bits
+ // x-JISAutoDetect on Windows XP and Java Sun 1.5.0_17 x86 32-bits
+ // x-JISAutoDetect on Windows XP and Java Sun 1.6.0_14 x86 32-bits
+ if (LOG) {
+ log("FAILED charsetSanityCheck=" + name + ", e=" + e);
+ log(e);
+ }
+ return false;
+ }
+ }
+
+ /**
+ * @param data
+ */
+ private void checkDecodeHexOddCharacters(char[] data) {
+ try {
+ HexCharCodec.decodeHex(data);
+ fail("An exception wasn't thrown when trying to decode an odd number of characters");
+ } catch (DecoderException e) {
+ // Expected exception
+ }
+ }
+
+ private void log(String s) {
+ if (LOG) {
+ System.out.println(s);
+ System.out.flush();
+ }
+ }
+
+ private void log(Throwable t) {
+ if (LOG) {
+ t.printStackTrace(System.out);
+ System.out.flush();
+ }
+ }
+
+ @Test
+ public void testCustomCharset() throws UnsupportedEncodingException, DecoderException, EncoderException {
+ for (String charsetName : Charset.availableCharsets().keySet()) {
+ testCustomCharset(charsetName, "testCustomCharset");
+ }
+ }
+
+ /**
+ * @param charsetName
+ * @param parent
+ * TODO
+ * @throws UnsupportedEncodingException
+ * @throws DecoderException
+ * @throws EncoderException
+ */
+ private void testCustomCharset(String charsetName, String parent) throws UnsupportedEncodingException, DecoderException,
+ EncoderException {
+ if (charsetSanityCheck(charsetName) == false) {
+ return;
+ }
+ log(parent + "=" + charsetName);
+ HexCharCodec customCodec = new HexCharCodec(charsetName);
+ // source data
+ String sourceString = "Hello World";
+ byte[] sourceBytes = sourceString.getBytes(charsetName);
+ // test 1
+ // encode source to hex string to bytes with charset
+ char[] actualEncodedChars = customCodec.encode(sourceString);
+ // encode source to hex string...
+ String expectedHexString = HexCharCodec.encodeHexString(sourceBytes);
+ // ... and get the bytes in the expected charset
+ char[] expectedHexStringChars = expectedHexString.toCharArray();
+ assertArrayEquals(expectedHexStringChars, actualEncodedChars);
+ // test 2
+ String actualStringFromBytes = new String(actualEncodedChars);
+ assertEquals(charsetName + ", expectedHexString=" + expectedHexString + ", actualStringFromBytes=" + actualStringFromBytes,
+ expectedHexString, actualStringFromBytes);
+ // second test:
+ HexCharCodec utf8Codec = new HexCharCodec();
+ expectedHexString = "48656c6c6f20576f726c64";
+ byte[] decodedUtf8Bytes = utf8Codec.decode(expectedHexString);
+ actualStringFromBytes = new String(decodedUtf8Bytes, utf8Codec.getCharsetName());
+ // sanity check:
+ assertEquals(charsetName, sourceString, actualStringFromBytes);
+ // actual check:
+ byte[] decodedCustomBytes = customCodec.decode(actualEncodedChars);
+ actualStringFromBytes = new String(decodedCustomBytes, charsetName);
+ assertEquals(charsetName, sourceString, actualStringFromBytes);
+ }
+
+ @Test
+ public void testCustomCharsetBadNameEncodeByteArray() throws UnsupportedEncodingException {
+ // The charsetName is not used by encode(byte[])
+ new HexCharCodec(BAD_ENCODING_NAME).encode("Hello World".getBytes("UTF-8"));
+ }
+
+ @Test
+ public void testCustomCharsetBadNameEncodeObject() {
+ try {
+ new HexCharCodec(BAD_ENCODING_NAME).encode("Hello World");
+ fail("Expected " + EncoderException.class.getName());
+ } catch (EncoderException e) {
+ // Expected
+ }
+ }
+
+ @Test
+ public void testCustomCharsetBadNameDecodeObject() throws UnsupportedEncodingException {
+ try {
+ new HexCharCodec(BAD_ENCODING_NAME).decode("Hello World".getBytes("UTF-8"));
+ fail("Expected " + DecoderException.class.getName());
+ } catch (DecoderException e) {
+ // Expected
+ }
+ }
+
+ @Test
+ public void testCustomCharsetToString() {
+ assertTrue(new HexCharCodec().toString().indexOf(HexCharCodec.DEFAULT_CHARSET_NAME) >= 0);
+ }
+
+ @Test
+ public void testDecodeArrayOddCharacters() {
+ try {
+ new HexCharCodec().decode(new byte[] { 65 });
+ fail("An exception wasn't thrown when trying to decode an odd number of characters");
+ } catch (DecoderException e) {
+ // Expected exception
+ }
+ }
+
+ @Test
+ public void testDecodeBadCharacterPos0() {
+ try {
+ new HexCharCodec().decode("q0");
+ fail("An exception wasn't thrown when trying to decode an illegal character");
+ } catch (DecoderException e) {
+ // Expected exception
+ }
+ }
+
+ @Test
+ public void testDecodeBadCharacterPos1() {
+ try {
+ new HexCharCodec().decode("0q");
+ fail("An exception wasn't thrown when trying to decode an illegal character");
+ } catch (DecoderException e) {
+ // Expected exception
+ }
+ }
+
+ @Test
+ public void testDecodeHexOddCharacters1() {
+ checkDecodeHexOddCharacters(new char[] { 'A' });
+ }
+
+ @Test
+ public void testDecodeHexOddCharacters3() {
+ checkDecodeHexOddCharacters(new char[] { 'A', 'B', 'C' });
+ }
+
+ @Test
+ public void testDecodeHexOddCharacters5() {
+ checkDecodeHexOddCharacters(new char[] { 'A', 'B', 'C', 'D', 'E' });
+ }
+
+ @Test
+ public void testDecodeStringOddCharacters() {
+ try {
+ new HexCharCodec().decode("6");
+ fail("An exception wasn't thrown when trying to decode an odd number of characters");
+ } catch (DecoderException e) {
+ // Expected exception
+ }
+ }
+
+ @Test
+ public void testDencodeEmpty() throws DecoderException {
+ assertTrue(Arrays.equals(new byte[0], HexCharCodec.decodeHex(new char[0])));
+ assertTrue(Arrays.equals(new byte[0], new HexCharCodec().decode(new byte[0])));
+ assertTrue(Arrays.equals(new byte[0], (byte[]) new HexCharCodec().decode("")));
+ }
+
+ @Test
+ public void testEncodeDecodeRandom() throws DecoderException, EncoderException {
+ Random random = new Random();
+
+ HexCharCodec hex = new HexCharCodec();
+ for (int i = 5; i > 0; i--) {
+ byte[] data = new byte[random.nextInt(10000) + 1];
+ random.nextBytes(data);
+
+ // static API
+ char[] encodedChars = HexCharCodec.encodeHex(data);
+ byte[] decodedBytes = HexCharCodec.decodeHex(encodedChars);
+ assertTrue(Arrays.equals(data, decodedBytes));
+
+ // instance API with array parameter
+ char[] encodedStringBytes = hex.encode(data);
+ decodedBytes = hex.decode(encodedStringBytes);
+ assertTrue(Arrays.equals(data, decodedBytes));
+
+ // instance API with char[] (Object) parameter
+ String dataString = new String(encodedChars);
+ char[] encodedStringChars = (char[]) hex.encode(dataString);
+ decodedBytes = (byte[]) hex.decode(encodedStringChars);
+ assertTrue(Arrays.equals(StringUtils.getBytesUtf8(dataString), decodedBytes));
+
+ // instance API with String (Object) parameter
+ dataString = new String(encodedChars);
+ encodedStringChars = (char[]) hex.encode(dataString);
+ decodedBytes = (byte[]) hex.decode(new String(encodedStringChars));
+ assertTrue(Arrays.equals(StringUtils.getBytesUtf8(dataString), decodedBytes));
+ }
+ }
+
+ @Test
+ public void testEncodeEmpty() throws EncoderException {
+ assertTrue(Arrays.equals(new char[0], HexCharCodec.encodeHex(new byte[0])));
+ assertTrue(Arrays.equals(new char[0], new HexCharCodec().encode(new byte[0])));
+ assertTrue(Arrays.equals(new char[0], (char[]) new HexCharCodec().encode("")));
+ }
+
+ @Test
+ public void testEncodeZeroes() {
+ char[] c = HexCharCodec.encodeHex(new byte[36]);
+ assertEquals("000000000000000000000000000000000000000000000000000000000000000000000000", new String(c));
+ }
+
+ @Test
+ public void testHelloWorldLowerCaseHex() {
+ byte[] b = StringUtils.getBytesUtf8("Hello World");
+ final String expected = "48656c6c6f20576f726c64";
+ char[] actual;
+ actual = HexCharCodec.encodeHex(b);
+ assertTrue(expected.equals(new String(actual)));
+ actual = HexCharCodec.encodeHex(b, true);
+ assertTrue(expected.equals(new String(actual)));
+ actual = HexCharCodec.encodeHex(b, false);
+ assertFalse(expected.equals(new String(actual)));
+ }
+
+ @Test
+ public void testHelloWorldUpperCaseHex() {
+ byte[] b = StringUtils.getBytesUtf8("Hello World");
+ final String expected = "48656C6C6F20576F726C64";
+ char[] actual;
+ actual = HexCharCodec.encodeHex(b);
+ assertFalse(expected.equals(new String(actual)));
+ actual = HexCharCodec.encodeHex(b, true);
+ assertFalse(expected.equals(new String(actual)));
+ actual = HexCharCodec.encodeHex(b, false);
+ assertTrue(expected.equals(new String(actual)));
+ }
+
+ @Test
+ public void testRequiredCharset() throws UnsupportedEncodingException, DecoderException, EncoderException {
+ testCustomCharset("UTF-8", "testRequiredCharset");
+ testCustomCharset("UTF-16", "testRequiredCharset");
+ testCustomCharset("UTF-16BE", "testRequiredCharset");
+ testCustomCharset("UTF-16LE", "testRequiredCharset");
+ testCustomCharset("US-ASCII", "testRequiredCharset");
+ testCustomCharset("ISO8859_1", "testRequiredCharset");
+ }
+}
\ No newline at end of file
Modified: commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/language/DoubleMetaphoneTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/language/DoubleMetaphoneTest.java?rev=1157231&r1=1157230&r2=1157231&view=diff
==============================================================================
--- commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/language/DoubleMetaphoneTest.java (original)
+++ commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/language/DoubleMetaphoneTest.java Fri Aug 12 19:50:56 2011
@@ -22,7 +22,6 @@ import static org.junit.Assert.assertFal
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoder;
import org.apache.commons.codec.StringEncoderAbstractTest;
import org.junit.Test;
@@ -1011,11 +1010,7 @@ public class DoubleMetaphoneTest extends
*/
private void assertDoubleMetaphone(String expected, String source) {
assertEquals(expected, this.getDoubleMetaphone().encode(source));
- try {
- assertEquals(expected, this.getDoubleMetaphone().encode((Object) source));
- } catch (EncoderException e) {
- fail("Unexpected expection: " + e);
- }
+ assertEquals(expected, this.getDoubleMetaphone().encode(source));
assertEquals(expected, this.getDoubleMetaphone().doubleMetaphone(source));
assertEquals(expected, this.getDoubleMetaphone().doubleMetaphone(source, false));
}
Modified: commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/language/bm/BeiderMorseEncoderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/language/bm/BeiderMorseEncoderTest.java?rev=1157231&r1=1157230&r2=1157231&view=diff
==============================================================================
--- commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/language/bm/BeiderMorseEncoderTest.java (original)
+++ commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/language/bm/BeiderMorseEncoderTest.java Fri Aug 12 19:50:56 2011
@@ -187,7 +187,7 @@ public class BeiderMorseEncoderTest exte
String phrase = "ItstheendoftheworldasweknowitandIfeelfine";
for (int i = 1; i <= phrase.length(); i++) {
- bmpm.encode(phrase.subSequence(0, i));
+ bmpm.encode(phrase.subSequence(0, i).toString());
}
}
@@ -197,7 +197,7 @@ public class BeiderMorseEncoderTest exte
String phrase = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
for (int i = 1; i <= phrase.length(); i++) {
- bmpm.encode(phrase.subSequence(0, i));
+ bmpm.encode(phrase.subSequence(0, i).toString());
}
}
}
Modified: commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/BCodecTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/BCodecTest.java?rev=1157231&r1=1157230&r2=1157231&view=diff
==============================================================================
--- commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/BCodecTest.java (original)
+++ commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/BCodecTest.java Fri Aug 12 19:50:56 2011
@@ -108,20 +108,12 @@ public class BCodecTest {
public void testEncodeObjects() throws Exception {
BCodec bcodec = new BCodec();
String plain = "what not";
- String encoded = (String) bcodec.encode((Object) plain);
+ String encoded = bcodec.encode(plain);
assertEquals("Basic B encoding test", "=?UTF-8?B?d2hhdCBub3Q=?=", encoded);
- Object result = bcodec.encode((Object) null);
+ String result = bcodec.encode((String) null);
assertEquals("Encoding a null Object should return null", null, result);
-
- try {
- Object dObj = new Double(3.0);
- bcodec.encode(dObj);
- fail("Trying to url encode a Double object should cause an exception.");
- } catch (EncoderException ee) {
- // Exception expected, test segment passes.
- }
}
@Test
@@ -142,21 +134,13 @@ public class BCodecTest {
}
@Test
- public void testDecodeObjects() throws Exception {
+ public void testDecodeStrings() throws Exception {
BCodec bcodec = new BCodec();
String decoded = "=?UTF-8?B?d2hhdCBub3Q=?=";
- String plain = (String) bcodec.decode((Object) decoded);
+ String plain = (String) bcodec.decode(decoded);
assertEquals("Basic B decoding test", "what not", plain);
- Object result = bcodec.decode((Object) null);
+ Object result = bcodec.decode(null);
assertEquals("Decoding a null Object should return null", null, result);
-
- try {
- Object dObj = new Double(3.0);
- bcodec.decode(dObj);
- fail("Trying to url encode a Double object should cause an exception.");
- } catch (DecoderException ee) {
- // Exception expected, test segment passes.
- }
}
}
Modified: commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/QCodecTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/QCodecTest.java?rev=1157231&r1=1157230&r2=1157231&view=diff
==============================================================================
--- commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/QCodecTest.java (original)
+++ commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/QCodecTest.java Fri Aug 12 19:50:56 2011
@@ -134,20 +134,12 @@ public class QCodecTest {
public void testEncodeObjects() throws Exception {
QCodec qcodec = new QCodec();
String plain = "1+1 = 2";
- String encoded = (String) qcodec.encode((Object) plain);
+ String encoded = qcodec.encode(plain);
assertEquals("Basic Q encoding test",
"=?UTF-8?Q?1+1 =3D 2?=", encoded);
- Object result = qcodec.encode((Object) null);
+ String result = qcodec.encode((String) null);
assertEquals( "Encoding a null Object should return null", null, result);
-
- try {
- Object dObj = new Double(3.0);
- qcodec.encode( dObj );
- fail( "Trying to url encode a Double object should cause an exception.");
- } catch (EncoderException ee) {
- // Exception expected, test segment passes.
- }
}
@@ -169,23 +161,15 @@ public class QCodecTest {
}
@Test
- public void testDecodeObjects() throws Exception {
+ public void testDecodeStrings() throws Exception {
QCodec qcodec = new QCodec();
String decoded = "=?UTF-8?Q?1+1 =3D 2?=";
- String plain = (String) qcodec.decode((Object) decoded);
+ String plain = (String) qcodec.decode(decoded);
assertEquals("Basic Q decoding test",
"1+1 = 2", plain);
- Object result = qcodec.decode((Object) null);
- assertEquals( "Decoding a null Object should return null", null, result);
-
- try {
- Object dObj = new Double(3.0);
- qcodec.decode( dObj );
- fail( "Trying to url encode a Double object should cause an exception.");
- } catch (DecoderException ee) {
- // Exception expected, test segment passes.
- }
+ Object result = qcodec.decode(null);
+ assertEquals( "Decoding a null Object should return null", null, result);
}
Added: commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/QuotedPrintableBinaryCodecTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/QuotedPrintableBinaryCodecTest.java?rev=1157231&view=auto
==============================================================================
--- commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/QuotedPrintableBinaryCodecTest.java (added)
+++ commons/proper/codec/branches/generics/src/test/org/apache/commons/codec/net/QuotedPrintableBinaryCodecTest.java Fri Aug 12 19:50:56 2011
@@ -0,0 +1,103 @@
+/*
+ * 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.net;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
+
+/**
+ * Quoted-printable codec test cases
+ *
+ * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @version $Id: QuotedPrintableStringCodecTest.java 1156841 2011-08-11 21:33:42Z ggregory $
+ */
+public class QuotedPrintableBinaryCodecTest {
+
+ static final int SWISS_GERMAN_STUFF_UNICODE [] = {
+ 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
+ };
+
+ static final int RUSSIAN_STUFF_UNICODE [] = {
+ 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438,
+ 0x432, 0x435, 0x442
+ };
+
+ private String constructString(int [] unicodeChars) {
+ StringBuffer buffer = new StringBuffer();
+ if (unicodeChars != null) {
+ for (int unicodeChar : unicodeChars) {
+ buffer.append((char)unicodeChar);
+ }
+ }
+ return buffer.toString();
+ }
+
+ @Test
+ public void testEncodeDecodeNull() throws Exception {
+ QuotedPrintableBinaryCodec qpcodec = new QuotedPrintableBinaryCodec();
+ assertNull("Null string quoted-printable encoding test", qpcodec.encode(null));
+ assertNull("Null string quoted-printable decoding test", qpcodec.decode(null));
+ }
+
+ @Test
+ public void testEncodeNull() throws Exception {
+ QuotedPrintableBinaryCodec qpcodec = new QuotedPrintableBinaryCodec();
+ byte[] plain = null;
+ byte[] encoded = qpcodec.encode(plain);
+ assertEquals("Encoding a null string should return null",
+ null, encoded);
+ }
+
+ @Test
+ public void testDecodeWithNullArray() throws Exception {
+ byte[] plain = null;
+ byte[] result = QuotedPrintableBinaryCodec.decodeQuotedPrintable( plain );
+ assertEquals("Result should be null", null, result);
+ }
+
+ @Test
+ public void testEncodeObjects() throws Exception {
+ QuotedPrintableBinaryCodec qpcodec = new QuotedPrintableBinaryCodec();
+ String plain = "1+1 = 2";
+
+ byte[] plainBA = plain.getBytes("UTF-8");
+ byte[] encodedBA = qpcodec.encode(plainBA);
+ String encoded = new String(encodedBA);
+ assertEquals("Basic quoted-printable encoding test", "1+1 =3D 2", encoded);
+
+ Object result = qpcodec.encode(null);
+ assertEquals("Encoding a null Object should return null", null, result);
+ }
+
+ @Test
+ public void testDecodeObjects() throws Exception {
+ QuotedPrintableBinaryCodec qpcodec = new QuotedPrintableBinaryCodec();
+ String plain = "1+1 =3D 2";
+
+ byte[] plainBA = plain.getBytes("UTF-8");
+ byte[] decodedBA = qpcodec.decode(plainBA);
+ String decoded = new String(decodedBA);
+ assertEquals("Basic quoted-printable decoding test", "1+1 = 2", decoded);
+
+ Object result = qpcodec.decode(null);
+ assertEquals("Decoding a null Object should return null", null, result);
+ }
+
+}
\ No newline at end of file