Skip to content

Commit

Permalink
Merge 708eecc into c9af856
Browse files Browse the repository at this point in the history
  • Loading branch information
whiskeysierra authored Sep 22, 2020
2 parents c9af856 + 708eecc commit b974f6b
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 116 deletions.
18 changes: 3 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,11 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.17.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hobsoft.hamcrest</groupId>
<artifactId>hamcrest-compose</artifactId>
<version>0.4.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
28 changes: 13 additions & 15 deletions src/test/java/org/zalando/fauxpas/DefaultImplementationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

import java.util.function.Function;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.zalando.fauxpas.FauxPas.throwingBiConsumer;
import static org.zalando.fauxpas.FauxPas.throwingBiFunction;
Expand Down Expand Up @@ -36,7 +34,7 @@ void shouldRethrowExceptionFromRunnable() {
}

@Test
void shouldNotRethrowExceptionFromRunnable() throws Exception {
void shouldNotRethrowExceptionFromRunnable() {
final ThrowingRunnable<Exception> runnable = throwingRunnable(() -> {
});
shouldNotThrow(runnable);
Expand All @@ -51,7 +49,7 @@ void shouldRethrowExceptionFromSupplier() {
}

@Test
void shouldNotRethrowExceptionFromSupplier() throws Exception {
void shouldNotRethrowExceptionFromSupplier() {
final ThrowingSupplier<Void, Exception> supplier = throwingSupplier(() -> null);
shouldNotThrow(supplier::get);
}
Expand All @@ -65,7 +63,7 @@ void shouldRethrowExceptionFromConsumer() {
}

@Test
void shouldNotRethrowExceptionFromConsumer() throws Exception {
void shouldNotRethrowExceptionFromConsumer() {
final ThrowingConsumer<Void, Exception> consumer = throwingConsumer($ -> {
});
shouldNotThrow(() -> consumer.accept(null));
Expand All @@ -80,7 +78,7 @@ void shouldRethrowExceptionFromFunction() {
}

@Test
void shouldNotRethrowExceptionFromFunction() throws Exception {
void shouldNotRethrowExceptionFromFunction() {
final ThrowingFunction<Void, Void, Exception> function = throwingFunction((Void $) -> null);
shouldNotThrow(() -> function.apply(null));
}
Expand All @@ -94,7 +92,7 @@ void shouldRethrowExceptionFromUnaryOperator() {
}

@Test
void shouldNotRethrowExceptionFromUnaryOperator() throws Exception {
void shouldNotRethrowExceptionFromUnaryOperator() {
final ThrowingUnaryOperator<Void, Exception> operator = throwingUnaryOperator((Void $) -> null);
shouldNotThrow(() -> operator.apply(null));
}
Expand All @@ -108,7 +106,7 @@ void shouldRethrowExceptionFromPredicate() {
}

@Test
void shouldNotRethrowExceptionFromPredicate() throws Exception {
void shouldNotRethrowExceptionFromPredicate() {
final ThrowingPredicate<Void, Exception> predicate = throwingPredicate((Void $) -> false);
shouldNotThrow(() -> predicate.test(null));
}
Expand All @@ -122,7 +120,7 @@ void shouldRethrowExceptionFromBiConsumer() {
}

@Test
void shouldNotRethrowExceptionFromBiConsumer() throws Exception {
void shouldNotRethrowExceptionFromBiConsumer() {
final ThrowingBiConsumer<Void, Void, Exception> consumer = throwingBiConsumer(($, $2) -> {
});
shouldNotThrow(() -> consumer.accept(null, null));
Expand All @@ -137,7 +135,7 @@ void shouldRethrowExceptionFromBiFunction() {
}

@Test
void shouldNotRethrowExceptionFromBiFunction() throws Exception {
void shouldNotRethrowExceptionFromBiFunction() {
final ThrowingBiFunction<Void, Void, Void, Exception> function = throwingBiFunction(($, $2) -> null);
shouldNotThrow(() -> function.apply(null, null));
}
Expand All @@ -151,7 +149,7 @@ void shouldRethrowExceptionFromBinaryOperator() {
}

@Test
void shouldNotRethrowExceptionFromBinaryOperator() throws Exception {
void shouldNotRethrowExceptionFromBinaryOperator() {
final ThrowingBinaryOperator<Void, Exception> operator = throwingBinaryOperator(($, $2) -> null);
shouldNotThrow(() -> operator.apply(null, null));
}
Expand All @@ -165,7 +163,7 @@ void shouldRethrowExceptionFromBiPredicate() {
}

@Test
void shouldNotRethrowExceptionFromBiPredicate() throws Exception {
void shouldNotRethrowExceptionFromBiPredicate() {
final ThrowingBiPredicate<Void, Void, Exception> predicate = throwingBiPredicate(($, $2) -> false);
shouldNotThrow(() -> predicate.test(null, null));
}
Expand All @@ -175,8 +173,8 @@ private void shouldNotThrow(final Runnable nonThrower) {
}

private void shouldThrow(final Runnable thrower) {
assertThrows(Exception.class, thrower::run);
assertThat(exception, is(sameInstance(exception)));
Exception thrown = assertThrows(Exception.class, thrower::run);
assertThat(thrown).isSameAs(exception);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import java.util.concurrent.CompletableFuture;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import static org.zalando.fauxpas.FauxPas.exceptionallyCompose;
import static org.zalando.fauxpas.FauxPas.partially;

Expand All @@ -20,7 +19,7 @@ void shouldExceptionallyCompose() {

original.completeExceptionally(new RuntimeException());

assertThat(unit.join(), is("result"));
assertThat(unit).isCompletedWithValue("result");
}

}
38 changes: 17 additions & 21 deletions src/test/java/org/zalando/fauxpas/ExceptionallyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
import java.util.function.Function;
import java.util.function.Predicate;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import static org.zalando.fauxpas.FauxPas.partially;
Expand All @@ -29,7 +24,7 @@ void shouldReturnResult() {

original.complete("result");

assertThat(unit.join(), is("result"));
assertThat(unit).isCompletedWithValue("result");
}

@Test
Expand All @@ -42,7 +37,7 @@ void shouldCascade() {
original.completeExceptionally(new IllegalArgumentException());

final CompletionException thrown = assertThrows(CompletionException.class, unit::join);
assertThat(thrown.getCause(), is(instanceOf(IllegalStateException.class)));
assertThat(thrown).getCause().isInstanceOf(IllegalStateException.class);
}

@Test
Expand All @@ -53,7 +48,7 @@ void shouldUseFallbackWhenExplicitlyCompletedExceptionally() {

original.completeExceptionally(new UnsupportedOperationException(new IOException()));

assertThat(unit.join(), is("fallback"));
assertThat(unit).isCompletedWithValue("fallback");
}

@Test
Expand All @@ -65,7 +60,7 @@ void shouldUseFallbackWhenImplicitlyCompletedExceptionally() {

original.complete("unused");

assertThat(unit.join(), is("fallback"));
assertThat(unit).isCompletedWithValue("fallback");
}

@Test
Expand All @@ -78,7 +73,7 @@ void shouldUseFallbackWhenImplicitlyCompletedExceptionallyWithNullCause() {
original.complete("unused");

final CompletionException thrown = assertThrows(CompletionException.class, unit::join);
assertThat(thrown.getCause(), is(nullValue()));
assertThat(thrown).hasNoCause();
}

@Test
Expand All @@ -87,23 +82,24 @@ void shouldNotRethrowOriginalCompletionExceptionWhenImplicitlyCompletedException
final CompletionException thrown = shouldRethrowOriginalWhenImplicitlyCompletedExceptionally(exception, e -> {
throw new CompletionException(e);
});
assertThat(thrown, is(not(sameInstance(exception))));
assertThat(thrown.getCause(), is(sameInstance(exception.getCause())));
assertThat(thrown)
.isNotSameAs(exception)
.getCause().isSameAs(exception.getCause());
}

@Test
void shouldRethrowOriginalRuntimeWhenImplicitlyCompletedExceptionally() {
final RuntimeException exception = new IllegalStateException();
final CompletionException thrown = shouldRethrowOriginalWhenImplicitlyCompletedExceptionally(exception, rethrow());
assertThat(thrown.getCause(), is(sameInstance(exception)));
assertThat(thrown).getCause().isSameAs(exception);
}

@Test
void shouldRethrowOriginalThrowableWhenImplicitlyCompletedExceptionally() {
final Exception exception = new IOException();
final CompletionException thrown = shouldRethrowOriginalWhenImplicitlyCompletedExceptionally(
new CompletionException(exception), rethrow());
assertThat(thrown.getCause(), is(sameInstance(exception)));
assertThat(thrown).getCause().isSameAs(exception);
}

private CompletionException shouldRethrowOriginalWhenImplicitlyCompletedExceptionally(
Expand All @@ -122,21 +118,21 @@ private CompletionException shouldRethrowOriginalWhenImplicitlyCompletedExceptio
void shouldRethrowPackedCompletionExceptionWhenImplicitlyCompletedExceptionally() {
final Exception exception = new CompletionException(new UnsupportedOperationException());
final CompletionException thrown = shouldRethrowPackedWhenImplicitlyCompletedExceptionally(exception);
assertThat(thrown, is(sameInstance(exception)));
assertThat(thrown).isSameAs(exception);
}

@Test
void shouldRethrowPackedRuntimeExceptionWhenImplicitlyCompletedExceptionally() {
final Exception exception = new UnsupportedOperationException();
final CompletionException thrown = shouldRethrowPackedWhenImplicitlyCompletedExceptionally(exception);
assertThat(thrown.getCause(), is(sameInstance(exception)));
assertThat(thrown).getCause().isSameAs(exception);
}

@Test
void shouldRethrowPackedThrowableWhenImplicitlyCompletedExceptionally() {
final Exception exception = new IOException();
final CompletionException thrown = shouldRethrowPackedWhenImplicitlyCompletedExceptionally(exception);
assertThat(thrown.getCause(), is(sameInstance(exception)));
assertThat(thrown).getCause().isSameAs(exception);
}

private CompletionException shouldRethrowPackedWhenImplicitlyCompletedExceptionally(final Exception exception) {
Expand Down Expand Up @@ -169,7 +165,7 @@ private void shouldRethrowPackedWhenExplicitlyCompletedExceptionally(final Excep
original.completeExceptionally(exception);

final CompletionException thrown = assertThrows(CompletionException.class, unit::join);
assertThat(thrown.getCause(), is(sameInstance(exception)));
assertThat(thrown).getCause().isSameAs(exception);
}

@Test
Expand All @@ -181,7 +177,7 @@ void shouldHandleIfInstanceOf() {
final IllegalStateException exception = new IllegalStateException();
original.completeExceptionally(exception);

assertThat(unit.join(), is("foo"));
assertThat(unit).isCompletedWithValue("foo");
}

@Test
Expand All @@ -194,7 +190,7 @@ void shouldThrowIfNotInstanceOf() {
original.completeExceptionally(exception);

final CompletionException thrown = assertThrows(CompletionException.class, unit::join);
assertThat(thrown.getCause(), is(sameInstance(exception)));
assertThat(thrown).getCause().isSameAs(exception);
}

private Function<String, String> failWith(final RuntimeException e) {
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/org/zalando/fauxpas/HandleComposeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import java.util.concurrent.CompletableFuture;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import static org.zalando.fauxpas.FauxPas.handleCompose;

class HandleComposeTest {
Expand All @@ -19,7 +18,7 @@ void shouldHandleComposeResult() {

original.complete("foo");

assertThat(unit.join(), is("result"));
assertThat(unit).isCompletedWithValue("result");
}

@Test
Expand All @@ -30,7 +29,7 @@ void shouldHandleComposeException() {

original.completeExceptionally(new RuntimeException());

assertThat(unit.join(), is("result"));
assertThat(unit).isCompletedWithValue("result");
}

}
31 changes: 14 additions & 17 deletions src/test/java/org/zalando/fauxpas/TryWithBiConsumerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
import java.io.Closeable;
import java.io.IOException;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.hasItemInArray;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -55,8 +50,8 @@ void shouldNotFailOnNullResourceWithException() throws Exception {
void shouldThrowException() throws Exception {
final Exception exception = new Exception();
doThrow(exception).when(consumer).tryAccept(any(), any());
final Exception e = assertThrows(Exception.class, this::run);
assertThat(e, is(sameInstance(exception)));
final Exception thrown = assertThrows(Exception.class, this::run);
assertThat(thrown).isSameAs(exception);
}

@Test
Expand All @@ -80,8 +75,8 @@ void shouldFailToCloseInner() throws Exception {
private void shouldFailToClose(final Closeable resource) throws IOException {
final IOException ioException = new IOException();
doThrow(ioException).when(resource).close();
final IOException e = assertThrows(IOException.class, this::run);
assertThat(e, is(sameInstance(ioException)));
final IOException thrown = assertThrows(IOException.class, this::run);
assertThat(thrown).isSameAs(ioException);
}

@Test
Expand All @@ -101,10 +96,11 @@ private void shouldFailToCloseWithException(final Closeable resource) throws Exc
doThrow(exception).when(consumer).tryAccept(any(), any());
doThrow(ioException).when(resource).close();

final Exception e = assertThrows(Exception.class, this::run);
final Exception thrown = assertThrows(Exception.class, this::run);

assertThat(e, is(sameInstance(exception)));
assertThat(e.getSuppressed(), hasItemInArray(sameInstance(ioException)));
assertThat(thrown)
.isSameAs(exception)
.hasSuppressedException(ioException);
}

@Test
Expand All @@ -117,11 +113,12 @@ void shouldFailToCloseOuterAndInnerWithException() throws Exception {
doThrow(ioException).when(inner).close();
doThrow(secondIOException).when(outer).close();

final Exception e = assertThrows(Exception.class, this::run);
final Exception thrown = assertThrows(Exception.class, this::run);

assertThat(e, is(sameInstance(exception)));
assertThat(e.getSuppressed(), arrayContaining(asList(
sameInstance(ioException), sameInstance(secondIOException))));
assertThat(thrown)
.isSameAs(exception)
.hasSuppressedException(ioException)
.hasSuppressedException(secondIOException);
}

private void run() throws Exception {
Expand Down
Loading

0 comments on commit b974f6b

Please sign in to comment.