You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/02/19 15:45:25 UTC
[commons-codec] branch master updated: Port most exception assertions to JUnit 5.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git
The following commit(s) were added to refs/heads/master by this push:
new 5a03f5a Port most exception assertions to JUnit 5.
5a03f5a is described below
commit 5a03f5a493240f26f27ccabe6b8ac7b701c2d5c5
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Feb 19 10:45:23 2022 -0500
Port most exception assertions to JUnit 5.
---
.../commons/codec/BinaryEncoderAbstractTest.java | 9 +-
.../codec/binary/Base16InputStreamTest.java | 38 ++-------
.../codec/binary/Base16OutputStreamTest.java | 38 ++-------
.../apache/commons/codec/binary/Base16Test.java | 28 ++-----
.../codec/binary/Base32InputStreamTest.java | 52 +++---------
.../codec/binary/Base32OutputStreamTest.java | 51 +++---------
.../apache/commons/codec/binary/Base32Test.java | 63 +++-----------
.../codec/binary/Base64InputStreamTest.java | 51 +++---------
.../codec/binary/Base64OutputStreamTest.java | 57 ++++---------
.../apache/commons/codec/binary/Base64Test.java | 95 ++++------------------
.../org/apache/commons/codec/binary/HexTest.java | 65 +++------------
.../commons/codec/binary/StringUtilsTest.java | 17 ++--
.../apache/commons/codec/digest/Blake3Test.java | 10 +--
.../apache/commons/codec/language/SoundexTest.java | 21 ++---
.../org/apache/commons/codec/net/BCodecTest.java | 24 +-----
.../org/apache/commons/codec/net/QCodecTest.java | 23 ++----
.../codec/net/QuotedPrintableCodecTest.java | 43 ++--------
.../apache/commons/codec/net/RFC1522CodecTest.java | 9 +-
.../org/apache/commons/codec/net/URLCodecTest.java | 72 ++++------------
19 files changed, 156 insertions(+), 610 deletions(-)
diff --git a/src/test/java/org/apache/commons/codec/BinaryEncoderAbstractTest.java b/src/test/java/org/apache/commons/codec/BinaryEncoderAbstractTest.java
index 2bacd24..783c33f 100644
--- a/src/test/java/org/apache/commons/codec/BinaryEncoderAbstractTest.java
+++ b/src/test/java/org/apache/commons/codec/BinaryEncoderAbstractTest.java
@@ -17,6 +17,7 @@
package org.apache.commons.codec;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import org.junit.Test;
@@ -35,12 +36,6 @@ public abstract class BinaryEncoderAbstractTest {
@Test
public void testEncodeNull() throws Exception {
- final BinaryEncoder encoder = makeEncoder();
- try {
- encoder.encode(null);
- fail("EncoderException exptected");
- } catch (final EncoderException ee) {
- // An exception should be thrown
- }
+ assertThrows(EncoderException.class, () -> makeEncoder().encode(null));
}
}
diff --git a/src/test/java/org/apache/commons/codec/binary/Base16InputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base16InputStreamTest.java
index e360fe0..0c55efc 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base16InputStreamTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base16InputStreamTest.java
@@ -26,6 +26,7 @@ import java.io.IOException;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
/**
@@ -308,10 +309,7 @@ public class Base16InputStreamTest {
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
try (final Base16InputStream in = new Base16InputStream(bin, true)) {
- in.read(null, 0, 0);
- fail("Base16InputStream.read(null, 0, 0) to throw a NullPointerException");
- } catch (final NullPointerException e) {
- // Expected
+ assertThrows(NullPointerException.class, () -> in.read(null, 0, 0));
}
}
@@ -326,34 +324,10 @@ public class Base16InputStreamTest {
final byte[] buf = new byte[1024];
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
try (final Base16InputStream in = new Base16InputStream(bin, true)) {
-
- try {
- in.read(buf, -1, 0);
- fail("Expected Base16InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, 0, -1);
- fail("Expected Base16InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, buf.length + 1, 0);
- fail("Base16InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, buf.length - 1, 2);
- fail("Base16InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
+ assertThrows("Base16InputStream.read(buf, -1, 0)", IndexOutOfBoundsException.class, () -> in.read(buf, -1, 0));
+ assertThrows("Base16InputStream.read(buf, 0, -1)", IndexOutOfBoundsException.class, () -> in.read(buf, 0, -1));
+ assertThrows("Base16InputStream.read(buf, buf.length + 1, 0)", IndexOutOfBoundsException.class, () -> in.read(buf, buf.length + 1, 0));
+ assertThrows("Base16InputStream.read(buf, buf.length - 1, 2)", IndexOutOfBoundsException.class, () -> in.read(buf, buf.length - 1, 2));
}
}
diff --git a/src/test/java/org/apache/commons/codec/binary/Base16OutputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base16OutputStreamTest.java
index 57237d6..465fb9b 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base16OutputStreamTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base16OutputStreamTest.java
@@ -24,6 +24,7 @@ import java.io.IOException;
import java.io.OutputStream;
import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
/**
@@ -238,34 +239,10 @@ public class Base16OutputStreamTest {
final byte[] buf = new byte[1024];
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (final Base16OutputStream out = new Base16OutputStream(bout)) {
-
- try {
- out.write(buf, -1, 1);
- fail("Expected Base16OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
-
- try {
- out.write(buf, 1, -1);
- fail("Expected Base16OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
-
- try {
- out.write(buf, buf.length + 1, 0);
- fail("Expected Base16OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
-
- try {
- out.write(buf, buf.length - 1, 2);
- fail("Expected Base16OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
+ assertThrows("Base16InputStream.write(buf, -1, 0)", IndexOutOfBoundsException.class, () -> out.write(buf, -1, 1));
+ assertThrows("Base16InputStream.write(buf, 1, -1)", IndexOutOfBoundsException.class, () -> out.write(buf, 1, -1));
+ assertThrows("Base16InputStream.write(buf, buf.length + 1, 0)", IndexOutOfBoundsException.class, () -> out.write(buf, buf.length + 1, 0));
+ assertThrows("Base16InputStream.write(buf, buf.length - 1, 2)", IndexOutOfBoundsException.class, () -> out.write(buf, buf.length - 1, 2));
}
}
@@ -278,10 +255,7 @@ public class Base16OutputStreamTest {
public void testWriteToNullCoverage() throws IOException {
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (final Base16OutputStream out = new Base16OutputStream(bout)) {
- out.write(null, 0, 0);
- fail("Expcted Base16OutputStream.write(null) to throw a NullPointerException");
- } catch (final NullPointerException e) {
- // Expected
+ assertThrows(NullPointerException.class, () -> out.write(null, 0, 0));
}
}
}
diff --git a/src/test/java/org/apache/commons/codec/binary/Base16Test.java b/src/test/java/org/apache/commons/codec/binary/Base16Test.java
index b6c9629..b2cb999 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base16Test.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base16Test.java
@@ -31,6 +31,7 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -211,27 +212,14 @@ public class Base16Test {
final byte[] encoded = new byte[1];
for (final byte invalidEncodedChar : invalidEncodedChars) {
- try {
- encoded[0] = invalidEncodedChar;
- new Base16().decode(encoded);
- fail("IllegalArgumentException should have been thrown when trying to decode invalid Base16 char: " + (char)invalidEncodedChar);
- } catch (final Exception e) {
- assertTrue(e instanceof IllegalArgumentException);
- }
+ encoded[0] = invalidEncodedChar;
+ assertThrows("Invalid Base16 char: " + (char) invalidEncodedChar, IllegalArgumentException.class, () -> new Base16().decode(encoded));
}
}
@Test
public void testObjectDecodeWithInvalidParameter() {
- final Base16 b16 = new Base16();
-
- try {
- b16.decode(Integer.valueOf(5));
- fail("decode(Object) didn't throw an exception when passed an Integer object");
- } catch (final DecoderException e) {
- // ignored
- }
-
+ assertThrows(DecoderException.class, () -> new Base16().decode(Integer.valueOf(5)));
}
@Test
@@ -249,13 +237,7 @@ public class Base16Test {
@Test
public void testObjectEncodeWithInvalidParameter() {
- final Base16 b16 = new Base16();
- try {
- b16.encode("Yadayadayada");
- fail("encode(Object) didn't throw an exception when passed a String object");
- } catch (final EncoderException e) {
- // Expected
- }
+ assertThrows(EncoderException.class, () -> new Base16().encode("Yadayadayada"));
}
@Test
diff --git a/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java
index 7ea986b..5645e98 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java
@@ -23,11 +23,13 @@ import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.codec.CodecPolicy;
+import org.apache.commons.codec.EncoderException;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -421,11 +423,8 @@ public class Base32InputStreamTest {
public void testReadNull() throws Exception {
final byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE);
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
- try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
- in.read(null, 0, 0);
- fail("Base32InputStream.read(null, 0, 0) to throw a NullPointerException");
- } catch (final NullPointerException e) {
- // Expected
+ try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] {0, 0, 0})) {
+ assertThrows(NullPointerException.class, () -> in.read(null, 0, 0));
}
}
@@ -441,34 +440,10 @@ public class Base32InputStreamTest {
final byte[] buf = new byte[1024];
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
try (final Base32InputStream in = new Base32InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
-
- try {
- in.read(buf, -1, 0);
- fail("Expected Base32InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, 0, -1);
- fail("Expected Base32InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, buf.length + 1, 0);
- fail("Base32InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, buf.length - 1, 2);
- fail("Base32InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
+ assertThrows("Base32InputStream.read(buf, -1, 0)", IndexOutOfBoundsException.class, () -> in.read(buf, -1, 0));
+ assertThrows("Base32InputStream.read(buf, 0, -1)", IndexOutOfBoundsException.class, () -> in.read(buf, 0, -1));
+ assertThrows("Base32InputStream.read(buf, buf.length + 1, 0)", IndexOutOfBoundsException.class, () -> in.read(buf, buf.length + 1, 0));
+ assertThrows("Base32InputStream.read(buf, buf.length - 1, 2)", IndexOutOfBoundsException.class, () -> in.read(buf, buf.length - 1, 2));
}
}
@@ -574,14 +549,9 @@ public class Base32InputStreamTest {
BaseNTestData.streamToBytes(in);
// Strict decoding should throw
- in = new Base32InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
- assertTrue(in.isStrictDecoding());
- try {
- BaseNTestData.streamToBytes(in);
- fail();
- } catch (final IllegalArgumentException ex) {
- // expected
- }
+ Base32InputStream in2 = new Base32InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
+ assertTrue(in2.isStrictDecoding());
+ assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in2));
}
}
}
diff --git a/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java
index ae928d3..c23f20b 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base32OutputStreamTest.java
@@ -25,6 +25,7 @@ import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -287,34 +288,10 @@ public class Base32OutputStreamTest {
final byte[] buf = new byte[1024];
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (final Base32OutputStream out = new Base32OutputStream(bout)) {
-
- try {
- out.write(buf, -1, 1);
- fail("Expected Base32OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
-
- try {
- out.write(buf, 1, -1);
- fail("Expected Base32OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
-
- try {
- out.write(buf, buf.length + 1, 0);
- fail("Expected Base32OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
-
- try {
- out.write(buf, buf.length - 1, 2);
- fail("Expected Base32OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
+ assertThrows("Base32OutputStream.write(buf, -1, 1)", IndexOutOfBoundsException.class, () -> out.write(buf, -1, 1));
+ assertThrows("Base32OutputStream.write(buf, 1, -1)", IndexOutOfBoundsException.class, () -> out.write(buf, 1, -1));
+ assertThrows("Base32OutputStream.write(buf, buf, buf.length + 1, 0)", IndexOutOfBoundsException.class, () -> out.write(buf, buf.length + 1, 0));
+ assertThrows("Base32OutputStream.write(buf, buf, buf.length - 1, 2)", IndexOutOfBoundsException.class, () -> out.write(buf, buf.length - 1, 2));
}
}
@@ -328,10 +305,7 @@ public class Base32OutputStreamTest {
public void testWriteToNullCoverage() throws Exception {
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (final Base32OutputStream out = new Base32OutputStream(bout)) {
- out.write(null, 0, 0);
- fail("Expcted Base32OutputStream.write(null) to throw a NullPointerException");
- } catch (final NullPointerException e) {
- // Expected
+ assertThrows(NullPointerException.class, () -> out.write(null, 0, 0));
}
}
@@ -355,15 +329,10 @@ public class Base32OutputStreamTest {
// Strict decoding should throw
bout = new ByteArrayOutputStream();
- out = new Base32OutputStream(bout, false, 0, null, CodecPolicy.STRICT);
- assertTrue(out.isStrictDecoding());
- try {
- out.write(encoded);
- out.close();
- fail();
- } catch (final IllegalArgumentException ex) {
- // expected
- }
+ final Base32OutputStream out2 = new Base32OutputStream(bout, false, 0, null, CodecPolicy.STRICT);
+ assertTrue(out2.isStrictDecoding());
+ assertThrows(IllegalArgumentException.class, () -> out2.write(encoded));
+ out2.close();
}
}
}
diff --git a/src/test/java/org/apache/commons/codec/binary/Base32Test.java b/src/test/java/org/apache/commons/codec/binary/Base32Test.java
index 7fdcbb1..713c80d 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base32Test.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base32Test.java
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -249,45 +250,15 @@ public class Base32Test {
base32 = new Base32(32, new byte[] {}, false);
// This is different behavior than Base64 which validates the separator
// even when line length is negative.
- base32 = new Base32(-1, new byte[] { 'A' });
- try {
- base32 = new Base32(32, null);
- fail("Should have rejected null line separator");
- } catch (final IllegalArgumentException ignored) {
- // Expected
- }
- try {
- base32 = new Base32(32, new byte[] { 'A' });
- fail("Should have rejected attempt to use 'A' as a line separator");
- } catch (final IllegalArgumentException ignored) {
- // Expected
- }
- try {
- base32 = new Base32(32, new byte[] { '=' });
- fail("Should have rejected attempt to use '=' as a line separator");
- } catch (final IllegalArgumentException ignored) {
- // Expected
- }
- base32 = new Base32(32, new byte[] { '$' }); // OK
- try {
- base32 = new Base32(32, new byte[] { 'A', '$' });
- fail("Should have rejected attempt to use 'A$' as a line separator");
- } catch (final IllegalArgumentException ignored) {
- // Expected
- }
- try {
- base32 = new Base32(32, new byte[] { '\n'}, false, (byte) 'A');
- fail("Should have rejected attempt to use 'A' as padding");
- } catch (final IllegalArgumentException ignored) {
- // Expected
- }
- try {
- base32 = new Base32(32, new byte[] { '\n'}, false, (byte) ' ');
- fail("Should have rejected attempt to use ' ' as padding");
- } catch (final IllegalArgumentException ignored) {
- // Expected
- }
- base32 = new Base32(32, new byte[] { ' ', '$', '\n', '\r', '\t' }); // OK
+ base32 = new Base32(-1, new byte[] {'A'});
+ base32 = new Base32(32, new byte[] {'$'}); // OK
+ assertThrows("null line separator", IllegalArgumentException.class, () -> new Base32(32, null));
+ assertThrows("'A' as a line separator", IllegalArgumentException.class, () -> new Base32(32, new byte[] {'A'}));
+ assertThrows("'=' as a line separator", IllegalArgumentException.class, () -> new Base32(32, new byte[] {'='}));
+ assertThrows("'A$' as a line separator", IllegalArgumentException.class, () -> new Base32(32, new byte[] {'A', '$'}));
+ assertThrows("'A' as padding", IllegalArgumentException.class, () -> new Base32(32, new byte[] {'\n'}, false, (byte) 'A'));
+ assertThrows("' ' as padding", IllegalArgumentException.class, () -> new Base32(32, new byte[] {'\n'}, false, (byte) ' '));
+ base32 = new Base32(32, new byte[] {' ', '$', '\n', '\r', '\t'}); // OK
assertNotNull(base32);
}
@@ -429,12 +400,7 @@ public class Base32Test {
private void testImpossibleCases(final Base32 codec, final String[] impossible_cases) {
for (final String impossible : impossible_cases) {
- try {
- codec.decode(impossible);
- fail();
- } catch (final IllegalArgumentException ex) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> codec.decode(impossible));
}
}
@@ -509,12 +475,7 @@ public class Base32Test {
// If the lower bits are set we expect an exception. This is not a valid
// final character.
if (invalid || (i & emptyBitsMask) != 0) {
- try {
- codec.decode(encoded);
- fail("Final base-32 digit should not be allowed");
- } catch (final IllegalArgumentException ex) {
- // expected
- }
+ assertThrows("Final base-32 digit should not be allowed", IllegalArgumentException.class, () -> codec.decode(encoded));
// The default lenient mode should decode this
final byte[] decoded = defaultCodec.decode(encoded);
// Re-encoding should not match the original array as it was invalid
diff --git a/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java
index 0dd033d..c84c3ab 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java
@@ -31,6 +31,7 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -461,10 +462,7 @@ public class Base64InputStreamTest {
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
try (final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
- in.read(null, 0, 0);
- fail("Base64InputStream.read(null, 0, 0) to throw a NullPointerException");
- } catch (final NullPointerException e) {
- // Expected
+ assertThrows(NullPointerException.class, () -> in.read(null, 0, 0));
}
}
@@ -479,35 +477,11 @@ public class Base64InputStreamTest {
final byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
final byte[] buf = new byte[1024];
final ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
- try (final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] { 0, 0, 0 })) {
-
- try {
- in.read(buf, -1, 0);
- fail("Expected Base64InputStream.read(buf, -1, 0) to throw IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, 0, -1);
- fail("Expected Base64InputStream.read(buf, 0, -1) to throw IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, buf.length + 1, 0);
- fail("Base64InputStream.read(buf, buf.length + 1, 0) throws IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
-
- try {
- in.read(buf, buf.length - 1, 2);
- fail("Base64InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // Expected
- }
+ try (final Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[] {0, 0, 0})) {
+ assertThrows("Base64InputStream.read(buf, -1, 0)", IndexOutOfBoundsException.class, () -> in.read(buf, -1, 0));
+ assertThrows("Base64InputStream.read(buf, 0, -1)", IndexOutOfBoundsException.class, () -> in.read(buf, 0, -1));
+ assertThrows("Base64InputStream.read(buf, buf.length + 1, 0)", IndexOutOfBoundsException.class, () -> in.read(buf, buf.length + 1, 0));
+ assertThrows("Base64InputStream.read(buf, buf.length - 1, 2)", IndexOutOfBoundsException.class, () -> in.read(buf, buf.length - 1, 2));
}
}
@@ -613,14 +587,9 @@ public class Base64InputStreamTest {
BaseNTestData.streamToBytes(in);
// Strict decoding should throw
- in = new Base64InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
- assertTrue(in.isStrictDecoding());
- try {
- BaseNTestData.streamToBytes(in);
- fail();
- } catch (final IllegalArgumentException ex) {
- // expected
- }
+ Base64InputStream in2 = new Base64InputStream(new ByteArrayInputStream(encoded), false, 0, null, CodecPolicy.STRICT);
+ assertTrue(in2.isStrictDecoding());
+ assertThrows(IllegalArgumentException.class, () -> BaseNTestData.streamToBytes(in2));
}
}
}
diff --git a/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java
index 08929a5..4864f89 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base64OutputStreamTest.java
@@ -26,6 +26,7 @@ import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -297,34 +298,10 @@ public class Base64OutputStreamTest {
final byte[] buf = new byte[1024];
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (final Base64OutputStream out = new Base64OutputStream(bout)) {
-
- try {
- out.write(buf, -1, 1);
- fail("Expected Base64OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
-
- try {
- out.write(buf, 1, -1);
- fail("Expected Base64OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
-
- try {
- out.write(buf, buf.length + 1, 0);
- fail("Expected Base64OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
-
- try {
- out.write(buf, buf.length - 1, 2);
- fail("Expected Base64OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException ioobe) {
- // Expected
- }
+ assertThrows("Base64OutputStream.write(buf, -1, 1)", IndexOutOfBoundsException.class, () -> out.write(buf, -1, 1));
+ assertThrows("Base64OutputStream.write(buf, 1, -1)", IndexOutOfBoundsException.class, () -> out.write(buf, 1, -1));
+ assertThrows("Base64OutputStream.write(buf, buf.length + 1, 0)", IndexOutOfBoundsException.class, () -> out.write(buf, buf.length + 1, 0));
+ assertThrows("Base64OutputStream.write(buf, buf.length - 1, 2)", IndexOutOfBoundsException.class, () -> out.write(buf, buf.length - 1, 2));
}
}
@@ -338,10 +315,7 @@ public class Base64OutputStreamTest {
public void testWriteToNullCoverage() throws Exception {
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (final Base64OutputStream out = new Base64OutputStream(bout)) {
- out.write(null, 0, 0);
- fail("Expcted Base64OutputStream.write(null) to throw a NullPointerException");
- } catch (final NullPointerException e) {
- // Expected
+ assertThrows(NullPointerException.class, () -> out.write(null, 0, 0));
}
}
@@ -353,26 +327,27 @@ public class Base64OutputStreamTest {
*/
@Test
public void testStrictDecoding() throws Exception {
- for (final String s : Base64Test.BASE64_IMPOSSIBLE_CASES) {
- final byte[] encoded = StringUtils.getBytesUtf8(s);
+ for (final String impossibleStr : Base64Test.BASE64_IMPOSSIBLE_CASES) {
+ final byte[] impossibleEncoded = StringUtils.getBytesUtf8(impossibleStr);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
- Base64OutputStream out = new Base64OutputStream(bout, false);
- // Default is lenient decoding; it should not throw
- assertFalse(out.isStrictDecoding());
- out.write(encoded);
- out.close();
+ try (Base64OutputStream out = new Base64OutputStream(bout, false)) {
+ // Default is lenient decoding; it should not throw
+ assertFalse(out.isStrictDecoding());
+ out.write(impossibleEncoded);
+ }
assertTrue(bout.size() > 0);
// Strict decoding should throw
bout = new ByteArrayOutputStream();
- out = new Base64OutputStream(bout, false, 0, null, CodecPolicy.STRICT);
+ Base64OutputStream out = new Base64OutputStream(bout, false, 0, null, CodecPolicy.STRICT);
assertTrue(out.isStrictDecoding());
try {
- out.write(encoded);
+ out.write(impossibleEncoded);
out.close();
fail();
} catch (final IllegalArgumentException ex) {
// expected
+ ex.printStackTrace();
}
}
}
diff --git a/src/test/java/org/apache/commons/codec/binary/Base64Test.java b/src/test/java/org/apache/commons/codec/binary/Base64Test.java
index 17cb268..d4e8611 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base64Test.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base64Test.java
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -88,15 +89,9 @@ public class Base64Test {
final String nullString = null;
final String emptyString = "";
final String validString = "abc===defg\n\r123456\r789\r\rABC\n\nDEF==GHI\r\nJKL==============";
- final String invalidString = validString + (char) 0; // append null
- // character
+ final String invalidString = validString + (char) 0; // append null character
- try {
- Base64.isBase64(nullString);
- fail("Base64.isStringBase64() should not be null-safe.");
- } catch (final NullPointerException npe) {
- assertNotNull("Base64.isStringBase64() should not be null-safe.", npe);
- }
+ assertThrows(NullPointerException.class, () -> Base64.isBase64(nullString));
assertTrue("Base64.isStringBase64(empty-string) is true", Base64.isBase64(emptyString));
assertTrue("Base64.isStringBase64(valid-string) is true", Base64.isBase64(validString));
@@ -261,14 +256,7 @@ public class Base64Test {
@Test
public void testCodeIntegerNull() {
- try {
- Base64.encodeInteger(null);
- fail("Exception not thrown when passing in null to encodeInteger(BigInteger)");
- } catch (final NullPointerException npe) {
- // expected
- } catch (final Exception e) {
- fail("Incorrect Exception caught when passing in null to encodeInteger(BigInteger)");
- }
+ assertThrows(NullPointerException.class, () -> Base64.encodeInteger(null));
}
@Test
@@ -278,34 +266,14 @@ public class Base64Test {
base64 = new Base64(-1);
base64 = new Base64(-1, new byte[] {});
base64 = new Base64(64, new byte[] {});
- try {
- base64 = new Base64(-1, new byte[] { 'A' }); // TODO do we need to
- // check sep if len
- // = -1?
- fail("Should have rejected attempt to use 'A' as a line separator");
- } catch (final IllegalArgumentException ignored) {
- // Expected
- }
- try {
- base64 = new Base64(64, new byte[] { 'A' });
- fail("Should have rejected attempt to use 'A' as a line separator");
- } catch (final IllegalArgumentException ignored) {
- // Expected
- }
- try {
- base64 = new Base64(64, new byte[] { '=' });
- fail("Should have rejected attempt to use '=' as a line separator");
- } catch (final IllegalArgumentException ignored) {
- // Expected
- }
- base64 = new Base64(64, new byte[] { '$' }); // OK
- try {
- base64 = new Base64(64, new byte[] { 'A', '$' });
- fail("Should have rejected attempt to use 'A$' as a line separator");
- } catch (final IllegalArgumentException ignored) {
- // Expected
- }
- base64 = new Base64(64, new byte[] { ' ', '$', '\n', '\r', '\t' }); // OK
+ base64 = new Base64(64, new byte[] {'$'}); // OK
+
+ assertThrows("'A' as a line separator", IllegalArgumentException.class, () -> new Base64(-1, new byte[] {'A'}));
+ assertThrows("'A' as a line separator", IllegalArgumentException.class, () -> new Base64(64, new byte[] {'A'}));
+ assertThrows("'=' as a line separator", IllegalArgumentException.class, () -> new Base64(64, new byte[] {'='}));
+ assertThrows("'A$' as a line separator", IllegalArgumentException.class, () -> new Base64(64, new byte[] {'A', '$'}));
+
+ base64 = new Base64(64, new byte[] {' ', '$', '\n', '\r', '\t'}); // OK
assertNotNull(base64);
}
@@ -455,12 +423,7 @@ public class Base64Test {
}
private void testEncodeOverMaxSize(final int maxSize) throws Exception {
- try {
- Base64.encodeBase64(BaseNTestData.DECODED, true, false, maxSize);
- fail("Expected " + IllegalArgumentException.class.getName());
- } catch (final IllegalArgumentException e) {
- // Expected
- }
+ assertThrows(IllegalArgumentException.class, () -> Base64.encodeBase64(BaseNTestData.DECODED, true, false, maxSize));
}
@Test
@@ -559,15 +522,7 @@ public class Base64Test {
@Test
public void testObjectDecodeWithInvalidParameter() throws Exception {
- final Base64 b64 = new Base64();
-
- try {
- b64.decode(Integer.valueOf(5));
- fail("decode(Object) didn't throw an exception when passed an Integer object");
- } catch (final DecoderException e) {
- // ignored
- }
-
+ assertThrows(DecoderException.class, () -> new Base64().decode(Integer.valueOf(5)));
}
@Test
@@ -586,13 +541,7 @@ public class Base64Test {
@Test
public void testObjectEncodeWithInvalidParameter() throws Exception {
- final Base64 b64 = new Base64();
- try {
- b64.encode("Yadayadayada");
- fail("encode(Object) didn't throw an exception when passed a String object");
- } catch (final EncoderException e) {
- // Expected
- }
+ assertThrows(EncoderException.class, () -> new Base64().encode("Yadayadayada"));
}
@Test
@@ -1333,12 +1282,7 @@ public class Base64Test {
public void testBase64ImpossibleSamples() {
final Base64 codec = new Base64(0, null, false, CodecPolicy.STRICT);
for (final String s : BASE64_IMPOSSIBLE_CASES) {
- try {
- codec.decode(s);
- fail();
- } catch (final IllegalArgumentException ex) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> codec.decode(s));
}
}
@@ -1393,12 +1337,7 @@ public class Base64Test {
// If the lower bits are set we expect an exception. This is not a valid
// final character.
if (invalid || (i & emptyBitsMask) != 0) {
- try {
- codec.decode(encoded);
- fail("Final base-64 digit should not be allowed");
- } catch (final IllegalArgumentException ex) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> codec.decode(encoded));
// The default lenient mode should decode this
final byte[] decoded = defaultCodec.decode(encoded);
// Re-encoding should not match the original array as it was invalid
diff --git a/src/test/java/org/apache/commons/codec/binary/HexTest.java b/src/test/java/org/apache/commons/codec/binary/HexTest.java
index eb46e48..d483562 100644
--- a/src/test/java/org/apache/commons/codec/binary/HexTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/HexTest.java
@@ -20,6 +20,7 @@ package org.apache.commons.codec.binary;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -119,30 +120,15 @@ public class HexTest {
}
private void checkDecodeHexCharArrayOddCharacters(final char[] data) {
- try {
- Hex.decodeHex(data);
- fail("An exception wasn't thrown when trying to decode an odd number of characters");
- } catch (final DecoderException e) {
- // Expected exception
- }
+ assertThrows(DecoderException.class, () -> Hex.decodeHex(data));
}
private void checkDecodeHexByteBufferOddCharacters(final ByteBuffer data) {
- try {
- new Hex().decode(data);
- fail("An exception wasn't thrown when trying to decode an odd number of characters");
- } catch (final DecoderException e) {
- // Expected exception
- }
+ assertThrows(DecoderException.class, () -> new Hex().decode(data));
}
private void checkDecodeHexCharArrayOddCharacters(final String data) {
- try {
- Hex.decodeHex(data);
- fail("An exception wasn't thrown when trying to decode an odd number of characters");
- } catch (final DecoderException e) {
- // Expected exception
- }
+ assertThrows(DecoderException.class, () -> Hex.decodeHex(data));
}
private void log(final String s) {
@@ -219,22 +205,12 @@ public class HexTest {
@Test
public void testDecodeBadCharacterPos0() {
- try {
- new Hex().decode("q0");
- fail("An exception wasn't thrown when trying to decode an illegal character");
- } catch (final DecoderException e) {
- // Expected exception
- }
+ assertThrows(DecoderException.class, () -> new Hex().decode("q0"));
}
@Test
public void testDecodeBadCharacterPos1() {
- try {
- new Hex().decode("0q");
- fail("An exception wasn't thrown when trying to decode an illegal character");
- } catch (final DecoderException e) {
- // Expected exception
- }
+ assertThrows(DecoderException.class, () -> new Hex().decode("0q"));
}
@Test
@@ -249,12 +225,7 @@ public class HexTest {
@Test
public void testDecodeByteArrayOddCharacters() {
- try {
- new Hex().decode(new byte[] { 65 });
- fail("An exception wasn't thrown when trying to decode an odd number of characters");
- } catch (final DecoderException e) {
- // Expected exception
- }
+ assertThrows("odd number of characters", DecoderException.class, () -> new Hex().decode(new byte[] { 65 }));
}
@Test
@@ -305,12 +276,7 @@ public class HexTest {
@Test
public void testDecodeClassCastException() {
- try {
- new Hex().decode(new int[] { 65 });
- fail("An exception wasn't thrown when trying to decode.");
- } catch (final DecoderException e) {
- // Expected exception
- }
+ assertThrows("odd number of characters", DecoderException.class, () -> new Hex().decode(new int[] { 65 }));
}
@Test
@@ -347,12 +313,8 @@ public class HexTest {
@Test
public void testDecodeHexStringOddCharacters() {
- try {
- new Hex().decode("6");
- fail("An exception wasn't thrown when trying to decode an odd number of characters");
- } catch (final DecoderException e) {
- // Expected exception
- }
+ assertThrows("odd number of characters", DecoderException.class, () -> new Hex().decode("6"));
+
}
@Test
@@ -404,12 +366,7 @@ public class HexTest {
@Test
public void testEncodeClassCastException() {
- try {
- new Hex().encode(new int[] { 65 });
- fail("An exception wasn't thrown when trying to encode.");
- } catch (final EncoderException e) {
- // Expected exception
- }
+ assertThrows(EncoderException.class, () -> new Hex().encode(new int[] { 65 }));
}
@Test
diff --git a/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java b/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
index d671a07..d19dd98 100644
--- a/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/StringUtilsTest.java
@@ -17,10 +17,13 @@
package org.apache.commons.codec.binary;
+import static org.junit.Assert.assertThrows;
+
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
+import org.apache.commons.codec.EncoderException;
import org.junit.Assert;
import org.junit.Test;
@@ -111,12 +114,7 @@ public class StringUtilsTest {
@Test
public void testGetBytesUncheckedBadName() {
- try {
- StringUtils.getBytesUnchecked(STRING_FIXTURE, "UNKNOWN");
- Assert.fail("Expected " + IllegalStateException.class.getName());
- } catch (final IllegalStateException e) {
- // Expected
- }
+ assertThrows(IllegalStateException.class, () -> StringUtils.getBytesUnchecked(STRING_FIXTURE, "UNKNOWN"));
}
@Test
@@ -132,12 +130,7 @@ public class StringUtilsTest {
@Test
public void testNewStringBadEnc() {
- try {
- StringUtils.newString(BYTES_FIXTURE, "UNKNOWN");
- Assert.fail("Expected " + IllegalStateException.class.getName());
- } catch (final IllegalStateException e) {
- // Expected
- }
+ assertThrows(IllegalStateException.class, () -> StringUtils.newString(BYTES_FIXTURE, "UNKNOWN"));
}
@Test
diff --git a/src/test/java/org/apache/commons/codec/digest/Blake3Test.java b/src/test/java/org/apache/commons/codec/digest/Blake3Test.java
index 1e8ee2d..9ff67ca 100644
--- a/src/test/java/org/apache/commons/codec/digest/Blake3Test.java
+++ b/src/test/java/org/apache/commons/codec/digest/Blake3Test.java
@@ -19,8 +19,11 @@ package org.apache.commons.codec.digest;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
+import org.apache.commons.codec.binary.StringUtils;
+
public class Blake3Test {
@Test
public void shouldThrowIllegalArgumentExceptionWhenIncorrectKeySize() {
@@ -31,11 +34,6 @@ public class Blake3Test {
}
private static void assertThrowsProperExceptionWithKeySize(final int keySize) {
- try {
- Blake3.initKeyedHash(new byte[keySize]);
- fail("Should have thrown exception");
- } catch (final IllegalArgumentException expected) {
- assertEquals("Blake3 keys must be 32 bytes", expected.getMessage());
- }
+ assertThrows("Blake3 keys must be 32 bytes", IllegalArgumentException.class, () -> Blake3.initKeyedHash(new byte[keySize]));
}
}
diff --git a/src/test/java/org/apache/commons/codec/language/SoundexTest.java b/src/test/java/org/apache/commons/codec/language/SoundexTest.java
index 45bd814..b2d2102 100644
--- a/src/test/java/org/apache/commons/codec/language/SoundexTest.java
+++ b/src/test/java/org/apache/commons/codec/language/SoundexTest.java
@@ -19,8 +19,11 @@
package org.apache.commons.codec.language;
+import static org.junit.Assert.assertThrows;
+
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.StringEncoderAbstractTest;
+import org.apache.commons.codec.digest.Blake3;
import org.junit.Assert;
import org.junit.Test;
@@ -354,13 +357,8 @@ public class SoundexTest extends StringEncoderAbstractTest<Soundex> {
public void testUsMappingEWithAcute() {
Assert.assertEquals("E000", this.getStringEncoder().encode("e"));
if (Character.isLetter('\u00e9')) { // e-acute
- try {
- // uppercase E-acute
- Assert.assertEquals("\u00c9000", this.getStringEncoder().encode("\u00e9"));
- Assert.fail("Expected IllegalArgumentException not thrown");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ // uppercase E-acute
+ assertThrows(IllegalArgumentException.class, () -> getStringEncoder().encode("\u00e9"));
} else {
Assert.assertEquals("", this.getStringEncoder().encode("\u00e9"));
}
@@ -375,13 +373,8 @@ public class SoundexTest extends StringEncoderAbstractTest<Soundex> {
public void testUsMappingOWithDiaeresis() {
Assert.assertEquals("O000", this.getStringEncoder().encode("o"));
if (Character.isLetter('\u00f6')) { // o-umlaut
- try {
- // uppercase O-umlaut
- Assert.assertEquals("\u00d6000", this.getStringEncoder().encode("\u00f6"));
- Assert.fail("Expected IllegalArgumentException not thrown");
- } catch (final IllegalArgumentException e) {
- // expected
- }
+ // uppercase O-umlaut
+ assertThrows(IllegalArgumentException.class, () -> getStringEncoder().encode("\u00f6"));
} else {
Assert.assertEquals("", this.getStringEncoder().encode("\u00f6"));
}
diff --git a/src/test/java/org/apache/commons/codec/net/BCodecTest.java b/src/test/java/org/apache/commons/codec/net/BCodecTest.java
index d96b044..a072f51 100644
--- a/src/test/java/org/apache/commons/codec/net/BCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/BCodecTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.codec.net;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import java.nio.charset.StandardCharsets;
@@ -126,13 +127,7 @@ public class BCodecTest {
final Object result = bcodec.encode((Object) null);
assertNull("Encoding a null Object should return null", result);
- try {
- final Object dObj = Double.valueOf(3.0d);
- bcodec.encode(dObj);
- fail("Trying to url encode a Double object should cause an exception.");
- } catch (final EncoderException ee) {
- // Exception expected, test segment passes.
- }
+ assertThrows(EncoderException.class, () -> bcodec.encode(Double.valueOf(3.0d)));
}
@Test(expected=UnsupportedCharsetException.class)
@@ -150,13 +145,7 @@ public class BCodecTest {
final Object result = bcodec.decode((Object) null);
assertNull("Decoding a null Object should return null", result);
- try {
- final Object dObj = Double.valueOf(3.0d);
- bcodec.decode(dObj);
- fail("Trying to url encode a Double object should cause an exception.");
- } catch (final DecoderException ee) {
- // Exception expected, test segment passes.
- }
+ assertThrows(DecoderException.class, () -> bcodec.decode(Double.valueOf(3.0d)));
}
@Test
@@ -184,12 +173,7 @@ public class BCodecTest {
final BCodec codec = new BCodec(StandardCharsets.UTF_8, CodecPolicy.STRICT);
Assert.assertTrue(codec.isStrictDecoding());
for (final String s : BASE64_IMPOSSIBLE_CASES) {
- try {
- codec.decode(s);
- fail("Expected an exception for impossible case");
- } catch (final DecoderException ex) {
- // expected
- }
+ assertThrows(DecoderException.class, () -> codec.decode(s));
}
}
diff --git a/src/test/java/org/apache/commons/codec/net/QCodecTest.java b/src/test/java/org/apache/commons/codec/net/QCodecTest.java
index 4b258dd..3be61ca 100644
--- a/src/test/java/org/apache/commons/codec/net/QCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/QCodecTest.java
@@ -21,6 +21,7 @@ package org.apache.commons.codec.net;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -135,19 +136,12 @@ public class QCodecTest {
final QCodec qcodec = new QCodec();
final String plain = "1+1 = 2";
final String encoded = (String) qcodec.encode((Object) plain);
- assertEquals("Basic Q encoding test",
- "=?UTF-8?Q?1+1 =3D 2?=", encoded);
+ assertEquals("Basic Q encoding test", "=?UTF-8?Q?1+1 =3D 2?=", encoded);
final Object result = qcodec.encode((Object) null);
assertNull("Encoding a null Object should return null", result);
- try {
- final Object dObj = Double.valueOf(3.0d);
- qcodec.encode( dObj );
- fail( "Trying to url encode a Double object should cause an exception.");
- } catch (final EncoderException ee) {
- // Exception expected, test segment passes.
- }
+ assertThrows(EncoderException.class, () -> qcodec.encode(Double.valueOf(3.0d)));
}
@@ -161,19 +155,12 @@ public class QCodecTest {
final QCodec qcodec = new QCodec();
final String decoded = "=?UTF-8?Q?1+1 =3D 2?=";
final String plain = (String) qcodec.decode((Object) decoded);
- assertEquals("Basic Q decoding test",
- "1+1 = 2", plain);
+ assertEquals("Basic Q decoding test", "1+1 = 2", plain);
final Object result = qcodec.decode((Object) null);
assertNull("Decoding a null Object should return null", result);
- try {
- final Object dObj = Double.valueOf(3.0d);
- qcodec.decode( dObj );
- fail( "Trying to url encode a Double object should cause an exception.");
- } catch (final DecoderException ee) {
- // Exception expected, test segment passes.
- }
+ assertThrows(DecoderException.class, () -> qcodec.decode(Double.valueOf(3.0d)));
}
diff --git a/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java b/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
index 8f7005e..94149f4 100644
--- a/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
@@ -117,24 +117,9 @@ public class QuotedPrintableCodecTest {
@Test
public void testDecodeInvalid() throws Exception {
final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
- try {
- qpcodec.decode("=");
- fail("DecoderException should have been thrown");
- } catch (final DecoderException e) {
- // Expected. Move on
- }
- try {
- qpcodec.decode("=A");
- fail("DecoderException should have been thrown");
- } catch (final DecoderException e) {
- // Expected. Move on
- }
- try {
- qpcodec.decode("=WW");
- fail("DecoderException should have been thrown");
- } catch (final DecoderException e) {
- // Expected. Move on
- }
+ assertThrows(DecoderException.class, () -> qpcodec.decode("="));
+ assertThrows(DecoderException.class, () -> qpcodec.decode("=A"));
+ assertThrows(DecoderException.class, () -> qpcodec.decode("=WW"));
}
@Test
@@ -186,25 +171,17 @@ public class QuotedPrintableCodecTest {
final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec();
final String plain = "1+1 = 2";
String encoded = (String) qpcodec.encode((Object) plain);
- assertEquals("Basic quoted-printable encoding test",
- "1+1 =3D 2", encoded);
+ assertEquals("Basic quoted-printable encoding test", "1+1 =3D 2", encoded);
final byte[] plainBA = plain.getBytes(StandardCharsets.UTF_8);
final byte[] encodedBA = (byte[]) qpcodec.encode((Object) plainBA);
encoded = new String(encodedBA);
- assertEquals("Basic quoted-printable encoding test",
- "1+1 =3D 2", encoded);
+ assertEquals("Basic quoted-printable encoding test", "1+1 =3D 2", encoded);
final Object result = qpcodec.encode((Object) null);
assertNull("Encoding a null Object should return null", result);
- try {
- final Object dObj = Double.valueOf(3.0d);
- qpcodec.encode( dObj );
- fail( "Trying to url encode a Double object should cause an exception.");
- } catch (final EncoderException ee) {
- // Exception expected, test segment passes.
- }
+ assertThrows(EncoderException.class, () -> qpcodec.encode(Double.valueOf(3.0d)));
}
@Test(expected=UnsupportedCharsetException.class)
@@ -229,13 +206,7 @@ public class QuotedPrintableCodecTest {
final Object result = qpcodec.decode((Object) null);
assertNull("Decoding a null Object should return null", result);
- try {
- final Object dObj = Double.valueOf(3.0d);
- qpcodec.decode( dObj );
- fail( "Trying to url encode a Double object should cause an exception.");
- } catch (final DecoderException ee) {
- // Exception expected, test segment passes.
- }
+ assertThrows(DecoderException.class, () -> qpcodec.decode(Double.valueOf(3.0d)));
}
@Test
diff --git a/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java b/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
index 5de75d6..1d13313 100644
--- a/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/RFC1522CodecTest.java
@@ -18,6 +18,7 @@
package org.apache.commons.codec.net;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import org.apache.commons.codec.CharEncoding;
@@ -57,13 +58,7 @@ public class RFC1522CodecTest {
}
private void assertExpectedDecoderException(final String s) throws Exception {
- final RFC1522TestCodec testcodec = new RFC1522TestCodec();
- try {
- testcodec.decodeText(s);
- fail("DecoderException should have been thrown");
- } catch (final DecoderException e) {
- // Expected.
- }
+ assertThrows(DecoderException.class, () -> new RFC1522TestCodec().decodeText(s));
}
@Test
diff --git a/src/test/java/org/apache/commons/codec/net/URLCodecTest.java b/src/test/java/org/apache/commons/codec/net/URLCodecTest.java
index 587d20e..3cd44b4 100644
--- a/src/test/java/org/apache/commons/codec/net/URLCodecTest.java
+++ b/src/test/java/org/apache/commons/codec/net/URLCodecTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.codec.net;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import java.io.UnsupportedEncodingException;
@@ -27,6 +28,7 @@ import java.nio.charset.StandardCharsets;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
+import org.apache.commons.codec.net.RFC1522CodecTest.RFC1522TestCodec;
import org.junit.Test;
/**
@@ -131,32 +133,12 @@ public class URLCodecTest {
@Test
public void testDecodeInvalid() throws Exception {
final URLCodec urlCodec = new URLCodec();
- try {
- urlCodec.decode("%");
- fail("DecoderException should have been thrown");
- } catch (final DecoderException e) {
- // Expected. Move on
- }
- try {
- urlCodec.decode("%A");
- fail("DecoderException should have been thrown");
- } catch (final DecoderException e) {
- // Expected. Move on
- }
- try {
- // Bad 1st char after %
- urlCodec.decode("%WW");
- fail("DecoderException should have been thrown");
- } catch (final DecoderException e) {
- // Expected. Move on
- }
- try {
- // Bad 2nd char after %
- urlCodec.decode("%0W");
- fail("DecoderException should have been thrown");
- } catch (final DecoderException e) {
- // Expected. Move on
- }
+ assertThrows(DecoderException.class, () -> urlCodec.decode("%"));
+ assertThrows(DecoderException.class, () -> urlCodec.decode("%A"));
+ // Bad 1st char after %
+ assertThrows(DecoderException.class, () -> urlCodec.decode("%A"));
+ // Bad 2nd char after %
+ assertThrows(DecoderException.class, () -> urlCodec.decode("%0W"));
this.validateState(urlCodec);
}
@@ -222,25 +204,18 @@ public class URLCodecTest {
final URLCodec urlCodec = new URLCodec();
final String plain = "Hello there!";
String encoded = (String) urlCodec.encode((Object) plain);
- assertEquals("Basic URL encoding test",
- "Hello+there%21", encoded);
+ assertEquals("Basic URL encoding test", "Hello+there%21", encoded);
final byte[] plainBA = plain.getBytes(StandardCharsets.UTF_8);
final byte[] encodedBA = (byte[]) urlCodec.encode((Object) plainBA);
encoded = new String(encodedBA);
- assertEquals("Basic URL encoding test",
- "Hello+there%21", encoded);
+ assertEquals("Basic URL encoding test", "Hello+there%21", encoded);
final Object result = urlCodec.encode((Object) null);
assertNull("Encoding a null Object should return null", result);
- try {
- final Object dObj = Double.valueOf(3.0d);
- urlCodec.encode( dObj );
- fail( "Trying to url encode a Double object should cause an exception.");
- } catch (final EncoderException ee) {
- // Exception expected, test segment passes.
- }
+ assertThrows(EncoderException.class, () -> urlCodec.encode(Double.valueOf(3.0d)));
+
this.validateState(urlCodec);
}
@@ -248,18 +223,8 @@ public class URLCodecTest {
public void testInvalidEncoding() {
final URLCodec urlCodec = new URLCodec("NONSENSE");
final String plain = "Hello there!";
- try {
- urlCodec.encode(plain);
- fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
- } catch (final EncoderException ee) {
- // Exception expected, test segment passes.
- }
- try {
- urlCodec.decode(plain);
- fail("We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
- } catch (final DecoderException ee) {
- // Exception expected, test segment passes.
- }
+ assertThrows("We set the encoding to a bogus NONSENSE value", EncoderException.class, () -> urlCodec.encode(plain));
+ assertThrows("We set the encoding to a bogus NONSENSE value", DecoderException.class, () -> urlCodec.decode(plain));
this.validateState(urlCodec);
}
@@ -280,13 +245,8 @@ public class URLCodecTest {
final Object result = urlCodec.decode((Object) null);
assertNull("Decoding a null Object should return null", result);
- try {
- final Object dObj = Double.valueOf(3.0d);
- urlCodec.decode( dObj );
- fail( "Trying to url encode a Double object should cause an exception.");
- } catch (final DecoderException ee) {
- // Exception expected, test segment passes.
- }
+ assertThrows(DecoderException.class, () -> urlCodec.decode(Double.valueOf(3.0d)));
+
this.validateState(urlCodec);
}