diff options
author | Gary Gregory <garydgregory@gmail.com> | 2020-06-12 11:36:13 -0400 |
---|---|---|
committer | Gary Gregory <garydgregory@gmail.com> | 2020-06-12 11:36:13 -0400 |
commit | 540ec09e25a3ec60fbe0a77dfc491475229caba7 (patch) | |
tree | 339a24bd5e4747e9371f710152b14fbbd0cd465b /src/test/java/org/apache | |
parent | d88f70e8ff3e73981809ddc530c19288c1f092c5 (diff) | |
download | apache-commons-lang-540ec09e25a3ec60fbe0a77dfc491475229caba7.tar.gz |
Sort members.
Diffstat (limited to 'src/test/java/org/apache')
-rw-r--r-- | src/test/java/org/apache/commons/lang3/FunctionsTest.java | 458 |
1 files changed, 229 insertions, 229 deletions
diff --git a/src/test/java/org/apache/commons/lang3/FunctionsTest.java b/src/test/java/org/apache/commons/lang3/FunctionsTest.java index 63375cd01..c9e6acfda 100644 --- a/src/test/java/org/apache/commons/lang3/FunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/FunctionsTest.java @@ -46,6 +46,53 @@ import org.junit.jupiter.api.Test; class FunctionsTest { + public static class CloseableObject { + private boolean closed; + + public void close() { + closed = true; + } + + public boolean isClosed() { + return closed; + } + + public void reset() { + closed = false; + } + + public void run(final Throwable pTh) throws Throwable { + if (pTh != null) { + throw pTh; + } + } + } + + public static class FailureOnOddInvocations { + private static int invocations; + + static boolean failingBool() throws SomeException { + throwOnOdd(); + return true; + } + + private static void throwOnOdd() throws SomeException { + final int i = ++invocations; + if (i % 2 == 1) { + throw new SomeException("Odd Invocation: " + i); + } + } + + FailureOnOddInvocations() throws SomeException { + throwOnOdd(); + } + + boolean getAsBoolean() throws SomeException { + throwOnOdd(); + return true; + } + } + public static class SomeException extends Exception { private static final long serialVersionUID = -4965704778119283411L; @@ -88,38 +135,19 @@ class FunctionsTest { } } - 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 { + public boolean testBooleanPrimitive(final Throwable throwable) throws Throwable { if (throwable != null) { throw throwable; } - return 0; + return false; } - public int testIntPrimitive(final Throwable throwable) throws Throwable { - if (throwable != null) { - throw throwable; - } - return 0; + public double testDoublePrimitive() throws Throwable { + return testDoublePrimitive(t); } public double testDoublePrimitive(final Throwable throwable) throws Throwable { @@ -129,124 +157,60 @@ class FunctionsTest { return 0; } - public long testLongPrimitive(final Throwable throwable) throws Throwable { - if (throwable != null) { - throw throwable; - } - return 0; + public Integer testInteger() throws Throwable { + return testInteger(t); } - public boolean testBooleanPrimitive(final Throwable throwable) throws Throwable { + public Integer testInteger(final Throwable throwable) throws Throwable { if (throwable != null) { throw throwable; } - return false; - } - } - - public static class FailureOnOddInvocations { - private static int invocations; - - private static void throwOnOdd() throws SomeException { - 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(); + return 0; } - boolean getAsBoolean() throws SomeException { - throwOnOdd(); - return true; + public int testIntPrimitive() throws Throwable { + return testIntPrimitive(t); } - } - public static class CloseableObject { - private boolean closed; - - public void run(final Throwable pTh) throws Throwable { - if (pTh != null) { - throw pTh; + public int testIntPrimitive(final Throwable throwable) throws Throwable { + if (throwable != null) { + throw throwable; } + return 0; } - public void reset() { - closed = false; - } - - public void close() { - closed = true; + public long testLongPrimitive() throws Throwable { + return testLongPrimitive(t); } - public boolean isClosed() { - return closed; + public long testLongPrimitive(final Throwable throwable) throws Throwable { + if (throwable != null) { + throw throwable; + } + return 0; } } @Test - void testRunnable() { - 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 invocations, should not throw an exception - Functions.run(FailureOnOddInvocations::new); - } - - @Test - void testAsRunnable() { - FailureOnOddInvocations.invocations = 0; - final Runnable runnable = Functions.asRunnable(FailureOnOddInvocations::new); - final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> runnable.run()); - final Throwable cause = e.getCause(); - assertNotNull(cause); - assertTrue(cause instanceof SomeException); - assertEquals("Odd Invocation: 1", cause.getMessage()); + void testAcceptBiConsumer() { + final IllegalStateException ise = new IllegalStateException(); + final Testable testable = new Testable(null); + Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(Testable::test, testable, ise)); + assertSame(ise, e); - // Even invocations, should not throw an exception - runnable.run(); - } + final Error error = new OutOfMemoryError(); + e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(Testable::test, testable, error)); + assertSame(error, e); - @Test - void testCallable() { - 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()); - final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new); - assertNotNull(instance); - } + final IOException ioe = new IOException("Unknown I/O error"); + testable.setThrowable(ioe); + e = assertThrows(UncheckedIOException.class, () -> Functions.accept(Testable::test, testable, ioe)); + final Throwable t = e.getCause(); + assertNotNull(t); + assertSame(ioe, t); - @Test - void testAsCallable() { - 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()); - final Throwable cause = e.getCause(); - assertNotNull(cause); - assertTrue(cause instanceof SomeException); - assertEquals("Odd Invocation: 1", cause.getMessage()); - final FailureOnOddInvocations instance; - try { - instance = callable.call(); - } catch (final Exception ex) { - throw Functions.rethrow(ex); - } - assertNotNull(instance); + testable.setThrowable(null); + Functions.accept(Testable::test, testable, (Throwable) null); } @Test @@ -273,49 +237,50 @@ class FunctionsTest { } @Test - void testAsConsumer() { + public void testApplyBiFunction() { final IllegalStateException ise = new IllegalStateException(); - final Testable testable = new Testable(ise); - final Consumer<Testable> consumer = Functions.asConsumer(Testable::test); - Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable)); + final Testable testable = new Testable(null); + Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInteger, testable, ise)); assertSame(ise, e); final Error error = new OutOfMemoryError(); - testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> consumer.accept(testable)); + e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInteger, testable, error)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); - testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> consumer.accept(testable)); + e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable, ioe)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); - testable.setThrowable(null); - Functions.accept(Testable::test, testable); + final Integer i = Functions.apply(Testable::testInteger, testable, (Throwable) null); + assertNotNull(i); + assertEquals(0, i.intValue()); } @Test - void testAcceptBiConsumer() { + public void testApplyFunction() { final IllegalStateException ise = new IllegalStateException(); - final Testable testable = new Testable(null); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(Testable::test, testable, ise)); + final Testable testable = new Testable(ise); + Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInteger, testable)); assertSame(ise, e); final Error error = new OutOfMemoryError(); - e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(Testable::test, testable, error)); + testable.setThrowable(error); + 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.accept(Testable::test, testable, ioe)); + e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); testable.setThrowable(null); - Functions.accept(Testable::test, testable, (Throwable) null); + final Integer i = Functions.apply(Testable::testInteger, testable); + assertNotNull(i); + assertEquals(0, i.intValue()); } @Test @@ -344,141 +309,169 @@ class FunctionsTest { } @Test - public void testApplyFunction() { + public void testAsBiFunction() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable(ise); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInteger, testable)); + final FailableBiFunction<Testable, Throwable, Integer, Throwable> failableBiFunction = (t, th) -> { + t.setThrowable(th); + return Integer.valueOf(t.testInteger()); + }; + final BiFunction<Testable, Throwable, Integer> biFunction = Functions.asBiFunction(failableBiFunction); + Throwable e = assertThrows(IllegalStateException.class, () -> biFunction.apply(testable, ise)); assertSame(ise, e); final Error error = new OutOfMemoryError(); testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInteger, testable)); + e = assertThrows(OutOfMemoryError.class, () -> biFunction.apply(testable, error)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable)); + e = assertThrows(UncheckedIOException.class, () -> biFunction.apply(testable, ioe)); 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()); + assertEquals(0, biFunction.apply(testable, null).intValue()); } @Test - public void testAsFunction() { - final IllegalStateException ise = new IllegalStateException(); - final Testable testable = new Testable(ise); - final FailableFunction<Throwable, Integer, Throwable> failableFunction = th -> { - testable.setThrowable(th); - return Integer.valueOf(testable.testInteger()); - }; - final Function<Throwable, Integer> function = Functions.asFunction(failableFunction); - Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ise)); - assertSame(ise, e); - - final Error error = new OutOfMemoryError(); - testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> function.apply(error)); - assertSame(error, e); - - final IOException ioe = new IOException("Unknown I/O error"); - testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> function.apply(ioe)); - final Throwable t = e.getCause(); - assertNotNull(t); - assertSame(ioe, t); + @DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ") + public void testAsBiPredicate() { + 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)); + final Throwable cause = e.getCause(); + assertNotNull(cause); + assertTrue(cause instanceof SomeException); + assertEquals("Odd Invocation: 1", cause.getMessage()); + final boolean instance = predicate.test(null, null); + assertNotNull(instance); + } - assertEquals(0, function.apply(null).intValue()); + @Test + void testAsCallable() { + 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()); + final Throwable cause = e.getCause(); + assertNotNull(cause); + assertTrue(cause instanceof SomeException); + assertEquals("Odd Invocation: 1", cause.getMessage()); + final FailureOnOddInvocations instance; + try { + instance = callable.call(); + } catch (final Exception ex) { + throw Functions.rethrow(ex); + } + assertNotNull(instance); } @Test - public void testApplyBiFunction() { + void testAsConsumer() { final IllegalStateException ise = new IllegalStateException(); - final Testable testable = new Testable(null); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInteger, testable, ise)); + final Testable testable = new Testable(ise); + final Consumer<Testable> consumer = Functions.asConsumer(Testable::test); + Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable)); assertSame(ise, e); final Error error = new OutOfMemoryError(); - e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInteger, testable, error)); + testable.setThrowable(error); + e = assertThrows(OutOfMemoryError.class, () -> consumer.accept(testable)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); - e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable, ioe)); + testable.setThrowable(ioe); + e = assertThrows(UncheckedIOException.class, () -> consumer.accept(testable)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); - final Integer i = Functions.apply(Testable::testInteger, testable, (Throwable) null); - assertNotNull(i); - assertEquals(0, i.intValue()); + testable.setThrowable(null); + Functions.accept(Testable::test, testable); } @Test - public void testAsBiFunction() { + public void testAsFunction() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable(ise); - final FailableBiFunction<Testable, Throwable, Integer, Throwable> failableBiFunction = (t, th) -> { - t.setThrowable(th); - return Integer.valueOf(t.testInteger()); + final FailableFunction<Throwable, Integer, Throwable> failableFunction = th -> { + testable.setThrowable(th); + return Integer.valueOf(testable.testInteger()); }; - final BiFunction<Testable, Throwable, Integer> biFunction = Functions.asBiFunction(failableBiFunction); - Throwable e = assertThrows(IllegalStateException.class, () -> biFunction.apply(testable, ise)); + final Function<Throwable, Integer> function = Functions.asFunction(failableFunction); + Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ise)); assertSame(ise, e); final Error error = new OutOfMemoryError(); testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> biFunction.apply(testable, error)); + e = assertThrows(OutOfMemoryError.class, () -> function.apply(error)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> biFunction.apply(testable, ioe)); + e = assertThrows(UncheckedIOException.class, () -> function.apply(ioe)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); - assertEquals(0, biFunction.apply(testable, null).intValue()); + assertEquals(0, function.apply(null).intValue()); } @Test - public void testGetFromSupplier() { + @DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ") + public void testAsPredicate() { FailureOnOddInvocations.invocations = 0; - final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new)); + final Functions.FailablePredicate<Object, Throwable> failablePredicate = t -> FailureOnOddInvocations.failingBool(); + final Predicate<?> predicate = Functions.asPredicate(failablePredicate); + final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null)); final Throwable cause = e.getCause(); assertNotNull(cause); assertTrue(cause instanceof SomeException); assertEquals("Odd Invocation: 1", cause.getMessage()); - final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new); + final boolean instance = predicate.test(null); assertNotNull(instance); } @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); + void testAsRunnable() { + FailureOnOddInvocations.invocations = 0; + final Runnable runnable = Functions.asRunnable(FailureOnOddInvocations::new); + final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> runnable.run()); + final Throwable cause = e.getCause(); + assertNotNull(cause); + assertTrue(cause instanceof SomeException); + assertEquals("Odd Invocation: 1", cause.getMessage()); - final Error error = new OutOfMemoryError(); - testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> Functions.get(testable::testInteger)); - assertSame(error, e); + // Even invocations, should not throw an exception + runnable.run(); + } - 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); + @Test + public void testAsSupplier() { + 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()); + final Throwable cause = e.getCause(); + assertNotNull(cause); + assertTrue(cause instanceof SomeException); + assertEquals("Odd Invocation: 1", cause.getMessage()); + assertNotNull(supplier.get()); + } - testable.setThrowable(null); - final Integer i = Functions.apply(Testable::testInteger, testable); - assertNotNull(i); - assertEquals(0, i.intValue()); + @Test + void testCallable() { + 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()); + final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new); + assertNotNull(instance); } @Test @@ -576,46 +569,53 @@ class FunctionsTest { } @Test - @DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ") - public void testAsPredicate() { + public void testGetFromSupplier() { 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)); + 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()); - final boolean instance = predicate.test(null); + final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new); assertNotNull(instance); } @Test - @DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ") - public void testAsBiPredicate() { - 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)); - final Throwable cause = e.getCause(); - assertNotNull(cause); - assertTrue(cause instanceof SomeException); - assertEquals("Odd Invocation: 1", cause.getMessage()); - final boolean instance = predicate.test(null, null); - assertNotNull(instance); + 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 testAsSupplier() { + void testRunnable() { 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()); + 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()); - assertNotNull(supplier.get()); + + // Even invocations, should not throw an exception + Functions.run(FailureOnOddInvocations::new); } @Test |