Skip to content

Commit f06bed0

Browse files
committed
100/100 code coverage.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@801410 13f79535-47bb-0310-9956-ffa450edef68
1 parent d89939d commit f06bed0

1 file changed

Lines changed: 78 additions & 46 deletions

File tree

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

Lines changed: 78 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,63 @@
3939
*/
4040
public class HexTest extends TestCase {
4141

42+
private static final String BAD_ENCODING_NAME = "UNKNOWN";
4243
private final static boolean LOG = true;
4344

4445
public HexTest(String name) {
4546
super(name);
4647
}
4748

49+
private boolean charsetSanityCheck(String name) {
50+
final String source = "the quick brown dog jumped over the lazy fox";
51+
try {
52+
byte[] bytes = source.getBytes(name);
53+
String str = new String(bytes, name);
54+
boolean equals = source.equals(str);
55+
if (equals == false) {
56+
// Here with:
57+
//
58+
// Java Sun 1.4.2_19 x86 32-bits on Windows XP
59+
// JIS_X0212-1990
60+
// x-JIS0208
61+
//
62+
// Java Sun 1.5.0_17 x86 32-bits on Windows XP
63+
// JIS_X0212-1990
64+
// x-IBM834
65+
// x-JIS0208
66+
// x-MacDingbat
67+
// x-MacSymbol
68+
//
69+
// Java Sun 1.6.0_14 x86 32-bits
70+
// JIS_X0212-1990
71+
// x-IBM834
72+
// x-JIS0208
73+
// x-MacDingbat
74+
// x-MacSymbol
75+
//
76+
log("FAILED charsetSanityCheck=Interesting Java charset oddity: Roundtrip failed for " + name);
77+
}
78+
return equals;
79+
} catch (UnsupportedEncodingException e) {
80+
// Should NEVER happen since we are getting the name from the Charset class.
81+
if (LOG) {
82+
log("FAILED charsetSanityCheck=" + name + ", e=" + e);
83+
log(e);
84+
}
85+
return false;
86+
} catch (UnsupportedOperationException e) {
87+
// Caught here with:
88+
// x-JISAutoDetect on Windows XP and Java Sun 1.4.2_19 x86 32-bits
89+
// x-JISAutoDetect on Windows XP and Java Sun 1.5.0_17 x86 32-bits
90+
// x-JISAutoDetect on Windows XP and Java Sun 1.6.0_14 x86 32-bits
91+
if (LOG) {
92+
log("FAILED charsetSanityCheck=" + name + ", e=" + e);
93+
log(e);
94+
}
95+
return false;
96+
}
97+
}
98+
4899
/**
49100
* @param data
50101
*/
@@ -123,56 +174,37 @@ private void testCustomCharset(String name, String parent) throws UnsupportedEnc
123174
assertEquals(name, sourceString, actualStringFromBytes);
124175
}
125176

126-
private boolean charsetSanityCheck(String name) {
127-
final String source = "the quick brown dog jumped over the lazy fox";
177+
public void testCustomCharsetBadNameEncodeByteArray() {
128178
try {
129-
byte[] bytes = source.getBytes(name);
130-
String str = new String(bytes, name);
131-
boolean equals = source.equals(str);
132-
if (equals == false) {
133-
// Here with:
134-
//
135-
// Java Sun 1.4.2_19 x86 32-bits on Windows XP
136-
// JIS_X0212-1990
137-
// x-JIS0208
138-
//
139-
// Java Sun 1.5.0_17 x86 32-bits on Windows XP
140-
// JIS_X0212-1990
141-
// x-IBM834
142-
// x-JIS0208
143-
// x-MacDingbat
144-
// x-MacSymbol
145-
//
146-
// Java Sun 1.6.0_14 x86 32-bits
147-
// JIS_X0212-1990
148-
// x-IBM834
149-
// x-JIS0208
150-
// x-MacDingbat
151-
// x-MacSymbol
152-
//
153-
log("FAILED charsetSanityCheck=Interesting Java charset oddity: Roundtrip failed for " + name);
154-
}
155-
return equals;
156-
} catch (UnsupportedEncodingException e) {
157-
// Should NEVER happen since we are getting the name from the Charset class.
158-
if (LOG) {
159-
log("FAILED charsetSanityCheck=" + name + ", e=" + e);
160-
log(e);
161-
}
162-
return false;
163-
} catch (UnsupportedOperationException e) {
164-
// Caught here with:
165-
// x-JISAutoDetect on Windows XP and Java Sun 1.4.2_19 x86 32-bits
166-
// x-JISAutoDetect on Windows XP and Java Sun 1.5.0_17 x86 32-bits
167-
// x-JISAutoDetect on Windows XP and Java Sun 1.6.0_14 x86 32-bits
168-
if (LOG) {
169-
log("FAILED charsetSanityCheck=" + name + ", e=" + e);
170-
log(e);
171-
}
172-
return false;
179+
new Hex(BAD_ENCODING_NAME).encode("Hello World".getBytes());
180+
fail("Expected " + IllegalStateException.class.getName());
181+
} catch (IllegalStateException e) {
182+
// Expected
173183
}
174184
}
175185

186+
public void testCustomCharsetBadNameEncodeObject() {
187+
try {
188+
new Hex(BAD_ENCODING_NAME).encode("Hello World");
189+
fail("Expected " + EncoderException.class.getName());
190+
} catch (EncoderException e) {
191+
// Expected
192+
}
193+
}
194+
195+
public void testCustomCharsetBadNameDecodeObject() {
196+
try {
197+
new Hex(BAD_ENCODING_NAME).decode("Hello World".getBytes());
198+
fail("Expected " + DecoderException.class.getName());
199+
} catch (DecoderException e) {
200+
// Expected
201+
}
202+
}
203+
204+
public void testCustomCharsetToString() {
205+
assertTrue(new Hex().toString().indexOf(Hex.DEFAULT_CHARSET_NAME) >= 0);
206+
}
207+
176208
public void testDecodeArrayOddCharacters() {
177209
try {
178210
new Hex().decode(new byte[]{65});

0 commit comments

Comments
 (0)