This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit ab6cf304acf2917080f037dd67271be13c3396e3 Author: Benoit Tellier <[email protected]> AuthorDate: Fri Mar 22 11:24:47 2019 +0700 MAILBOX-385 ZipAssert should handle outputStream conversion This technical operation is duplicated in each and every zip test Note that ZipAssert thus need to be closeable --- .../org/apache/james/mailbox/backup/ZipAssert.java | 15 ++++- .../apache/james/mailbox/backup/ZipperTest.java | 70 ++++++++-------------- .../james/vault/DeletedMessageZipperTest.java | 17 ++---- 3 files changed, 44 insertions(+), 58 deletions(-) diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssert.java b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssert.java index cf1c41e..7c46fad 100644 --- a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssert.java +++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssert.java @@ -21,6 +21,8 @@ package org.apache.james.mailbox.backup; import static org.apache.james.mailbox.backup.ZipArchiveEntryAssert.assertThatZipEntry; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; @@ -29,12 +31,13 @@ import java.util.List; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipExtraField; import org.apache.commons.compress.archivers.zip.ZipFile; +import org.apache.commons.compress.utils.SeekableInMemoryByteChannel; import org.assertj.core.api.AbstractAssert; import org.assertj.core.error.BasicErrorMessageFactory; import com.github.steveash.guavate.Guavate; -public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile> { +public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile> implements AutoCloseable { interface EntryCheck { default EntryCheck compose(EntryCheck other) { return assertion -> other.test(this.test(assertion)); @@ -74,10 +77,14 @@ public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile> { } } - public static ZipAssert assertThatZip(ZipFile zipFile) { + static ZipAssert assertThatZip(ZipFile zipFile) { return new ZipAssert(zipFile); } + public static ZipAssert assertThatZip(ByteArrayOutputStream outputStream) throws IOException { + return assertThatZip(new ZipFile(new SeekableInMemoryByteChannel(outputStream.toByteArray()))); + } + private static BasicErrorMessageFactory shouldHaveSize(ZipFile zipFile, int expected, int actual) { return new BasicErrorMessageFactory("%nExpecting %s to have size %s but was %s", zipFile, expected, actual); } @@ -119,4 +126,8 @@ public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile> { return myself; } + @Override + public void close() throws Exception { + zipFile.close(); + } } diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java index e6da066..a7c4ea0 100644 --- a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java +++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java @@ -46,8 +46,6 @@ import java.io.ByteArrayOutputStream; import java.util.List; import java.util.stream.Stream; -import org.apache.commons.compress.archivers.zip.ZipFile; -import org.apache.commons.compress.utils.SeekableInMemoryByteChannel; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -71,8 +69,8 @@ class ZipperTest { @Test void archiveShouldWriteEmptyValidArchiveWhenNoMessage() throws Exception { testee.archive(NO_MAILBOXES, Stream.of(), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile).hasNoEntry(); + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.hasNoEntry(); } } @@ -80,9 +78,8 @@ class ZipperTest { void archiveShouldWriteOneMessageWhenOne() throws Exception { testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_1), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MESSAGE_ID_1.serialize()) .hasStringContent(MESSAGE_CONTENT_1)); } @@ -92,9 +89,8 @@ class ZipperTest { void archiveShouldWriteTwoMessagesWhenTwo() throws Exception { testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_1, MESSAGE_2), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MESSAGE_ID_1.serialize()) .hasStringContent(MESSAGE_CONTENT_1), hasName(MESSAGE_ID_2.serialize()) @@ -106,9 +102,8 @@ class ZipperTest { void archiveShouldWriteMetadata() throws Exception { testee.archive(NO_MAILBOXES, Stream.of(MESSAGE_1), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MESSAGE_ID_1.serialize()) .containsExtraFields(new SizeExtraField(SIZE_1)) .containsExtraFields(new UidExtraField(MESSAGE_UID_1_VALUE)) @@ -123,9 +118,8 @@ class ZipperTest { void archiveShouldWriteOneMailboxWhenPresent() throws Exception { testee.archive(ImmutableList.of(MAILBOX_1_WITHOUT_ANNOTATION), Stream.of(), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MAILBOX_1.getName() + "/") .isDirectory()); } @@ -135,9 +129,8 @@ class ZipperTest { void archiveShouldWriteMailboxesWhenPresent() throws Exception { testee.archive(ImmutableList.of(MAILBOX_1_WITHOUT_ANNOTATION, MAILBOX_2_WITHOUT_ANNOTATION), Stream.of(), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MAILBOX_1.getName() + "/") .isDirectory(), hasName(MAILBOX_2.getName() + "/") @@ -149,9 +142,8 @@ class ZipperTest { void archiveShouldWriteMailboxHierarchyWhenPresent() throws Exception { testee.archive(ImmutableList.of(MAILBOX_1_WITHOUT_ANNOTATION, MAILBOX_1_SUB_1_WITHOUT_ANNOTATION, MAILBOX_2_WITHOUT_ANNOTATION), Stream.of(), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MAILBOX_1.getName() + "/") .isDirectory(), hasName(MAILBOX_1_SUB_1.getName() + "/") @@ -165,9 +157,8 @@ class ZipperTest { void archiveShouldWriteMailboxHierarchyWhenMissingParent() throws Exception { testee.archive(ImmutableList.of(MAILBOX_1_SUB_1_WITHOUT_ANNOTATION, MAILBOX_2_WITHOUT_ANNOTATION), Stream.of(), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MAILBOX_1_SUB_1.getName() + "/") .isDirectory(), hasName(MAILBOX_2.getName() + "/") @@ -179,9 +170,8 @@ class ZipperTest { void archiveShouldWriteMailboxMetadataWhenPresent() throws Exception { testee.archive(ImmutableList.of(MAILBOX_1_WITHOUT_ANNOTATION), Stream.of(), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MAILBOX_1.getName() + "/") .containsExtraFields( new MailboxIdExtraField(MAILBOX_1.getMailboxId()), @@ -193,9 +183,8 @@ class ZipperTest { void archiveShouldWriteMailBoxWithoutAnAnnotationSubDirWhenEmpty() throws Exception { testee.archive(ImmutableList.of(MAILBOX_1_WITHOUT_ANNOTATION), Stream.of(), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MAILBOX_1.getName() + "/") ); } @@ -205,9 +194,8 @@ class ZipperTest { void archiveShouldWriteMailboxAnnotationsInASubDirWhenPresent() throws Exception { testee.archive(ImmutableList.of(new MailboxWithAnnotations(MAILBOX_1, WITH_ANNOTATION_1)), Stream.of(), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MAILBOX_1.getName() + "/"), hasName(MAILBOX_1.getName() + "/annotations/").isDirectory(), hasName(MAILBOX_1.getName() + "/annotations/" + ANNOTATION_1.getKey().asString()) @@ -219,9 +207,8 @@ class ZipperTest { void archiveShouldWriteMailboxAnnotationsInASubDirWhenTwoPresent() throws Exception { testee.archive(ImmutableList.of(new MailboxWithAnnotations(MAILBOX_1, WITH_ANNOTATION_1_AND_2)), Stream.of(), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MAILBOX_1.getName() + "/"), hasName(MAILBOX_1.getName() + "/annotations/").isDirectory(), hasName(MAILBOX_1.getName() + "/annotations/" + ANNOTATION_1.getKey().asString()) @@ -236,9 +223,8 @@ class ZipperTest { void archiveShouldWriteMailboxAnnotationsInASubDirWhenTwoPresentWithTheSameName() throws Exception { testee.archive(ImmutableList.of(new MailboxWithAnnotations(MAILBOX_1, ImmutableList.of(ANNOTATION_1, ANNOTATION_1_BIS))), Stream.of(), output); - try (ZipFile zipFile = new ZipFile(toSeekableByteChannel(output))) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(output)) { + zipAssert.containsOnlyEntriesMatching( hasName(MAILBOX_1.getName() + "/"), hasName(MAILBOX_1.getName() + "/annotations/").isDirectory(), hasName(MAILBOX_1.getName() + "/annotations/" + ANNOTATION_1.getKey().asString()) @@ -248,8 +234,4 @@ class ZipperTest { ); } } - - private SeekableInMemoryByteChannel toSeekableByteChannel(ByteArrayOutputStream output) { - return new SeekableInMemoryByteChannel(output.toByteArray()); - } } diff --git a/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageZipperTest.java b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageZipperTest.java index 9ae108f..3f59798 100644 --- a/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageZipperTest.java +++ b/mailbox/plugin/deleted-messages-vault/src/test/java/org/apache/james/vault/DeletedMessageZipperTest.java @@ -49,10 +49,9 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Stream; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; -import org.apache.commons.compress.archivers.zip.ZipFile; -import org.apache.commons.compress.utils.SeekableInMemoryByteChannel; import org.apache.james.mailbox.backup.MessageIdExtraField; import org.apache.james.mailbox.backup.SizeExtraField; +import org.apache.james.mailbox.backup.ZipAssert; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.stubbing.Answer; @@ -75,9 +74,8 @@ class DeletedMessageZipperTest { zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE, DELETED_MESSAGE_2), outputStream); - try (ZipFile zipFile = zipFile(outputStream)) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(outputStream)) { + zipAssert.containsOnlyEntriesMatching( hasName(MESSAGE_ID.serialize()).hasStringContent(MESSAGE_CONTENT), hasName(MESSAGE_ID_2.serialize()).hasStringContent(MESSAGE_CONTENT)); } @@ -89,9 +87,8 @@ class DeletedMessageZipperTest { zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE), outputStream); - try (ZipFile zipFile = zipFile(outputStream)) { - assertThatZip(zipFile) - .containsOnlyEntriesMatching( + try (ZipAssert zipAssert = assertThatZip(outputStream)) { + zipAssert.containsOnlyEntriesMatching( hasName(MESSAGE_ID.serialize()) .containsExtraFields(new MessageIdExtraField(MESSAGE_ID)) .containsExtraFields(new SizeExtraField(CONTENT.length))); @@ -194,10 +191,6 @@ class DeletedMessageZipperTest { verify(zipOutputStreamReference.get(), times(1)).close(); } - private ZipFile zipFile(ByteArrayOutputStream output) throws IOException { - return new ZipFile(new SeekableInMemoryByteChannel(output.toByteArray())); - } - private DeletedMessageZipper.DeletedMessageContentLoader spyLoadedContents(Collection<InputStream> loadedContents) { Answer<InputStream> spyedContent = invocationOnMock -> { InputStream result = spy(new ByteArrayInputStream(CONTENT)); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
