Skip to content

Commit 2a02fe7

Browse files
committed
PR: 24360
Submitted by: Gary Gregory [codec] ClassCastException in Hex.decode(Object) fixed. Complete code coverage for Hex (See clover report). git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/codec/trunk@130225 13f79535-47bb-0310-9956-ffa450edef68
1 parent 5fc5353 commit 2a02fe7

1 file changed

Lines changed: 94 additions & 35 deletions

File tree

src/test/org/apache/commons/codec/binary/HexTest.java

Lines changed: 94 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -61,70 +61,129 @@
6161
import java.util.Random;
6262

6363
import junit.framework.TestCase;
64+
import org.apache.commons.codec.DecoderException;
65+
import org.apache.commons.codec.EncoderException;
6466

6567
/**
68+
* Tests {@link org.apache.commons.codec.binary.Hex}.
6669
*
6770
* @author <a href="mailto:siege@preoccupied.net">Christopher O'Brien</a>
6871
* @author Tim O'Brien
72+
* @author Gary Gregory
73+
* @version $Id: HexTest.java,v 1.6 2003/11/03 19:04:34 ggregory Exp $
6974
*/
7075

7176
public class HexTest extends TestCase {
7277

7378
public HexTest(String name) {
7479
super(name);
7580
}
81+
82+
public void testDecodeArrayOddCharacters() {
83+
try {
84+
new Hex().decode(new byte[] { 65 });
85+
fail("An exception wasn't thrown when trying to decode an odd number of characters");
86+
}
87+
catch (DecoderException e) {
88+
// Expected exception
89+
}
90+
}
7691

77-
public void testEncodeEmpty() throws Exception {
78-
char[] c = Hex.encodeHex(new byte[0]);
79-
assertTrue(Arrays.equals(new char[0], c));
92+
public void testDecodeClassCastException() {
93+
try {
94+
new Hex().decode(new int[] { 65 });
95+
fail("An exception wasn't thrown when trying to decode.");
96+
}
97+
catch (DecoderException e) {
98+
// Expected exception
99+
}
80100
}
81101

82-
public void testEncodeZeroes() throws Exception {
83-
char[] c = Hex.encodeHex(new byte[36]);
84-
assertEquals(
85-
"000000000000000000000000000000000000"
86-
+ "000000000000000000000000000000000000",
87-
new String(c));
102+
public void testDecodeHexOddCharacters() {
103+
try {
104+
Hex.decodeHex(new char[] { 'A' });
105+
fail("An exception wasn't thrown when trying to decode an odd number of characters");
106+
}
107+
catch (DecoderException e) {
108+
// Expected exception
109+
}
88110
}
89111

90-
public void testHelloWorld() throws Exception {
91-
byte[] b = "Hello World".getBytes();
92-
char[] c = Hex.encodeHex(b);
93-
assertEquals("48656c6c6f20576f726c64", new String(c));
112+
public void testDecodeStringOddCharacters() {
113+
try {
114+
new Hex().decode("6");
115+
fail("An exception wasn't thrown when trying to decode an odd number of characters");
116+
}
117+
catch (DecoderException e) {
118+
// Expected exception
119+
}
120+
}
121+
122+
public void testDencodeEmpty() throws DecoderException {
123+
assertTrue(Arrays.equals(new byte[0], Hex.decodeHex(new char[0])));
124+
assertTrue(Arrays.equals(new byte[0], new Hex().decode(new byte[0])));
125+
assertTrue(Arrays.equals(new byte[0], (byte[])new Hex().decode("")));
126+
}
127+
128+
public void testEncodeClassCastException() {
129+
try {
130+
new Hex().encode(new int[] { 65 });
131+
fail("An exception wasn't thrown when trying to encode.");
132+
}
133+
catch (EncoderException e) {
134+
// Expected exception
135+
}
94136
}
95137

96-
public void testEncodeDecodeRandom() throws Exception {
138+
public void testEncodeDecodeRandom() throws DecoderException, EncoderException {
97139
Random random = new Random();
98140

141+
Hex hex = new Hex();
99142
for (int i = 5; i > 0; i--) {
100143
byte[] data = new byte[random.nextInt(10000) + 1];
101144
random.nextBytes(data);
102145

103-
char[] enc = Hex.encodeHex(data);
104-
byte[] data2 = Hex.decodeHex(enc);
105-
106-
assertTrue(Arrays.equals(data, data2));
146+
// static API
147+
char[] encodedChars = Hex.encodeHex(data);
148+
byte[] decodedBytes = Hex.decodeHex(encodedChars);
149+
assertTrue(Arrays.equals(data, decodedBytes));
150+
151+
// instance API with array parameter
152+
byte[] encodedStringBytes = hex.encode(data);
153+
decodedBytes = hex.decode(encodedStringBytes);
154+
assertTrue(Arrays.equals(data, decodedBytes));
155+
156+
// instance API with char[] (Object) parameter
157+
String dataString = new String(encodedChars);
158+
char[] encodedStringChars = (char[])hex.encode(dataString);
159+
decodedBytes = (byte[])hex.decode(encodedStringChars);
160+
assertTrue(Arrays.equals(dataString.getBytes(), decodedBytes));
161+
162+
// instance API with String (Object) parameter
163+
dataString = new String(encodedChars);
164+
encodedStringChars = (char[])hex.encode(dataString);
165+
decodedBytes = (byte[])hex.decode(new String(encodedStringChars));
166+
assertTrue(Arrays.equals(dataString.getBytes(), decodedBytes));
107167
}
108168
}
109169

110-
public void testOddCharacters() throws Exception {
111-
112-
boolean exceptionThrown = false;
113-
114-
try {
115-
char[] singleChar = new char[1];
116-
singleChar[0] = 'a';
117-
118-
Hex.decodeHex( singleChar );
119-
}
120-
catch (Exception e) {
121-
exceptionThrown = true;
122-
}
123-
124-
assertTrue( "An exception wasn't thrown when trying to " +
125-
"decode an odd number of characters", exceptionThrown );
126-
170+
public void testEncodeEmpty() throws EncoderException {
171+
assertTrue(Arrays.equals(new char[0], Hex.encodeHex(new byte[0])));
172+
assertTrue(Arrays.equals(new byte[0], new Hex().encode(new byte[0])));
173+
assertTrue(Arrays.equals(new char[0], (char[])new Hex().encode("")));
174+
}
127175

176+
public void testEncodeZeroes() {
177+
char[] c = Hex.encodeHex(new byte[36]);
178+
assertEquals(
179+
"000000000000000000000000000000000000"
180+
+ "000000000000000000000000000000000000",
181+
new String(c));
128182
}
129183

184+
public void testHelloWorld() {
185+
byte[] b = "Hello World".getBytes();
186+
char[] c = Hex.encodeHex(b);
187+
assertEquals("48656c6c6f20576f726c64", new String(c));
188+
}
130189
}

0 commit comments

Comments
 (0)