You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by gg...@apache.org on 2003/11/03 20:04:34 UTC
cvs commit: jakarta-commons/codec/src/test/org/apache/commons/codec/binary HexTest.java
ggregory 2003/11/03 11:04:34
Modified: codec/src/test/org/apache/commons/codec/binary HexTest.java
Log:
PR: 24360
Submitted by: Gary Gregory
[codec] ClassCastException in Hex.decode(Object) fixed.
Complete code coverage for Hex (See clover report).
Revision Changes Path
1.6 +94 -35 jakarta-commons/codec/src/test/org/apache/commons/codec/binary/HexTest.java
Index: HexTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/codec/src/test/org/apache/commons/codec/binary/HexTest.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- HexTest.java 5 Oct 2003 21:45:49 -0000 1.5
+++ HexTest.java 3 Nov 2003 19:04:34 -0000 1.6
@@ -61,11 +61,16 @@
import java.util.Random;
import junit.framework.TestCase;
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.EncoderException;
/**
+ * Tests {@link org.apache.commons.codec.binary.Hex}.
*
* @author <a href="mailto:siege@preoccupied.net">Christopher O'Brien</a>
* @author Tim O'Brien
+ * @author Gary Gregory
+ * @version $Id$
*/
public class HexTest extends TestCase {
@@ -73,58 +78,112 @@
public HexTest(String name) {
super(name);
}
+
+ public void testDecodeArrayOddCharacters() {
+ try {
+ new Hex().decode(new byte[] { 65 });
+ fail("An exception wasn't thrown when trying to decode an odd number of characters");
+ }
+ catch (DecoderException e) {
+ // Expected exception
+ }
+ }
- public void testEncodeEmpty() throws Exception {
- char[] c = Hex.encodeHex(new byte[0]);
- assertTrue(Arrays.equals(new char[0], c));
+ 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
+ }
}
- public void testEncodeZeroes() throws Exception {
- char[] c = Hex.encodeHex(new byte[36]);
- assertEquals(
- "000000000000000000000000000000000000"
- + "000000000000000000000000000000000000",
- new String(c));
+ public void testDecodeHexOddCharacters() {
+ try {
+ Hex.decodeHex(new char[] { 'A' });
+ fail("An exception wasn't thrown when trying to decode an odd number of characters");
+ }
+ catch (DecoderException e) {
+ // Expected exception
+ }
}
- public void testHelloWorld() throws Exception {
- byte[] b = "Hello World".getBytes();
- char[] c = Hex.encodeHex(b);
- assertEquals("48656c6c6f20576f726c64", new String(c));
+ public void testDecodeStringOddCharacters() {
+ try {
+ new Hex().decode("6");
+ fail("An exception wasn't thrown when trying to decode an odd number of characters");
+ }
+ catch (DecoderException e) {
+ // Expected exception
+ }
+ }
+
+ 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("")));
+ }
+
+ 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
+ }
}
- public void testEncodeDecodeRandom() throws Exception {
+ public void testEncodeDecodeRandom() throws DecoderException, EncoderException {
Random random = new Random();
+ Hex hex = new Hex();
for (int i = 5; i > 0; i--) {
byte[] data = new byte[random.nextInt(10000) + 1];
random.nextBytes(data);
- char[] enc = Hex.encodeHex(data);
- byte[] data2 = Hex.decodeHex(enc);
-
- assertTrue(Arrays.equals(data, data2));
+ // static API
+ char[] encodedChars = Hex.encodeHex(data);
+ byte[] decodedBytes = Hex.decodeHex(encodedChars);
+ assertTrue(Arrays.equals(data, decodedBytes));
+
+ // instance API with array parameter
+ byte[] 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(dataString.getBytes(), 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(dataString.getBytes(), decodedBytes));
}
}
- public void testOddCharacters() throws Exception {
-
- boolean exceptionThrown = false;
-
- try {
- char[] singleChar = new char[1];
- singleChar[0] = 'a';
-
- Hex.decodeHex( singleChar );
- }
- catch (Exception e) {
- exceptionThrown = true;
- }
-
- assertTrue( "An exception wasn't thrown when trying to " +
- "decode an odd number of characters", exceptionThrown );
-
+ 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("")));
+ }
+ public void testEncodeZeroes() {
+ char[] c = Hex.encodeHex(new byte[36]);
+ assertEquals(
+ "000000000000000000000000000000000000"
+ + "000000000000000000000000000000000000",
+ new String(c));
}
+ public void testHelloWorld() {
+ byte[] b = "Hello World".getBytes();
+ char[] c = Hex.encodeHex(b);
+ assertEquals("48656c6c6f20576f726c64", new String(c));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org