From 752b7864bed21e9f3ba12bcbc8712fc39af8b13b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 18 Jul 2018 21:57:25 +0100 Subject: [PATCH] Add `thenThrow(Exception)` --- CHANGELOG | 5 +++++ .../net/kemitix/conditional/Condition.java | 8 ++++++++ .../kemitix/conditional/FalseCondition.java | 5 +++++ .../kemitix/conditional/TrueCondition.java | 5 +++++ .../kemitix/conditional/ConditionalTest.java | 20 ++++++++++++++++--- 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index bdec8c3..46303f5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,11 @@ CHANGELOG ========= +0.5.0 +----- + +* Add `thenThrow(Exception)` + 0.4.0 ----- diff --git a/src/main/java/net/kemitix/conditional/Condition.java b/src/main/java/net/kemitix/conditional/Condition.java index c87a74f..1f7437c 100644 --- a/src/main/java/net/kemitix/conditional/Condition.java +++ b/src/main/java/net/kemitix/conditional/Condition.java @@ -123,4 +123,12 @@ public interface Condition { return where(clause.get()); } + /** + * Throw then exception if the {@code Condition} is {@code true}. + * + * @param exception the Exception to throw + * @throws Exception the exception + */ + @SuppressWarnings({"illegalthrows", "PMD.SignatureDeclareThrowsException"}) + void thenThrow(Exception exception) throws Exception; } diff --git a/src/main/java/net/kemitix/conditional/FalseCondition.java b/src/main/java/net/kemitix/conditional/FalseCondition.java index 3ffdbd2..5ec9f5e 100644 --- a/src/main/java/net/kemitix/conditional/FalseCondition.java +++ b/src/main/java/net/kemitix/conditional/FalseCondition.java @@ -53,4 +53,9 @@ final class FalseCondition implements Condition { response.perform(); } + @Override + public void thenThrow(final Exception exception) { + // do nothing + } + } diff --git a/src/main/java/net/kemitix/conditional/TrueCondition.java b/src/main/java/net/kemitix/conditional/TrueCondition.java index 0691433..5001c5b 100644 --- a/src/main/java/net/kemitix/conditional/TrueCondition.java +++ b/src/main/java/net/kemitix/conditional/TrueCondition.java @@ -54,4 +54,9 @@ final class TrueCondition implements Condition { // do nothing } + @Override + public void thenThrow(final Exception exception) throws Exception { + throw exception; + } + } diff --git a/src/test/java/net/kemitix/conditional/ConditionalTest.java b/src/test/java/net/kemitix/conditional/ConditionalTest.java index 5124b7f..9590438 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -1,16 +1,16 @@ package net.kemitix.conditional; +import org.assertj.core.api.WithAssertions; import org.junit.Before; import org.junit.Test; +import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; -import static org.assertj.core.api.Assertions.assertThat; - /** * @author Paul Campbell (pcampbell@kemitix.net). */ -public class ConditionalTest { +public class ConditionalTest implements WithAssertions { private Action thenResponse; @@ -311,4 +311,18 @@ public class ConditionalTest { assertThatTheThenResponseDidNotRun(); assertThat(atomicInteger).hasValue(0); } + + @Test + public void whereTrue_thenThrowException() { + //given + assertThatExceptionOfType(IOException.class) + .isThrownBy(() -> Condition.where(true) + .thenThrow(new IOException())); + } + + @Test + public void whereFalse_thenDoNotThrowException() throws Exception { + Condition.where(false) + .thenThrow(new IOException()); + } }