From d9f1b22b2f1dee13905c6e8cc5be639a0bcc5aa9 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 27 Jan 2019 12:43:58 +0000 Subject: [PATCH] Fix API issues (#43) * Deprecate Condition.*Throw(Exception) and add Supplier versions * Added: Condition.and(boolean) and Condition.or(boolean) --- CHANGELOG.org | 27 +- .../net/kemitix/conditional/Condition.java | 46 +++- .../kemitix/conditional/FalseCondition.java | 10 + .../kemitix/conditional/TrueCondition.java | 10 + .../kemitix/conditional/ConditionalTest.java | 246 ++++++++++++++++-- 5 files changed, 309 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.org b/CHANGELOG.org index 45f0927..5936633 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -5,12 +5,29 @@ All notable changes to this project will be documented in this file. The format is based on [[https://keepachangelog.com/en/1.0.0/][Keep a Changelog]], and this project adheres to [[https://semver.org/spec/v2.0.0.html][Semantic Versioning]]. -* 0.7.1 +* 1.0.0 + +** Added - - Bump assertj-core from 3.11.0 to 3.11.1 (#38) - - Bump kemitix-maven-tiles from 0.9.0 to 1.3.1 (#40) - - [jenkins] Compatibility build against java 11 (#40) - - Bump kemitix-parent from 5.1.1 to 5.2.0 (#39) + - ~Condition.thenThrow(Supplier)~ + - ~Condition.otherwiseThrow(Supplier)~ + - ~Condition.and(boolean)~ + - ~Condition.or(boolean)~ + +** Changed + + - [jenkins] Compatibility build against java 11 (#40) + +** Deprecated + + - ~Condition.thenThrow(Exception)~ + - ~Condition.otherwiseThrow(Exception)~ + +** Dependencies + + - Bump assertj-core from 3.11.0 to 3.11.1 (#38) + - Bump kemitix-maven-tiles from 0.9.0 to 1.3.1 (#40) + - Bump kemitix-parent from 5.1.1 to 5.2.0 (#39) * 0.7.0 diff --git a/src/main/java/net/kemitix/conditional/Condition.java b/src/main/java/net/kemitix/conditional/Condition.java index 62b8f91..58373ee 100644 --- a/src/main/java/net/kemitix/conditional/Condition.java +++ b/src/main/java/net/kemitix/conditional/Condition.java @@ -77,7 +77,7 @@ public interface Condition { Condition and(Supplier clause); /** - * Logicaly OR current {@code Condition} with the other {@code Condition}. + * Logically AND current {@code Condition} with the other {@code Condition}. * * @param other the other Condition * @return true if both Conditions are true @@ -86,6 +86,16 @@ public interface Condition { return Condition.where(isTrue()).and(other::isTrue); } + /** + * Logically AND current {@code Condition} with the other boolean. + * + * @param other the other boolean + * @return true if both Conditions are true + */ + default Condition and(boolean other) { + return Condition.where(isTrue()).and(Condition.where(other)); + } + /** * Logically OR combine the current {@code Condition} with the clause. * @@ -105,6 +115,16 @@ public interface Condition { return where(isTrue()).or(other::isTrue); } + /** + * Logically OR the current {@code Condition} with the other boolean. + * + * @param other the other boolean + * @return true if either Condition is true + */ + default Condition or(boolean other) { + return where(isTrue()).or(Condition.where(other)); + } + /** * Perform this response if the {@code Condition} is {@code true}. * @@ -131,23 +151,45 @@ public interface Condition { } /** - * Throw then exception if the {@code Condition} is {@code true}. + * Throw the exception if the {@code Condition} is {@code true}. * * @param exception the Exception to throw * @throws Exception the exception + * @deprecated use {@link #thenThrow(Supplier)} */ + @Deprecated @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) void thenThrow(Exception exception) throws Exception; + /** + * Throw the exception supplied if the {@code Condition} is {@code true}. + * + * @param exceptionSupplier the supplier of the Exception to throw + * @throws Exception the exception + */ + @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) + void thenThrow(Supplier exceptionSupplier) throws Exception; + /** * Throw then exception if the {@code Condition} is {@code false}. * * @param exception the Exception to throw * @throws Exception the exception + * @deprecated use {@link #otherwiseThrow(Supplier)} */ + @Deprecated @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) void otherwiseThrow(Exception exception) throws Exception; + /** + * Throw then exception if the {@code Condition} is {@code false}. + * + * @param exceptionSupplier the supplier of the Exception to throw + * @throws Exception the exception + */ + @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) + void otherwiseThrow(Supplier exceptionSupplier) throws Exception; + /** * Apply the function to the Condtion, resulting an another Condition. * diff --git a/src/main/java/net/kemitix/conditional/FalseCondition.java b/src/main/java/net/kemitix/conditional/FalseCondition.java index b98f058..aaa0f2c 100644 --- a/src/main/java/net/kemitix/conditional/FalseCondition.java +++ b/src/main/java/net/kemitix/conditional/FalseCondition.java @@ -58,11 +58,21 @@ final class FalseCondition implements Condition { // do nothing } + @Override + public void thenThrow(final Supplier exceptionSupplier) throws Exception { + // do nothing + } + @Override public void otherwiseThrow(final Exception exception) throws Exception { throw exception; } + @Override + public void otherwiseThrow(final Supplier exceptionSupplier) throws Exception { + throw exceptionSupplier.get(); + } + @Override public boolean isTrue() { return false; diff --git a/src/main/java/net/kemitix/conditional/TrueCondition.java b/src/main/java/net/kemitix/conditional/TrueCondition.java index a261441..256c762 100644 --- a/src/main/java/net/kemitix/conditional/TrueCondition.java +++ b/src/main/java/net/kemitix/conditional/TrueCondition.java @@ -59,11 +59,21 @@ final class TrueCondition implements Condition { throw exception; } + @Override + public void thenThrow(final Supplier exceptionSupplier) throws Exception { + throw exceptionSupplier.get(); + } + @Override public void otherwiseThrow(final Exception exception) throws Exception { // do nothing } + @Override + public void otherwiseThrow(final Supplier exceptionSupplier) throws Exception { + // do nothing + } + @Override public boolean isTrue() { return true; diff --git a/src/test/java/net/kemitix/conditional/ConditionalTest.java b/src/test/java/net/kemitix/conditional/ConditionalTest.java index 8b0c3be..8dc80cd 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -30,7 +30,7 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueThenRuns() { //when - when(true); + whenAndCondition(true); //then assertThatTheThenResponseRuns(); } @@ -38,39 +38,103 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseOtherwiseRuns() { //when - when(false); + whenAndCondition(false); //then assertThatTheOtherwiseResponseRuns(); } @Test - public void whereTrueAndTrueThenRuns() { + public void whereTrueAndSupplierTrueThenRuns() { //when - when(true, true); + whenAndSupplier(true, true); //then assertThatTheThenResponseRuns(); } @Test - public void whereTrueAndFalseThenOtherwiseRuns() { + public void whereTrueAndSupplierFalseThenOtherwiseRuns() { //when - when(true, false); + whenAndSupplier(true, false); //then assertThatTheOtherwiseResponseRuns(); } @Test - public void whereFalseAndTrueThenOtherwiseRuns() { + public void whereFalseAndSupplierTrueThenOtherwiseRuns() { //when - when(false, true); + whenAndSupplier(false, true); //then assertThatTheOtherwiseResponseRuns(); } @Test - public void whereFalseAndFalseThenOtherwiseRuns() { + public void whereFalseAndSupplierFalseThenOtherwiseRuns() { //when - when(false, false); + whenAndSupplier(false, false); + //then + assertThatTheOtherwiseResponseRuns(); + } + + @Test + public void whereTrueAndConditionTrueThenRuns() { + //when + whenAndCondition(true, true); + //then + assertThatTheThenResponseRuns(); + } + + @Test + public void whereTrueAndConditionFalseThenOtherwiseRuns() { + //when + whenAndCondition(true, false); + //then + assertThatTheOtherwiseResponseRuns(); + } + + @Test + public void whereFalseAndTrueConditionThenOtherwiseRuns() { + //when + whenAndCondition(false, true); + //then + assertThatTheOtherwiseResponseRuns(); + } + + @Test + public void whereFalseAndConditionFalseThenOtherwiseRuns() { + //when + whenAndCondition(false, false); + //then + assertThatTheOtherwiseResponseRuns(); + } + + @Test + public void whereTrueAndBooleanTrueThenRuns() { + //when + whenAndBoolean(true, true); + //then + assertThatTheThenResponseRuns(); + } + + @Test + public void whereTrueAndBooleanFalseThenOtherwiseRuns() { + //when + whenAndBoolean(true, false); + //then + assertThatTheOtherwiseResponseRuns(); + } + + @Test + public void whereFalseAndBooleanTrueThenOtherwiseRuns() { + //when + whenAndBoolean(false, true); + //then + assertThatTheOtherwiseResponseRuns(); + } + + @Test + public void whereFalseAndBooleanFalseThenOtherwiseRuns() { + //when + whenAndBoolean(false, false); //then assertThatTheOtherwiseResponseRuns(); } @@ -87,33 +151,97 @@ public class ConditionalTest implements WithAssertions { } @Test - public void whereTrueOrTrueThenDoSomething() { + public void whereTrueOrSupplierTrueThenDoSomething() { //when - whenOr(true, true); + whenOrSupplier(true, true); //then assertThatTheThenResponseRuns(); } @Test - public void whereTrueOrFalseThenDoSomething() { + public void whereTrueOrSupplierFalseThenDoSomething() { //when - whenOr(true, false); + whenOrSupplier(true, false); //then assertThatTheThenResponseRuns(); } @Test - public void whereFalseOrTrueThenDoSomething() { + public void whereFalseOrSupplierTrueThenDoSomething() { //when - whenOr(false, true); + whenOrSupplier(false, true); //then assertThatTheThenResponseRuns(); } @Test - public void whereFalseOrFalseThenDoSomethingElse() { + public void whereFalseOrSupplierFalseThenDoSomethingElse() { //when - whenOr(false, false); + whenOrSupplier(false, false); + //then + assertThatTheOtherwiseResponseRuns(); + } + + @Test + public void whereTrueOrConditionTrueThenDoSomething() { + //when + whenOrCondition(true, true); + //then + assertThatTheThenResponseRuns(); + } + + @Test + public void whereTrueOrConditionFalseThenDoSomething() { + //when + whenOrCondition(true, false); + //then + assertThatTheThenResponseRuns(); + } + + @Test + public void whereFalseOrConditionTrueThenDoSomething() { + //when + whenOrCondition(false, true); + //then + assertThatTheThenResponseRuns(); + } + + @Test + public void whereFalseOrConditionFalseThenDoSomethingElse() { + //when + whenOrCondition(false, false); + //then + assertThatTheOtherwiseResponseRuns(); + } + + @Test + public void whereTrueOrBooleanTrueThenDoSomething() { + //when + whenOrBoolean(true, true); + //then + assertThatTheThenResponseRuns(); + } + + @Test + public void whereTrueOrBooleanFalseThenDoSomething() { + //when + whenOrBoolean(true, false); + //then + assertThatTheThenResponseRuns(); + } + + @Test + public void whereFalseOrBooleanTrueThenDoSomething() { + //when + whenOrBoolean(false, true); + //then + assertThatTheThenResponseRuns(); + } + + @Test + public void whereFalseOrBooleanFalseThenDoSomethingElse() { + //when + whenOrBoolean(false, false); //then assertThatTheOtherwiseResponseRuns(); } @@ -261,13 +389,23 @@ public class ConditionalTest implements WithAssertions { assertThatTheOtherwiseResponseDidNotRun(); } - private void when(final boolean clause) { + private void whenAndCondition(final boolean clause) { Condition.where(clause) .then(thenResponse) .otherwise(otherwiseResponse); } - private void when( + private void whenAndCondition( + final boolean firstClause, + final boolean secondClause + ) { + Condition.where(firstClause) + .and(Condition.where(secondClause)) + .then(thenResponse) + .otherwise(otherwiseResponse); + } + + private void whenAndSupplier( final boolean firstClause, final boolean secondClause ) { @@ -277,7 +415,27 @@ public class ConditionalTest implements WithAssertions { .otherwise(otherwiseResponse); } - private void whenOr( + private void whenAndBoolean( + final boolean firstClause, + final boolean secondClause + ) { + Condition.where(firstClause) + .and(secondClause) + .then(thenResponse) + .otherwise(otherwiseResponse); + } + + private void whenOrCondition( + final boolean firstClause, + final boolean secondClause + ) { + Condition.where(firstClause) + .or(Condition.where(secondClause)) + .then(thenResponse) + .otherwise(otherwiseResponse); + } + + private void whenOrSupplier( final boolean firstClause, final boolean secondClause ) { @@ -287,8 +445,18 @@ public class ConditionalTest implements WithAssertions { .otherwise(otherwiseResponse); } + private void whenOrBoolean( + final boolean firstClause, + final boolean secondClause + ) { + Condition.where(firstClause) + .or(secondClause) + .then(thenResponse) + .otherwise(otherwiseResponse); + } + @Test - public void shortCurcuitOr() { + public void shortCircuitOr() { //given final AtomicInteger atomicInteger = new AtomicInteger(); //when @@ -301,7 +469,7 @@ public class ConditionalTest implements WithAssertions { } @Test - public void shortCurcuitAnd() { + public void shortCircuitAnd() { //given final AtomicInteger atomicInteger = new AtomicInteger(); //when @@ -315,6 +483,14 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueThenThrowException() { + //given + assertThatExceptionOfType(IOException.class) + .isThrownBy(() -> Condition.where(true) + .thenThrow(IOException::new)); + } + + @Test + public void whereTrueThenThrowExceptionDeprecated() { //given assertThatExceptionOfType(IOException.class) .isThrownBy(() -> Condition.where(true) @@ -323,6 +499,14 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseThenDoNotThrowException() throws Exception { + assertThatCode(() -> + Condition.where(false) + .thenThrow(IOException::new)) + .doesNotThrowAnyException(); + } + + @Test + public void whereFalseThenDoNotThrowExceptionDeprecated() throws Exception { assertThatCode(() -> Condition.where(false) .thenThrow(new IOException())) @@ -331,6 +515,14 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseOtherwiseThenThrowException() { + //given + assertThatExceptionOfType(IOException.class) + .isThrownBy(() -> Condition.where(false) + .otherwiseThrow(IOException::new)); + } + + @Test + public void whereFalseOtherwiseThenThrowExceptionDeprecated() { //given assertThatExceptionOfType(IOException.class) .isThrownBy(() -> Condition.where(false) @@ -339,6 +531,14 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueOtherwiseThenDoNotThrowException() throws Exception { + assertThatCode(() -> + Condition.where(true) + .otherwiseThrow(IOException::new)) + .doesNotThrowAnyException(); + } + + @Test + public void whereTrueOtherwiseThenDoNotThrowExceptionDeprecated() throws Exception { assertThatCode(() -> Condition.where(true) .otherwiseThrow(new IOException()))