From 780b224cb7f8e2a3c23ecac8db93fedf8933d14b Mon Sep 17 00:00:00 2001 From: iakunin Date: Sat, 2 May 2020 13:57:12 +0300 Subject: [PATCH] ScalarWithFallback: extra ctors were added --- .../cactoos/scalar/ScalarWithFallback.java | 37 +++++++++++ .../scalar/ScalarWithFallbackTest.java | 62 +++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/src/main/java/org/cactoos/scalar/ScalarWithFallback.java b/src/main/java/org/cactoos/scalar/ScalarWithFallback.java index 8747e9356a..52b63b354a 100644 --- a/src/main/java/org/cactoos/scalar/ScalarWithFallback.java +++ b/src/main/java/org/cactoos/scalar/ScalarWithFallback.java @@ -25,8 +25,10 @@ import java.util.Comparator; import java.util.Map; +import org.cactoos.Func; import org.cactoos.Scalar; import org.cactoos.func.FuncWithFallback; +import org.cactoos.iterable.IterableOf; import org.cactoos.iterator.Filtered; import org.cactoos.iterator.Sorted; import org.cactoos.map.MapOf; @@ -52,6 +54,41 @@ public final class ScalarWithFallback implements Scalar { */ private final Iterable> fallbacks; + /** + * Ctor. + * @param origin Original scalar + * @param exception Supported exception type + * @param fallback Function that converts the given exception into fallback value + */ + public ScalarWithFallback( + final Scalar origin, + final Class exception, + final Func fallback + ) { + this(origin, new IterableOf>(exception), fallback); + } + + /** + * Ctor. + * @param origin Original scalar + * @param exceptions Supported exceptions types + * @param fallback Function that converts the given exception into fallback value + */ + public ScalarWithFallback( + final Scalar origin, + final Iterable> exceptions, + final Func fallback + ) { + this( + origin, + new IterableOf>( + new FallbackFrom<>( + exceptions, fallback + ) + ) + ); + } + /** * Ctor. * @param origin Original scalar diff --git a/src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java b/src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java index 9511692de2..67efbed940 100644 --- a/src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java +++ b/src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java @@ -29,6 +29,7 @@ import org.cactoos.iterable.IterableOf; import org.hamcrest.MatcherAssert; import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.ScalarHasValue; /** @@ -36,6 +37,7 @@ * * @since 0.31 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ @SuppressWarnings("unchecked") public final class ScalarWithFallbackTest { @@ -58,6 +60,34 @@ public void usesMainFunc() throws Exception { ); } + @Test + public void usesMainFuncFromExceptionAndFallback() throws Exception { + final String message = "Main function's result #1 (exp & flbck)"; + new Assertion<>( + "Using the main function if no exception (exp & flbck)", + new ScalarWithFallback<>( + () -> message, + IOException.class, + Throwable::getMessage + ), + new ScalarHasValue<>(message) + ).affirm(); + } + + @Test + public void usesMainFuncFromIterableExceptionAndFallback() throws Exception { + final String message = "Main function's result #1 (exp iterable & flbck)"; + new Assertion<>( + "Using the main function if no exception (exp iterable & flbck)", + new ScalarWithFallback<>( + () -> message, + new IterableOf<>(IOException.class), + Throwable::getMessage + ), + new ScalarHasValue<>(message) + ).affirm(); + } + @Test public void usesFallback() throws Exception { final String message = "Fallback from IOException"; @@ -78,6 +108,38 @@ public void usesFallback() throws Exception { ); } + @Test + public void usesFallbackFromExceptionAndFallback() throws Exception { + final String message = "Fallback from IOException (exp & flbck)"; + new Assertion<>( + "Using a single fallback in case of exception (exp & flbck)", + new ScalarWithFallback<>( + () -> { + throw new IOException("Failure with IOException (exp & flbck)"); + }, + IOException.class, + exp -> message + ), + new ScalarHasValue<>(message) + ).affirm(); + } + + @Test + public void usesFallbackFromIterableExceptionAndFallback() throws Exception { + final String message = "Fallback from IOException (exp iterable & flbck)"; + new Assertion<>( + "Using a single fallback in case of exception (exp iterable & flbck)", + new ScalarWithFallback<>( + () -> { + throw new IOException("Failure with IOException (exp iterable & flbck)"); + }, + new IterableOf<>(IOException.class), + exp -> message + ), + new ScalarHasValue<>(message) + ).affirm(); + } + @Test public void usesFallbackOfInterruptedException() throws Exception { final String message = "Fallback from InterruptedException";