From caaf0d9d86acb03fecae7e78160a588b764ecb6b Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Tue, 9 Mar 2021 08:49:24 +0000 Subject: [PATCH] Introduce tests for Checked class #1533 --- .../org/cactoos/bytes/UncheckedBytes.java | 4 +-- .../java/org/cactoos/scalar/CheckedTest.java | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/bytes/UncheckedBytes.java b/src/main/java/org/cactoos/bytes/UncheckedBytes.java index b7bc1879a1..24ef172c15 100644 --- a/src/main/java/org/cactoos/bytes/UncheckedBytes.java +++ b/src/main/java/org/cactoos/bytes/UncheckedBytes.java @@ -44,7 +44,7 @@ public final class UncheckedBytes implements Bytes { /** * Fallback. */ - private final Func fallback; + private final Func fallback; /** * Ctor. @@ -67,7 +67,7 @@ public UncheckedBytes(final Bytes bts) { * @since 0.5 */ public UncheckedBytes(final Bytes bts, - final Func fbk) { + final Func fbk) { this.bytes = bts; this.fallback = fbk; } diff --git a/src/test/java/org/cactoos/scalar/CheckedTest.java b/src/test/java/org/cactoos/scalar/CheckedTest.java index cba36c1caf..9594799bcf 100644 --- a/src/test/java/org/cactoos/scalar/CheckedTest.java +++ b/src/test/java/org/cactoos/scalar/CheckedTest.java @@ -25,9 +25,12 @@ import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.channels.AcceptPendingException; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.hamcrest.core.IsEqual; import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; /** * Test case for {@link Checked}. @@ -46,6 +49,30 @@ public void runtimeExceptionGoesOut() throws Exception { ).value(); } + @Test(expected = IllegalStateException.class) + public void usesGenericVarianceOnExceptionTypes() throws Exception { + new Checked( + () -> { + throw new IllegalStateException(); + }, + (Throwable ex) -> { + return new AcceptPendingException(); + } + ).value(); + } + + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + public void usesContravarianceOnResultType() throws Exception { + new Assertion<>( + "Must use contravariance on result", + new Checked( + () -> new String("contravariance"), + IOException::new + ).value(), + new IsEqual<>("contravariance") + ); + } + @Test(expected = IOException.class) public void throwsIoException() throws Exception { new Checked<>(