You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2018/10/13 19:11:11 UTC
[07/14] [lang] Clean up testing of exceptions
Clean up testing of exceptions
Now that the entire project is ported to JUnit Jupiter, there are more
elegant ways to test for exceptions, which this patch applies
throughtout the code base.
If throwing an exception is supposed to fail a test, just throwing it
outside of the method cleans up the code and makes it more elegant,
instead of catching it and calling Assertions#fail.
If an exception is supposed to be thrown, calling
Assertions#assertThrows is a more elegant option than calling
Assertions#fail in the try block and then catching and ignoring the
expected exception.
Note that assertThrows uses a lambda block, so the variables inside it
should be final or effectively final. Reusing variables is a common
practice in the tests, so where needed new final variables were
introduced, or the variables used were inlined.
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/3387734b
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/3387734b
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/3387734b
Branch: refs/heads/master
Commit: 3387734b4fa501c355588b736cccb83f13347ae2
Parents: 0fa0bfd
Author: Allon Mureinik <mu...@gmail.com>
Authored: Sat Oct 13 17:08:48 2018 +0300
Committer: pascalschumacher <pa...@gmx.net>
Committed: Sat Oct 13 21:09:36 2018 +0200
----------------------------------------------------------------------
.../apache/commons/lang3/ArrayUtilsAddTest.java | 199 ++--
.../commons/lang3/ArrayUtilsInsertTest.java | 137 +--
.../commons/lang3/ArrayUtilsRemoveTest.java | 137 +--
.../apache/commons/lang3/ArrayUtilsTest.java | 115 +--
.../org/apache/commons/lang3/CharRangeTest.java | 31 +-
.../commons/lang3/CharSequenceUtilsTest.java | 14 +-
.../org/apache/commons/lang3/CharUtilsTest.java | 32 +-
.../apache/commons/lang3/ClassUtilsTest.java | 48 +-
.../apache/commons/lang3/ConversionTest.java | 93 +-
.../apache/commons/lang3/LocaleUtilsTest.java | 175 ++--
.../apache/commons/lang3/ObjectUtilsTest.java | 109 +--
.../commons/lang3/RandomStringUtilsTest.java | 71 +-
.../org/apache/commons/lang3/RangeTest.java | 8 +-
.../apache/commons/lang3/RegExUtilsTest.java | 42 +-
.../commons/lang3/SerializationUtilsTest.java | 18 +-
.../commons/lang3/StringEscapeUtilsTest.java | 151 +--
.../apache/commons/lang3/StringUtilsTest.java | 336 +++----
.../commons/lang3/builder/DiffResultTest.java | 2 +-
.../lang3/builder/JsonToStringStyleTest.java | 187 +---
.../concurrent/BackgroundInitializerTest.java | 49 +-
.../lang3/concurrent/ConcurrentUtilsTest.java | 108 +-
.../commons/lang3/concurrent/MemoizerTest.java | 25 +-
.../MultiBackgroundInitializerTest.java | 21 +-
.../lang3/exception/ExceptionUtilsTest.java | 74 +-
.../apache/commons/lang3/math/FractionTest.java | 480 +++------
.../commons/lang3/math/IEEE754rUtilsTest.java | 81 +-
.../commons/lang3/math/NumberUtilsTest.java | 73 +-
.../commons/lang3/mutable/MutableShortTest.java | 17 +-
.../lang3/reflect/ConstructorUtilsTest.java | 29 +-
.../commons/lang3/reflect/FieldUtilsTest.java | 977 ++++++-------------
.../commons/lang3/reflect/MethodUtilsTest.java | 67 +-
.../lang3/text/StrBuilderAppendInsertTest.java | 637 +++++-------
.../commons/lang3/text/StrBuilderTest.java | 300 ++----
.../commons/lang3/text/StrLookupTest.java | 9 +-
.../commons/lang3/text/StrSubstitutorTest.java | 52 +-
.../commons/lang3/text/StrTokenizerTest.java | 32 +-
.../translate/NumericEntityUnescaperTest.java | 15 +-
.../text/translate/UnicodeUnescaperTest.java | 12 +-
.../lang3/time/DateUtilsFragmentTest.java | 131 +--
.../commons/lang3/time/DateUtilsTest.java | 208 ++--
.../commons/lang3/time/FastDateParserTest.java | 32 +-
.../FastDateParser_TimeZoneStrategyTest.java | 16 +-
.../commons/lang3/time/StopWatchTest.java | 140 ++-
43 files changed, 1700 insertions(+), 3790 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/ArrayUtilsAddTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsAddTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsAddTest.java
index ce7ed2c..bbd1c8a 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayUtilsAddTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsAddTest.java
@@ -21,8 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
import java.util.Arrays;
@@ -40,12 +40,9 @@ public class ArrayUtilsAddTest {
n = ArrayUtils.addAll(new Number[]{Integer.valueOf(1)}, new Long[]{Long.valueOf(2)});
assertEquals(2,n.length);
assertEquals(Number.class,n.getClass().getComponentType());
- try {
- // Invalid - can't store Long in Integer array
- n = ArrayUtils.addAll(new Integer[]{Integer.valueOf(1)}, new Long[]{Long.valueOf(2)});
- fail("Should have generated IllegalArgumentException");
- } catch (final IllegalArgumentException expected) {
- }
+ // Invalid - can't store Long in Integer array
+ assertThrows(IllegalArgumentException.class,
+ () -> ArrayUtils.addAll(new Integer[]{Integer.valueOf(1)}, new Long[]{Long.valueOf(2)}));
}
@Test
@@ -226,25 +223,12 @@ public class ArrayUtilsAddTest {
}
@Test
+ @SuppressWarnings("deprecation")
public void testLANG571(){
final String[] stringArray=null;
final String aString=null;
- try {
- @SuppressWarnings("unused")
- final
- String[] sa = ArrayUtils.add(stringArray, aString);
- fail("Should have caused IllegalArgumentException");
- } catch (final IllegalArgumentException iae){
- //expected
- }
- try {
- @SuppressWarnings({ "unused", "deprecation" })
- final
- String[] sa = ArrayUtils.add(stringArray, 0, aString);
- fail("Should have caused IllegalArgumentException");
- } catch (final IllegalArgumentException iae){
- //expected
- }
+ assertThrows(IllegalArgumentException.class, () -> ArrayUtils.add(stringArray, aString));
+ assertThrows(IllegalArgumentException.class, () -> ArrayUtils.add(stringArray, 0, aString));
}
@Test
@@ -408,36 +392,25 @@ public class ArrayUtilsAddTest {
// boolean tests
boolean[] booleanArray = ArrayUtils.add( null, 0, true );
assertTrue( Arrays.equals( new boolean[] { true }, booleanArray ) );
- try {
- booleanArray = ArrayUtils.add( null, -1, true );
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 0", e.getMessage());
- }
+ IndexOutOfBoundsException e =
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( null, -1, true));
+ assertEquals("Index: -1, Length: 0", e.getMessage());
booleanArray = ArrayUtils.add( new boolean[] { true }, 0, false);
assertTrue( Arrays.equals( new boolean[] { false, true }, booleanArray ) );
booleanArray = ArrayUtils.add( new boolean[] { false }, 1, true);
assertTrue( Arrays.equals( new boolean[] { false, true }, booleanArray ) );
booleanArray = ArrayUtils.add( new boolean[] { true, false }, 1, true);
assertTrue( Arrays.equals( new boolean[] { true, true, false }, booleanArray ) );
- try {
- booleanArray = ArrayUtils.add( new boolean[] { true, false }, 4, true);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: 4, Length: 2", e.getMessage());
- }
- try {
- booleanArray = ArrayUtils.add( new boolean[] { true, false }, -1, true);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 2", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new boolean[] { true, false }, 4, true));
+ assertEquals("Index: 4, Length: 2", e.getMessage());
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(new boolean[] { true, false }, -1, true));
+ assertEquals("Index: -1, Length: 2", e.getMessage());
// char tests
char[] charArray = ArrayUtils.add( (char[]) null, 0, 'a' );
assertTrue( Arrays.equals( new char[] { 'a' }, charArray ) );
- try {
- charArray = ArrayUtils.add( (char[]) null, -1, 'a' );
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 0", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( (char[]) null, -1, 'a' ));
+ assertEquals("Index: -1, Length: 0", e.getMessage());
charArray = ArrayUtils.add( new char[] { 'a' }, 0, 'b');
assertTrue( Arrays.equals( new char[] { 'b', 'a' }, charArray ) );
charArray = ArrayUtils.add( new char[] { 'a', 'b' }, 0, 'c');
@@ -446,166 +419,106 @@ public class ArrayUtilsAddTest {
assertTrue( Arrays.equals( new char[] { 'a', 'k', 'b' }, charArray ) );
charArray = ArrayUtils.add( new char[] { 'a', 'b', 'c' }, 1, 't');
assertTrue( Arrays.equals( new char[] { 'a', 't', 'b', 'c' }, charArray ) );
- try {
- charArray = ArrayUtils.add( new char[] { 'a', 'b' }, 4, 'c');
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: 4, Length: 2", e.getMessage());
- }
- try {
- charArray = ArrayUtils.add( new char[] { 'a', 'b' }, -1, 'c');
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 2", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new char[] { 'a', 'b' }, 4, 'c'));
+ assertEquals("Index: 4, Length: 2", e.getMessage());
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new char[] { 'a', 'b' }, -1, 'c'));
+ assertEquals("Index: -1, Length: 2", e.getMessage());
// short tests
short[] shortArray = ArrayUtils.add( new short[] { 1 }, 0, (short) 2);
assertTrue( Arrays.equals( new short[] { 2, 1 }, shortArray ) );
- try {
- shortArray = ArrayUtils.add( (short[]) null, -1, (short) 2);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 0", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( (short[]) null, -1, (short) 2));
+ assertEquals("Index: -1, Length: 0", e.getMessage());
shortArray = ArrayUtils.add( new short[] { 2, 6 }, 2, (short) 10);
assertTrue( Arrays.equals( new short[] { 2, 6, 10 }, shortArray ) );
shortArray = ArrayUtils.add( new short[] { 2, 6 }, 0, (short) -4);
assertTrue( Arrays.equals( new short[] { -4, 2, 6 }, shortArray ) );
shortArray = ArrayUtils.add( new short[] { 2, 6, 3 }, 2, (short) 1);
assertTrue( Arrays.equals( new short[] { 2, 6, 1, 3 }, shortArray ) );
- try {
- shortArray = ArrayUtils.add( new short[] { 2, 6 }, 4, (short) 10);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: 4, Length: 2", e.getMessage());
- }
- try {
- shortArray = ArrayUtils.add( new short[] { 2, 6 }, -1, (short) 10);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 2", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new short[] { 2, 6 }, 4, (short) 10));
+ assertEquals("Index: 4, Length: 2", e.getMessage());
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new short[] { 2, 6 }, -1, (short) 10));
+ assertEquals("Index: -1, Length: 2", e.getMessage());
// byte tests
byte[] byteArray = ArrayUtils.add( new byte[] { 1 }, 0, (byte) 2);
assertTrue( Arrays.equals( new byte[] { 2, 1 }, byteArray ) );
- try {
- byteArray = ArrayUtils.add( (byte[]) null, -1, (byte) 2);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 0", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( (byte[]) null, -1, (byte) 2));
+ assertEquals("Index: -1, Length: 0", e.getMessage());
byteArray = ArrayUtils.add( new byte[] { 2, 6 }, 2, (byte) 3);
assertTrue( Arrays.equals( new byte[] { 2, 6, 3 }, byteArray ) );
byteArray = ArrayUtils.add( new byte[] { 2, 6 }, 0, (byte) 1);
assertTrue( Arrays.equals( new byte[] { 1, 2, 6 }, byteArray ) );
byteArray = ArrayUtils.add( new byte[] { 2, 6, 3 }, 2, (byte) 1);
assertTrue( Arrays.equals( new byte[] { 2, 6, 1, 3 }, byteArray ) );
- try {
- byteArray = ArrayUtils.add( new byte[] { 2, 6 }, 4, (byte) 3);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: 4, Length: 2", e.getMessage());
- }
- try {
- byteArray = ArrayUtils.add( new byte[] { 2, 6 }, -1, (byte) 3);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 2", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new byte[] { 2, 6 }, 4, (byte) 3));
+ assertEquals("Index: 4, Length: 2", e.getMessage());
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new byte[] { 2, 6 }, -1, (byte) 3));
+ assertEquals("Index: -1, Length: 2", e.getMessage());
// int tests
int[] intArray = ArrayUtils.add( new int[] { 1 }, 0, 2);
assertTrue( Arrays.equals( new int[] { 2, 1 }, intArray ) );
- try {
- intArray = ArrayUtils.add( (int[]) null, -1, 2);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 0", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( (int[]) null, -1, 2));
+ assertEquals("Index: -1, Length: 0", e.getMessage());
intArray = ArrayUtils.add( new int[] { 2, 6 }, 2, 10);
assertTrue( Arrays.equals( new int[] { 2, 6, 10 }, intArray ) );
intArray = ArrayUtils.add( new int[] { 2, 6 }, 0, -4);
assertTrue( Arrays.equals( new int[] { -4, 2, 6 }, intArray ) );
intArray = ArrayUtils.add( new int[] { 2, 6, 3 }, 2, 1);
assertTrue( Arrays.equals( new int[] { 2, 6, 1, 3 }, intArray ) );
- try {
- intArray = ArrayUtils.add( new int[] { 2, 6 }, 4, 10);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: 4, Length: 2", e.getMessage());
- }
- try {
- intArray = ArrayUtils.add( new int[] { 2, 6 }, -1, 10);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 2", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new int[] { 2, 6 }, 4, 10));
+ assertEquals("Index: 4, Length: 2", e.getMessage());
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new int[] { 2, 6 }, -1, 10));
+ assertEquals("Index: -1, Length: 2", e.getMessage());
// long tests
long[] longArray = ArrayUtils.add( new long[] { 1L }, 0, 2L);
assertTrue( Arrays.equals( new long[] { 2L, 1L }, longArray ) );
- try {
- longArray = ArrayUtils.add( (long[]) null, -1, 2L);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 0", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( (long[]) null, -1, 2L));
+ assertEquals("Index: -1, Length: 0", e.getMessage());
longArray = ArrayUtils.add( new long[] { 2L, 6L }, 2, 10L);
assertTrue( Arrays.equals( new long[] { 2L, 6L, 10L }, longArray ) );
longArray = ArrayUtils.add( new long[] { 2L, 6L }, 0, -4L);
assertTrue( Arrays.equals( new long[] { -4L, 2L, 6L }, longArray ) );
longArray = ArrayUtils.add( new long[] { 2L, 6L, 3L }, 2, 1L);
assertTrue( Arrays.equals( new long[] { 2L, 6L, 1L, 3L }, longArray ) );
- try {
- longArray = ArrayUtils.add( new long[] { 2L, 6L }, 4, 10L);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: 4, Length: 2", e.getMessage());
- }
- try {
- longArray = ArrayUtils.add( new long[] { 2L, 6L }, -1, 10L);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 2", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new long[] { 2L, 6L }, 4, 10L));
+ assertEquals("Index: 4, Length: 2", e.getMessage());
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new long[] { 2L, 6L }, -1, 10L));
+ assertEquals("Index: -1, Length: 2", e.getMessage());
// float tests
float[] floatArray = ArrayUtils.add( new float[] { 1.1f }, 0, 2.2f);
assertTrue( Arrays.equals( new float[] { 2.2f, 1.1f }, floatArray ) );
- try {
- floatArray = ArrayUtils.add( (float[]) null, -1, 2.2f);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 0", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( (float[]) null, -1, 2.2f));
+ assertEquals("Index: -1, Length: 0", e.getMessage());
floatArray = ArrayUtils.add( new float[] { 2.3f, 6.4f }, 2, 10.5f);
assertTrue( Arrays.equals( new float[] { 2.3f, 6.4f, 10.5f }, floatArray ) );
floatArray = ArrayUtils.add( new float[] { 2.6f, 6.7f }, 0, -4.8f);
assertTrue( Arrays.equals( new float[] { -4.8f, 2.6f, 6.7f }, floatArray ) );
floatArray = ArrayUtils.add( new float[] { 2.9f, 6.0f, 0.3f }, 2, 1.0f);
assertTrue( Arrays.equals( new float[] { 2.9f, 6.0f, 1.0f, 0.3f }, floatArray ) );
- try {
- floatArray = ArrayUtils.add( new float[] { 2.3f, 6.4f }, 4, 10.5f);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: 4, Length: 2", e.getMessage());
- }
- try {
- floatArray = ArrayUtils.add( new float[] { 2.3f, 6.4f }, -1, 10.5f);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 2", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new float[] { 2.3f, 6.4f }, 4, 10.5f));
+ assertEquals("Index: 4, Length: 2", e.getMessage());
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new float[] { 2.3f, 6.4f }, -1, 10.5f));
+ assertEquals("Index: -1, Length: 2", e.getMessage());
// double tests
double[] doubleArray = ArrayUtils.add( new double[] { 1.1 }, 0, 2.2);
assertTrue( Arrays.equals( new double[] { 2.2, 1.1 }, doubleArray ) );
- try {
- doubleArray = ArrayUtils.add(null, -1, 2.2);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 0", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add(null, -1, 2.2));
+ assertEquals("Index: -1, Length: 0", e.getMessage());
doubleArray = ArrayUtils.add( new double[] { 2.3, 6.4 }, 2, 10.5);
assertTrue( Arrays.equals( new double[] { 2.3, 6.4, 10.5 }, doubleArray ) );
doubleArray = ArrayUtils.add( new double[] { 2.6, 6.7 }, 0, -4.8);
assertTrue( Arrays.equals( new double[] { -4.8, 2.6, 6.7 }, doubleArray ) );
doubleArray = ArrayUtils.add( new double[] { 2.9, 6.0, 0.3 }, 2, 1.0);
assertTrue( Arrays.equals( new double[] { 2.9, 6.0, 1.0, 0.3 }, doubleArray ) );
- try {
- doubleArray = ArrayUtils.add( new double[] { 2.3, 6.4 }, 4, 10.5);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: 4, Length: 2", e.getMessage());
- }
- try {
- doubleArray = ArrayUtils.add( new double[] { 2.3, 6.4 }, -1, 10.5);
- } catch(final IndexOutOfBoundsException e) {
- assertEquals("Index: -1, Length: 2", e.getMessage());
- }
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new double[] { 2.3, 6.4 }, 4, 10.5));
+ assertEquals("Index: 4, Length: 2", e.getMessage());
+ e = assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.add( new double[] { 2.3, 6.4 }, -1, 10.5));
+ assertEquals("Index: -1, Length: 2", e.getMessage());
}
}
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/ArrayUtilsInsertTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsInsertTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsInsertTest.java
index a3bfbf4..cf2d4a5 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayUtilsInsertTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsInsertTest.java
@@ -20,7 +20,7 @@ package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
@@ -42,19 +42,8 @@ public class ArrayUtilsInsertTest {
assertArrayEquals(new boolean[0], ArrayUtils.insert(0, new boolean[0], null));
assertNull(ArrayUtils.insert(42, (boolean[]) null, null));
- try {
- ArrayUtils.insert(-1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- ArrayUtils.insert(array.length + 1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array));
assertArrayEquals(new boolean[]{false,true,false,true}, ArrayUtils.insert(0, array, false));
assertArrayEquals(new boolean[]{true,false,false,true}, ArrayUtils.insert(1, array, false));
@@ -78,19 +67,8 @@ public class ArrayUtilsInsertTest {
assertArrayEquals(new byte[0], ArrayUtils.insert(0, new byte[0], null));
assertNull(ArrayUtils.insert(42, (byte[]) null, null));
- try {
- ArrayUtils.insert(-1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- ArrayUtils.insert(array.length + 1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array));
assertArrayEquals(new byte[]{0,1,2,3}, ArrayUtils.insert(0, array, (byte) 0));
assertArrayEquals(new byte[]{1,0,2,3}, ArrayUtils.insert(1, array, (byte) 0));
@@ -113,19 +91,8 @@ public class ArrayUtilsInsertTest {
assertArrayEquals(new char[0], ArrayUtils.insert(0, new char[0], null));
assertNull(ArrayUtils.insert(42, (char[]) null, null));
- try {
- ArrayUtils.insert(-1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- ArrayUtils.insert(array.length + 1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array));
assertArrayEquals(new char[]{'z','a','b','c'}, ArrayUtils.insert(0, array, 'z'));
assertArrayEquals(new char[]{'a','z','b','c'}, ArrayUtils.insert(1, array, 'z'));
@@ -149,19 +116,8 @@ public class ArrayUtilsInsertTest {
assertArrayEquals(new double[0], ArrayUtils.insert(0, new double[0], null), delta);
assertNull(ArrayUtils.insert(42, (double[]) null, null));
- try {
- ArrayUtils.insert(-1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- ArrayUtils.insert(array.length + 1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array));
assertArrayEquals(new double[]{0,1,2,3}, ArrayUtils.insert(0, array, 0), delta);
assertArrayEquals(new double[]{1,0,2,3}, ArrayUtils.insert(1, array, 0), delta);
@@ -185,19 +141,8 @@ public class ArrayUtilsInsertTest {
assertArrayEquals(new float[0], ArrayUtils.insert(0, new float[0], null), delta);
assertNull(ArrayUtils.insert(42, (float[]) null, null));
- try {
- ArrayUtils.insert(-1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- ArrayUtils.insert(array.length + 1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array));
assertArrayEquals(new float[]{0,1,2,3}, ArrayUtils.insert(0, array, 0), delta);
assertArrayEquals(new float[]{1,0,2,3}, ArrayUtils.insert(1, array, 0), delta);
@@ -220,19 +165,8 @@ public class ArrayUtilsInsertTest {
assertArrayEquals(new int[0], ArrayUtils.insert(0, new int[0], null));
assertNull(ArrayUtils.insert(42, (int[]) null, null));
- try {
- ArrayUtils.insert(-1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- ArrayUtils.insert(array.length + 1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array));
assertArrayEquals(new int[]{0,1,2,3}, ArrayUtils.insert(0, array, 0));
assertArrayEquals(new int[]{1,0,2,3}, ArrayUtils.insert(1, array, 0));
@@ -256,19 +190,8 @@ public class ArrayUtilsInsertTest {
assertArrayEquals(new long[0], ArrayUtils.insert(0, new long[0], null));
assertNull(ArrayUtils.insert(42, (long[]) null, null));
- try {
- ArrayUtils.insert(-1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- ArrayUtils.insert(array.length + 1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array));
assertArrayEquals(new long[]{0,1,2,3}, ArrayUtils.insert(0, array, 0));
assertArrayEquals(new long[]{1,0,2,3}, ArrayUtils.insert(1, array, 0));
@@ -292,19 +215,8 @@ public class ArrayUtilsInsertTest {
assertArrayEquals(new short[0], ArrayUtils.insert(0, new short[0], null));
assertNull(ArrayUtils.insert(42, (short[]) null, null));
- try {
- ArrayUtils.insert(-1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- ArrayUtils.insert(array.length + 1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array));
assertArrayEquals(new short[]{0,1,2,3}, ArrayUtils.insert(0, array, (short) 0));
assertArrayEquals(new short[]{1,0,2,3}, ArrayUtils.insert(1, array, (short) 0));
@@ -328,19 +240,8 @@ public class ArrayUtilsInsertTest {
assertArrayEquals(new String[0], ArrayUtils.insert(0, new String[0], (String[]) null));
assertNull(ArrayUtils.insert(42, null, (String[]) null));
- try {
- ArrayUtils.insert(-1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
-
- try {
- ArrayUtils.insert(array.length + 1, array, array);
- fail("Expected IndexOutOfBoundsException");
- } catch (final IndexOutOfBoundsException e) {
- // expected
- }
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(-1, array, array));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.insert(array.length + 1, array, array));
assertArrayEquals(new String[]{"z","a","b","c"}, ArrayUtils.insert(0, array, "z"));
assertArrayEquals(new String[]{"a","z","b","c"}, ArrayUtils.insert(1, array, "z"));
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java
index 9bb46c1..d8ce365 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsRemoveTest.java
@@ -19,8 +19,8 @@ package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
import java.util.Arrays;
@@ -46,18 +46,9 @@ public class ArrayUtilsRemoveTest {
array = ArrayUtils.remove(new Object[] {"a", "b", "c"}, 1);
assertTrue(Arrays.equals(new Object[] {"a", "c"}, array));
assertEquals(Object.class, array.getClass().getComponentType());
- try {
- ArrayUtils.remove(new Object[] {"a", "b"}, -1);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove(new Object[] {"a", "b"}, 2);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove((Object[]) null, 0);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new Object[] {"a", "b"}, -1));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new Object[] {"a", "b"}, 2));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((Object[]) null, 0));
}
@Test
@@ -91,18 +82,9 @@ public class ArrayUtilsRemoveTest {
array = ArrayUtils.remove(new boolean[] {true, false, true}, 1);
assertTrue(Arrays.equals(new boolean[] {true, true}, array));
assertEquals(Boolean.TYPE, array.getClass().getComponentType());
- try {
- ArrayUtils.remove(new boolean[] {true, false}, -1);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove(new boolean[] {true, false}, 2);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove((boolean[]) null, 0);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new boolean[] {true, false}, -1));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new boolean[] {true, false}, 2));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((boolean[]) null, 0));
}
@Test
@@ -120,18 +102,9 @@ public class ArrayUtilsRemoveTest {
array = ArrayUtils.remove(new byte[] {1, 2, 1}, 1);
assertTrue(Arrays.equals(new byte[] {1, 1}, array));
assertEquals(Byte.TYPE, array.getClass().getComponentType());
- try {
- ArrayUtils.remove(new byte[] {1, 2}, -1);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove(new byte[] {1, 2}, 2);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove((byte[]) null, 0);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new byte[] {1, 2}, -1));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new byte[] {1, 2}, 2));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((byte[]) null, 0));
}
@Test
@@ -149,18 +122,9 @@ public class ArrayUtilsRemoveTest {
array = ArrayUtils.remove(new char[] {'a', 'b', 'c'}, 1);
assertTrue(Arrays.equals(new char[] {'a', 'c'}, array));
assertEquals(Character.TYPE, array.getClass().getComponentType());
- try {
- ArrayUtils.remove(new char[] {'a', 'b'}, -1);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove(new char[] {'a', 'b'}, 2);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove((char[]) null, 0);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new char[] {'a', 'b'}, -1));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new char[] {'a', 'b'}, 2));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((char[]) null, 0));
}
@Test
@@ -178,18 +142,9 @@ public class ArrayUtilsRemoveTest {
array = ArrayUtils.remove(new double[] {1, 2, 1}, 1);
assertTrue(Arrays.equals(new double[] {1, 1}, array));
assertEquals(Double.TYPE, array.getClass().getComponentType());
- try {
- ArrayUtils.remove(new double[] {1, 2}, -1);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove(new double[] {1, 2}, 2);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove((double[]) null, 0);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new double[] {1, 2}, -1));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new double[] {1, 2}, 2));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((double[]) null, 0));
}
@Test
@@ -207,18 +162,9 @@ public class ArrayUtilsRemoveTest {
array = ArrayUtils.remove(new float[] {1, 2, 1}, 1);
assertTrue(Arrays.equals(new float[] {1, 1}, array));
assertEquals(Float.TYPE, array.getClass().getComponentType());
- try {
- ArrayUtils.remove(new float[] {1, 2}, -1);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove(new float[] {1, 2}, 2);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove((float[]) null, 0);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new float[] {1, 2}, -1));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new float[] {1, 2}, 2));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((float[]) null, 0));
}
@Test
@@ -236,18 +182,9 @@ public class ArrayUtilsRemoveTest {
array = ArrayUtils.remove(new int[] {1, 2, 1}, 1);
assertTrue(Arrays.equals(new int[] {1, 1}, array));
assertEquals(Integer.TYPE, array.getClass().getComponentType());
- try {
- ArrayUtils.remove(new int[] {1, 2}, -1);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove(new int[] {1, 2}, 2);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove((int[]) null, 0);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new int[] {1, 2}, -1));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new int[] {1, 2}, 2));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((int[]) null, 0));
}
@Test
@@ -265,18 +202,9 @@ public class ArrayUtilsRemoveTest {
array = ArrayUtils.remove(new long[] {1, 2, 1}, 1);
assertTrue(Arrays.equals(new long[] {1, 1}, array));
assertEquals(Long.TYPE, array.getClass().getComponentType());
- try {
- ArrayUtils.remove(new long[] {1, 2}, -1);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove(new long[] {1, 2}, 2);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove((long[]) null, 0);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new long[] {1, 2}, -1));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new long[] {1, 2}, 2));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((long[]) null, 0));
}
@Test
@@ -294,18 +222,9 @@ public class ArrayUtilsRemoveTest {
array = ArrayUtils.remove(new short[] {1, 2, 1}, 1);
assertTrue(Arrays.equals(new short[] {1, 1}, array));
assertEquals(Short.TYPE, array.getClass().getComponentType());
- try {
- ArrayUtils.remove(new short[] {1, 2}, -1);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove(new short[] {1, 2}, 2);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
- try {
- ArrayUtils.remove((short[]) null, 0);
- fail("IndexOutOfBoundsException expected");
- } catch (final IndexOutOfBoundsException e) {}
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new short[] {1, 2}, -1));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove(new short[] {1, 2}, 2));
+ assertThrows(IndexOutOfBoundsException.class, () -> ArrayUtils.remove((short[]) null, 0));
}
@Test
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
index c049b97..380cee2 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
@@ -25,7 +25,6 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
@@ -230,21 +229,12 @@ public class ArrayUtilsTest {
assertEquals("world", map.get("hello"));
assertNull(ArrayUtils.toMap(null));
- try {
- ArrayUtils.toMap(new String[][]{{"foo", "bar"}, {"short"}});
- fail("exception expected");
- } catch (final IllegalArgumentException ex) {
- }
- try {
- ArrayUtils.toMap(new Object[]{new Object[]{"foo", "bar"}, "illegal type"});
- fail("exception expected");
- } catch (final IllegalArgumentException ex) {
- }
- try {
- ArrayUtils.toMap(new Object[]{new Object[]{"foo", "bar"}, null});
- fail("exception expected");
- } catch (final IllegalArgumentException ex) {
- }
+ assertThrows(IllegalArgumentException.class, () ->
+ ArrayUtils.toMap(new String[][]{{"foo", "bar"}, {"short"}}));
+ assertThrows(IllegalArgumentException.class, () ->
+ ArrayUtils.toMap(new Object[]{new Object[]{"foo", "bar"}, "illegal type"}));
+ assertThrows(IllegalArgumentException.class, () ->
+ ArrayUtils.toMap(new Object[]{new Object[]{"foo", "bar"}, null}));
map = ArrayUtils.toMap(new Object[]{new Map.Entry<Object, Object>() {
@Override
@@ -795,11 +785,9 @@ public class ArrayUtilsTest {
"java.util.Date type");
assertNotSame(java.sql.Date.class, ArrayUtils.subarray(dateArray, 1, 4).getClass().getComponentType(),
"java.sql.Date type");
- try {
- @SuppressWarnings("unused") final java.sql.Date[] dummy = (java.sql.Date[]) ArrayUtils.subarray(dateArray, 1, 3);
- fail("Invalid downcast");
- } catch (final ClassCastException e) {
- }
+ assertThrows(ClassCastException.class,
+ () -> java.sql.Date[].class.cast(ArrayUtils.subarray(dateArray, 1, 3)),
+ "Invalid downcast");
}
@Test
@@ -1406,21 +1394,9 @@ public class ArrayUtilsTest {
//-----------------------------------------------------------------------
@Test
public void testSameType() {
- try {
- ArrayUtils.isSameType(null, null);
- fail();
- } catch (final IllegalArgumentException ex) {
- }
- try {
- ArrayUtils.isSameType(null, new Object[0]);
- fail();
- } catch (final IllegalArgumentException ex) {
- }
- try {
- ArrayUtils.isSameType(new Object[0], null);
- fail();
- } catch (final IllegalArgumentException ex) {
- }
+ assertThrows(IllegalArgumentException.class, () -> ArrayUtils.isSameType(null, null));
+ assertThrows(IllegalArgumentException.class, () -> ArrayUtils.isSameType(null, new Object[0]));
+ assertThrows(IllegalArgumentException.class, () -> ArrayUtils.isSameType(new Object[0], null));
assertTrue(ArrayUtils.isSameType(new Object[0], new Object[0]));
assertFalse(ArrayUtils.isSameType(new String[0], new Object[0]));
@@ -3901,11 +3877,7 @@ public class ArrayUtilsTest {
ArrayUtils.toPrimitive(new Boolean[]{Boolean.TRUE, Boolean.FALSE, Boolean.TRUE}))
);
- try {
- ArrayUtils.toPrimitive(new Boolean[]{Boolean.TRUE, null});
- fail();
- } catch (final NullPointerException ex) {
- }
+ assertThrows(NullPointerException.class, () -> ArrayUtils.toPrimitive(new Boolean[]{Boolean.TRUE, null}));
}
@Test
@@ -3952,11 +3924,8 @@ public class ArrayUtilsTest {
new Character(Character.MAX_VALUE), new Character('0')}))
);
- try {
- ArrayUtils.toPrimitive(new Character[]{new Character(Character.MIN_VALUE), null});
- fail();
- } catch (final NullPointerException ex) {
- }
+ assertThrows(NullPointerException.class,
+ () -> ArrayUtils.toPrimitive(new Character[]{new Character(Character.MIN_VALUE), null}));
}
@Test
@@ -4012,11 +3981,8 @@ public class ArrayUtilsTest {
Byte.valueOf(Byte.MAX_VALUE), Byte.valueOf((byte) 9999999)}))
);
- try {
- ArrayUtils.toPrimitive(new Byte[]{Byte.valueOf(Byte.MIN_VALUE), null});
- fail();
- } catch (final NullPointerException ex) {
- }
+ assertThrows(NullPointerException.class,
+ () -> ArrayUtils.toPrimitive(new Byte[]{Byte.valueOf(Byte.MIN_VALUE), null}));
}
@Test
@@ -4072,11 +4038,8 @@ public class ArrayUtilsTest {
Short.valueOf(Short.MAX_VALUE), Short.valueOf((short) 9999999)}))
);
- try {
- ArrayUtils.toPrimitive(new Short[]{Short.valueOf(Short.MIN_VALUE), null});
- fail();
- } catch (final NullPointerException ex) {
- }
+ assertThrows(NullPointerException.class,
+ () -> ArrayUtils.toPrimitive(new Short[]{Short.valueOf(Short.MIN_VALUE), null}));
}
@Test
@@ -4129,11 +4092,8 @@ public class ArrayUtilsTest {
Integer.valueOf(Integer.MAX_VALUE), Integer.valueOf(9999999)}))
);
- try {
- ArrayUtils.toPrimitive(new Integer[]{Integer.valueOf(Integer.MIN_VALUE), null});
- fail();
- } catch (final NullPointerException ex) {
- }
+ assertThrows(NullPointerException.class,
+ () -> ArrayUtils.toPrimitive(new Integer[]{Integer.valueOf(Integer.MIN_VALUE), null}));
}
@Test
@@ -4194,11 +4154,8 @@ public class ArrayUtilsTest {
Long.valueOf(Long.MAX_VALUE), Long.valueOf(9999999)}))
);
- try {
- ArrayUtils.toPrimitive(new Long[]{Long.valueOf(Long.MIN_VALUE), null});
- fail();
- } catch (final NullPointerException ex) {
- }
+ assertThrows(NullPointerException.class,
+ () -> ArrayUtils.toPrimitive(new Long[]{Long.valueOf(Long.MIN_VALUE), null}));
}
@Test
@@ -4256,11 +4213,8 @@ public class ArrayUtilsTest {
Float.valueOf(Float.MAX_VALUE), Float.valueOf(9999999)}))
);
- try {
- ArrayUtils.toPrimitive(new Float[]{Float.valueOf(Float.MIN_VALUE), null});
- fail();
- } catch (final NullPointerException ex) {
- }
+ assertThrows(NullPointerException.class,
+ () -> ArrayUtils.toPrimitive(new Float[]{Float.valueOf(Float.MIN_VALUE), null}));
}
@Test
@@ -4318,11 +4272,8 @@ public class ArrayUtilsTest {
Double.valueOf(Double.MAX_VALUE), Double.valueOf(9999999)}))
);
- try {
- ArrayUtils.toPrimitive(new Float[]{Float.valueOf(Float.MIN_VALUE), null});
- fail();
- } catch (final NullPointerException ex) {
- }
+ assertThrows(NullPointerException.class,
+ () -> ArrayUtils.toPrimitive(new Float[]{Float.valueOf(Float.MIN_VALUE), null}));
}
@Test
@@ -4565,11 +4516,7 @@ public class ArrayUtilsTest {
assertEquals(0, ArrayUtils.getLength(emptyBooleanArray));
assertEquals(1, ArrayUtils.getLength(notEmptyBooleanArray));
- try {
- ArrayUtils.getLength("notAnArray");
- fail("IllegalArgumentException should have been thrown");
- } catch (final IllegalArgumentException e) {
- }
+ assertThrows(IllegalArgumentException.class, () -> ArrayUtils.getLength("notAnArray"));
}
@Test
@@ -4753,11 +4700,7 @@ public class ArrayUtilsTest {
final Object[] array = new Object[]{1, 2, 3, "array", "test"};
assertArrayEquals(new String[]{"1", "2", "3", "array", "test"}, ArrayUtils.toStringArray(array));
- try {
- ArrayUtils.toStringArray(new Object[]{null});
- fail("NullPointerException expected!");
- } catch (final NullPointerException expected) {
- }
+ assertThrows(NullPointerException.class, () -> ArrayUtils.toStringArray(new Object[]{null}));
}
@Test
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/CharRangeTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/CharRangeTest.java b/src/test/java/org/apache/commons/lang3/CharRangeTest.java
index 05afa6d..9704dfe 100644
--- a/src/test/java/org/apache/commons/lang3/CharRangeTest.java
+++ b/src/test/java/org/apache/commons/lang3/CharRangeTest.java
@@ -23,7 +23,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.Modifier;
import java.util.Iterator;
@@ -311,13 +310,8 @@ public class CharRangeTest {
@Test
public void testContainsNullArg() {
final CharRange range = CharRange.is('a');
- try {
- @SuppressWarnings("unused")
- final
- boolean contains = range.contains(null);
- } catch(final IllegalArgumentException e) {
- assertEquals("The Range must not be null", e.getMessage());
- }
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> range.contains(null));
+ assertEquals("The Range must not be null", e.getMessage());
}
@Test
@@ -355,36 +349,21 @@ public class CharRangeTest {
final Iterator<Character> emptySetIt = emptySet.iterator();
assertNotNull(emptySetIt);
assertFalse(emptySetIt.hasNext());
- try {
- emptySetIt.next();
- fail("Should throw NoSuchElementException");
- } catch (final NoSuchElementException e) {
- assertTrue(true);
- }
+ assertThrows(NoSuchElementException.class, emptySetIt::next);
final Iterator<Character> notFirstIt = notFirst.iterator();
assertNotNull(notFirstIt);
assertTrue(notFirstIt.hasNext());
assertEquals(Character.valueOf((char) 0), notFirstIt.next());
assertFalse(notFirstIt.hasNext());
- try {
- notFirstIt.next();
- fail("Should throw NoSuchElementException");
- } catch (final NoSuchElementException e) {
- assertTrue(true);
- }
+ assertThrows(NoSuchElementException.class, notFirstIt::next);
final Iterator<Character> notLastIt = notLast.iterator();
assertNotNull(notLastIt);
assertTrue(notLastIt.hasNext());
assertEquals(Character.valueOf(Character.MAX_VALUE), notLastIt.next());
assertFalse(notLastIt.hasNext());
- try {
- notLastIt.next();
- fail("Should throw NoSuchElementException");
- } catch (final NoSuchElementException e) {
- assertTrue(true);
- }
+ assertThrows(NoSuchElementException.class, notLastIt::next);
}
//-----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java b/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java
index 5b59f9e..83b2ff8 100644
--- a/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/CharSequenceUtilsTest.java
@@ -23,7 +23,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
@@ -83,7 +82,7 @@ public class CharSequenceUtilsTest {
final int ooffset;
final int len;
final boolean expected;
- final Class<?> throwable;
+ final Class<? extends Throwable> throwable;
TestData(final String source, final boolean ignoreCase, final int toffset,
final String other, final int ooffset, final int len, final boolean expected){
this.source = source;
@@ -96,7 +95,7 @@ public class CharSequenceUtilsTest {
this.throwable = null;
}
TestData(final String source, final boolean ignoreCase, final int toffset,
- final String other, final int ooffset, final int len, final Class<?> throwable){
+ final String other, final int ooffset, final int len, final Class<? extends Throwable> throwable){
this.source = source;
this.ignoreCase = ignoreCase;
this.toffset = toffset;
@@ -145,14 +144,7 @@ public class CharSequenceUtilsTest {
void run(final TestData data, final String id) {
if (data.throwable != null) {
- try {
- invoke();
- fail(id + " Expected " + data.throwable);
- } catch (final Exception e) {
- if (!e.getClass().equals(data.throwable)) {
- fail(id + " Expected " + data.throwable + " got " + e.getClass());
- }
- }
+ assertThrows(data.throwable, this::invoke, id + " Expected " + data.throwable);
} else {
final boolean stringCheck = invoke();
assertEquals(data.expected, stringCheck, id + " Failed test " + data);
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/CharUtilsTest.java b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java
index 1b3716a..5d4d69a 100644
--- a/src/test/java/org/apache/commons/lang3/CharUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/CharUtilsTest.java
@@ -21,8 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
@@ -199,10 +199,7 @@ public class CharUtilsTest {
public void testToChar_Character() {
assertEquals('A', CharUtils.toChar(CHARACTER_A));
assertEquals('B', CharUtils.toChar(CHARACTER_B));
- try {
- CharUtils.toChar((Character) null);
- fail("An IllegalArgumentException should have been thrown");
- } catch (final IllegalArgumentException ex) {}
+ assertThrows(IllegalArgumentException.class, () -> CharUtils.toChar((Character) null));
}
@Test
@@ -216,14 +213,8 @@ public class CharUtilsTest {
public void testToChar_String() {
assertEquals('A', CharUtils.toChar("A"));
assertEquals('B', CharUtils.toChar("BA"));
- try {
- CharUtils.toChar((String) null);
- fail("An IllegalArgumentException should have been thrown");
- } catch (final IllegalArgumentException ex) {}
- try {
- CharUtils.toChar("");
- fail("An IllegalArgumentException should have been thrown");
- } catch (final IllegalArgumentException ex) {}
+ assertThrows(IllegalArgumentException.class, () -> CharUtils.toChar((String) null));
+ assertThrows(IllegalArgumentException.class, () -> CharUtils.toChar(""));
}
@Test
@@ -278,10 +269,7 @@ public class CharUtilsTest {
assertEquals(7, CharUtils.toIntValue('7'));
assertEquals(8, CharUtils.toIntValue('8'));
assertEquals(9, CharUtils.toIntValue('9'));
- try {
- CharUtils.toIntValue('a');
- fail("An IllegalArgumentException should have been thrown");
- } catch (final IllegalArgumentException ex) {}
+ assertThrows(IllegalArgumentException.class, () -> CharUtils.toIntValue('a'));
}
@Test
@@ -295,14 +283,8 @@ public class CharUtilsTest {
public void testToIntValue_Character() {
assertEquals(0, CharUtils.toIntValue(new Character('0')));
assertEquals(3, CharUtils.toIntValue(new Character('3')));
- try {
- CharUtils.toIntValue(null);
- fail("An IllegalArgumentException should have been thrown");
- } catch (final IllegalArgumentException ex) {}
- try {
- CharUtils.toIntValue(CHARACTER_A);
- fail("An IllegalArgumentException should have been thrown");
- } catch (final IllegalArgumentException ex) {}
+ assertThrows(IllegalArgumentException.class, () -> CharUtils.toIntValue(null));
+ assertThrows(IllegalArgumentException.class, () -> CharUtils.toIntValue(CHARACTER_A));
}
@Test
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java b/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
index 84c1139..54d8949 100644
--- a/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
@@ -25,7 +25,6 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
@@ -97,13 +96,10 @@ public class ClassUtilsTest {
assertGetClassThrowsException( className, ClassNotFoundException.class );
}
- private void assertGetClassThrowsException( final String className, final Class<?> exceptionType ) throws Exception {
- try {
- ClassUtils.getClass( className );
- fail( "ClassUtils.getClass() should fail with an exception of type " + exceptionType.getName() + " when given class name \"" + className + "\"." );
- } catch( final Exception e ) {
- assertTrue( exceptionType.isAssignableFrom( e.getClass() ) );
- }
+ private void assertGetClassThrowsException(final String className, final Class<? extends Exception> exceptionType) {
+ assertThrows(exceptionType,
+ () -> ClassUtils.getClass(className),
+ "ClassUtils.getClass() should fail with an exception of type " + exceptionType.getName() + " when given class name \"" + className + "\"." );
}
private void assertGetClassThrowsNullPointerException( final String className ) throws Exception {
@@ -128,12 +124,9 @@ public class ClassUtilsTest {
@SuppressWarnings("unchecked") // test what happens when non-generic code adds wrong type of element
final List<Object> olist = (List<Object>) (List<?>) list;
olist.add(new Object());
- try {
- ClassUtils.convertClassesToClassNames(list);
- fail("Should not have been able to convert list");
- } catch (final ClassCastException expected) {
- // empty
- }
+ assertThrows(ClassCastException.class,
+ () -> ClassUtils.convertClassesToClassNames(list),
+ "Should not have been able to convert list");
assertNull(ClassUtils.convertClassesToClassNames(null));
}
@@ -156,12 +149,9 @@ public class ClassUtilsTest {
@SuppressWarnings("unchecked") // test what happens when non-generic code adds wrong type of element
final List<Object> olist = (List<Object>) (List<?>) list;
olist.add(new Object());
- try {
- ClassUtils.convertClassNamesToClasses(list);
- fail("Should not have been able to convert list");
- } catch (final ClassCastException expected) {
- // empty
- }
+ assertThrows(ClassCastException.class,
+ () -> ClassUtils.convertClassNamesToClasses(list),
+ "Should not have been able to convert list");
assertNull(ClassUtils.convertClassNamesToClasses(null));
}
@@ -1208,17 +1198,12 @@ public class ClassUtilsTest {
// Tests with Collections$UnmodifiableSet
final Set<?> set = Collections.unmodifiableSet(new HashSet<>());
final Method isEmptyMethod = ClassUtils.getPublicMethod(set.getClass(), "isEmpty");
- assertTrue(Modifier.isPublic(isEmptyMethod.getDeclaringClass().getModifiers()));
-
- try {
- isEmptyMethod.invoke(set);
- } catch(final java.lang.IllegalAccessException iae) {
- fail("Should not have thrown IllegalAccessException");
- }
+ assertTrue(Modifier.isPublic(isEmptyMethod.getDeclaringClass().getModifiers()));
+ assertTrue((Boolean) isEmptyMethod.invoke(set));
// Tests with a public Class
final Method toStringMethod = ClassUtils.getPublicMethod(Object.class, "toString");
- assertEquals(Object.class.getMethod("toString", new Class[0]), toStringMethod);
+ assertEquals(Object.class.getMethod("toString", new Class[0]), toStringMethod);
}
@Test
@@ -1370,12 +1355,7 @@ public class ClassUtilsTest {
// Tests with Collections$UnmodifiableSet
final Set<?> set = Collections.unmodifiableSet(new HashSet<>());
final Method isEmptyMethod = set.getClass().getMethod("isEmpty");
- try {
- isEmptyMethod.invoke(set);
- fail("Failed to throw IllegalAccessException as expected");
- } catch(final IllegalAccessException iae) {
- // expected
- }
+ assertThrows(IllegalAccessException.class, () -> isEmptyMethod.invoke(set));
}
@Test
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/ConversionTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ConversionTest.java b/src/test/java/org/apache/commons/lang3/ConversionTest.java
index 0e258f3..74e6b13 100644
--- a/src/test/java/org/apache/commons/lang3/ConversionTest.java
+++ b/src/test/java/org/apache/commons/lang3/ConversionTest.java
@@ -18,7 +18,7 @@ package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.UUID;
@@ -57,12 +57,7 @@ public class ConversionTest {
assertEquals(14, Conversion.hexDigitToInt('e'));
assertEquals(15, Conversion.hexDigitToInt('F'));
assertEquals(15, Conversion.hexDigitToInt('f'));
- try {
- Conversion.hexDigitToInt('G');
- fail("Thrown " + IllegalArgumentException.class.getName() + " expected");
- } catch (final IllegalArgumentException e) {
- // OK
- }
+ assertThrows(IllegalArgumentException.class, () -> Conversion.hexDigitToInt('G'));
}
/**
@@ -92,12 +87,7 @@ public class ConversionTest {
assertEquals(0x7, Conversion.hexDigitMsb0ToInt('e'));
assertEquals(0xF, Conversion.hexDigitMsb0ToInt('F'));
assertEquals(0xF, Conversion.hexDigitMsb0ToInt('f'));
- try {
- Conversion.hexDigitMsb0ToInt('G');
- fail("Thrown " + IllegalArgumentException.class.getName() + " expected");
- } catch (final IllegalArgumentException e) {
- // OK
- }
+ assertThrows(IllegalArgumentException.class, () -> Conversion.hexDigitMsb0ToInt('G'));
}
/**
@@ -149,12 +139,7 @@ public class ConversionTest {
new boolean[]{true, true, true, true}, Conversion.hexDigitToBinary('F'));
assertArrayEquals(
new boolean[]{true, true, true, true}, Conversion.hexDigitToBinary('f'));
- try {
- Conversion.hexDigitToBinary('G');
- fail("Thrown " + IllegalArgumentException.class.getName() + " expected");
- } catch (final IllegalArgumentException e) {
- // OK
- }
+ assertThrows(IllegalArgumentException.class, () -> Conversion.hexDigitToBinary('G'));
}
/**
@@ -206,12 +191,7 @@ public class ConversionTest {
new boolean[]{true, true, true, true}, Conversion.hexDigitMsb0ToBinary('F'));
assertArrayEquals(
new boolean[]{true, true, true, true}, Conversion.hexDigitMsb0ToBinary('f'));
- try {
- Conversion.hexDigitMsb0ToBinary('G');
- fail("Thrown " + IllegalArgumentException.class.getName() + " expected");
- } catch (final IllegalArgumentException e) {
- // OK
- }
+ assertThrows(IllegalArgumentException.class, () -> Conversion.hexDigitMsb0ToBinary('G'));
}
/**
@@ -239,12 +219,7 @@ public class ConversionTest {
assertEquals('1', Conversion.binaryToHexDigit(new boolean[]{true}));
assertEquals(
'f', Conversion.binaryToHexDigit(new boolean[]{true, true, true, true, true}));
- try {
- Conversion.binaryToHexDigit(new boolean[]{});
- fail("Thrown " + IllegalArgumentException.class.getName() + " expected");
- } catch (final IllegalArgumentException e) {
- // OK
- }
+ assertThrows(IllegalArgumentException.class, () -> Conversion.binaryToHexDigit(new boolean[]{}));
}
/**
@@ -308,12 +283,7 @@ public class ConversionTest {
'e', Conversion.binaryToHexDigitMsb0_4bits(new boolean[]{true, true, true, false}));
assertEquals(
'f', Conversion.binaryToHexDigitMsb0_4bits(new boolean[]{true, true, true, true}));
- try {
- Conversion.binaryToHexDigitMsb0_4bits(new boolean[]{});
- fail("Thrown " + IllegalArgumentException.class.getName() + " expected");
- } catch (final IllegalArgumentException e) {
- // OK
- }
+ assertThrows(IllegalArgumentException.class, () -> Conversion.binaryToHexDigitMsb0_4bits(new boolean[]{}));
}
/**
@@ -393,12 +363,7 @@ public class ConversionTest {
Conversion.binaryBeMsb0ToHexDigit(new boolean[]{
true, false, false, false, false, false, false, false, false, false, false,
false, false, true, false, false}));
- try {
- Conversion.binaryBeMsb0ToHexDigit(new boolean[]{});
- fail("Thrown " + IllegalArgumentException.class.getName() + " expected");
- } catch (final IllegalArgumentException e) {
- // OK
- }
+ assertThrows(IllegalArgumentException.class, () -> Conversion.binaryBeMsb0ToHexDigit(new boolean[]{}));
}
/**
@@ -469,12 +434,7 @@ public class ConversionTest {
assertEquals('d', Conversion.intToHexDigit(13));
assertEquals('e', Conversion.intToHexDigit(14));
assertEquals('f', Conversion.intToHexDigit(15));
- try {
- Conversion.intToHexDigit(16);
- fail("Thrown " + IllegalArgumentException.class.getName() + " expected");
- } catch (final IllegalArgumentException e) {
- // OK
- }
+ assertThrows(IllegalArgumentException.class, () -> Conversion.intToHexDigit(16));
}
/**
@@ -498,12 +458,7 @@ public class ConversionTest {
assertEquals('b', Conversion.intToHexDigitMsb0(13));
assertEquals('7', Conversion.intToHexDigitMsb0(14));
assertEquals('f', Conversion.intToHexDigitMsb0(15));
- try {
- Conversion.intToHexDigitMsb0(16);
- fail("Thrown " + IllegalArgumentException.class.getName() + " expected");
- } catch (final IllegalArgumentException e) {
- // OK
- }
+ assertThrows(IllegalArgumentException.class, () -> Conversion.intToHexDigitMsb0(16));
}
static String dbgPrint(final boolean[] src) {
@@ -1276,12 +1231,7 @@ public class ConversionTest {
Conversion.longToHex(0x1234567890ABCDEFL, 4, "ffffffffffffffffffffffff", 3, 15));
assertEquals(
"fedcba0987654321", Conversion.longToHex(0x1234567890ABCDEFL, 0, "", 0, 16));
- try {
- Conversion.longToHex(0x1234567890ABCDEFL, 0, "", 1, 8);
- fail("Thrown " + StringIndexOutOfBoundsException.class.getName() + " expected");
- } catch (final StringIndexOutOfBoundsException e) {
- // OK
- }
+ assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.longToHex(0x1234567890ABCDEFL, 0, "", 1, 8));
}
/**
@@ -1340,12 +1290,7 @@ public class ConversionTest {
"fffedcba09ffffffffffffff",
Conversion.intToHex(0x90ABCDEF, 4, "ffffffffffffffffffffffff", 3, 7));
assertEquals("fedcba09", Conversion.intToHex(0x90ABCDEF, 0, "", 0, 8));
- try {
- Conversion.intToHex(0x90ABCDEF, 0, "", 1, 8);
- fail("Thrown " + StringIndexOutOfBoundsException.class.getName() + " expected");
- } catch (final StringIndexOutOfBoundsException e) {
- // OK
- }
+ assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.intToHex(0x90ABCDEF, 0, "", 1, 8));
}
/**
@@ -1392,12 +1337,7 @@ public class ConversionTest {
"fffedcffffffffffffffffff",
Conversion.shortToHex((short)0xCDEF, 4, "ffffffffffffffffffffffff", 3, 3));
assertEquals("fedc", Conversion.shortToHex((short)0xCDEF, 0, "", 0, 4));
- try {
- Conversion.shortToHex((short)0xCDEF, 0, "", 1, 4);
- fail("Thrown " + StringIndexOutOfBoundsException.class.getName() + " expected");
- } catch (final StringIndexOutOfBoundsException e) {
- // OK
- }
+ assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.shortToHex((short)0xCDEF, 0, "", 1, 4));
}
/**
@@ -1420,12 +1360,7 @@ public class ConversionTest {
// assertion
assertEquals("000e0", Conversion.byteToHex((byte)0xEF, 4, "00000", 3, 1));
assertEquals("fe", Conversion.byteToHex((byte)0xEF, 0, "", 0, 2));
- try {
- Conversion.byteToHex((byte)0xEF, 0, "", 1, 2);
- fail("Thrown " + StringIndexOutOfBoundsException.class.getName() + " expected");
- } catch (final StringIndexOutOfBoundsException e) {
- // OK
- }
+ assertThrows(StringIndexOutOfBoundsException.class, () -> Conversion.byteToHex((byte)0xEF, 0, "", 1, 2));
}
/**
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
index 390a250..c445e6e 100644
--- a/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -138,32 +139,13 @@ public class LocaleUtilsTest {
// LANG-941: JDK 8 introduced the empty locale as one of the default locales
assertValidToLocale("");
- try {
- LocaleUtils.toLocale("Us");
- fail("Should fail if not lowercase");
- } catch (final IllegalArgumentException iae) {}
- try {
- LocaleUtils.toLocale("US");
- fail("Should fail if not lowercase");
- } catch (final IllegalArgumentException iae) {}
- try {
- LocaleUtils.toLocale("uS");
- fail("Should fail if not lowercase");
- } catch (final IllegalArgumentException iae) {}
- try {
- LocaleUtils.toLocale("u#");
- fail("Should fail if not lowercase");
- } catch (final IllegalArgumentException iae) {}
-
- try {
- LocaleUtils.toLocale("u");
- fail("Must be 2 chars if less than 5");
- } catch (final IllegalArgumentException iae) {}
-
- try {
- LocaleUtils.toLocale("uu_U");
- fail("Must be 2 chars if less than 5");
- } catch (final IllegalArgumentException iae) {}
+ assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("Us"), "Should fail if not lowercase");
+ assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("uS"), "Should fail if not lowercase");
+ assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("u#"), "Should fail if not lowercase");
+ assertThrows(
+ IllegalArgumentException.class, () -> LocaleUtils.toLocale("u"), "Must be 2 chars if less than 5");
+ assertThrows(
+ IllegalArgumentException.class, () -> LocaleUtils.toLocale("uu_U"), "Must be 2 chars if less than 5");
}
/**
@@ -175,30 +157,28 @@ public class LocaleUtilsTest {
//valid though doesn't exist
assertValidToLocale("us_ZH", "us", "ZH");
- try {
- LocaleUtils.toLocale("us-EN");
- fail("Should fail as not underscore");
- } catch (final IllegalArgumentException iae) {}
- try {
- LocaleUtils.toLocale("us_En");
- fail("Should fail second part not uppercase");
- } catch (final IllegalArgumentException iae) {}
- try {
- LocaleUtils.toLocale("us_en");
- fail("Should fail second part not uppercase");
- } catch (final IllegalArgumentException iae) {}
- try {
- LocaleUtils.toLocale("us_eN");
- fail("Should fail second part not uppercase");
- } catch (final IllegalArgumentException iae) {}
- try {
- LocaleUtils.toLocale("uS_EN");
- fail("Should fail first part not lowercase");
- } catch (final IllegalArgumentException iae) {}
- try {
- LocaleUtils.toLocale("us_E3");
- fail("Should fail second part not uppercase");
- } catch (final IllegalArgumentException iae) {}
+ assertThrows(
+ IllegalArgumentException.class, () -> LocaleUtils.toLocale("us-EN"), "Should fail as not underscore");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("us_En"),
+ "Should fail second part not uppercase");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("us_en"),
+ "Should fail second part not uppercase");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("us_eN"),
+ "Should fail second part not uppercase");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("uS_EN"),
+ "Should fail first part not lowercase");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("us_E3"),
+ "Should fail second part not uppercase");
}
/**
@@ -217,14 +197,10 @@ public class LocaleUtilsTest {
assertValidToLocale("us_EN_SFsafdFDsdfF", "us", "EN", "SFSAFDFDSDFF");
}
- try {
- LocaleUtils.toLocale("us_EN-a");
- fail("Should fail as not underscore");
- } catch (final IllegalArgumentException iae) {}
- try {
- LocaleUtils.toLocale("uu_UU_");
- fail("Must be 3, 5 or 7+ in length");
- } catch (final IllegalArgumentException iae) {}
+ assertThrows(
+ IllegalArgumentException.class, () -> LocaleUtils.toLocale("us_EN-a"), "Should fail as not underscore");
+ assertThrows(
+ IllegalArgumentException.class, () -> LocaleUtils.toLocale("uu_UU_"), "Must be 3, 5 or 7+ in length");
}
//-----------------------------------------------------------------------
@@ -492,10 +468,7 @@ public class LocaleUtilsTest {
* @param coll the collection to check
*/
private static void assertUnmodifiableCollection(final Collection<?> coll) {
- try {
- coll.add(null);
- fail();
- } catch (final UnsupportedOperationException ex) {}
+ assertThrows(UnsupportedOperationException.class, () -> coll.add(null));
}
/**
@@ -526,41 +499,34 @@ public class LocaleUtilsTest {
assertValidToLocale("_GB", "", "GB", "");
assertValidToLocale("_GB_P", "", "GB", "P");
assertValidToLocale("_GB_POSIX", "", "GB", "POSIX");
- try {
- LocaleUtils.toLocale("_G");
- fail("Must be at least 3 chars if starts with underscore");
- } catch (final IllegalArgumentException iae) {
- }
- try {
- LocaleUtils.toLocale("_Gb");
- fail("Must be uppercase if starts with underscore");
- } catch (final IllegalArgumentException iae) {
- }
- try {
- LocaleUtils.toLocale("_gB");
- fail("Must be uppercase if starts with underscore");
- } catch (final IllegalArgumentException iae) {
- }
- try {
- LocaleUtils.toLocale("_1B");
- fail("Must be letter if starts with underscore");
- } catch (final IllegalArgumentException iae) {
- }
- try {
- LocaleUtils.toLocale("_G1");
- fail("Must be letter if starts with underscore");
- } catch (final IllegalArgumentException iae) {
- }
- try {
- LocaleUtils.toLocale("_GB_");
- fail("Must be at least 5 chars if starts with underscore");
- } catch (final IllegalArgumentException iae) {
- }
- try {
- LocaleUtils.toLocale("_GBAP");
- fail("Must have underscore after the country if starts with underscore and is at least 5 chars");
- } catch (final IllegalArgumentException iae) {
- }
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("_G"),
+ "Must be at least 3 chars if starts with underscore");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("_Gb"),
+ "Must be uppercase if starts with underscore");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("_gB"),
+ "Must be uppercase if starts with underscore");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("_1B"),
+ "Must be letter if starts with underscore");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("_G1"),
+ "Must be letter if starts with underscore");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("_GB_"),
+ "Must be at least 5 chars if starts with underscore");
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> LocaleUtils.toLocale("_GBAP"),
+ "Must have underscore after the country if starts with underscore and is at least 5 chars");
}
@ParameterizedTest
@@ -569,22 +535,19 @@ public class LocaleUtilsTest {
// Check if it's possible to recreate the Locale using just the standard constructor
final Locale locale = new Locale(l.getLanguage(), l.getCountry(), l.getVariant());
if (l.equals(locale)) { // it is possible for LocaleUtils.toLocale to handle these Locales
- String str = l.toString();
+ final String str = l.toString();
// Look for the script/extension suffix
int suff = str.indexOf("_#");
if (suff == - 1) {
suff = str.indexOf("#");
}
+ String localeStr = str;
if (suff >= 0) { // we have a suffix
- try {
- LocaleUtils.toLocale(str); // should cause IAE
- fail("Should not have parsed: " + str);
- } catch (final IllegalArgumentException iae) {
- // expected; try without suffix
- str = str.substring(0, suff);
- }
+ assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale(str));
+ // try without suffix
+ localeStr = str.substring(0, suff);
}
- final Locale loc = LocaleUtils.toLocale(str);
+ final Locale loc = LocaleUtils.toLocale(localeStr);
assertEquals(l, loc);
}
}