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:09 UTC
[05/14] [lang] Clean up testing of exceptions
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/math/FractionTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/math/FractionTest.java b/src/test/java/org/apache/commons/lang3/math/FractionTest.java
index 9128141..416c16b 100644
--- a/src/test/java/org/apache/commons/lang3/math/FractionTest.java
+++ b/src/test/java/org/apache/commons/lang3/math/FractionTest.java
@@ -23,7 +23,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
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 org.junit.jupiter.api.Test;
@@ -119,30 +118,13 @@ public class FractionTest {
assertEquals(10, f.getDenominator());
// zero denominator
- try {
- f = Fraction.getFraction(1, 0);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(2, 0);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(-3, 0);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(1, 0));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(2, 0));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(-3, 0));
// very large: can't represent as unsimplified fraction, although
- try {
- f = Fraction.getFraction(4, Integer.MIN_VALUE);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
- try {
- f = Fraction.getFraction(1, Integer.MIN_VALUE);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(4, Integer.MIN_VALUE));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(1, Integer.MIN_VALUE));
}
@Test
@@ -168,86 +150,37 @@ public class FractionTest {
assertEquals(2, f.getDenominator());
// negatives
- try {
- f = Fraction.getFraction(1, -6, -10);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(1, -6, -10);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(1, -6, -10);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(1, -6, -10));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(1, -6, -10));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(1, -6, -10));
// negative whole
f = Fraction.getFraction(-1, 6, 10);
assertEquals(-16, f.getNumerator());
assertEquals(10, f.getDenominator());
- try {
- f = Fraction.getFraction(-1, -6, 10);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(-1, 6, -10);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(-1, -6, -10);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(-1, -6, 10));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(-1, 6, -10));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(-1, -6, -10));
// zero denominator
- try {
- f = Fraction.getFraction(0, 1, 0);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(1, 2, 0);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(-1, -3, 0);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(Integer.MAX_VALUE, 1, 2);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(-Integer.MAX_VALUE, 1, 2);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(0, 1, 0));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(1, 2, 0));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(-1, -3, 0));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Integer.MAX_VALUE, 1, 2));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(-Integer.MAX_VALUE, 1, 2));
// very large
f = Fraction.getFraction(-1, 0, Integer.MAX_VALUE);
assertEquals(-Integer.MAX_VALUE, f.getNumerator());
assertEquals(Integer.MAX_VALUE, f.getDenominator());
- try {
- // negative denominators not allowed in this constructor.
- f = Fraction.getFraction(0, 4, Integer.MIN_VALUE);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
- try {
- f = Fraction.getFraction(1, 1, Integer.MAX_VALUE);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
- try {
- f = Fraction.getFraction(-1, 2, Integer.MAX_VALUE);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ // negative denominators not allowed in this constructor.
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(0, 4, Integer.MIN_VALUE));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(1, 1, Integer.MAX_VALUE));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(-1, 2, Integer.MAX_VALUE));
}
+
@Test
public void testReducedFactory_int_int() {
Fraction f = null;
@@ -285,20 +218,9 @@ public class FractionTest {
assertEquals(5, f.getDenominator());
// zero denominator
- try {
- f = Fraction.getReducedFraction(1, 0);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getReducedFraction(2, 0);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getReducedFraction(-3, 0);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getReducedFraction(1, 0));
+ assertThrows(ArithmeticException.class, () -> Fraction.getReducedFraction(2, 0));
+ assertThrows(ArithmeticException.class, () -> Fraction.getReducedFraction(-3, 0));
// reduced
f = Fraction.getReducedFraction(0, 2);
@@ -328,10 +250,7 @@ public class FractionTest {
assertEquals(-(Integer.MIN_VALUE / 2), f.getDenominator());
// Can't reduce, negation will throw
- try {
- f = Fraction.getReducedFraction(-7, Integer.MIN_VALUE);
- fail("Expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getReducedFraction(-7, Integer.MIN_VALUE));
// LANG-662
f = Fraction.getReducedFraction(Integer.MIN_VALUE, 2);
@@ -341,30 +260,13 @@ public class FractionTest {
@Test
public void testFactory_double() {
- Fraction f = null;
-
- try {
- f = Fraction.getFraction(Double.NaN);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(Double.POSITIVE_INFINITY);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction(Double.NEGATIVE_INFINITY);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f = Fraction.getFraction((double) Integer.MAX_VALUE + 1);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Double.NaN));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Double.POSITIVE_INFINITY));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Double.NEGATIVE_INFINITY));
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction((double) Integer.MAX_VALUE + 1));
// zero
- f = Fraction.getFraction(0.0d);
+ Fraction f = Fraction.getFraction(0.0d);
assertEquals(0, f.getNumerator());
assertEquals(1, f.getDenominator());
@@ -402,12 +304,8 @@ public class FractionTest {
Fraction f2 = null;
for (int i = 1; i <= 100; i++) { // denominator
for (int j = 1; j <= i; j++) { // numerator
- try {
- f = Fraction.getFraction((double) j / (double) i);
- } catch (final ArithmeticException ex) {
- System.err.println(j + " " + i);
- throw ex;
- }
+ f = Fraction.getFraction((double) j / (double) i);
+
f2 = Fraction.getReducedFraction(j, i);
assertEquals(f2.getNumerator(), f.getNumerator());
assertEquals(f2.getDenominator(), f.getDenominator());
@@ -416,12 +314,7 @@ public class FractionTest {
// save time by skipping some tests! (
for (int i = 1001; i <= 10000; i+=SKIP) { // denominator
for (int j = 1; j <= i; j++) { // numerator
- try {
- f = Fraction.getFraction((double) j / (double) i);
- } catch (final ArithmeticException ex) {
- System.err.println(j + " " + i);
- throw ex;
- }
+ f = Fraction.getFraction((double) j / (double) i);
f2 = Fraction.getReducedFraction(j, i);
assertEquals(f2.getNumerator(), f.getNumerator());
assertEquals(f2.getDenominator(), f.getDenominator());
@@ -455,20 +348,9 @@ public class FractionTest {
assertEquals(2, f.getNumerator());
assertEquals(3, f.getDenominator());
- try {
- f = Fraction.getFraction("2.3R");
- fail("Expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
-
- try {
- f = Fraction.getFraction("2147483648"); // too big
- fail("Expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
-
- try {
- f = Fraction.getFraction(".");
- fail("Expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("2.3R"));
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("2147483648")); // too big
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("."));
}
@Test
@@ -499,35 +381,12 @@ public class FractionTest {
assertEquals(-6, f.getNumerator());
assertEquals(4, f.getDenominator());
- try {
- f = Fraction.getFraction("2 3");
- fail("expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
-
- try {
- f = Fraction.getFraction("a 3");
- fail("expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
-
- try {
- f = Fraction.getFraction("2 b/4");
- fail("expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
-
- try {
- f = Fraction.getFraction("2 ");
- fail("expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
-
- try {
- f = Fraction.getFraction(" 3");
- fail("expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
-
- try {
- f = Fraction.getFraction(" ");
- fail("expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("2 3"));
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("a 3"));
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("2 b/4"));
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("2 "));
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction(" 3"));
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction(" "));
}
@Test
@@ -558,25 +417,10 @@ public class FractionTest {
assertEquals(2, f.getNumerator());
assertEquals(4, f.getDenominator());
- try {
- f = Fraction.getFraction("2/d");
- fail("expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
-
- try {
- f = Fraction.getFraction("2e/3");
- fail("expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
-
- try {
- f = Fraction.getFraction("2/");
- fail("expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
-
- try {
- f = Fraction.getFraction("/");
- fail("expecting NumberFormatException");
- } catch (final NumberFormatException ex) {}
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("2/d"));
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("2e/3"));
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("2/"));
+ assertThrows(NumberFormatException.class, () -> Fraction.getFraction("/"));
}
@Test
@@ -681,18 +525,8 @@ public class FractionTest {
assertEquals(-47, f.getNumerator());
assertEquals(15, f.getDenominator());
- f = Fraction.getFraction(0, 3);
- try {
- f = f.invert();
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- // large values
- f = Fraction.getFraction(Integer.MIN_VALUE, 1);
- try {
- f = f.invert();
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(0, 3).invert());
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Integer.MIN_VALUE, 1).invert());
f = Fraction.getFraction(Integer.MAX_VALUE, 1);
f = f.invert();
@@ -720,11 +554,7 @@ public class FractionTest {
assertEquals(Integer.MIN_VALUE+2, f.getNumerator());
assertEquals(Integer.MAX_VALUE, f.getDenominator());
- f = Fraction.getFraction(Integer.MIN_VALUE, 1);
- try {
- f = f.negate();
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Integer.MIN_VALUE, 1).negate());
}
@Test
@@ -751,11 +581,7 @@ public class FractionTest {
assertEquals(Integer.MAX_VALUE, f.getNumerator());
assertEquals(1, f.getDenominator());
- f = Fraction.getFraction(Integer.MIN_VALUE, 1);
- try {
- f = f.abs();
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Integer.MIN_VALUE, 1).abs());
}
@Test
@@ -829,14 +655,9 @@ public class FractionTest {
assertEquals(1, f.getDenominator());
// zero to negative powers should throw an exception
- try {
- f = f.pow(-1);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
- try {
- f = f.pow(Integer.MIN_VALUE);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ final Fraction fr = f;
+ assertThrows(ArithmeticException.class, () -> fr.pow(-1));
+ assertThrows(ArithmeticException.class, () -> fr.pow(Integer.MIN_VALUE));
// one to any power is still one.
f = Fraction.getFraction(1, 1);
@@ -851,24 +672,12 @@ public class FractionTest {
f = f.pow(Integer.MIN_VALUE);
assertEquals(f, Fraction.ONE);
- f = Fraction.getFraction(Integer.MAX_VALUE, 1);
- try {
- f = f.pow(2);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Integer.MAX_VALUE, 1).pow(2));
// Numerator growing too negative during the pow operation.
- f = Fraction.getFraction(Integer.MIN_VALUE, 1);
- try {
- f = f.pow(3);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- f = Fraction.getFraction(65536, 1);
- try {
- f = f.pow(2);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(Integer.MIN_VALUE, 1).pow(3));
+
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(65536, 1).pow(2));
}
@Test
@@ -928,14 +737,11 @@ public class FractionTest {
f1 = Fraction.getFraction(-1, 13*13*2*2);
f2 = Fraction.getFraction(-2, 13*17*2);
- f = f1.add(f2);
- assertEquals(13*13*17*2*2, f.getDenominator());
- assertEquals(-17 - 2*13*2, f.getNumerator());
+ final Fraction fr = f1.add(f2);
+ assertEquals(13*13*17*2*2, fr.getDenominator());
+ assertEquals(-17 - 2*13*2, fr.getNumerator());
- try {
- f.add(null);
- fail("expecting IllegalArgumentException");
- } catch (final IllegalArgumentException ex) {}
+ assertThrows(IllegalArgumentException.class, () -> fr.add(null));
// if this fraction is added naively, it will overflow.
// check that it doesn't.
@@ -957,37 +763,23 @@ public class FractionTest {
assertEquals(Integer.MAX_VALUE, f.getNumerator());
assertEquals(1, f.getDenominator());
- try {
- f = f.add(Fraction.ONE); // should overflow
- fail("expecting ArithmeticException but got: " + f.toString());
- } catch (final ArithmeticException ex) {}
+ final Fraction overflower = f;
+ assertThrows(ArithmeticException.class, () -> overflower.add(Fraction.ONE)); // should overflow
// denominator should not be a multiple of 2 or 3 to trigger overflow
- f1 = Fraction.getFraction(Integer.MIN_VALUE, 5);
- f2 = Fraction.getFraction(-1,5);
- try {
- f = f1.add(f2); // should overflow
- fail("expecting ArithmeticException but got: " + f.toString());
- } catch (final ArithmeticException ex) {}
-
- try {
- f= Fraction.getFraction(-Integer.MAX_VALUE, 1);
- f = f.add(f);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f= Fraction.getFraction(-Integer.MAX_VALUE, 1);
- f = f.add(f);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- f1 = Fraction.getFraction(3,327680);
- f2 = Fraction.getFraction(2,59049);
- try {
- f = f1.add(f2); // should overflow
- fail("expecting ArithmeticException but got: " + f.toString());
- } catch (final ArithmeticException ex) {}
+ assertThrows(
+ ArithmeticException.class,
+ () -> Fraction.getFraction(Integer.MIN_VALUE, 5).add(Fraction.getFraction(-1,5)));
+
+ final Fraction maxValue = Fraction.getFraction(-Integer.MAX_VALUE, 1);
+ assertThrows(ArithmeticException.class, () -> maxValue.add(maxValue));
+
+ final Fraction negativeMaxValue = Fraction.getFraction(-Integer.MAX_VALUE, 1);
+ assertThrows(ArithmeticException.class, () -> negativeMaxValue.add(negativeMaxValue));
+
+ final Fraction f3 = Fraction.getFraction(3,327680);
+ final Fraction f4 = Fraction.getFraction(2,59049);
+ assertThrows(ArithmeticException.class, () -> f3.add(f4)); // should overflow
}
@Test
@@ -1043,10 +835,8 @@ public class FractionTest {
f = f2.subtract(f1);
assertSame(f2, f);
- try {
- f.subtract(null);
- fail("expecting IllegalArgumentException");
- } catch (final IllegalArgumentException ex) {}
+ final Fraction fr = f;
+ assertThrows(IllegalArgumentException.class, () -> fr.subtract(null));
// if this fraction is subtracted naively, it will overflow.
// check that it doesn't.
@@ -1068,39 +858,28 @@ public class FractionTest {
assertEquals(Integer.MAX_VALUE-1, f.getNumerator());
assertEquals(1, f.getDenominator());
- try {
- f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
- f2 = Fraction.getFraction(1, Integer.MAX_VALUE - 1);
+ // Should overflow
+ assertThrows(
+ ArithmeticException.class,
+ () -> Fraction.getFraction(1, Integer.MAX_VALUE).subtract(Fraction.getFraction(1, Integer.MAX_VALUE - 1)));
f = f1.subtract(f2);
- fail("expecting ArithmeticException"); //should overflow
- } catch (final ArithmeticException ex) {}
// denominator should not be a multiple of 2 or 3 to trigger overflow
- f1 = Fraction.getFraction(Integer.MIN_VALUE, 5);
- f2 = Fraction.getFraction(1,5);
- try {
- f = f1.subtract(f2); // should overflow
- fail("expecting ArithmeticException but got: " + f.toString());
- } catch (final ArithmeticException ex) {}
-
- try {
- f= Fraction.getFraction(Integer.MIN_VALUE, 1);
- f = f.subtract(Fraction.ONE);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f= Fraction.getFraction(Integer.MAX_VALUE, 1);
- f = f.subtract(Fraction.ONE.negate());
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- f1 = Fraction.getFraction(3,327680);
- f2 = Fraction.getFraction(2,59049);
- try {
- f = f1.subtract(f2); // should overflow
- fail("expecting ArithmeticException but got: " + f.toString());
- } catch (final ArithmeticException ex) {}
+ assertThrows(
+ ArithmeticException.class,
+ () -> Fraction.getFraction(Integer.MIN_VALUE, 5).subtract(Fraction.getFraction(1,5)));
+
+ assertThrows(
+ ArithmeticException.class, () -> Fraction.getFraction(Integer.MIN_VALUE, 1).subtract(Fraction.ONE));
+
+ assertThrows(
+ ArithmeticException.class,
+ () -> Fraction.getFraction(Integer.MAX_VALUE, 1).subtract(Fraction.ONE.negate()));
+
+ // Should overflow
+ assertThrows(
+ ArithmeticException.class,
+ () -> Fraction.getFraction(3,327680).subtract(Fraction.getFraction(2,59049)));
}
@Test
@@ -1154,22 +933,14 @@ public class FractionTest {
assertEquals(Integer.MIN_VALUE, f.getNumerator());
assertEquals(1, f.getDenominator());
- try {
- f.multiplyBy(null);
- fail("expecting IllegalArgumentException");
- } catch (final IllegalArgumentException ex) {}
-
- try {
- f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
- f = f1.multiplyBy(f1); // should overflow
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
-
- try {
- f1 = Fraction.getFraction(1, -Integer.MAX_VALUE);
- f = f1.multiplyBy(f1); // should overflow
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ final Fraction fr = f;
+ assertThrows(IllegalArgumentException.class, () -> fr.multiplyBy(null));
+
+ final Fraction fr1 = Fraction.getFraction(1, Integer.MAX_VALUE);
+ assertThrows(ArithmeticException.class, () -> fr1.multiplyBy(fr1));
+
+ final Fraction fr2 = Fraction.getFraction(1, -Integer.MAX_VALUE);
+ assertThrows(ArithmeticException.class, () -> fr2.multiplyBy(fr2));
}
@Test
@@ -1184,12 +955,7 @@ public class FractionTest {
assertEquals(3, f.getNumerator());
assertEquals(2, f.getDenominator());
- f1 = Fraction.getFraction(3, 5);
- f2 = Fraction.ZERO;
- try {
- f = f1.divideBy(f2);
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ assertThrows(ArithmeticException.class, () -> Fraction.getFraction(3, 5).divideBy(Fraction.ZERO));
f1 = Fraction.getFraction(0, 5);
f2 = Fraction.getFraction(2, 7);
@@ -1209,25 +975,17 @@ public class FractionTest {
f1 = Fraction.getFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
f2 = Fraction.getFraction(1, Integer.MAX_VALUE);
- f = f1.divideBy(f2);
- assertEquals(Integer.MIN_VALUE, f.getNumerator());
- assertEquals(1, f.getDenominator());
+ final Fraction fr = f1.divideBy(f2);
+ assertEquals(Integer.MIN_VALUE, fr.getNumerator());
+ assertEquals(1, fr.getDenominator());
+
+ assertThrows(IllegalArgumentException.class, () -> fr.divideBy(null));
+
+ final Fraction smallest = Fraction.getFraction(1, Integer.MAX_VALUE);
+ assertThrows(ArithmeticException.class, () -> smallest.divideBy(smallest.invert())); // Should overflow
- try {
- f.divideBy(null);
- fail("IllegalArgumentException");
- } catch (final IllegalArgumentException ex) {}
-
- try {
- f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
- f = f1.divideBy(f1.invert()); // should overflow
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
- try {
- f1 = Fraction.getFraction(1, -Integer.MAX_VALUE);
- f = f1.divideBy(f1.invert()); // should overflow
- fail("expecting ArithmeticException");
- } catch (final ArithmeticException ex) {}
+ final Fraction negative = Fraction.getFraction(1, -Integer.MAX_VALUE);
+ assertThrows(ArithmeticException.class, () -> negative.divideBy(negative.invert())); // Should overflow
}
@Test
@@ -1275,10 +1033,8 @@ public class FractionTest {
f1 = Fraction.getFraction(3, 5);
assertTrue(f1.compareTo(f1) == 0);
- try {
- f1.compareTo(null);
- fail("expecting NullPointerException");
- } catch (final NullPointerException ex) {}
+ final Fraction fr = f1;
+ assertThrows(NullPointerException.class, () -> fr.compareTo(null));
f2 = Fraction.getFraction(2, 5);
assertTrue(f1.compareTo(f2) > 0);
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/math/IEEE754rUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/math/IEEE754rUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/IEEE754rUtilsTest.java
index 8f7c4f3..2ed6670 100644
--- a/src/test/java/org/apache/commons/lang3/math/IEEE754rUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/math/IEEE754rUtilsTest.java
@@ -17,8 +17,8 @@
package org.apache.commons.lang3.math;
import static org.junit.jupiter.api.Assertions.assertEquals;
+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 org.junit.jupiter.api.Test;
@@ -55,46 +55,45 @@ public class IEEE754rUtilsTest {
@Test
public void testEnforceExceptions() {
- try {
- IEEE754rUtils.min( (float[]) null);
- fail("IllegalArgumentException expected for null input");
- } catch(final IllegalArgumentException iae) { /* expected */ }
-
- try {
- IEEE754rUtils.min();
- fail("IllegalArgumentException expected for empty input");
- } catch(final IllegalArgumentException iae) { /* expected */ }
-
- try {
- IEEE754rUtils.max( (float[]) null);
- fail("IllegalArgumentException expected for null input");
- } catch(final IllegalArgumentException iae) { /* expected */ }
-
- try {
- IEEE754rUtils.max();
- fail("IllegalArgumentException expected for empty input");
- } catch(final IllegalArgumentException iae) { /* expected */ }
-
- try {
- IEEE754rUtils.min( (double[]) null);
- fail("IllegalArgumentException expected for null input");
- } catch(final IllegalArgumentException iae) { /* expected */ }
-
- try {
- IEEE754rUtils.min(new double[0]);
- fail("IllegalArgumentException expected for empty input");
- } catch(final IllegalArgumentException iae) { /* expected */ }
-
- try {
- IEEE754rUtils.max( (double[]) null);
- fail("IllegalArgumentException expected for null input");
- } catch(final IllegalArgumentException iae) { /* expected */ }
-
- try {
- IEEE754rUtils.max(new double[0]);
- fail("IllegalArgumentException expected for empty input");
- } catch(final IllegalArgumentException iae) { /* expected */ }
-
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> IEEE754rUtils.min( (float[]) null),
+ "IllegalArgumentException expected for null input");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> IEEE754rUtils.min(),
+ "IllegalArgumentException expected for empty input");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> IEEE754rUtils.max( (float[]) null),
+ "IllegalArgumentException expected for null input");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ IEEE754rUtils::max,
+ "IllegalArgumentException expected for empty input");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> IEEE754rUtils.min( (double[]) null),
+ "IllegalArgumentException expected for null input");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> IEEE754rUtils.min(new double[0]),
+ "IllegalArgumentException expected for empty input");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> IEEE754rUtils.max( (double[]) null),
+ "IllegalArgumentException expected for null input");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> IEEE754rUtils.max(new double[0]),
+ "IllegalArgumentException expected for empty input");
}
@Test
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
index b52a93a..a935931 100644
--- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
@@ -616,12 +616,9 @@ public class NumberUtilsTest {
}
protected void testCreateFloatFailure(final String str) {
- try {
- final Float value = NumberUtils.createFloat(str);
- fail("createFloat(\"" + str + "\") should have failed: " + value);
- } catch (final NumberFormatException ex) {
- // empty
- }
+ assertThrows(
+ NumberFormatException.class,
+ () -> NumberUtils.createFloat(str), "createFloat(\"" + str + "\") should have failed.");
}
@Test
@@ -636,12 +633,10 @@ public class NumberUtilsTest {
}
protected void testCreateDoubleFailure(final String str) {
- try {
- final Double value = NumberUtils.createDouble(str);
- fail("createDouble(\"" + str + "\") should have failed: " + value);
- } catch (final NumberFormatException ex) {
- // empty
- }
+ assertThrows(
+ NumberFormatException.class,
+ () -> NumberUtils.createDouble(str),
+ "createDouble(\"" + str + "\") should have failed.");
}
@Test
@@ -656,12 +651,10 @@ public class NumberUtilsTest {
}
protected void testCreateIntegerFailure(final String str) {
- try {
- final Integer value = NumberUtils.createInteger(str);
- fail("createInteger(\"" + str + "\") should have failed: " + value);
- } catch (final NumberFormatException ex) {
- // empty
- }
+ assertThrows(
+ NumberFormatException.class,
+ () -> NumberUtils.createInteger(str),
+ "createInteger(\"" + str + "\") should have failed.");
}
@Test
@@ -676,12 +669,10 @@ public class NumberUtilsTest {
}
protected void testCreateLongFailure(final String str) {
- try {
- final Long value = NumberUtils.createLong(str);
- fail("createLong(\"" + str + "\") should have failed: " + value);
- } catch (final NumberFormatException ex) {
- // empty
- }
+ assertThrows(
+ NumberFormatException.class,
+ () -> NumberUtils.createLong(str),
+ "createLong(\"" + str + "\") should have failed.");
}
@Test
@@ -709,12 +700,10 @@ public class NumberUtilsTest {
}
protected void testCreateBigIntegerFailure(final String str) {
- try {
- final BigInteger value = NumberUtils.createBigInteger(str);
- fail("createBigInteger(\"" + str + "\") should have failed: " + value);
- } catch (final NumberFormatException ex) {
- // empty
- }
+ assertThrows(
+ NumberFormatException.class,
+ () -> NumberUtils.createBigInteger(str),
+ "createBigInteger(\"" + str + "\") should have failed.");
}
@Test
@@ -735,12 +724,10 @@ public class NumberUtilsTest {
}
protected void testCreateBigDecimalFailure(final String str) {
- try {
- final BigDecimal value = NumberUtils.createBigDecimal(str);
- fail("createBigDecimal(\"" + str + "\") should have failed: " + value);
- } catch (final NumberFormatException ex) {
- // empty
- }
+ assertThrows(
+ NumberFormatException.class,
+ () -> NumberUtils.createBigDecimal(str),
+ "createBigDecimal(\"" + str + "\") should have failed.");
}
// min/max tests
@@ -954,15 +941,13 @@ public class NumberUtilsTest {
@Test
public void testMaxDouble() {
final double[] d = null;
- try {
- NumberUtils.max(d);
- fail("No exception was thrown for null input.");
- } catch (final IllegalArgumentException ex) {}
+ assertThrows(
+ IllegalArgumentException.class, () -> NumberUtils.max(d), "No exception was thrown for null input.");
- try {
- NumberUtils.max(new double[0]);
- fail("No exception was thrown for empty input.");
- } catch (final IllegalArgumentException ex) {}
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> NumberUtils.max(new double[0]),
+ "No exception was thrown for empty input.");
// TODO: JUnit Jupiter 5.3.1 doesn't support delta=0.
// This should be replaced when it is supported in JUnit Jupiter 5.4.
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/mutable/MutableShortTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/mutable/MutableShortTest.java b/src/test/java/org/apache/commons/lang3/mutable/MutableShortTest.java
index 85373c5..78ffe63 100644
--- a/src/test/java/org/apache/commons/lang3/mutable/MutableShortTest.java
+++ b/src/test/java/org/apache/commons/lang3/mutable/MutableShortTest.java
@@ -20,8 +20,8 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
/**
* JUnit tests.
@@ -42,10 +42,7 @@ public class MutableShortTest {
assertEquals((short) 2, new MutableShort("2").shortValue());
- try {
- new MutableShort((Number)null);
- fail();
- } catch (final NullPointerException ex) {}
+ assertThrows(NullPointerException.class, () -> new MutableShort((Number)null));
}
@Test
@@ -65,10 +62,7 @@ public class MutableShortTest {
mutNum.setValue(new MutableShort((short) 3));
assertEquals((short) 3, mutNum.shortValue());
assertEquals(Short.valueOf((short) 3), mutNum.getValue());
- try {
- mutNum.setValue(null);
- fail();
- } catch (final NullPointerException ex) {}
+ assertThrows(NullPointerException.class, () -> mutNum.setValue(null));
}
@Test
@@ -108,10 +102,7 @@ public class MutableShortTest {
assertEquals((short) 0, mutNum.compareTo(new MutableShort((short) 0)));
assertEquals((short) +1, mutNum.compareTo(new MutableShort((short) -1)));
assertEquals((short) -1, mutNum.compareTo(new MutableShort((short) 1)));
- try {
- mutNum.compareTo(null);
- fail();
- } catch (final NullPointerException ex) {}
+ assertThrows(NullPointerException.class, () -> mutNum.compareTo(null));
}
@Test
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/3387734b/src/test/java/org/apache/commons/lang3/reflect/ConstructorUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/reflect/ConstructorUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/ConstructorUtilsTest.java
index 42c0644..ed4cbb2 100644
--- a/src/test/java/org/apache/commons/lang3/reflect/ConstructorUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/reflect/ConstructorUtilsTest.java
@@ -20,8 +20,8 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
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.util.Arrays;
@@ -186,24 +186,15 @@ public class ConstructorUtilsTest {
TestBean.class, new Object[] { NumberUtils.DOUBLE_ONE },
new Class[] { Double.TYPE }).toString());
- try {
- ConstructorUtils.invokeExactConstructor(TestBean.class,
- NumberUtils.BYTE_ONE);
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException e) {
- }
- try {
- ConstructorUtils.invokeExactConstructor(TestBean.class,
- NumberUtils.LONG_ONE);
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException e) {
- }
- try {
- ConstructorUtils.invokeExactConstructor(TestBean.class,
- Boolean.TRUE);
- fail("should throw NoSuchMethodException");
- } catch (final NoSuchMethodException e) {
- }
+ assertThrows(
+ NoSuchMethodException.class,
+ () -> ConstructorUtils.invokeExactConstructor(TestBean.class,NumberUtils.BYTE_ONE));
+ assertThrows(
+ NoSuchMethodException.class,
+ () -> ConstructorUtils.invokeExactConstructor(TestBean.class, NumberUtils.LONG_ONE));
+ assertThrows(
+ NoSuchMethodException.class,
+ () -> ConstructorUtils.invokeExactConstructor(TestBean.class, Boolean.TRUE));
}
@Test