Repository: james-project Updated Branches: refs/heads/master 97a5eb2a1 -> 546d63f7d
JAMES-2583 Parameterized tests for both current and legacy fail cases - There are many combinations between current, legacy & throwing, future throwing. Then I have to use parameterized test to reduce line numbers Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/546d63f7 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/546d63f7 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/546d63f7 Branch: refs/heads/master Commit: 546d63f7d46c09dcfa47e4931a8c0191451ed00b Parents: 12a26f9 Author: tran tien duc <dt...@linagora.com> Authored: Wed Nov 7 11:20:35 2018 +0700 Committer: Antoine Duprat <adup...@linagora.com> Committed: Wed Nov 7 14:12:19 2018 +0100 ---------------------------------------------------------------------- server/blob/blob-union/pom.xml | 5 ++ .../apache/james/blob/union/UnionBlobStore.java | 9 +++ .../james/blob/union/UnionBlobStoreTest.java | 78 ++++++++++++++++++++ 3 files changed, 92 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/546d63f7/server/blob/blob-union/pom.xml ---------------------------------------------------------------------- diff --git a/server/blob/blob-union/pom.xml b/server/blob/blob-union/pom.xml index 5a6e84f..45022cf 100644 --- a/server/blob/blob-union/pom.xml +++ b/server/blob/blob-union/pom.xml @@ -72,6 +72,11 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-params</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <scope>test</scope> http://git-wip-us.apache.org/repos/asf/james-project/blob/546d63f7/server/blob/blob-union/src/main/java/org/apache/james/blob/union/UnionBlobStore.java ---------------------------------------------------------------------- diff --git a/server/blob/blob-union/src/main/java/org/apache/james/blob/union/UnionBlobStore.java b/server/blob/blob-union/src/main/java/org/apache/james/blob/union/UnionBlobStore.java index 9d87213..97bad3f 100644 --- a/server/blob/blob-union/src/main/java/org/apache/james/blob/union/UnionBlobStore.java +++ b/server/blob/blob-union/src/main/java/org/apache/james/blob/union/UnionBlobStore.java @@ -34,6 +34,7 @@ import org.slf4j.LoggerFactory; import com.github.fge.lambdas.Throwing; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.MoreObjects; public class UnionBlobStore implements BlobStore { @@ -150,4 +151,12 @@ public class UnionBlobStore implements BlobStore { private boolean hasContent(byte [] bytes) { return bytes.length > 0; } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("currentBlobStore", currentBlobStore) + .add("legacyBlobStore", legacyBlobStore) + .toString(); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/546d63f7/server/blob/blob-union/src/test/java/org/apache/james/blob/union/UnionBlobStoreTest.java ---------------------------------------------------------------------- diff --git a/server/blob/blob-union/src/test/java/org/apache/james/blob/union/UnionBlobStoreTest.java b/server/blob/blob-union/src/test/java/org/apache/james/blob/union/UnionBlobStoreTest.java index 9afa0f9..e217957 100644 --- a/server/blob/blob-union/src/test/java/org/apache/james/blob/union/UnionBlobStoreTest.java +++ b/server/blob/blob-union/src/test/java/org/apache/james/blob/union/UnionBlobStoreTest.java @@ -21,11 +21,15 @@ package org.apache.james.blob.union; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.PushbackInputStream; +import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.function.Function; +import java.util.stream.Stream; import org.apache.james.blob.api.BlobId; import org.apache.james.blob.api.BlobStore; @@ -33,10 +37,17 @@ import org.apache.james.blob.api.BlobStoreContract; import org.apache.james.blob.api.HashBlobId; import org.apache.james.blob.memory.MemoryBlobStore; import org.apache.james.util.CompletableFutureUtil; +import org.apache.james.util.StreamUtils; import org.assertj.core.api.SoftAssertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.testcontainers.shaded.com.google.common.base.MoreObjects; +import org.testcontainers.shaded.com.google.common.collect.ImmutableList; class UnionBlobStoreTest implements BlobStoreContract { @@ -61,6 +72,12 @@ class UnionBlobStoreTest implements BlobStoreContract { public InputStream read(BlobId blobId) { throw new RuntimeException("broken everywhere"); } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .toString(); + } } private static class ThrowingBlobStore implements BlobStore { @@ -84,6 +101,12 @@ class UnionBlobStoreTest implements BlobStoreContract { public InputStream read(BlobId blobId) { throw new RuntimeException("broken everywhere"); } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .toString(); + } } private static final HashBlobId.Factory BLOB_ID_FACTORY = new HashBlobId.Factory(); @@ -224,6 +247,61 @@ class UnionBlobStoreTest implements BlobStoreContract { } } + @TestInstance(TestInstance.Lifecycle.PER_CLASS) + @Nested + class CurrentAndLegacyCouldNotComplete { + + + Stream<Function<UnionBlobStore, CompletableFuture>> blobStoreOperationsReturnFutures() { + return Stream.of( + blobStore -> blobStore.save(BLOB_CONTENT), + blobStore -> blobStore.save(new ByteArrayInputStream(BLOB_CONTENT)), + blobStore -> blobStore.readBytes(BLOB_ID_FACTORY.randomId())); + } + + Stream<Function<UnionBlobStore, InputStream>> blobStoreOperationsNotReturnFutures() { + return Stream.of( + blobStore -> blobStore.read(BLOB_ID_FACTORY.randomId())); + } + + Stream<Arguments> blobStoresCauseReturnExceptionallyFutures() { + List<UnionBlobStore> futureThrowingUnionBlobStores = ImmutableList.of( + new UnionBlobStore(new ThrowingBlobStore(), new FutureThrowingBlobStore()), + new UnionBlobStore(new FutureThrowingBlobStore(), new ThrowingBlobStore()), + new UnionBlobStore(new FutureThrowingBlobStore(), new FutureThrowingBlobStore())); + + return blobStoreOperationsReturnFutures() + .flatMap(blobStoreFunction -> futureThrowingUnionBlobStores + .stream() + .map(blobStore -> Arguments.of(blobStore, blobStoreFunction))); + } + + Stream<Arguments> blobStoresCauseThrowExceptions() { + UnionBlobStore throwingUnionBlobStore = new UnionBlobStore(new ThrowingBlobStore(), new ThrowingBlobStore()); + + return StreamUtils.flatten( + blobStoreOperationsReturnFutures() + .map(blobStoreFunction -> Arguments.of(throwingUnionBlobStore, blobStoreFunction)), + blobStoreOperationsNotReturnFutures() + .map(blobStoreFunction -> Arguments.of(throwingUnionBlobStore, blobStoreFunction))); + } + + @ParameterizedTest + @MethodSource("blobStoresCauseThrowExceptions") + void operationShouldThrow(UnionBlobStore blobStoreThrowsException, + Function<UnionBlobStore, CompletableFuture> blobStoreOperation) { + assertThatThrownBy(() -> blobStoreOperation.apply(blobStoreThrowsException)) + .isInstanceOf(RuntimeException.class); + } + + @ParameterizedTest + @MethodSource("blobStoresCauseReturnExceptionallyFutures") + void operationShouldReturnExceptionallyFuture(UnionBlobStore blobStoreReturnsExceptionallyFuture, + Function<UnionBlobStore, CompletableFuture> blobStoreOperation) { + assertThat(blobStoreOperation.apply(blobStoreReturnsExceptionallyFuture)) + .isCompletedExceptionally(); + } + } @Test void readShouldReturnFromCurrentWhenAvailable() throws Exception { --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org