Merge remote-tracking branch 'origin/pr/403'

This commit is contained in:
pascalschumacher 2019-02-08 19:41:40 +01:00
commit 43badd4dfa
2 changed files with 575 additions and 455 deletions

View File

@ -28,7 +28,7 @@ import java.lang.reflect.UndeclaredThrowableException;
* not to throw Exceptions, at least not checked Exceptions, aka instances of
* {@link Exception}. This enforces the use of constructs like
* <pre>
* Consumer<java.lang.reflect.Method> consumer = (m) -> {
* Consumer&lt;java.lang.reflect.Method&gt; consumer = (m) -&gt; {
* try {
* m.invoke(o, args);
* } catch (Throwable t) {
@ -36,216 +36,333 @@ import java.lang.reflect.UndeclaredThrowableException;
* }
* };
* </pre>
* By replacing a {@link Consumer Consumer<O>} with a
* {@link FailableConsumer FailableConsumer<O,? extends Throwable}, this can be
* By replacing a {@link java.util.function.Consumer Consumer&lt;O&gt;} with a
* {@link FailableConsumer FailableConsumer&lt;O,? extends Throwable&gt;}, this can be
* written like follows:
* <pre>
* Functions.accept((m) -> m.invoke(o,args));
* Functions.accept((m) -&gt; m.invoke(o,args));
* </pre>
* Obviously, the second version is much more concise and the spirit of
* Lambda expressions is met better than the second version.
*/
public class Functions {
@FunctionalInterface
public interface FailableRunnable<T extends Throwable> {
public void run() throws T;
}
@FunctionalInterface
public interface FailableCallable<O,T extends Throwable> {
public O call() throws T;
}
@FunctionalInterface
public interface FailableConsumer<O,T extends Throwable> {
public void accept(O pObject) throws T;
}
@FunctionalInterface
public interface FailableBiConsumer<O1,O2,T extends Throwable> {
public void accept(O1 pObject1, O2 pObject2) throws T;
}
@FunctionalInterface
public interface FailableFunction<I,O,T extends Throwable> {
public O apply(I pInput) throws T;
}
@FunctionalInterface
public interface FailableBiFunction<I1,I2,O,T extends Throwable> {
public O apply(I1 pInput1, I2 pInput2) throws T;
}
@FunctionalInterface
public interface FailablePredicate<O,T extends Throwable> {
public boolean test(O pObject) throws T;
}
@FunctionalInterface
public interface FailableBiPredicate<O1,O2,T extends Throwable> {
public boolean test(O1 pObject1, O2 pObject2) throws T;
}
@FunctionalInterface
public interface FailableRunnable<T extends Throwable> {
/**
* Runs the function.
* @throws T if the function fails
*/
void run() throws T;
}
@FunctionalInterface
public interface FailableCallable<O, T extends Throwable> {
/**
* Calls the callable.
* @return The value returned from the callable
* @throws T if the callable fails
*/
O call() throws T;
}
@FunctionalInterface
public interface FailableConsumer<O, T extends Throwable> {
/**
* Accepts the consumer.
* @param pObject the parameter for the consumable to accept
* @throws T if the consumer fails
*/
void accept(O pObject) throws T;
}
@FunctionalInterface
public interface FailableBiConsumer<O1, O2, T extends Throwable> {
/**
* Accepts the consumer.
* @param pObject1 the first parameter for the consumable to accept
* @param pObject2 the second parameter for the consumable to accept
* @throws T if the consumer fails
*/
void accept(O1 pObject1, O2 pObject2) throws T;
}
@FunctionalInterface
public interface FailableFunction<I, O, T extends Throwable> {
/**
* Apply the function.
* @param pInput the input for the function
* @return the result of the function
* @throws T if the function fails
*/
O apply(I pInput) throws T;
}
@FunctionalInterface
public interface FailableBiFunction<I1, I2, O, T extends Throwable> {
/**
* Apply the function.
* @param pInput1 the first input for the function
* @param pInput2 the second input for the function
* @return the result of the function
* @throws T if the function fails
*/
O apply(I1 pInput1, I2 pInput2) throws T;
}
@FunctionalInterface
public interface FailablePredicate<O, T extends Throwable> {
/**
* Test the predicate.
* @param pObject the object to test the predicate on
* @return the predicate's evaluation
* @throws T if the predicate fails
*/
boolean test(O pObject) throws T;
}
@FunctionalInterface
public interface FailableBiPredicate<O1, O2, T extends Throwable> {
/**
* Test the predicate.
* @param pObject1 the first object to test the predicate on
* @param pObject2 the second object to test the predicate on
* @return the predicate's evaluation
* @throws T if the predicate fails
*/
boolean test(O1 pObject1, O2 pObject2) throws T;
}
public static <T extends Throwable> void run(FailableRunnable<T> pRunnable) {
try {
pRunnable.run();
} catch (Throwable t) {
throw rethrow(t);
}
}
/**
* Runs a runnable and rethrows any exception as a {@link RuntimeException}.
* @param pRunnable The runnable to run
* @param <T> the type of checked exception the runnable may throw
*/
public static <T extends Throwable> void run(FailableRunnable<T> pRunnable) {
try {
pRunnable.run();
} catch (Throwable t) {
throw rethrow(t);
}
}
public static <O,T extends Throwable> O call(FailableCallable<O,T> pCallable) {
try {
return pCallable.call();
} catch (Throwable t) {
throw rethrow(t);
}
}
/**
* Calls a callable and rethrows any exception as a {@link RuntimeException}.
* @param pCallable the callable to call
* @param <O> the return type of the callable
* @param <T> the type of checked exception the callable may throw
* @return the value returned from the callable
*/
public static <O, T extends Throwable> O call(FailableCallable<O, T> pCallable) {
try {
return pCallable.call();
} catch (Throwable t) {
throw rethrow(t);
}
}
public static <O,T extends Throwable> void accept(FailableConsumer<O,T> pConsumer, O pObject) {
try {
pConsumer.accept(pObject);
} catch (Throwable t) {
throw rethrow(t);
}
}
/**
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
* @param pConsumer the consumer to consume
* @param pObject the object to consume by <code>pConsumer</code>
* @param <O> the type the consumer accepts
* @param <T> the type of checked exception the consumer may throw
*/
public static <O, T extends Throwable> void accept(FailableConsumer<O, T> pConsumer, O pObject) {
try {
pConsumer.accept(pObject);
} catch (Throwable t) {
throw rethrow(t);
}
}
public static <O1,O2,T extends Throwable> void accept(FailableBiConsumer<O1,O2,T> pConsumer, O1 pObject1, O2 pObject2) {
try {
pConsumer.accept(pObject1, pObject2);
} catch (Throwable t) {
throw rethrow(t);
}
}
/**
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
* @param pConsumer the consumer to consume
* @param pObject1 the first object to consume by <code>pConsumer</code>
* @param pObject2 the second object to consume by <code>pConsumer</code>
* @param <O1> the type of the first argument the consumer accepts
* @param <O2> the type of the second argument the consumer accepts
* @param <T> the type of checked exception the consumer may throw
*/
public static <O1, O2, T extends Throwable> void accept(FailableBiConsumer<O1, O2, T> pConsumer, O1 pObject1, O2 pObject2) {
try {
pConsumer.accept(pObject1, pObject2);
} catch (Throwable t) {
throw rethrow(t);
}
}
public static <I,O,T extends Throwable> O apply(FailableFunction<I,O,T> pFunction, I pInput) {
try {
return pFunction.apply(pInput);
} catch (Throwable t) {
throw rethrow(t);
}
}
/**
* Applies a function and rethrows any exception as a {@link RuntimeException}.
* @param pFunction the function to apply
* @param pInput the input to apply <code>pFunction</code> on
* @param <I> the type of the argument the function accepts
* @param <O> the return type of the function
* @param <T> the type of checked exception the function may throw
* @return the value returned from the function
*/
public static <I, O, T extends Throwable> O apply(FailableFunction<I, O, T> pFunction, I pInput) {
try {
return pFunction.apply(pInput);
} catch (Throwable t) {
throw rethrow(t);
}
}
public static <I1,I2,O,T extends Throwable> O apply(FailableBiFunction<I1,I2,O,T> pFunction, I1 pInput1, I2 pInput2) {
try {
return pFunction.apply(pInput1, pInput2);
} catch (Throwable t) {
throw rethrow(t);
}
}
/**
* Applies a function and rethrows any exception as a {@link RuntimeException}.
* @param pFunction the function to apply
* @param pInput1 the first input to apply <code>pFunction</code> on
* @param pInput2 the second input to apply <code>pFunction</code> on
* @param <I1> the type of the first argument the function accepts
* @param <I2> the type of the second argument the function accepts
* @param <O> the return type of the function
* @param <T> the type of checked exception the function may throw
* @return the value returned from the function
*/
public static <I1, I2, O, T extends Throwable> O apply(FailableBiFunction<I1, I2, O, T> pFunction, I1 pInput1, I2 pInput2) {
try {
return pFunction.apply(pInput1, pInput2);
} catch (Throwable t) {
throw rethrow(t);
}
}
public static <O,T extends Throwable> boolean test(FailablePredicate<O,T> pPredicate, O pObject) {
try {
return pPredicate.test(pObject);
} catch (Throwable t) {
throw rethrow(t);
}
}
/**
* Tests a predicate and rethrows any exception as a {@link RuntimeException}.
* @param pPredicate the predicate to test
* @param pObject the input to test by <code>pPredicate</code>
* @param <O> the type of argument the predicate tests
* @param <T> the type of checked exception the predicate may throw
* @return the boolean value returned by the predicate
*/
public static <O, T extends Throwable> boolean test(FailablePredicate<O, T> pPredicate, O pObject) {
try {
return pPredicate.test(pObject);
} catch (Throwable t) {
throw rethrow(t);
}
}
public static <O1,O2,T extends Throwable> boolean test(FailableBiPredicate<O1,O2,T> pPredicate, O1 pObject1, O2 pObject2) {
try {
return pPredicate.test(pObject1, pObject2);
} catch (Throwable t) {
throw rethrow(t);
}
}
/**
* Tests a predicate and rethrows any exception as a {@link RuntimeException}.
* @param pPredicate the predicate to test
* @param pObject1 the first input to test by <code>pPredicate</code>
* @param pObject2 the second input to test by <code>pPredicate</code>
* @param <O1> the type of the first argument the predicate tests
* @param <O2> the type of the second argument the predicate tests
* @param <T> the type of checked exception the predicate may throw
* @return the boolean value returned by the predicate
*/
public static <O1, O2, T extends Throwable> boolean test(FailableBiPredicate<O1, O2, T> pPredicate, O1 pObject1, O2 pObject2) {
try {
return pPredicate.test(pObject1, pObject2);
} catch (Throwable t) {
throw rethrow(t);
}
}
/**
* A simple try-with-resources implementation, that can be used, if your
* objects do not implement the {@link AutoCloseable} interface. The method
* executes the {@code pAction}. The method guarantees, that <em>all</em>
* the {@code pResources} are being executed, in the given order, afterwards,
* and regardless of success, or failure. If either the original action, or
* any of the resource action fails, then the <em>first</em> failure (aka
* {@link Throwable} is rethrown. Example use:
* <pre>
* final FileInputStream fis = new FileInputStream("my.file");
* Functions.tryWithResources(useInputStream(fis), null, () -> fis.close());
* </pre>
* @param pAction The action to execute. This object <em>will</em> always
* be invoked.
* @param pErrorHandler An optional error handler, which will be invoked finally,
* if any error occurred. The error handler will receive the first
* error, aka {@link Throwable}.
* @param pResources The resource actions to execute. <em>All</em> resource
* actions will be invoked, in the given order. A resource action is an
* instance of {@link FailableRunnable}, which will be executed.
* @see #tryWithResources(FailableRunnable, FailableRunnable...)
*/
@SafeVarargs
public static <O> void tryWithResources(FailableRunnable<? extends Throwable> pAction,
FailableConsumer<Throwable,? extends Throwable> pErrorHandler,
FailableRunnable<? extends Throwable>... pResources) {
final FailableConsumer<Throwable,? extends Throwable> errorHandler;
if (pErrorHandler == null) {
errorHandler = (t) -> rethrow(t);
} else {
errorHandler = pErrorHandler;
}
if (pResources != null) {
for (FailableRunnable<? extends Throwable> runnable : pResources) {
if (runnable == null) {
throw new NullPointerException("A resource action must not be null.");
}
}
}
Throwable th = null;
try {
pAction.run();
} catch (Throwable t) {
th = t;
}
if (pResources != null) {
for (FailableRunnable<? extends Object> runnable : pResources) {
try {
runnable.run();
} catch (Throwable t) {
if (th == null) {
th = t;
}
}
}
}
if (th != null) {
try {
errorHandler.accept(th);
} catch (Throwable t) {
throw rethrow(t);
}
}
}
/**
* A simple try-with-resources implementation, that can be used, if your
* objects do not implement the {@link AutoCloseable} interface. The method
* executes the {@code pAction}. The method guarantees, that <em>all</em>
* the {@code pResources} are being executed, in the given order, afterwards,
* and regardless of success, or failure. If either the original action, or
* any of the resource action fails, then the <em>first</em> failure (aka
* {@link Throwable} is rethrown. Example use:
* <pre>
* final FileInputStream fis = new FileInputStream("my.file");
* Functions.tryWithResources(useInputStream(fis), null, () -&gt; fis.close());
* </pre>
* @param pAction The action to execute. This object <em>will</em> always
* be invoked.
* @param pErrorHandler An optional error handler, which will be invoked finally,
* if any error occurred. The error handler will receive the first
* error, aka {@link Throwable}.
* @param pResources The resource actions to execute. <em>All</em> resource
* actions will be invoked, in the given order. A resource action is an
* instance of {@link FailableRunnable}, which will be executed.
* @see #tryWithResources(FailableRunnable, FailableRunnable...)
*/
@SafeVarargs
public static void tryWithResources(FailableRunnable<? extends Throwable> pAction,
FailableConsumer<Throwable, ? extends Throwable> pErrorHandler,
FailableRunnable<? extends Throwable>... pResources) {
final FailableConsumer<Throwable, ? extends Throwable> errorHandler;
if (pErrorHandler == null) {
errorHandler = (t) -> rethrow(t);
} else {
errorHandler = pErrorHandler;
}
if (pResources != null) {
for (FailableRunnable<? extends Throwable> runnable : pResources) {
if (runnable == null) {
throw new NullPointerException("A resource action must not be null.");
}
}
}
Throwable th = null;
try {
pAction.run();
} catch (Throwable t) {
th = t;
}
if (pResources != null) {
for (FailableRunnable<? extends Object> runnable : pResources) {
try {
runnable.run();
} catch (Throwable t) {
if (th == null) {
th = t;
}
}
}
}
if (th != null) {
try {
errorHandler.accept(th);
} catch (Throwable t) {
throw rethrow(t);
}
}
}
/**
* A simple try-with-resources implementation, that can be used, if your
* objects do not implement the {@link AutoCloseable} interface. The method
* executes the {@code pAction}. The method guarantees, that <em>all</em>
* the {@code pResources} are being executed, in the given order, afterwards,
* and regardless of success, or failure. If either the original action, or
* any of the resource action fails, then the <em>first</em> failure (aka
* {@link Throwable} is rethrown. Example use:
* <pre>
* final FileInputStream fis = new FileInputStream("my.file");
* Functions.tryWithResources(useInputStream(fis), () -> fis.close());
* </pre>
* @param pAction The action to execute. This object <em>will</em> always
* be invoked.
* @param pResources The resource actions to execute. <em>All</em> resource
* actions will be invoked, in the given order. A resource action is an
* instance of {@link FailableRunnable}, which will be executed.
* @see #tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
*/
@SafeVarargs
public static <O> void tryWithResources(FailableRunnable<? extends Throwable> pAction,
FailableRunnable<? extends Throwable>... pResources) {
tryWithResources(pAction, null, pResources);
}
/**
* A simple try-with-resources implementation, that can be used, if your
* objects do not implement the {@link AutoCloseable} interface. The method
* executes the {@code pAction}. The method guarantees, that <em>all</em>
* the {@code pResources} are being executed, in the given order, afterwards,
* and regardless of success, or failure. If either the original action, or
* any of the resource action fails, then the <em>first</em> failure (aka
* {@link Throwable} is rethrown. Example use:
* <pre>
* final FileInputStream fis = new FileInputStream("my.file");
* Functions.tryWithResources(useInputStream(fis), () -&gt; fis.close());
* </pre>
* @param pAction The action to execute. This object <em>will</em> always
* be invoked.
* @param pResources The resource actions to execute. <em>All</em> resource
* actions will be invoked, in the given order. A resource action is an
* instance of {@link FailableRunnable}, which will be executed.
* @see #tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
*/
@SafeVarargs
public static void tryWithResources(FailableRunnable<? extends Throwable> pAction,
FailableRunnable<? extends Throwable>... pResources) {
tryWithResources(pAction, null, pResources);
}
public static RuntimeException rethrow(Throwable pThrowable) {
if (pThrowable == null) {
throw new NullPointerException("The Throwable must not be null.");
} else {
if (pThrowable instanceof RuntimeException) {
throw (RuntimeException) pThrowable;
} else if (pThrowable instanceof Error) {
throw (Error) pThrowable;
} else if (pThrowable instanceof IOException) {
throw new UncheckedIOException((IOException) pThrowable);
} else {
throw new UndeclaredThrowableException(pThrowable);
}
}
}
/**
* Rethrow a {@link Throwable} as an unchecked exception.
* @param pThrowable The throwable to rethrow
* @return Never returns anything, this method never terminates normally
*/
public static RuntimeException rethrow(Throwable pThrowable) {
if (pThrowable == null) {
throw new NullPointerException("The Throwable must not be null.");
} else {
if (pThrowable instanceof RuntimeException) {
throw (RuntimeException) pThrowable;
} else if (pThrowable instanceof Error) {
throw (Error) pThrowable;
} else if (pThrowable instanceof IOException) {
throw new UncheckedIOException((IOException) pThrowable);
} else {
throw new UndeclaredThrowableException(pThrowable);
}
}
}
}

View File

@ -16,293 +16,296 @@
*/
package org.apache.commons.lang3;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.UndeclaredThrowableException;
import org.apache.commons.lang3.Functions.FailableBiConsumer;
import org.apache.commons.lang3.Functions.FailableConsumer;
import org.junit.jupiter.api.Test;
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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
class FunctionsTest {
public static class SomeException extends Exception {
private static final long serialVersionUID = -4965704778119283411L;
public static class SomeException extends Exception {
private static final long serialVersionUID = -4965704778119283411L;
private Throwable t;
private Throwable t;
public SomeException(String pMsg) {
super(pMsg);
}
SomeException(String pMsg) {
super(pMsg);
}
public void setThrowable(Throwable pThrowable) {
t = pThrowable;
}
public void setThrowable(Throwable pThrowable) {
t = pThrowable;
}
public void test() throws Throwable {
if (t != null) {
throw t;
}
}
}
public static class Testable {
private Throwable t;
public void test() throws Throwable {
if (t != null) {
throw t;
}
}
}
public static class Testable {
private Throwable t;
public Testable(Throwable pTh) {
t = pTh;
}
Testable(Throwable pTh) {
t = pTh;
}
public void setThrowable(Throwable pThrowable) {
t = pThrowable;
}
public void setThrowable(Throwable pThrowable) {
t = pThrowable;
}
public void test() throws Throwable {
test(t);
}
public void test(Throwable pThrowable) throws Throwable {
if (pThrowable != null) {
throw pThrowable;
}
}
public void test() throws Throwable {
test(t);
}
public Integer testInt() throws Throwable {
return testInt(t);
}
public void test(Throwable pThrowable) throws Throwable {
if (pThrowable != null) {
throw pThrowable;
}
}
public Integer testInt(Throwable pThrowable) throws Throwable {
if (pThrowable != null) {
throw pThrowable;
}
return 0;
}
}
public Integer testInt() throws Throwable {
return testInt(t);
}
public static class FailureOnOddInvocations {
private static int invocation;
public FailureOnOddInvocations() throws SomeException {
final int i = ++invocation;
if (i % 2 == 1) {
throw new SomeException("Odd Invocation: " + i);
}
}
}
public Integer testInt(Throwable pThrowable) throws Throwable {
if (pThrowable != null) {
throw pThrowable;
}
return 0;
}
}
public static class CloseableObject {
private boolean closed;
public static class FailureOnOddInvocations {
private static int invocation;
FailureOnOddInvocations() throws SomeException {
final int i = ++invocation;
if (i % 2 == 1) {
throw new SomeException("Odd Invocation: " + i);
}
}
}
public void run(Throwable pTh) throws Throwable {
if (pTh != null) {
throw pTh;
}
}
public static class CloseableObject {
private boolean closed;
public void reset() {
closed = false;
}
public void run(Throwable pTh) throws Throwable {
if (pTh != null) {
throw pTh;
}
}
public void close() {
closed = true;
}
public void reset() {
closed = false;
}
public boolean isClosed() {
return closed;
}
}
@Test
void testRunnable() {
FailureOnOddInvocations.invocation = 0;
try {
Functions.run(() -> new FailureOnOddInvocations());
fail("Expected Exception");
} catch (UndeclaredThrowableException e) {
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage());
}
Functions.run(() -> new FailureOnOddInvocations());
}
public void close() {
closed = true;
}
@Test
void testCallable() {
FailureOnOddInvocations.invocation = 0;
try {
Functions.call(() -> new FailureOnOddInvocations());
fail("Expected Exception");
} catch (UndeclaredThrowableException e) {
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage());
}
final FailureOnOddInvocations instance = Functions.call(() -> new FailureOnOddInvocations());
assertNotNull(instance);
}
public boolean isClosed() {
return closed;
}
}
@Test
void testAcceptConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
try {
Functions.accept((t) -> t.test(), testable);
fail("Expected Exception");
} catch (IllegalStateException e) {
assertSame(ise, e);
}
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
try {
Functions.accept((t) -> t.test(), testable);
} catch (OutOfMemoryError e) {
assertSame(error, e);
}
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
try {
Functions.accept((t) -> t.test(), testable);
fail("Expected Exception");
} catch (UncheckedIOException e) {
final Throwable t = e.getCause();
assertNotNull(t);
assertTrue(t instanceof IOException);
assertSame(ioe, t);
}
testable.setThrowable(null);
Functions.accept((t) -> t.test(), testable);
}
@Test
void testRunnable() {
FailureOnOddInvocations.invocation = 0;
try {
Functions.run(FailureOnOddInvocations::new);
fail("Expected Exception");
} catch (UndeclaredThrowableException e) {
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertEquals("Odd Invocation: 1", cause.getMessage());
}
Functions.run(FailureOnOddInvocations::new);
}
@Test
void testAcceptBiConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(null);
try {
Functions.accept((t1,t2) -> t1.test(t2), testable, ise);
fail("Expected Exception");
} catch (IllegalStateException e) {
assertSame(ise, e);
}
final Error error = new OutOfMemoryError();
try {
Functions.accept((t1,t2) -> t1.test(t2), testable, error);
} catch (OutOfMemoryError e) {
assertSame(error, e);
}
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
try {
Functions.accept((t1,t2) -> t1.test(t2), testable, ioe);
fail("Expected Exception");
} catch (UncheckedIOException e) {
final Throwable t = e.getCause();
assertNotNull(t);
assertTrue(t instanceof IOException);
assertSame(ioe, t);
}
testable.setThrowable(null);
Functions.accept((t1,t2) -> t1.test(t2), testable, (Throwable) null);
}
@Test
void testCallable() {
FailureOnOddInvocations.invocation = 0;
try {
Functions.call(FailureOnOddInvocations::new);
fail("Expected Exception");
} catch (UndeclaredThrowableException e) {
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
public void testApplyFunction() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
try {
Functions.apply((t) -> t.testInt(), testable);
fail("Expected Exception");
} catch (IllegalStateException e) {
assertSame(ise, e);
}
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
try {
Functions.apply((t) -> t.testInt(), testable);
} catch (OutOfMemoryError e) {
assertSame(error, e);
}
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
try {
Functions.apply((t) -> t.testInt(), testable);
fail("Expected Exception");
} catch (UncheckedIOException e) {
final Throwable t = e.getCause();
assertNotNull(t);
assertTrue(t instanceof IOException);
assertSame(ioe, t);
}
testable.setThrowable(null);
final Integer i = Functions.apply((t) -> t.testInt(), testable);
assertNotNull(i);
assertEquals(0, i.intValue());
}
@Test
void testAcceptConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
try {
Functions.accept(Testable::test, testable);
fail("Expected Exception");
} catch (IllegalStateException e) {
assertSame(ise, e);
}
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
try {
Functions.accept(Testable::test, testable);
} catch (OutOfMemoryError e) {
assertSame(error, e);
}
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
try {
Functions.accept(Testable::test, testable);
fail("Expected Exception");
} catch (UncheckedIOException e) {
final Throwable t = e.getCause();
assertNotNull(t);
assertTrue(t instanceof IOException);
assertSame(ioe, t);
}
testable.setThrowable(null);
Functions.accept(Testable::test, testable);
}
@Test
public void testApplyBiFunction() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(null);
try {
Functions.apply((t1,t2) -> t1.testInt(t2), testable, ise);
fail("Expected Exception");
} catch (IllegalStateException e) {
assertSame(ise, e);
}
final Error error = new OutOfMemoryError();
try {
Functions.apply((t1,t2) -> t1.testInt(t2), testable, error);
} catch (OutOfMemoryError e) {
assertSame(error, e);
}
final IOException ioe = new IOException("Unknown I/O error");
try {
Functions.apply((t1,t2) -> t1.testInt(t2), testable, ioe);
fail("Expected Exception");
} catch (UncheckedIOException e) {
final Throwable t = e.getCause();
assertNotNull(t);
assertTrue(t instanceof IOException);
assertSame(ioe, t);
}
final Integer i = Functions.apply((t1,t2) -> t1.testInt(t2), testable, (Throwable) null);
assertNotNull(i);
assertEquals(0, i.intValue());
}
@Test
void testAcceptBiConsumer() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(null);
try {
Functions.accept(Testable::test, testable, ise);
fail("Expected Exception");
} catch (IllegalStateException e) {
assertSame(ise, e);
}
final Error error = new OutOfMemoryError();
try {
Functions.accept(Testable::test, testable, error);
} catch (OutOfMemoryError e) {
assertSame(error, e);
}
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
try {
Functions.accept(Testable::test, testable, ioe);
fail("Expected Exception");
} catch (UncheckedIOException e) {
final Throwable t = e.getCause();
assertNotNull(t);
assertTrue(t instanceof IOException);
assertSame(ioe, t);
}
testable.setThrowable(null);
Functions.accept(Testable::test, testable, (Throwable) null);
}
@Test
public void testTryWithResources() {
final CloseableObject co = new CloseableObject();
final FailableConsumer<Throwable,? extends Throwable> consumer = (th) -> co.run(th);
final IllegalStateException ise = new IllegalStateException();
try {
Functions.tryWithResources(() -> consumer.accept(ise), () -> co.close());
fail("Expected Exception");
} catch (IllegalStateException e) {
assertSame(ise, e);
}
assertTrue(co.isClosed());
co.reset();
final Error error = new OutOfMemoryError();
try {
Functions.tryWithResources(() -> consumer.accept(error), () -> co.close());
fail("Expected Exception");
} catch (OutOfMemoryError e) {
assertSame(error, e);
}
assertTrue(co.isClosed());
co.reset();
final IOException ioe = new IOException("Unknown I/O error");
try {
Functions.tryWithResources(() -> consumer.accept(ioe), () -> co.close());
fail("Expected Exception");
} catch (UncheckedIOException e) {
final IOException cause = e.getCause();
assertSame(ioe, cause);
}
assertTrue(co.isClosed());
co.reset();
Functions.tryWithResources(() -> consumer.accept(null), () -> co.close());
assertTrue(co.isClosed());
}
@Test
public void testApplyFunction() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(ise);
try {
Functions.apply(Testable::testInt, testable);
fail("Expected Exception");
} catch (IllegalStateException e) {
assertSame(ise, e);
}
final Error error = new OutOfMemoryError();
testable.setThrowable(error);
try {
Functions.apply(Testable::testInt, testable);
} catch (OutOfMemoryError e) {
assertSame(error, e);
}
final IOException ioe = new IOException("Unknown I/O error");
testable.setThrowable(ioe);
try {
Functions.apply(Testable::testInt, testable);
fail("Expected Exception");
} catch (UncheckedIOException e) {
final Throwable t = e.getCause();
assertNotNull(t);
assertTrue(t instanceof IOException);
assertSame(ioe, t);
}
testable.setThrowable(null);
final Integer i = Functions.apply(Testable::testInt, testable);
assertNotNull(i);
assertEquals(0, i.intValue());
}
@Test
public void testApplyBiFunction() {
final IllegalStateException ise = new IllegalStateException();
final Testable testable = new Testable(null);
try {
Functions.apply(Testable::testInt, testable, ise);
fail("Expected Exception");
} catch (IllegalStateException e) {
assertSame(ise, e);
}
final Error error = new OutOfMemoryError();
try {
Functions.apply(Testable::testInt, testable, error);
} catch (OutOfMemoryError e) {
assertSame(error, e);
}
final IOException ioe = new IOException("Unknown I/O error");
try {
Functions.apply(Testable::testInt, testable, ioe);
fail("Expected Exception");
} catch (UncheckedIOException e) {
final Throwable t = e.getCause();
assertNotNull(t);
assertTrue(t instanceof IOException);
assertSame(ioe, t);
}
final Integer i = Functions.apply(Testable::testInt, testable, (Throwable) null);
assertNotNull(i);
assertEquals(0, i.intValue());
}
@Test
public void testTryWithResources() {
final CloseableObject co = new CloseableObject();
final FailableConsumer<Throwable, ? extends Throwable> consumer = co::run;
final IllegalStateException ise = new IllegalStateException();
try {
Functions.tryWithResources(() -> consumer.accept(ise), co::close);
fail("Expected Exception");
} catch (IllegalStateException e) {
assertSame(ise, e);
}
assertTrue(co.isClosed());
co.reset();
final Error error = new OutOfMemoryError();
try {
Functions.tryWithResources(() -> consumer.accept(error), co::close);
fail("Expected Exception");
} catch (OutOfMemoryError e) {
assertSame(error, e);
}
assertTrue(co.isClosed());
co.reset();
final IOException ioe = new IOException("Unknown I/O error");
try {
Functions.tryWithResources(() -> consumer.accept(ioe), co::close);
fail("Expected Exception");
} catch (UncheckedIOException e) {
final IOException cause = e.getCause();
assertSame(ioe, cause);
}
assertTrue(co.isClosed());
co.reset();
Functions.tryWithResources(() -> consumer.accept(null), co::close);
assertTrue(co.isClosed());
}
}