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 0cedba9a4c8232fdf0f0e1a906a398a34e0e7ba1
Author: Tran Tien Duc <[email protected]>
AuthorDate: Wed Apr 3 17:19:55 2019 +0700

    JAMES-2705 ZippAssert hasEntriesSize & allSatisfies assertions
---
 .../org/apache/james/mailbox/backup/ZipAssert.java |  38 ++++++
 .../apache/james/mailbox/backup/ZipAssertTest.java | 127 +++++++++++++++++++--
 2 files changed, 153 insertions(+), 12 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 4721ffd..17eb3d7 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
@@ -20,6 +20,7 @@
 package org.apache.james.mailbox.backup;
 
 import static 
org.apache.james.mailbox.backup.ZipArchiveEntryAssert.assertThatZipEntry;
+import static 
org.apache.james.mailbox.backup.ZipAssert.EntryCheck.defaultNoCheck;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.ByteArrayOutputStream;
@@ -30,6 +31,7 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
+import java.util.function.UnaryOperator;
 import java.util.stream.Stream;
 
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
@@ -46,6 +48,11 @@ import com.google.common.collect.ImmutableList;
 
 public class ZipAssert extends AbstractAssert<ZipAssert, ZipFile> implements 
AutoCloseable {
     interface EntryCheck {
+
+        static EntryCheck defaultNoCheck() {
+            return assertion -> assertion;
+        }
+
         default EntryCheck compose(EntryCheck other) {
             return assertion -> other.test(this.test(assertion));
         }
@@ -117,6 +124,11 @@ public class ZipAssert extends AbstractAssert<ZipAssert, 
ZipFile> implements Aut
             expectedEntries.size(), expectedEntries, entries);
     }
 
+    private static BasicErrorMessageFactory shouldHaveEntriesSize(int 
entriesSize, int expectedEntriesSize) {
+        return new BasicErrorMessageFactory("%nExpecting zipFile to contains 
%s entries but actually contains (%s) entries",
+            expectedEntriesSize, entriesSize);
+    }
+
     private static ErrorMessageFactory entriesShouldHaveSameContentAt(int 
entryIndex) {
         return new BasicErrorMessageFactory("%nExpecting zipFile entry at 
index %s has same content", entryIndex);
     }
@@ -164,6 +176,32 @@ public class ZipAssert extends AbstractAssert<ZipAssert, 
ZipFile> implements Aut
         return myself;
     }
 
