Skip to content

Commit

Permalink
ScalarWithFallback: extra ctors were added
Browse files Browse the repository at this point in the history
  • Loading branch information
iakunin committed May 2, 2020
1 parent edf9fe8 commit 780b224
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/org/cactoos/scalar/ScalarWithFallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,6 +54,41 @@ public final class ScalarWithFallback<T> implements Scalar<T> {
*/
private final Iterable<FallbackFrom<T>> 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<T> origin,
final Class<? extends Throwable> exception,
final Func<Throwable, T> fallback
) {
this(origin, new IterableOf<Class<? extends Throwable>>(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<T> origin,
final Iterable<Class<? extends Throwable>> exceptions,
final Func<Throwable, T> fallback
) {
this(
origin,
new IterableOf<FallbackFrom<T>>(
new FallbackFrom<>(
exceptions, fallback
)
)
);
}

/**
* Ctor.
* @param origin Original scalar
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/org/cactoos/scalar/ScalarWithFallbackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
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;

/**
* Test case for {@link ScalarWithFallback}.
*
* @since 0.31
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
@SuppressWarnings("unchecked")
public final class ScalarWithFallbackTest {
Expand All @@ -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";
Expand All @@ -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";
Expand Down

0 comments on commit 780b224

Please sign in to comment.