Skip to content

Commit 5a03f5a

Browse files
committed
Port most exception assertions to JUnit 5.
1 parent 0cf3f11 commit 5a03f5a

19 files changed

Lines changed: 156 additions & 610 deletions

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.commons.codec;
1919

20+
import static org.junit.Assert.assertThrows;
2021
import static org.junit.Assert.fail;
2122

2223
import org.junit.Test;
@@ -35,12 +36,6 @@ public void testEncodeEmpty() throws Exception {
3536

3637
@Test
3738
public void testEncodeNull() throws Exception {
38-
final BinaryEncoder encoder = makeEncoder();
39-
try {
40-
encoder.encode(null);
41-
fail("EncoderException exptected");
42-
} catch (final EncoderException ee) {
43-
// An exception should be thrown
44-
}
39+
assertThrows(EncoderException.class, () -> makeEncoder().encode(null));
4540
}
4641
}

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

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.junit.Assert.assertArrayEquals;
2727
import static org.junit.Assert.assertEquals;
2828
import static org.junit.Assert.assertFalse;
29+
import static org.junit.Assert.assertThrows;
2930
import static org.junit.Assert.fail;
3031

3132
/**
@@ -308,10 +309,7 @@ public void testReadNull() throws IOException {
308309
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
309310
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
310311
try (final Base16InputStream in = new Base16InputStream(bin, true)) {
311-
in.read(null, 0, 0);
312-
fail("Base16InputStream.read(null, 0, 0) to throw a NullPointerException");
313-
} catch (final NullPointerException e) {
314-
// Expected
312+
assertThrows(NullPointerException.class, () -> in.read(null, 0, 0));
315313
}
316314
}
317315

@@ -326,34 +324,10 @@ public void testReadOutOfBounds() throws IOException {
326324
final byte[] buf = new byte[1024];
327325
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
328326
try (final Base16InputStream in = new Base16InputStream(bin, true)) {
329-
330-
try {
331-
in.read(buf, -1, 0);
332-
fail("Expected Base16InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException");
333-
} catch (final IndexOutOfBoundsException e) {
334-
// Expected
335-
}
336-
337-
try {
338-
in.read(buf, 0, -1);
339-
fail("Expected Base16InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
340-
} catch (final IndexOutOfBoundsException e) {
341-
// Expected
342-
}
343-
344-
try {
345-
in.read(buf, buf.length + 1, 0);
346-
fail("Base16InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
347-
} catch (final IndexOutOfBoundsException e) {
348-
// Expected
349-
}
350-
351-
try {
352-
in.read(buf, buf.length - 1, 2);
353-
fail("Base16InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
354-
} catch (final IndexOutOfBoundsException e) {
355-
// Expected
356-
}
327+
assertThrows("Base16InputStream.read(buf, -1, 0)", IndexOutOfBoundsException.class, () -> in.read(buf, -1, 0));
328+
assertThrows("Base16InputStream.read(buf, 0, -1)", IndexOutOfBoundsException.class, () -> in.read(buf, 0, -1));
329+
assertThrows("Base16InputStream.read(buf, buf.length + 1, 0)", IndexOutOfBoundsException.class, () -> in.read(buf, buf.length + 1, 0));
330+
assertThrows("Base16InputStream.read(buf, buf.length - 1, 2)", IndexOutOfBoundsException.class, () -> in.read(buf, buf.length - 1, 2));
357331
}
358332
}
359333

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

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.OutputStream;
2525

2626
import static org.junit.Assert.assertArrayEquals;
27+
import static org.junit.Assert.assertThrows;
2728
import static org.junit.Assert.fail;
2829

2930
/**
@@ -238,34 +239,10 @@ public void testWriteOutOfBounds() throws IOException {
238239
final byte[] buf = new byte[1024];
239240
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
240241
try (final Base16OutputStream out = new Base16OutputStream(bout)) {
241-
242-
try {
243-
out.write(buf, -1, 1);
244-
fail("Expected Base16OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException");
245-
} catch (final IndexOutOfBoundsException ioobe) {
246-
// Expected
247-
}
248-
249-
try {
250-
out.write(buf, 1, -1);
251-
fail("Expected Base16OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException");
252-
} catch (final IndexOutOfBoundsException ioobe) {
253-
// Expected
254-
}
255-
256-
try {
257-
out.write(buf, buf.length + 1, 0);
258-
fail("Expected Base16OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException");
259-
} catch (final IndexOutOfBoundsException ioobe) {
260-
// Expected
261-
}
262-
263-
try {
264-
out.write(buf, buf.length - 1, 2);
265-
fail("Expected Base16OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException");
266-
} catch (final IndexOutOfBoundsException ioobe) {
267-
// Expected
268-
}
242+
assertThrows("Base16InputStream.write(buf, -1, 0)", IndexOutOfBoundsException.class, () -> out.write(buf, -1, 1));
243+
assertThrows("Base16InputStream.write(buf, 1, -1)", IndexOutOfBoundsException.class, () -> out.write(buf, 1, -1));
244+
assertThrows("Base16InputStream.write(buf, buf.length + 1, 0)", IndexOutOfBoundsException.class, () -> out.write(buf, buf.length + 1, 0));
245+
assertThrows("Base16InputStream.write(buf, buf.length - 1, 2)", IndexOutOfBoundsException.class, () -> out.write(buf, buf.length - 1, 2));
269246
}
270247
}
271248

@@ -278,10 +255,7 @@ public void testWriteOutOfBounds() throws IOException {
278255
public void testWriteToNullCoverage() throws IOException {
279256
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
280257
try (final Base16OutputStream out = new Base16OutputStream(bout)) {
281-
out.write(null, 0, 0);
282-
fail("Expcted Base16OutputStream.write(null) to throw a NullPointerException");
283-
} catch (final NullPointerException e) {
284-
// Expected
258+
assertThrows(NullPointerException.class, () -> out.write(null, 0, 0));
285259
}
286260
}
287261
}

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static org.junit.Assert.assertEquals;
3232
import static org.junit.Assert.assertFalse;
3333
import static org.junit.Assert.assertNull;
34+
import static org.junit.Assert.assertThrows;
3435
import static org.junit.Assert.assertTrue;
3536
import static org.junit.Assert.fail;
3637

@@ -211,27 +212,14 @@ public void testNonBase16Test() {
211212

212213
final byte[] encoded = new byte[1];
213214
for (final byte invalidEncodedChar : invalidEncodedChars) {
214-
try {
215-
encoded[0] = invalidEncodedChar;
216-
new Base16().decode(encoded);
217-
fail("IllegalArgumentException should have been thrown when trying to decode invalid Base16 char: " + (char)invalidEncodedChar);
218-
} catch (final Exception e) {
219-
assertTrue(e instanceof IllegalArgumentException);
220-
}
215+
encoded[0] = invalidEncodedChar;
216+
assertThrows("Invalid Base16 char: " + (char) invalidEncodedChar, IllegalArgumentException.class, () -> new Base16().decode(encoded));
221217
}
222218
}
223219

224220
@Test
225221
public void testObjectDecodeWithInvalidParameter() {
226-
final Base16 b16 = new Base16();
227-
228-
try {
229-
b16.decode(Integer.valueOf(5));
230-
fail("decode(Object) didn't throw an exception when passed an Integer object");
231-
} catch (final DecoderException e) {
232-
// ignored
233-
}
234-
222+
assertThrows(DecoderException.class, () -> new Base16().decode(Integer.valueOf(5)));
235223
}
236224

237225
@Test
@@ -249,13 +237,7 @@ public void testObjectDecodeWithValidParameter() throws Exception {
249237

250238
@Test
251239
public void testObjectEncodeWithInvalidParameter() {
252-
final Base16 b16 = new Base16();
253-
try {
254-
b16.encode("Yadayadayada");
255-
fail("encode(Object) didn't throw an exception when passed a String object");
256-
} catch (final EncoderException e) {
257-
// Expected
258-
}
240+
assertThrows(EncoderException.class, () -> new Base16().encode("Yadayadayada"));
259241
}
260242

261243
@Test

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

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import java.io.InputStream;
2424

2525
import org.apache.commons.codec.CodecPolicy;
26+
import org.apache.commons.codec.EncoderException;
2627
import org.junit.Test;
2728

2829
import static org.junit.Assert.assertArrayEquals;
2930
import static org.junit.Assert.assertEquals;
3031
import static org.junit.Assert.assertFalse;
32+
import static org.junit.Assert.assertThrows;
3133
import static org.junit.Assert.assertTrue;
3234
import static org.junit.Assert.fail;
3335

@@ -421,11 +423,8 @@ public void testRead0() throws Exception {
421423
public void testReadNull() throws Exception {
422424
final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
423425
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
424-
try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
425-
in.read(null, 0, 0);
426-
fail("Base32InputStream.read(null, 0, 0) to throw a NullPointerException");
427-
} catch (final NullPointerException e) {
428-
// Expected
426+
try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] {0, 0, 0})) {
427+
assertThrows(NullPointerException.class, () -> in.read(null, 0, 0));
429428
}
430429
}
431430

@@ -441,34 +440,10 @@ public void testReadOutOfBounds() throws Exception {
441440
final byte[] buf = new byte[1024];
442441
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
443442
try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
444-
445-
try {
446-
in.read(buf, -1, 0);
447-
fail("Expected Base32InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException");
448-
} catch (final IndexOutOfBoundsException e) {
449-
// Expected
450-
}
451-
452-
try {
453-
in.read(buf, 0, -1);
454-
fail("Expected Base32InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
455-
} catch (final IndexOutOfBoundsException e) {
456-
// Expected
457-
}
458-
459-
try {
460-
in.read(buf, buf.length + 1, 0);
461-
fail("Base32InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
462-
} catch (final IndexOutOfBoundsException e) {
463-
// Expected
464-
}
465-
466-
try {
467-
in.read(buf, buf.length - 1, 2);
468-
fail("Base32InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
469-
} catch (final IndexOutOfBoundsException e) {
470-
// Expected
471-
}
443+
assertThrows("Base32InputStream.read(buf, -1, 0)", IndexOutOfBoundsException.class, () -> in.read(buf, -1, 0));
444+
assertThrows("Base32InputStream.read(buf, 0, -1)", IndexOutOfBoundsException.class, () -> in.read(buf, 0, -1));
445+
assertThrows("Base32InputStream.read(buf, buf.length + 1, 0)", IndexOutOfBoundsException.class, () -> in.read(buf, buf.length + 1, 0));
446+
assertThrows("Base32InputStream.read(buf, buf.length - 1, 2)", IndexOutOfBoundsException.class, () -> in.read(buf, buf.length - 1, 2));
472447
}
473448
}
474449

@@ -574,14 +549,9 @@ public void testStrictDecoding() throws Exception {
574549
BaseNTestData.streamToBytes(in);
575550

576551
// Strict decoding should throw
577-
in = new Base32InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
578-
assertTrue(in.isStrictDecoding());
579-
try {
580-
BaseNTestData.streamToBytes(in);
581-
fail();
582-
} catch (final IllegalArgumentException ex) {
583-
// expected
584-
}
552+
Base32InputStream in2 = new Base32InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
553+
assertTrue(in2.isStrictDecoding());
554+
assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in2));
585555
}
586556
}
587557
}

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

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import static org.junit.Assert.assertArrayEquals;
2727
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertThrows;
2829
import static org.junit.Assert.assertTrue;
2930
import static org.junit.Assert.fail;
3031

@@ -287,34 +288,10 @@ public void testWriteOutOfBounds() throws Exception {
287288
final byte[] buf = new byte[1024];
288289
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
289290
try (final Base32OutputStream out = new Base32OutputStream(bout)) {
290-
291-
try {
292-
out.write(buf, -1, 1);
293-
fail("Expected Base32OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException");
294-
} catch (final IndexOutOfBoundsException ioobe) {
295-
// Expected
296-
}
297-
298-
try {
299-
out.write(buf, 1, -1);
300-
fail("Expected Base32OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException");
301-
} catch (final IndexOutOfBoundsException ioobe) {
302-
// Expected
303-
}
304-
305-
try {
306-
out.write(buf, buf.length + 1, 0);
307-
fail("Expected Base32OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException");
308-
} catch (final IndexOutOfBoundsException ioobe) {
309-
// Expected
310-
}
311-
312-
try {
313-
out.write(buf, buf.length - 1, 2);
314-
fail("Expected Base32OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException");
315-
} catch (final IndexOutOfBoundsException ioobe) {
316-
// Expected
317-
}
291+
assertThrows("Base32OutputStream.write(buf, -1, 1)", IndexOutOfBoundsException.class, () -> out.write(buf, -1, 1));
292+
assertThrows("Base32OutputStream.write(buf, 1, -1)", IndexOutOfBoundsException.class, () -> out.write(buf, 1, -1));
293+
assertThrows("Base32OutputStream.write(buf, buf, buf.length + 1, 0)", IndexOutOfBoundsException.class, () -> out.write(buf, buf.length + 1, 0));
294+
assertThrows("Base32OutputStream.write(buf, buf, buf.length - 1, 2)", IndexOutOfBoundsException.class, () -> out.write(buf, buf.length - 1, 2));
318295
}
319296
}
320297

@@ -328,10 +305,7 @@ public void testWriteOutOfBounds() throws Exception {
328305
public void testWriteToNullCoverage() throws Exception {
329306
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
330307
try (final Base32OutputStream out = new Base32OutputStream(bout)) {
331-
out.write(null, 0, 0);
332-
fail("Expcted Base32OutputStream.write(null) to throw a NullPointerException");
333-
} catch (final NullPointerException e) {
334-
// Expected
308+
assertThrows(NullPointerException.class, () -> out.write(null, 0, 0));
335309
}
336310
}
337311

@@ -355,15 +329,10 @@ public void testStrictDecoding() throws Exception {
355329

356330
// Strict decoding should throw
357331
bout = new ByteArrayOutputStream();
358-
out = new Base32OutputStream(bout, false, 0, null, CodecPolicy.STRICT);
359-
assertTrue(out.isStrictDecoding());
360-
try {
361-
out.write(encoded);
362-
out.close();
363-
fail();
364-
} catch (final IllegalArgumentException ex) {
365-
// expected
366-
}
332+
final Base32OutputStream out2 = new Base32OutputStream(bout, false, 0, null, CodecPolicy.STRICT);
333+
assertTrue(out2.isStrictDecoding());
334+
assertThrows(IllegalArgumentException.class, () -> out2.write(encoded));
335+
out2.close();
367336
}
368337
}
369338
}

0 commit comments

Comments
 (0)