From 752b7864bed21e9f3ba12bcbc8712fc39af8b13b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 18 Jul 2018 21:57:25 +0100 Subject: [PATCH 1/4] 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()); + } } From fbb6117ab181c7df01a11a795bfe6087bb58ddd9 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 18 Jul 2018 22:44:45 +0100 Subject: [PATCH 2/4] Add `otherwiseThrow(Exception)` --- CHANGELOG | 1 + pom.xml | 1 - .../net/kemitix/conditional/Condition.java | 12 +++++- .../kemitix/conditional/FalseCondition.java | 4 ++ .../kemitix/conditional/SuppressHelper.java | 38 +++++++++++++++++++ .../kemitix/conditional/TrueCondition.java | 5 +++ .../kemitix/conditional/ConditionalTest.java | 14 +++++++ .../conditional/SuppressHelperTest.java | 18 +++++++++ 8 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/main/java/net/kemitix/conditional/SuppressHelper.java create mode 100644 src/test/java/net/kemitix/conditional/SuppressHelperTest.java diff --git a/CHANGELOG b/CHANGELOG index 46303f5..09046a8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ CHANGELOG ----- * Add `thenThrow(Exception)` +* Add `otherwiseThrow(Exception)` 0.4.0 ----- diff --git a/pom.xml b/pom.xml index c5b830e..0684d89 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,6 @@ net.kemitix.tiles:all:${kemitix-tiles.version} - net.kemitix.tiles:pmd-strict:${kemitix-tiles.version} net.kemitix.checkstyle:tile:${kemitix-checkstyle.version} diff --git a/src/main/java/net/kemitix/conditional/Condition.java b/src/main/java/net/kemitix/conditional/Condition.java index 1f7437c..aa4669b 100644 --- a/src/main/java/net/kemitix/conditional/Condition.java +++ b/src/main/java/net/kemitix/conditional/Condition.java @@ -28,6 +28,7 @@ import java.util.function.Supplier; * * @author Paul Campbell (pcampbell@kemitix.net). */ +@SuppressWarnings({"methodcount", "PMD.TooManyMethods"}) public interface Condition { /** @@ -129,6 +130,15 @@ public interface Condition { * @param exception the Exception to throw * @throws Exception the exception */ - @SuppressWarnings({"illegalthrows", "PMD.SignatureDeclareThrowsException"}) + @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) void thenThrow(Exception exception) throws Exception; + + /** + * Throw then exception if the {@code Condition} is {@code false}. + * + * @param exception the Exception to throw + * @throws Exception the exception + */ + @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS) + void otherwiseThrow(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 5ec9f5e..f47d8a4 100644 --- a/src/main/java/net/kemitix/conditional/FalseCondition.java +++ b/src/main/java/net/kemitix/conditional/FalseCondition.java @@ -58,4 +58,8 @@ final class FalseCondition implements Condition { // do nothing } + @Override + public void otherwiseThrow(final Exception exception) throws Exception { + throw exception; + } } diff --git a/src/main/java/net/kemitix/conditional/SuppressHelper.java b/src/main/java/net/kemitix/conditional/SuppressHelper.java new file mode 100644 index 0000000..0e28ff9 --- /dev/null +++ b/src/main/java/net/kemitix/conditional/SuppressHelper.java @@ -0,0 +1,38 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Paul Campbell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies + * or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.conditional; + +/** + * Holder for strings for suppressing Checkstyle and PMD warnings. + * + * @author Paul Campbell (pcampbell@kemitix.net). + */ +final class SuppressHelper { + public static final String CS_ILLEGALTHROWS = "illegalthrows"; + + /** + * Restricted constructor. + */ + protected SuppressHelper() { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/net/kemitix/conditional/TrueCondition.java b/src/main/java/net/kemitix/conditional/TrueCondition.java index 5001c5b..3597cb3 100644 --- a/src/main/java/net/kemitix/conditional/TrueCondition.java +++ b/src/main/java/net/kemitix/conditional/TrueCondition.java @@ -59,4 +59,9 @@ final class TrueCondition implements Condition { throw exception; } + @Override + public void otherwiseThrow(final Exception exception) throws Exception { + // do nothing + } + } diff --git a/src/test/java/net/kemitix/conditional/ConditionalTest.java b/src/test/java/net/kemitix/conditional/ConditionalTest.java index 9590438..6c07660 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -325,4 +325,18 @@ public class ConditionalTest implements WithAssertions { Condition.where(false) .thenThrow(new IOException()); } + + @Test + public void whereFalse_otherwiseThenThrowException() { + //given + assertThatExceptionOfType(IOException.class) + .isThrownBy(() -> Condition.where(false) + .otherwiseThrow(new IOException())); + } + + @Test + public void whereTrue_otherwiseThenDoNotThrowException() throws Exception { + Condition.where(true) + .otherwiseThrow(new IOException()); + } } diff --git a/src/test/java/net/kemitix/conditional/SuppressHelperTest.java b/src/test/java/net/kemitix/conditional/SuppressHelperTest.java new file mode 100644 index 0000000..91ae946 --- /dev/null +++ b/src/test/java/net/kemitix/conditional/SuppressHelperTest.java @@ -0,0 +1,18 @@ +package net.kemitix.conditional; + +import org.assertj.core.api.WithAssertions; +import org.junit.Test; + +import java.lang.reflect.Constructor; + +public class SuppressHelperTest implements WithAssertions { + + @Test + public void utilityClass_canNotInstantiate() throws NoSuchMethodException { + final Constructor constructor = SuppressHelper.class.getDeclaredConstructor(); + constructor.setAccessible(true); + assertThatExceptionOfType(UnsupportedOperationException.class) + .isThrownBy(() -> new SuppressHelper()); + } + +} \ No newline at end of file From 4662e35478847eca61ca0b7df170b7c3b00f850b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 18 Jul 2018 22:50:09 +0100 Subject: [PATCH 3/4] Remove underscores from test method names --- .../java/net/kemitix/conditional/ConditionalTest.java | 8 ++++---- .../java/net/kemitix/conditional/SuppressHelperTest.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/net/kemitix/conditional/ConditionalTest.java b/src/test/java/net/kemitix/conditional/ConditionalTest.java index 6c07660..8e85a13 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -313,7 +313,7 @@ public class ConditionalTest implements WithAssertions { } @Test - public void whereTrue_thenThrowException() { + public void whereTrueThenThrowException() { //given assertThatExceptionOfType(IOException.class) .isThrownBy(() -> Condition.where(true) @@ -321,13 +321,13 @@ public class ConditionalTest implements WithAssertions { } @Test - public void whereFalse_thenDoNotThrowException() throws Exception { + public void whereFalseThenDoNotThrowException() throws Exception { Condition.where(false) .thenThrow(new IOException()); } @Test - public void whereFalse_otherwiseThenThrowException() { + public void whereFalseOtherwiseThenThrowException() { //given assertThatExceptionOfType(IOException.class) .isThrownBy(() -> Condition.where(false) @@ -335,7 +335,7 @@ public class ConditionalTest implements WithAssertions { } @Test - public void whereTrue_otherwiseThenDoNotThrowException() throws Exception { + public void whereTrueOtherwiseThenDoNotThrowException() throws Exception { Condition.where(true) .otherwiseThrow(new IOException()); } diff --git a/src/test/java/net/kemitix/conditional/SuppressHelperTest.java b/src/test/java/net/kemitix/conditional/SuppressHelperTest.java index 91ae946..5a4e813 100644 --- a/src/test/java/net/kemitix/conditional/SuppressHelperTest.java +++ b/src/test/java/net/kemitix/conditional/SuppressHelperTest.java @@ -8,7 +8,7 @@ import java.lang.reflect.Constructor; public class SuppressHelperTest implements WithAssertions { @Test - public void utilityClass_canNotInstantiate() throws NoSuchMethodException { + public void utilityClassCannotBeInstantiate() throws NoSuchMethodException { final Constructor constructor = SuppressHelper.class.getDeclaredConstructor(); constructor.setAccessible(true); assertThatExceptionOfType(UnsupportedOperationException.class) From 7d03dd3e1573031b808366d1160da7eda50e9b38 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 19 Jul 2018 06:37:13 +0100 Subject: [PATCH 4/4] ConditionalTest: explicitly check that exceptions are not thrown --- .../net/kemitix/conditional/ConditionalTest.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/test/java/net/kemitix/conditional/ConditionalTest.java b/src/test/java/net/kemitix/conditional/ConditionalTest.java index 8e85a13..7cc8465 100644 --- a/src/test/java/net/kemitix/conditional/ConditionalTest.java +++ b/src/test/java/net/kemitix/conditional/ConditionalTest.java @@ -322,8 +322,10 @@ public class ConditionalTest implements WithAssertions { @Test public void whereFalseThenDoNotThrowException() throws Exception { - Condition.where(false) - .thenThrow(new IOException()); + assertThatCode(() -> + Condition.where(false) + .thenThrow(new IOException())) + .doesNotThrowAnyException(); } @Test @@ -336,7 +338,9 @@ public class ConditionalTest implements WithAssertions { @Test public void whereTrueOtherwiseThenDoNotThrowException() throws Exception { - Condition.where(true) - .otherwiseThrow(new IOException()); + assertThatCode(() -> + Condition.where(true) + .otherwiseThrow(new IOException())) + .doesNotThrowAnyException(); } }