Add otherwiseThrow(Exception)

This commit is contained in:
Paul Campbell 2018-07-18 22:44:45 +01:00
parent 752b7864be
commit fbb6117ab1
8 changed files with 91 additions and 2 deletions

View file

@ -5,6 +5,7 @@ CHANGELOG
----- -----
* Add `thenThrow(Exception)` * Add `thenThrow(Exception)`
* Add `otherwiseThrow(Exception)`
0.4.0 0.4.0
----- -----

View file

@ -59,7 +59,6 @@
<configuration> <configuration>
<tiles> <tiles>
<tile>net.kemitix.tiles:all:${kemitix-tiles.version}</tile> <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> <tile>net.kemitix.checkstyle:tile:${kemitix-checkstyle.version}</tile>
</tiles> </tiles>
</configuration> </configuration>

View file

@ -28,6 +28,7 @@ import java.util.function.Supplier;
* *
* @author Paul Campbell (pcampbell@kemitix.net). * @author Paul Campbell (pcampbell@kemitix.net).
*/ */
@SuppressWarnings({"methodcount", "PMD.TooManyMethods"})
public interface Condition { public interface Condition {
/** /**
@ -129,6 +130,15 @@ public interface Condition {
* @param exception the Exception to throw * @param exception the Exception to throw
* @throws Exception the exception * @throws Exception the exception
*/ */
@SuppressWarnings({"illegalthrows", "PMD.SignatureDeclareThrowsException"}) @SuppressWarnings(SuppressHelper.CS_ILLEGALTHROWS)
void thenThrow(Exception exception) throws Exception; 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

@ -58,4 +58,8 @@ final class FalseCondition implements Condition {
// do nothing // 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

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

View file

@ -325,4 +325,18 @@ public class ConditionalTest implements WithAssertions {
Condition.where(false) Condition.where(false)
.thenThrow(new IOException()); .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());
}
} }

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