Merge pull request #31 from kemitix/thenthrow-otherwisethrow

Add thenThrow and otherwiseThrow
This commit is contained in:
Paul Campbell 2018-07-19 06:50:42 +01:00 committed by GitHub
commit d56d864ef2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 134 additions and 4 deletions

View file

@ -1,6 +1,12 @@
CHANGELOG
=========
0.5.0
-----
* Add `thenThrow(Exception)`
* Add `otherwiseThrow(Exception)`
0.4.0
-----

View file

@ -59,7 +59,6 @@
<configuration>
<tiles>
<tile>net.kemitix.tiles:all:${kemitix-tiles.version}</tile>
<tile>net.kemitix.tiles:pmd-strict:${kemitix-tiles.version}</tile>
<tile>net.kemitix.checkstyle:tile:${kemitix-checkstyle.version}</tile>
</tiles>
</configuration>

View file

@ -28,6 +28,7 @@ import java.util.function.Supplier;
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
@SuppressWarnings({"methodcount", "PMD.TooManyMethods"})
public interface Condition {
/**
@ -123,4 +124,21 @@ 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(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;
}

View file

@ -53,4 +53,13 @@ final class FalseCondition implements Condition {
response.perform();
}
@Override
public void thenThrow(final Exception exception) {
// do nothing
}
@Override
public void otherwiseThrow(final Exception exception) throws Exception {
throw exception;
}
}

View file

@ -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();
}
}

View file

@ -54,4 +54,14 @@ final class TrueCondition implements Condition {
// do nothing
}
@Override
public void thenThrow(final Exception exception) throws Exception {
throw exception;
}
@Override
public void otherwiseThrow(final Exception exception) throws Exception {
// do nothing
}
}

View file

@ -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,36 @@ public class ConditionalTest {
assertThatTheThenResponseDidNotRun();
assertThat(atomicInteger).hasValue(0);
}
@Test
public void whereTrueThenThrowException() {
//given
assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> Condition.where(true)
.thenThrow(new IOException()));
}
@Test
public void whereFalseThenDoNotThrowException() throws Exception {
assertThatCode(() ->
Condition.where(false)
.thenThrow(new IOException()))
.doesNotThrowAnyException();
}
@Test
public void whereFalseOtherwiseThenThrowException() {
//given
assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> Condition.where(false)
.otherwiseThrow(new IOException()));
}
@Test
public void whereTrueOtherwiseThenDoNotThrowException() throws Exception {
assertThatCode(() ->
Condition.where(true)
.otherwiseThrow(new IOException()))
.doesNotThrowAnyException();
}
}

View file

@ -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 utilityClassCannotBeInstantiate() throws NoSuchMethodException {
final Constructor<SuppressHelper> constructor = SuppressHelper.class.getDeclaredConstructor();
constructor.setAccessible(true);
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> new SuppressHelper());
}
}