+    public ZipAssert hasEntriesSize(int expectedSize) {
+        isNotNull();
+        assertThat(expectedSize).describedAs("expectedSize cannot be a 
negative number")
+            .isGreaterThanOrEqualTo(0);
+
+        ArrayList<ZipArchiveEntry> zipEntries = 
Collections.list(zipFile.getEntries());
+        if (zipEntries.size() != expectedSize) {
+            throwAssertionError(shouldHaveEntriesSize(zipEntries.size(), 
expectedSize));
+        }
+        return myself;
+    }
+
+    public ZipAssert allSatisfies(UnaryOperator<EntryChecks> 
entryChecksOperator) throws Exception {
+        isNotNull();
+        List<ZipArchiveEntry> entries = Collections.list(zipFile.getEntries());
+        for (int entryIndex = 0; entryIndex < entries.size(); entryIndex++) {
+            ZipArchiveEntry entry = entries.get(entryIndex);
+            EntryChecks composedEntryChecks = entryChecksOperator.apply(new 
EntryChecks(entry.getName(), defaultNoCheck()));
+            ZipArchiveEntryAssert zipAssertionOfEntry = 
assertThatZipEntry(zipFile, entry);
+
+            composedEntryChecks.check.test(zipAssertionOfEntry);
+        }
+
+        return myself;
+    }
+
     @Override
     public void close() throws Exception {
         zipFile.close();
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
index b000831..ce473e4 100644
--- 
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
@@ -30,6 +30,7 @@ import java.io.File;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.AbstractMap.SimpleImmutableEntry;
+import java.util.UUID;
 
 import org.apache.commons.compress.archivers.zip.ExtraFieldUtils;
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
@@ -107,6 +108,23 @@ public class ZipAssertTest {
 
     private static final SimpleImmutableEntry<String, byte[]> ENTRY = new 
SimpleImmutableEntry<>(ENTRY_NAME, ENTRY_CONTENT);
     private static final SimpleImmutableEntry<String, byte[]> ENTRY_2 = new 
SimpleImmutableEntry<>(ENTRY_NAME_2, ENTRY_CONTENT_2);
+
+    private static ZipFile zipFile(File destination, 
ZipEntryWithContent...entries) throws Exception {
+        try (ZipArchiveOutputStream archiveOutputStream = new 
ZipArchiveOutputStream(destination)) {
+            for (ZipEntryWithContent entry : entries) {
+                ZipArchiveEntry archiveEntry = (ZipArchiveEntry) 
archiveOutputStream.createArchiveEntry(new File(UUID.randomUUID().toString()), 
entry.name);
+                entry.extraFields.
+                    forEach(archiveEntry::addExtraField);
+                archiveOutputStream.putArchiveEntry(archiveEntry);
+                IOUtils.copy(entry.content, archiveOutputStream);
+                archiveOutputStream.closeArchiveEntry();
+            }
+            archiveOutputStream.finish();
+        }
+
+        return new ZipFile(destination);
+    }
+
     private File destination;
     private File destination2;
 
@@ -447,21 +465,106 @@ public class ZipAssertTest {
                     .doesNotThrowAnyException();
             }
         }
+    }
 
-        private ZipFile zipFile(File destination, 
ZipEntryWithContent...entries) throws Exception {
-            try (ZipArchiveOutputStream archiveOutputStream = new 
ZipArchiveOutputStream(destination)) {
-                for (ZipEntryWithContent entry : entries) {
-                    ZipArchiveEntry archiveEntry = (ZipArchiveEntry) 
archiveOutputStream.createArchiveEntry(new File("any"), entry.name);
-                    entry.extraFields.
-                        forEach(archiveEntry::addExtraField);
-                    archiveOutputStream.putArchiveEntry(archiveEntry);
-                    IOUtils.copy(entry.content, archiveOutputStream);
-                    archiveOutputStream.closeArchiveEntry();
-                }
-                archiveOutputStream.finish();
+    @Nested
+    class HasEntriesSize {
+
+        @Test
+        void hasEntriesSizeShouldNotThrowWhenExpectingSizeEqualsEntriesSize() 
throws Exception {
+            ZipEntryWithContent firstEntry = 
entryBuilder().name(ENTRY_NAME).content(ENTRY_CONTENT).build();
+            ZipEntryWithContent secondEntry = 
entryBuilder().name(ENTRY_NAME_2).content(ENTRY_CONTENT).build();
+
+            try (ZipFile assertedZipFile = zipFile(destination, firstEntry, 
secondEntry)) {
+                assertThatCode(() -> assertThatZip(assertedZipFile)
+                    .hasEntriesSize(2))
+                    .doesNotThrowAnyException();
             }
+        }
 
-            return new ZipFile(destination);
+        @Test
+        void hasEntriesSizeShouldNotThrowWhenNoEntriesAndExpectingSizeIsZero() 
throws Exception {
+            try (ZipFile assertedZipFile = zipFile(destination)) {
+                assertThatCode(() -> assertThatZip(assertedZipFile)
+                    .hasEntriesSize(0))
+                    .doesNotThrowAnyException();
+            }
+        }
+
+        @Test
+        void hasEntriesSizeShouldThrowWhenExpectingSizeIsNegative() throws 
Exception {
+            try (ZipFile assertedZipFile = zipFile(destination)) {
+                assertThatThrownBy(() -> assertThatZip(assertedZipFile)
+                    .hasEntriesSize(-1))
+                    .isInstanceOf(AssertionError.class);
+            }
+        }
+
+        @Test
+        void 
hasEntriesSizeShouldThrowWhenExpectingSizeDoesntEqualsEntriesSize() throws 
Exception {
+            ZipEntryWithContent firstEntry = 
entryBuilder().name(ENTRY_NAME).content(ENTRY_CONTENT).build();
+            ZipEntryWithContent secondEntry = 
entryBuilder().name(ENTRY_NAME_2).content(ENTRY_CONTENT).build();
+
+            try (ZipFile assertedZipFile = zipFile(destination, firstEntry, 
secondEntry)) {
+                assertThatThrownBy(() -> assertThatZip(assertedZipFile)
+                    .hasEntriesSize(3))
+                    .isInstanceOf(AssertionError.class);
+            }
+        }
+    }
+
+    @Nested
+    class AllSatisfies {
+
+        @Test
+        void allSatisfiesShouldNotThrowWhenNoEntries() throws Exception {
+            try (ZipFile assertedZipFile = zipFile(destination)) {
+                assertThatCode(() -> assertThatZip(assertedZipFile)
+                    .allSatisfies(entry -> entry.hasStringContent("sub 
string")))
+                    .doesNotThrowAnyException();
+            }
+        }
+
+        @Test
+        void allSatisfiesShouldNotThrowWhenAllEntriesMatchAssertion() throws 
Exception {
+            ZipEntryWithContent firstEntry = entryBuilder().name("entry 
1").content(ENTRY_CONTENT).build();
+            ZipEntryWithContent secondEntry = entryBuilder().name("entry 
2").content(ENTRY_CONTENT).build();
+
+            try (ZipFile assertedZipFile = zipFile(destination, firstEntry, 
secondEntry)) {
+                assertThatCode(() -> assertThatZip(assertedZipFile)
+                    .allSatisfies(entry -> 
entry.hasStringContent(STRING_ENTRY_CONTENT)))
+                    .doesNotThrowAnyException();
+            }
+        }
+
+        @Test
+        void allSatisfiesShouldNotThrowWhenAllEntriesMatchAllAssertions() 
throws Exception {
+            UidExtraField zipExtraField = new UidExtraField(1L);
+            ZipEntryWithContent firstEntry = entryBuilder().name("entry 
1").content(ENTRY_CONTENT)
+                .addField(zipExtraField)
+                .build();
+            ZipEntryWithContent secondEntry = entryBuilder().name("entry 
2").content(ENTRY_CONTENT)
+                .addField(zipExtraField)
+                .build();
+
+            try (ZipFile assertedZipFile = zipFile(destination, firstEntry, 
secondEntry)) {
+                assertThatCode(() -> assertThatZip(assertedZipFile)
+                    .allSatisfies(entry -> 
entry.hasStringContent(STRING_ENTRY_CONTENT))
+                    .allSatisfies(entry -> 
entry.containsExtraFields(zipExtraField)))
+                    .doesNotThrowAnyException();
+            }
+        }
+
+        @Test
+        void allSatisfiesShouldThrowWhenNotAllEntriesMatchAssertion() throws 
Exception {
+            ZipEntryWithContent firstEntry = entryBuilder().name("entry 
1").content(ENTRY_CONTENT).build();
+            ZipEntryWithContent secondEntry = entryBuilder().name("entry 
2").content(ENTRY_CONTENT).build();
+
+            try (ZipFile assertedZipFile = zipFile(destination, firstEntry, 
secondEntry)) {
+                assertThatThrownBy(() -> assertThatZip(assertedZipFile)
+                    .allSatisfies(entry -> entry.hasName("entry 1")))
+                    .isInstanceOf(AssertionError.class);
+            }
         }
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to