Fix API issues (#43)

* Deprecate Condition.*Throw(Exception) and add Supplier versions

* Added: Condition.and(boolean) and Condition.or(boolean)
This commit is contained in:
Paul Campbell 2019-01-27 12:43:58 +00:00 committed by GitHub
parent 1fa6a3c05a
commit d9f1b22b2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 309 additions and 30 deletions

View file

@ -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
- 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)
** Added
- ~Condition.thenThrow(Supplier<Exception>)~
- ~Condition.otherwiseThrow(Supplier<Exception>)~
- ~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

View file

@ -77,7 +77,7 @@ public interface Condition {
Condition and(Supplier<Boolean> 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<Exception> 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<Exception> exceptionSupplier) throws Exception;
/**
* Apply the function to the Condtion, resulting an another Condition.
*

View file

@ -58,11 +58,21 @@ final class FalseCondition implements Condition {
// do nothing
}
@Override
public void thenThrow(final Supplier<Exception> exceptionSupplier) throws Exception {
// do nothing
}
@Override
public void otherwiseThrow(final Exception exception) throws Exception {
throw exception;
}
@Override
public void otherwiseThrow(final Supplier<Exception> exceptionSupplier) throws Exception {
throw exceptionSupplier.get();
}
@Override
public boolean isTrue() {
return false;

View file

@ -59,11 +59,21 @@ final class TrueCondition implements Condition {
throw exception;
}
@Override
public void thenThrow(final Supplier<Exception> exceptionSupplier) throws Exception {
throw exceptionSupplier.get();
}
@Override
public void otherwiseThrow(final Exception exception) throws Exception {
// do nothing
}
@Override
public void otherwiseThrow(final Supplier<Exception> exceptionSupplier) throws Exception {
// do nothing
}
@Override
public boolean isTrue() {
return true;

View file

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