You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2018/06/19 08:11:31 UTC
[03/11] james-project git commit: JAMES-2426 Relax and test ZipAsserts
JAMES-2426 Relax and test ZipAsserts
Order can not be enforced as it highly depends on common-compress internals.
This commit makes the choice to enforce order by natural entry-name order so that we have a reference order. The downside is that entry names needs to be present.
Also, extra field ordering had been relaxed.
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/32ed4d8b
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/32ed4d8b
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/32ed4d8b
Branch: refs/heads/master
Commit: 32ed4d8b721f719b7c0c89f76605f634089d1841
Parents: feb1801
Author: benwa <bt...@linagora.com>
Authored: Mon Jun 18 12:06:16 2018 +0700
Committer: benwa <bt...@linagora.com>
Committed: Tue Jun 19 15:07:55 2018 +0700
----------------------------------------------------------------------
.../mailbox/backup/ZipArchiveEntryAssert.java | 26 +-
.../apache/james/mailbox/backup/ZipAssert.java | 52 ++-
.../james/mailbox/backup/ZipAssertTest.java | 328 +++++++++++++++++++
.../apache/james/mailbox/backup/ZipperTest.java | 28 +-
4 files changed, 397 insertions(+), 37 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/32ed4d8b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipArchiveEntryAssert.java
----------------------------------------------------------------------
diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipArchiveEntryAssert.java b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipArchiveEntryAssert.java
index 4f268ec..2a6aae3 100644
--- a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipArchiveEntryAssert.java
+++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipArchiveEntryAssert.java
@@ -19,10 +19,11 @@
package org.apache.james.mailbox.backup;
+import static org.assertj.core.api.Assertions.assertThat;
+
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipExtraField;
@@ -49,15 +50,6 @@ public class ZipArchiveEntryAssert extends AbstractAssert<ZipArchiveEntryAssert,
return new BasicErrorMessageFactory("%nExpecting %s to have content %s but was %s", zipArchiveEntry, expectedContent, actualContent);
}
- private static BasicErrorMessageFactory shouldHaveExtraFields(ZipArchiveEntry zipArchiveEntry,
- ZipExtraField[] expectedExtraFields,
- ZipExtraField[] actualExtraFields) {
- return new BasicErrorMessageFactory("%nExpecting %s to contain exactly being %s" +
- " but was containing being %s", zipArchiveEntry,
- Arrays.toString(expectedExtraFields),
- Arrays.toString(actualExtraFields));
- }
-
private final ZipFile zipFile;
private final ZipArchiveEntry actual;
@@ -88,17 +80,15 @@ public class ZipArchiveEntryAssert extends AbstractAssert<ZipArchiveEntryAssert,
return myself;
}
- public ZipArchiveEntryAssert containsExactlyExtraFields(ZipExtraField... expectedExtraFields) {
+ public ZipArchiveEntryAssert containsExtraFields(ZipExtraField... expectedExtraFields) {
isNotNull();
ZipExtraField[] actualExtraFields = actual.getExtraFields();
- if (expectedExtraFields.length != actualExtraFields.length) {
- throwAssertionError(shouldHaveExtraFields(actual, expectedExtraFields, actualExtraFields));
- }
- for (int i = 0; i < expectedExtraFields.length; i++) {
- if (!expectedExtraFields[i].equals(actualExtraFields[i])) {
- throwAssertionError(shouldHaveExtraFields(actual, expectedExtraFields, actualExtraFields));
- }
+ if (expectedExtraFields.length == 0) {
+ return myself;
}
+ assertThat(actualExtraFields)
+ .as(String.format("Asserting Zip entry %s extra fields.", actual))
+ .contains(expectedExtraFields);
return myself;
}
}
http://git-wip-us.apache.org/repos/asf/james-project/blob/32ed4d8b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssert.java
----------------------------------------------------------------------
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 f521b20..c007421 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,17 +21,53 @@ package org.apache.james.mailbox.backup;
import static org.apache.james.mailbox.backup.ZipArchiveEntryAssert.assertThatZipEntry;
+import java.util.Arrays;
import java.util.Collections;
+import java.util.Comparator;
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.assertj.core.api.AbstractAssert;
import org.assertj.core.error.BasicErrorMessageFactory;
+import com.github.steveash.guavate.Guavate;
+
public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile> {
interface EntryCheck {
- void test(ZipArchiveEntryAssert assertion) throws Exception;
+ default EntryCheck compose(EntryCheck other) {
+ return assertion -> other.test(this.test(assertion));
+ }
+
+ ZipArchiveEntryAssert test(ZipArchiveEntryAssert assertion) throws Exception;
+ }
+
+ public static class EntryChecks {
+ public static EntryChecks hasName(String name) {
+ return new EntryChecks(name, assertion -> assertion.hasName(name));
+ }
+
+ private final String name;
+ private final EntryCheck check;
+
+ private EntryChecks(String name, EntryCheck check) {
+ this.name = name;
+ this.check = check;
+ }
+
+ public EntryChecks check(EntryCheck additionalCheck) {
+ return new EntryChecks(name,
+ check.compose(additionalCheck));
+ }
+
+ public EntryChecks hasStringContent(String stringConyent) {
+ return check(check.compose(assertion -> assertion.hasStringContent(stringConyent)));
+ }
+
+ public EntryChecks containsExtraFields(ZipExtraField... expectedExtraFields) {
+ return check(check.compose(assertion -> assertion.containsExtraFields(expectedExtraFields)));
+ }
}
public static ZipAssert assertThatZip(ZipFile zipFile) {
@@ -39,7 +75,7 @@ public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile> {
}
private static BasicErrorMessageFactory shouldHaveSize(ZipFile zipFile, int expected, int actual) {
- return new BasicErrorMessageFactory("%nExpecting %s to have side %d but was %d", zipFile, expected, actual);
+ return new BasicErrorMessageFactory("%nExpecting %s to have side %s but was %s", zipFile, expected, actual);
}
private static BasicErrorMessageFactory shouldBeEmpty(ZipFile zipFile) {
@@ -53,14 +89,20 @@ public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile> {
this.zipFile = zipFile;
}
- public ZipAssert containsExactlyEntriesMatching(EntryCheck... entryChecks) throws Exception {
+ public ZipAssert containsOnlyEntriesMatching(EntryChecks... entryChecks) throws Exception {
isNotNull();
- List<ZipArchiveEntry> entries = Collections.list(zipFile.getEntries());
+ List<EntryChecks> sortedEntryChecks= Arrays.stream(entryChecks)
+ .sorted(Comparator.comparing(checks -> checks.name))
+ .collect(Guavate.toImmutableList());
+ List<ZipArchiveEntry> entries = Collections.list(zipFile.getEntries())
+ .stream()
+ .sorted(Comparator.comparing(ZipArchiveEntry::getName))
+ .collect(Guavate.toImmutableList());
if (entries.size() != entryChecks.length) {
throwAssertionError(shouldHaveSize(zipFile, entryChecks.length, entries.size()));
}
for (int i = 0; i < entries.size(); i++) {
- entryChecks[i].test(assertThatZipEntry(zipFile, entries.get(i)));
+ sortedEntryChecks.get(i).check.test(assertThatZipEntry(zipFile, entries.get(i)));
}
return myself;
}
http://git-wip-us.apache.org/repos/asf/james-project/blob/32ed4d8b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssertTest.java
----------------------------------------------------------------------
diff --git a/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssertTest.java b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssertTest.java
new file mode 100644
index 0000000..b903eb2
--- /dev/null
+++ b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipAssertTest.java
@@ -0,0 +1,328 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.mailbox.backup;
+
+import static org.apache.james.mailbox.backup.ZipAssert.EntryChecks.hasName;
+import static org.apache.james.mailbox.backup.ZipAssert.assertThatZip;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.commons.compress.archivers.zip.ExtraFieldUtils;
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
+import org.apache.commons.compress.archivers.zip.ZipFile;
+import org.apache.commons.io.IOUtils;
+import org.apache.james.junit.TemporaryFolderExtension;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+@ExtendWith(TemporaryFolderExtension.class)
+public class ZipAssertTest {
+ public static final String ENTRY_NAME = "entryName";
+ public static final String ENTRY_NAME_2 = "entryName2";
+ public static final String STRING_ENTRY_CONTENT = "abcdefghijkl";
+ public static final String STRING_ENTRY_CONTENT_2 = "mnopqrstuvwxyz";
+ public static final byte[] ENTRY_CONTENT = STRING_ENTRY_CONTENT.getBytes(StandardCharsets.UTF_8);
+ public static final byte[] ENTRY_CONTENT_2 = STRING_ENTRY_CONTENT_2.getBytes(StandardCharsets.UTF_8);
+ public static final SizeExtraField EXTRA_FIELD = new SizeExtraField(42);
+
+
+ private File destination;
+
+ @BeforeEach
+ void beforeEach(TemporaryFolderExtension.TemporaryFolder temporaryFolder) throws Exception {
+ destination = File.createTempFile("backup-test", ".zip", temporaryFolder.getTempDir());
+
+ ExtraFieldUtils.register(SizeExtraField.class);
+ }
+
+ @Test
+ public void hasNoEntryShouldNotThrowWhenEmpty() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+ archiveOutputStream.finish();
+ }
+
+ assertThatCode(() -> assertThatZip(new ZipFile(destination))
+ .hasNoEntry())
+ .doesNotThrowAnyException();
+ }
+
+ @Test
+ public void hasNoEntryShouldThrowWhenNotEmpty() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatThrownBy(() -> assertThatZip(new ZipFile(destination))
+ .hasNoEntry())
+ .isInstanceOf(AssertionError.class);
+ }
+
+ @Test
+ public void containsExactlyEntriesMatchingShouldNotThrowWhenBothEmpty() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+ archiveOutputStream.finish();
+ }
+
+ assertThatCode(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching())
+ .doesNotThrowAnyException();
+ }
+
+ @Test
+ public void containsExactlyEntriesMatchingShouldNotThrowWhenRightOrder() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ ZipArchiveEntry archiveEntry2 = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME_2);
+ archiveOutputStream.putArchiveEntry(archiveEntry2);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT_2), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatCode(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME),
+ hasName(ENTRY_NAME_2)))
+ .doesNotThrowAnyException();
+ }
+
+ @Test
+ public void hasNameShouldThrowWhenWrongName() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+ archiveOutputStream.finish();
+ }
+
+ assertThatThrownBy(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME_2)))
+ .isInstanceOf(AssertionError.class);
+ }
+
+ @Test
+ public void containsExactlyEntriesMatchingShouldNotThrowWhenWrongOrder() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ ZipArchiveEntry archiveEntry2 = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME_2);
+ archiveOutputStream.putArchiveEntry(archiveEntry2);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT_2), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatCode(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME),
+ hasName(ENTRY_NAME_2)))
+ .doesNotThrowAnyException();
+ }
+
+ @Test
+ public void containsExactlyEntriesMatchingShouldThrowWhenExpectingMoreEntries() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ ZipArchiveEntry archiveEntry2 = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME_2);
+ archiveOutputStream.putArchiveEntry(archiveEntry2);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT_2), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatThrownBy(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME),
+ hasName(ENTRY_NAME_2),
+ hasName("extraEntry")))
+ .isInstanceOf(AssertionError.class);
+ }
+
+ @Test
+ public void containsExactlyEntriesMatchingShouldThrowWhenExpectingLessEntries() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ ZipArchiveEntry archiveEntry2 = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME_2);
+ archiveOutputStream.putArchiveEntry(archiveEntry2);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT_2), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatThrownBy(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME)))
+ .isInstanceOf(AssertionError.class);
+ }
+
+ @Test
+ public void hasStringContentShouldNotThrowWhenIdentical() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatCode(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME)
+ .hasStringContent(STRING_ENTRY_CONTENT)))
+ .doesNotThrowAnyException();
+ }
+
+ @Test
+ public void hasStringContentShouldThrowWhenDifferent() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatThrownBy(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME)
+ .hasStringContent(STRING_ENTRY_CONTENT_2)))
+ .isInstanceOf(AssertionError.class);
+ }
+
+ @Test
+ public void containsExactlyExtraFieldsShouldNotThrowWhenBothEmpty() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatCode(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME)
+ .containsExtraFields()))
+ .doesNotThrowAnyException();
+ }
+
+ @Test
+ public void containsExactlyExtraFieldsShouldThrowWhenMissingExpectedField() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatThrownBy(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME)
+ .containsExtraFields(EXTRA_FIELD)))
+ .isInstanceOf(AssertionError.class);
+ }
+
+ @Test
+ public void containsExactlyExtraFieldsShouldNotThrowWhenUnexpectedField() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveEntry.addExtraField(EXTRA_FIELD);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatCode(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME)
+ .containsExtraFields()))
+ .doesNotThrowAnyException();
+ }
+
+ @Test
+ public void containsExactlyExtraFieldsShouldNotThrowWhenContainingExpectedExtraFields() throws Exception {
+ try (ZipArchiveOutputStream archiveOutputStream = new ZipArchiveOutputStream(destination)) {
+
+ ZipArchiveEntry archiveEntry = (ZipArchiveEntry) archiveOutputStream.createArchiveEntry(new File("any"), ENTRY_NAME);
+ archiveEntry.addExtraField(EXTRA_FIELD);
+ archiveOutputStream.putArchiveEntry(archiveEntry);
+ IOUtils.copy(new ByteArrayInputStream(ENTRY_CONTENT), archiveOutputStream);
+ archiveOutputStream.closeArchiveEntry();
+
+ archiveOutputStream.finish();
+ }
+
+ assertThatCode(() -> assertThatZip(new ZipFile(destination))
+ .containsOnlyEntriesMatching(
+ hasName(ENTRY_NAME)
+ .containsExtraFields(EXTRA_FIELD)))
+ .doesNotThrowAnyException();
+ }
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/32ed4d8b/mailbox/backup/src/test/java/org/apache/james/mailbox/backup/ZipperTest.java
----------------------------------------------------------------------
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 ed4e17a..824b022 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
@@ -25,11 +25,13 @@ import static org.apache.james.mailbox.backup.MailboxMessageFixture.MESSAGE_CONT
import static org.apache.james.mailbox.backup.MailboxMessageFixture.MESSAGE_ID_1;
import static org.apache.james.mailbox.backup.MailboxMessageFixture.MESSAGE_ID_2;
import static org.apache.james.mailbox.backup.MailboxMessageFixture.SIZE_1;
+import static org.apache.james.mailbox.backup.ZipAssert.EntryChecks.hasName;
import static org.apache.james.mailbox.backup.ZipAssert.assertThatZip;
import java.io.File;
import java.io.FileOutputStream;
+import org.apache.commons.compress.archivers.zip.ExtraFieldUtils;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.james.junit.TemporaryFolderExtension;
import org.apache.james.junit.TemporaryFolderExtension.TemporaryFolder;
@@ -48,6 +50,8 @@ public class ZipperTest {
void beforeEach(TemporaryFolder temporaryFolder) throws Exception {
testee = new Zipper();
destination = File.createTempFile("backup-test", ".zip", temporaryFolder.getTempDir());
+
+ ExtraFieldUtils.register(SizeExtraField.class);
}
@Test
@@ -65,9 +69,8 @@ public class ZipperTest {
try (ZipFile zipFile = new ZipFile(destination)) {
assertThatZip(zipFile)
- .containsExactlyEntriesMatching(
- zipEntryAssert -> zipEntryAssert
- .hasName(MESSAGE_ID_1.serialize())
+ .containsOnlyEntriesMatching(
+ hasName(MESSAGE_ID_1.serialize())
.hasStringContent(MESSAGE_CONTENT_1));
}
}
@@ -78,12 +81,10 @@ public class ZipperTest {
try (ZipFile zipFile = new ZipFile(destination)) {
assertThatZip(zipFile)
- .containsExactlyEntriesMatching(
- zipEntryAssert -> zipEntryAssert
- .hasName(MESSAGE_ID_1.serialize())
+ .containsOnlyEntriesMatching(
+ hasName(MESSAGE_ID_1.serialize())
.hasStringContent(MESSAGE_CONTENT_1),
- zipEntryAssert -> zipEntryAssert
- .hasName(MESSAGE_ID_2.serialize())
+ hasName(MESSAGE_ID_2.serialize())
.hasStringContent(MESSAGE_CONTENT_2));
}
}
@@ -95,9 +96,8 @@ public class ZipperTest {
try (ZipFile zipFile = new ZipFile(destination)) {
assertThatZip(zipFile)
- .containsExactlyEntriesMatching(
- zipEntryAssert -> zipEntryAssert
- .hasName(MESSAGE_ID_2.serialize())
+ .containsOnlyEntriesMatching(
+ hasName(MESSAGE_ID_2.serialize())
.hasStringContent(MESSAGE_CONTENT_2));
}
}
@@ -108,9 +108,9 @@ public class ZipperTest {
try (ZipFile zipFile = new ZipFile(destination)) {
assertThatZip(zipFile)
- .containsExactlyEntriesMatching(
- zipEntryAssert -> zipEntryAssert
- .containsExactlyExtraFields(new SizeExtraField(SIZE_1)));
+ .containsOnlyEntriesMatching(
+ hasName(MESSAGE_ID_1.serialize())
+ .containsExtraFields(new SizeExtraField(SIZE_1)));
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org