aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/apache/commons/lang3
diff options
context:
space:
mode:
authorGary Gregory <garydgregory@gmail.com>2020-06-12 11:35:37 -0400
committerGary Gregory <garydgregory@gmail.com>2020-06-12 11:35:37 -0400
commitd88f70e8ff3e73981809ddc530c19288c1f092c5 (patch)
tree4240a4084be3145171411403e2693f2f305d4c6f /src/test/java/org/apache/commons/lang3
parent53aa7a2e9a4d0e186139dc72ddc9709595c2183a (diff)
downloadapache-commons-lang-d88f70e8ff3e73981809ddc530c19288c1f092c5.tar.gz
[LANG-1568] FailableBooleanSupplier, FailableIntSupplier,
FailableLongSupplier, FailableDoubleSupplier.
Diffstat (limited to 'src/test/java/org/apache/commons/lang3')
-rw-r--r--src/test/java/org/apache/commons/lang3/FunctionsTest.java275
1 files changed, 224 insertions, 51 deletions
diff --git a/src/test/java/org/apache/commons/lang3/FunctionsTest.java b/src/test/java/org/apache/commons/lang3/FunctionsTest.java
index 4180829f4..63375cd01 100644
--- a/src/test/java/org/apache/commons/lang3/FunctionsTest.java
+++ b/src/test/java/org/apache/commons/lang3/FunctionsTest.java
@@ -16,14 +16,12 @@
*/
package org.apache.commons.lang3;
-import org.apache.commons.lang3.Functions.FailableBiConsumer;
-import org.apache.commons.lang3.Functions.FailableBiFunction;
-import org.apache.commons.lang3.Functions.FailableCallable;
-import org.apache.commons.lang3.Functions.FailableConsumer;
-import org.apache.commons.lang3.Functions.FailableFunction;
-import org.apache.commons.lang3.Functions.FailableSupplier;
-import org.junit.jupiter.api.DisplayName;
-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.assertNotNull;
+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 java.io.IOException;
import java.io.UncheckedIOException;
@@ -37,24 +35,29 @@ import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-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 org.apache.commons.lang3.Functions.FailableBiConsumer;
+import org.apache.commons.lang3.Functions.FailableBiFunction;
+import org.apache.commons.lang3.Functions.FailableCallable;
+import org.apache.commons.lang3.Functions.FailableConsumer;
+import org.apache.commons.lang3.Functions.FailableFunction;
+import org.apache.commons.lang3.Functions.FailableSupplier;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
class FunctionsTest {
+
public static class SomeException extends Exception {
+
private static final long serialVersionUID = -4965704778119283411L;
private Throwable t;
- SomeException(final String pMsg) {
- super(pMsg);
+ SomeException(final String message) {
+ super(message);
}
- public void setThrowable(final Throwable pThrowable) {
- t = pThrowable;
+ public void setThrowable(final Throwable throwable) {
+ t = throwable;
}
public void test() throws Throwable {
@@ -63,6 +66,7 @@ class FunctionsTest {
}
}
}
+
public static class Testable {
private Throwable t;
@@ -70,48 +74,99 @@ class FunctionsTest {
t = pTh;
}
- public void setThrowable(final Throwable pThrowable) {
- t = pThrowable;
+ public void setThrowable(final Throwable throwable) {
+ t = throwable;
}
public void test() throws Throwable {
test(t);
}
- public void test(final Throwable pThrowable) throws Throwable {
- if (pThrowable != null) {
- throw pThrowable;
+ public void test(final Throwable throwable) throws Throwable {
+ if (throwable != null) {
+ throw throwable;
+ }
+ }
+
+ public Integer testInteger() throws Throwable {
+ return testInteger(t);
+ }
+
+ public int testIntPrimitive() throws Throwable {
+ return testIntPrimitive(t);
+ }
+
+ public long testLongPrimitive() throws Throwable {
+ return testLongPrimitive(t);
+ }
+
+ public boolean testBooleanPrimitive() throws Throwable {
+ return testBooleanPrimitive(t);
+ }
+
+ public double testDoublePrimitive() throws Throwable {
+ return testDoublePrimitive(t);
+ }
+
+ public Integer testInteger(final Throwable throwable) throws Throwable {
+ if (throwable != null) {
+ throw throwable;
}
+ return 0;
}
- public Integer testInt() throws Throwable {
- return testInt(t);
+ public int testIntPrimitive(final Throwable throwable) throws Throwable {
+ if (throwable != null) {
+ throw throwable;
+ }
+ return 0;
}
- public Integer testInt(final Throwable pThrowable) throws Throwable {
- if (pThrowable != null) {
- throw pThrowable;
+ public double testDoublePrimitive(final Throwable throwable) throws Throwable {
+ if (throwable != null) {
+ throw throwable;
}
return 0;
}
+
+ public long testLongPrimitive(final Throwable throwable) throws Throwable {
+ if (throwable != null) {
+ throw throwable;
+ }
+ return 0;
+ }
+
+ public boolean testBooleanPrimitive(final Throwable throwable) throws Throwable {
+ if (throwable != null) {
+ throw throwable;
+ }
+ return false;
+ }
}
public static class FailureOnOddInvocations {
- private static int invocation;
+ private static int invocations;
private static void throwOnOdd() throws SomeException {
- final int i = ++invocation;
+ final int i = ++invocations;
if (i % 2 == 1) {
throw new SomeException("Odd Invocation: " + i);
}
}
+
static boolean failingBool() throws SomeException {
throwOnOdd();
return true;
}
+
FailureOnOddInvocations() throws SomeException {
throwOnOdd();
}
+
+ boolean getAsBoolean() throws SomeException {
+ throwOnOdd();
+ return true;
+ }
}
public static class CloseableObject {
@@ -138,20 +193,20 @@ class FunctionsTest {
@Test
void testRunnable() {
- FailureOnOddInvocations.invocation = 0;
+ FailureOnOddInvocations.invocations = 0;
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage());
- // Even invocation, should not throw an exception
+ // Even invocations, should not throw an exception
Functions.run(FailureOnOddInvocations::new);
}
@Test
void testAsRunnable() {
- FailureOnOddInvocations.invocation = 0;
+ FailureOnOddInvocations.invocations = 0;
final Runnable runnable = Functions.asRunnable(FailureOnOddInvocations::new);
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> runnable.run());
final Throwable cause = e.getCause();
@@ -159,13 +214,13 @@ class FunctionsTest {
assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage());
- // Even invocation, should not throw an exception
+ // Even invocations, should not throw an exception
runnable.run();
}
@Test
void testCallable() {
- FailureOnOddInvocations.invocation = 0;
+ FailureOnOddInvocations.invocations = 0;
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
@@ -177,7 +232,7 @@ class FunctionsTest {
@Test
void testAsCallable() {
- FailureOnOddInvocations.invocation = 0;
+ FailureOnOddInvocations.invocations = 0;
final FailableCallable<FailureOnOddInvocations, SomeException> failableCallable = FailureOnOddInvocations::new;
final Callable<FailureOnOddInvocations> callable = Functions.asCallable(failableCallable);
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> callable.call());
@@ -292,23 +347,23 @@ class FunctionsTest {
public void testApplyFunction() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
- Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInt, testable));
+ Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInteger, testable));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
- e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInt, testable));
+ e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInteger, testable));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
- e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInt, testable));
+ e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
testable.setThrowable(null);
- final Integer i = Functions.apply(Testable::testInt, testable);
+ final Integer i = Functions.apply(Testable::testInteger, testable);
assertNotNull(i);
assertEquals(0, i.intValue());
}
@@ -319,7 +374,7 @@ class FunctionsTest {
final Testable testable = new Testable(ise);
final FailableFunction<Throwable, Integer, Throwable> failableFunction = th -> {
testable.setThrowable(th);
- return Integer.valueOf(testable.testInt());
+ return Integer.valueOf(testable.testInteger());
};
final Function<Throwable, Integer> function = Functions.asFunction(failableFunction);
Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ise));
@@ -344,20 +399,20 @@ class FunctionsTest {
public void testApplyBiFunction() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(null);
- Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInt, testable, ise));
+ Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInteger, testable, ise));
assertSame(ise, e);
final Error error = new OutOfMemoryError();
- e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInt, testable, error));
+ e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInteger, testable, error));
assertSame(error, e);
final IOException ioe = new IOException("Unknown I/O error");
- e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInt, testable, ioe));
+ e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable, ioe));
final Throwable t = e.getCause();
assertNotNull(t);
assertSame(ioe, t);
- final Integer i = Functions.apply(Testable::testInt, testable, (Throwable) null);
+ final Integer i = Functions.apply(Testable::testInteger, testable, (Throwable) null);
assertNotNull(i);
assertEquals(0, i.intValue());
}
@@ -368,7 +423,7 @@ class FunctionsTest {
final Testable testable = new Testable(ise);
final FailableBiFunction<Testable, Throwable, Integer, Throwable> failableBiFunction = (t, th) -> {
t.setThrowable(th);
- return Integer.valueOf(t.testInt());
+ return Integer.valueOf(t.testInteger());
};
final BiFunction<Testable, Throwable, Integer> biFunction = Functions.asBiFunction(failableBiFunction);
Throwable e = assertThrows(IllegalStateException.class, () -> biFunction.apply(testable, ise));
@@ -391,7 +446,7 @@ class FunctionsTest {
@Test
public void testGetFromSupplier() {
- FailureOnOddInvocations.invocation = 0;
+ FailureOnOddInvocations.invocations = 0;
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
@@ -402,9 +457,128 @@ class FunctionsTest {
}
@Test
+ public void testGetSupplier() {
+ final IllegalStateException ise = new IllegalStateException();
+ final Testable testable = new Testable(ise);
+ Throwable e = assertThrows(IllegalStateException.class, () -> Functions.get(testable::testInteger));
+ assertSame(ise, e);
+
+ final Error error = new OutOfMemoryError();
+ testable.setThrowable(error);
+ e = assertThrows(OutOfMemoryError.class, () -> Functions.get(testable::testInteger));
+ assertSame(error, e);
+
+ final IOException ioe = new IOException("Unknown I/O error");
+ testable.setThrowable(ioe);
+ e = assertThrows(UncheckedIOException.class, () -> Functions.get(testable::testInteger));
+ final Throwable t = e.getCause();
+ assertNotNull(t);
+ assertSame(ioe, t);
+
+ testable.setThrowable(null);
+ final Integer i = Functions.apply(Testable::testInteger, testable);
+ assertNotNull(i);
+ assertEquals(0, i.intValue());
+ }
+
+ @Test
+ public void testGetAsBooleanSupplier() {
+ final IllegalStateException ise = new IllegalStateException();
+ final Testable testable = new Testable(ise);
+ Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsBoolean(testable::testBooleanPrimitive));
+ assertSame(ise, e);
+
+ final Error error = new OutOfMemoryError();
+ testable.setThrowable(error);
+ e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsBoolean(testable::testBooleanPrimitive));
+ assertSame(error, e);
+
+ final IOException ioe = new IOException("Unknown I/O error");
+ testable.setThrowable(ioe);
+ e = assertThrows(UncheckedIOException.class, () -> Functions.getAsBoolean(testable::testBooleanPrimitive));
+ final Throwable t = e.getCause();
+ assertNotNull(t);
+ assertSame(ioe, t);
+
+ testable.setThrowable(null);
+ assertFalse(Functions.getAsBoolean(testable::testBooleanPrimitive));
+ }
+
+ @Test
+ public void testGetAsDoubleSupplier() {
+ final IllegalStateException ise = new IllegalStateException();
+ final Testable testable = new Testable(ise);
+ Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsDouble(testable::testDoublePrimitive));
+ assertSame(ise, e);
+
+ final Error error = new OutOfMemoryError();
+ testable.setThrowable(error);
+ e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsDouble(testable::testDoublePrimitive));
+ assertSame(error, e);
+
+ final IOException ioe = new IOException("Unknown I/O error");
+ testable.setThrowable(ioe);
+ e = assertThrows(UncheckedIOException.class, () -> Functions.getAsDouble(testable::testDoublePrimitive));
+ final Throwable t = e.getCause();
+ assertNotNull(t);
+ assertSame(ioe, t);
+
+ testable.setThrowable(null);
+ assertEquals(0, Functions.getAsDouble(testable::testDoublePrimitive));
+ }
+
+ @Test
+ public void testGetAsIntSupplier() {
+ final IllegalStateException ise = new IllegalStateException();
+ final Testable testable = new Testable(ise);
+ Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsInt(testable::testIntPrimitive));
+ assertSame(ise, e);
+
+ final Error error = new OutOfMemoryError();
+ testable.setThrowable(error);
+ e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsInt(testable::testIntPrimitive));
+ assertSame(error, e);
+
+ final IOException ioe = new IOException("Unknown I/O error");
+ testable.setThrowable(ioe);
+ e = assertThrows(UncheckedIOException.class, () -> Functions.getAsInt(testable::testIntPrimitive));
+ final Throwable t = e.getCause();
+ assertNotNull(t);
+ assertSame(ioe, t);
+
+ testable.setThrowable(null);
+ final int i = Functions.getAsInt(testable::testInteger);
+ assertEquals(0, i);
+ }
+
+ @Test
+ public void testGetAsLongSupplier() {
+ final IllegalStateException ise = new IllegalStateException();
+ final Testable testable = new Testable(ise);
+ Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsLong(testable::testLongPrimitive));
+ assertSame(ise, e);
+
+ final Error error = new OutOfMemoryError();
+ testable.setThrowable(error);
+ e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsLong(testable::testLongPrimitive));
+ assertSame(error, e);
+
+ final IOException ioe = new IOException("Unknown I/O error");
+ testable.setThrowable(ioe);
+ e = assertThrows(UncheckedIOException.class, () -> Functions.getAsLong(testable::testLongPrimitive));
+ final Throwable t = e.getCause();
+ assertNotNull(t);
+ assertSame(ioe, t);
+
+ testable.setThrowable(null);
+ final long i = Functions.getAsLong(testable::testLongPrimitive);
+ assertEquals(0, i);
+ }
+
+ @Test
@DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
public void testAsPredicate() {
- FailureOnOddInvocations.invocation = 0;
+ FailureOnOddInvocations.invocations = 0;
final Functions.FailablePredicate<Object, Throwable> failablePredicate = t -> FailureOnOddInvocations.failingBool();
final Predicate<?> predicate = Functions.asPredicate(failablePredicate);
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null));
@@ -419,7 +593,7 @@ class FunctionsTest {
@Test
@DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ")
public void testAsBiPredicate() {
- FailureOnOddInvocations.invocation = 0;
+ FailureOnOddInvocations.invocations = 0;
final Functions.FailableBiPredicate<Object, Object, Throwable> failableBiPredicate = (t1, t2) -> FailureOnOddInvocations.failingBool();
final BiPredicate<?, ?> predicate = Functions.asBiPredicate(failableBiPredicate);
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null, null));
@@ -433,7 +607,7 @@ class FunctionsTest {
@Test
public void testAsSupplier() {
- FailureOnOddInvocations.invocation = 0;
+ FailureOnOddInvocations.invocations = 0;
final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = FailureOnOddInvocations::new;
final Supplier<FailureOnOddInvocations> supplier = Functions.asSupplier(failableSupplier);
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> supplier.get());
@@ -441,8 +615,7 @@ class FunctionsTest {
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage());
- final FailureOnOddInvocations instance = supplier.get();
- assertNotNull(instance);
+ assertNotNull(supplier.get());
}
@Test