Skip to content

Commit 80fffec

Browse files
authored
CODEC-285 replace JUnit v4 test expected with assertThrows (#112)
1 parent 438b54b commit 80fffec

25 files changed

Lines changed: 146 additions & 134 deletions

src/test/java/org/apache/commons/codec/BinaryEncoderAbstractTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void testEncodeEmpty() throws Exception {
3434
}
3535

3636
@Test
37-
public void testEncodeNull() throws Exception {
37+
public void testEncodeNull() {
3838
assertThrows(EncoderException.class, () -> makeEncoder().encode(null));
3939
}
4040
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,11 @@ public void testSkipToEnd() throws IOException {
403403
*
404404
* @throws IOException for some failure scenarios.
405405
*/
406-
@Test(expected=IllegalArgumentException.class)
406+
@Test
407407
public void testSkipWrongArgument() throws IOException {
408408
final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B16));
409409
try (final Base16InputStream b16Stream = new Base16InputStream(ins)) {
410-
b16Stream.skip(-10);
410+
assertThrows(IllegalArgumentException.class, () -> b16Stream.skip(-10));
411411
}
412412
}
413413
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ private void testBase16InBuffer(final int startPasSize, final int endPadSize) {
9797
* isBase16 throws RuntimeException on some
9898
* non-Base16 bytes
9999
*/
100-
@Test(expected=RuntimeException.class)
100+
@Test
101101
public void testCodec68() {
102102
final byte[] x = { 'n', 'H', '=', '=', (byte) 0x9c };
103103
final Base16 b16 = new Base16();
104-
b16.decode(x);
104+
assertThrows(RuntimeException.class, () -> b16.decode(x));
105105
}
106106

107107
@Test
@@ -460,10 +460,10 @@ private String toString(final byte[] data) {
460460
return buf.toString();
461461
}
462462

463-
@Test(expected = IllegalArgumentException.class)
463+
@Test
464464
public void checkEncodeLengthBounds() {
465465
final Base16 base16 = new Base16();
466-
base16.encode(new byte[10], 0, 1 << 30);
466+
assertThrows(IllegalArgumentException.class, () -> base16.encode(new byte[10], 0, 1 << 30));
467467
}
468468

469469
@Test
@@ -565,13 +565,13 @@ public void testDecodeSingleBytesOptimisation() {
565565
assertEquals((byte)0xEF, context.buffer[0]);
566566
}
567567

568-
@Test(expected=IllegalArgumentException.class)
568+
@Test
569569
public void testStrictDecoding() {
570570
final String encoded = "aabbccdde"; // Note the trailing `e` which does not make up a hex-pair and so is only 1/2 byte
571571

572572
final Base16 b16 = new Base16(true, CodecPolicy.STRICT);
573573
assertEquals(CodecPolicy.STRICT, b16.getCodecPolicy());
574-
b16.decode(StringUtils.getBytesUtf8(encoded));
574+
assertThrows(IllegalArgumentException.class, () -> b16.decode(StringUtils.getBytesUtf8(encoded)));
575575
}
576576

577577
@Test

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,11 +523,11 @@ public void testSkipToEnd() throws Throwable {
523523
* @throws Throwable
524524
* for some failure scenarios.
525525
*/
526-
@Test(expected=IllegalArgumentException.class)
526+
@Test
527527
public void testSkipWrongArgument() throws Throwable {
528528
final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO));
529529
try (final Base32InputStream b32stream = new Base32InputStream(ins)) {
530-
b32stream.skip(-10);
530+
assertThrows(IllegalArgumentException.class, () -> b32stream.skip(-10));
531531
}
532532
}
533533

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,11 @@ public void testSkipToEnd() throws Throwable {
562562
* @throws Throwable
563563
* for some failure scenarios.
564564
*/
565-
@Test(expected=IllegalArgumentException.class)
565+
@Test
566566
public void testSkipWrongArgument() throws Throwable {
567567
final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64));
568568
try (final Base64InputStream b64stream = new Base64InputStream(ins)) {
569-
b64stream.skip(-10);
569+
assertThrows(IllegalArgumentException.class, () -> b64stream.skip(-10));
570570
}
571571
}
572572

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public void testCodec112() { // size calculation assumes always chunked
422422
Base64.encodeBase64(in, false, false, out.length);
423423
}
424424

