Skip to content

Commit

Permalink
Use Throws in CheckedTest yegor256#1533
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier B. OURA committed Mar 13, 2021
1 parent fad0225 commit 1b6841a
Showing 1 changed file with 49 additions and 37 deletions.
86 changes: 49 additions & 37 deletions src/test/java/org/cactoos/scalar/CheckedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,48 @@
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;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link Checked}.
* @since 0.30
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class CheckedTest {

@Test(expected = IllegalStateException.class)
@Test
public void runtimeExceptionGoesOut() throws Exception {
new Checked<>(
() -> {
throw new IllegalStateException("runtime");
},
IOException::new
).value();
new Assertion<>(
"Must throw runtime exception",
() -> new Checked<>(
() -> {
throw new IllegalStateException("runtime");
},
IOException::new
).value(),
new Throws<>(IllegalStateException.class)
).affirm();
}

@Test(expected = IllegalStateException.class)
@Test
public void usesGenericVarianceOnExceptionTypes() throws Exception {
new Checked<String, IllegalStateException>(
() -> {
throw new IllegalStateException();
},
(Throwable ex) -> {
return new AcceptPendingException();
}
).value();
new Assertion<>(
"Must use generic variance on exception types",
() -> new Checked<String, IllegalStateException>(
() -> {
throw new IllegalStateException();
},
(Throwable ex) -> {
return new AcceptPendingException();
}
).value(),
new Throws<>(IllegalStateException.class)
).affirm();
}

@SuppressWarnings("PMD.AvoidDuplicateLiterals")
Expand All @@ -73,14 +82,18 @@ public void usesContravarianceOnResultType() throws Exception {
);
}

@Test(expected = IOException.class)
@Test
public void throwsIoException() throws Exception {
new Checked<>(
() -> {
throw new InterruptedException("interrupt");
},
IOException::new
).value();
new Assertion<>(
"Must throw io exception",
() -> new Checked<>(
() -> {
throw new InterruptedException("interrupt");
},
IOException::new
).value(),
new Throws<>(IOException.class)
).affirm();
}

@Test
Expand All @@ -93,10 +106,11 @@ public void ioExceptionGoesOut() throws Exception {
IOException::new
).value();
} catch (final IOException exp) {
MatcherAssert.assertThat(
new Assertion<>(
"Must not have cause when throwing io exception",
exp.getCause(),
Matchers.nullValue()
);
).affirm();
}
}

Expand All @@ -110,28 +124,26 @@ public void fileNotFoundExceptionGoesOut() throws Exception {
IOException::new
).value();
} catch (final FileNotFoundException exp) {
MatcherAssert.assertThat(
new Assertion<>(
"Must not have cause when throwing file not found exception",
exp.getCause(),
Matchers.nullValue()
);
).affirm();
}
}

@Test
public void throwsIoExceptionWithModifiedMessage() throws Exception {
final String message = "error msg";
try {
new Checked<>(
new Assertion<>(
"Must throw io exception with modified message",
() -> new Checked<>(
() -> {
throw new IOException("io");
},
exp -> new IOException(message, exp)
).value();
} catch (final IOException exp) {
MatcherAssert.assertThat(
exp.getMessage(),
Matchers.containsString(message)
);
}
).value(),
new Throws<>(message, IOException.class)
).affirm();
}
}

0 comments on commit 1b6841a

Please sign in to comment.