425-
private void testEncodeOverMaxSize(final int maxSize) throws Exception {
425+
private void testEncodeOverMaxSize(final int maxSize) {
426426
assertThrows(IllegalArgumentException.class, () -> Base64.encodeBase64(BaseNTestData.DECODED, true, false, maxSize));
427427
}
428428

@@ -521,7 +521,7 @@ public void testNonBase64Test() throws Exception {
521521
}
522522

523523
@Test
524-
public void testObjectDecodeWithInvalidParameter() throws Exception {
524+
public void testObjectDecodeWithInvalidParameter() {
525525
assertThrows(DecoderException.class, () -> new Base64().decode(Integer.valueOf(5)));
526526
}
527527

@@ -540,7 +540,7 @@ public void testObjectDecodeWithValidParameter() throws Exception {
540540
}
541541

542542
@Test
543-
public void testObjectEncodeWithInvalidParameter() throws Exception {
543+
public void testObjectEncodeWithInvalidParameter() {
544544
assertThrows(EncoderException.class, () -> new Base64().encode("Yadayadayada"));
545545
}
546546

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertNotNull;
2323
import static org.junit.Assert.assertTrue;
24+
import static org.junit.jupiter.api.Assertions.assertThrows;
2425

2526
import org.apache.commons.codec.binary.BaseNCodec.Context;
2627
import org.junit.Assert;
@@ -356,7 +357,7 @@ static long getPresumableFreeMemory() {
356357
return Runtime.getRuntime().maxMemory() - allocatedMemory;
357358
}
358359

359-
@Test(expected = OutOfMemoryError.class)
360+
@Test
360361
public void testEnsureBufferSizeThrowsOnOverflow() {
361362
final BaseNCodec ncodec = new NoOpBaseNCodec();
362363
final Context context = new Context();
@@ -365,7 +366,7 @@ public void testEnsureBufferSizeThrowsOnOverflow() {
365366
context.buffer = new byte[length];
366367
context.pos = length;
367368
final int extra = Integer.MAX_VALUE;
368-
ncodec.ensureBufferSize(extra, context);
369+
assertThrows(OutOfMemoryError.class, () -> ncodec.ensureBufferSize(extra, context));
369370
}
370371

371372
/**

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ private void testCustomCharset(final String name, final String parent) throws Un
192192
assertEquals(name, sourceString, actualStringFromBytes);
193193
}
194194

195-
@Test(expected = UnsupportedCharsetException.class)
195+
@Test
196196
public void testCustomCharsetBadName() {
197-
new Hex(BAD_ENCODING_NAME);
197+
assertThrows(UnsupportedCharsetException.class, () -> new Hex(BAD_ENCODING_NAME));
198198
}
199199

200200
@Test
@@ -298,16 +298,16 @@ public void testDecodeHexCharArrayOddCharacters5() {
298298
checkDecodeHexCharArrayOddCharacters(new char[] { 'A', 'B', 'C', 'D', 'E' });
299299
}
300300

301-
@Test(expected = DecoderException.class)
302-
public void testDecodeHexCharArrayOutBufferUnderSized() throws DecoderException {
301+
@Test
302+
public void testDecodeHexCharArrayOutBufferUnderSized() {
303303
final byte[] out = new byte[4];
304-
Hex.decodeHex("aabbccddeeff".toCharArray(), out, 0);
304+
assertThrows(DecoderException.class, () -> Hex.decodeHex("aabbccddeeff".toCharArray(), out, 0));
305305
}
306306

307-
@Test(expected = DecoderException.class)
308-
public void testDecodeHexCharArrayOutBufferUnderSizedByOffset() throws DecoderException {
307+
@Test
308+
public void testDecodeHexCharArrayOutBufferUnderSizedByOffset() {
309309
final byte[] out = new byte[6];
310-
Hex.decodeHex("aabbccddeeff".toCharArray(), out, 1);
310+
assertThrows(DecoderException.class, () -> Hex.decodeHex("aabbccddeeff".toCharArray(), out, 1));
311311
}
312312

313313
@Test

src/test/java/org/apache/commons/codec/cli/DigestTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323

24+
import static org.junit.jupiter.api.Assertions.assertThrows;
2425

2526
/**
2627
* Tests {@link Digest}.
@@ -31,20 +32,16 @@ public class DigestTest {
3132

3233
/**
3334
* Tests if empty arguments are handled correctly.
34-
*
35-
* @throws IllegalArgumentException for some failure scenarios.
3635
*/
37-
@Test(expected = IllegalArgumentException.class)
38-
public void testEmptyArguments() throws IOException {
39-
Digest.main(new String[0]);
36+
@Test
37+
public void testEmptyArguments() {
38+
assertThrows(IllegalArgumentException.class, () -> Digest.main(new String[0]));
4039
}
4140
/**
4241
* Tests if null arguments are handled correctly.
43-
*
44-
* @throws IllegalArgumentException for some failure scenarios.
4542
*/
46-
@Test(expected = IllegalArgumentException.class)
47-
public void testNullArguments() throws IOException {
48-
Digest.main(null);
43+
@Test
44+
public void testNullArguments() {
45+
assertThrows(IllegalArgumentException.class, () -> Digest.main(null));
4946
}
5047
}

src/test/java/org/apache/commons/codec/digest/Apr1CryptTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNotSame;
2121
import static org.junit.Assert.assertTrue;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
2223

2324
import org.junit.Test;
2425

@@ -87,14 +88,14 @@ public void testApr1LongSalt() {
8788
assertEquals("$apr1$12345678$0lqb/6VUFP8JY/s/jTrIk0", Md5Crypt.apr1Crypt("secret", "12345678901234567890"));
8889
}
8990

90-
@Test(expected = NullPointerException.class)
91+
@Test
9192
public void testApr1CryptNullData() {
92-
Md5Crypt.apr1Crypt((byte[]) null);
93+
assertThrows(NullPointerException.class, () -> Md5Crypt.apr1Crypt((byte[]) null));
9394
}
9495

95-
@Test(expected = IllegalArgumentException.class)
96+
@Test
9697
public void testApr1CryptWithEmptySalt() {
97-
Md5Crypt.apr1Crypt("secret".getBytes(), "");
98+
assertThrows(IllegalArgumentException.class, () -> Md5Crypt.apr1Crypt("secret".getBytes(), ""));
9899
}
99100

100101
@Test
@@ -106,8 +107,8 @@ public void testApr1CryptWithoutSalt() {
106107
assertNotSame(hash, hash2);
107108
}
108109

109-
@Test(expected = IllegalArgumentException.class)
110+
@Test
110111
public void testApr1CryptWithInvalidSalt() {
111-
Md5Crypt.apr1Crypt(new byte[0], "!");
112+
assertThrows(IllegalArgumentException.class, () -> Md5Crypt.apr1Crypt(new byte[0], "!"));
112113
}
113114
}

0 commit comments

Comments
 (0)