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 bce0b04b4cf36ad14597eb716414259a2003a9c7
Author: Benoit Tellier <[email protected]>
AuthorDate: Fri Mar 22 11:39:54 2019 +0700

    MAILBOX-385 DeletedMessageZipperTest: nested class + method move
---
 .../james/vault/DeletedMessageZipperTest.java      | 191 +++++++++++----------
 1 file changed, 99 insertions(+), 92 deletions(-)

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 3f59798..2c8bfdf 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
@@ -53,6 +53,7 @@ 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.Nested;
 import org.junit.jupiter.api.Test;
 import org.mockito.stubbing.Answer;
 
@@ -68,127 +69,133 @@ class DeletedMessageZipperTest {
         zipper = spy(new DeletedMessageZipper());
     }
 
-    @Test
-    void zipShouldPutEntriesToOutputStream() throws Exception {
-        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-
-        zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE, 
DELETED_MESSAGE_2), outputStream);
-
-        try (ZipAssert zipAssert = assertThatZip(outputStream)) {
-            zipAssert.containsOnlyEntriesMatching(
-                    
hasName(MESSAGE_ID.serialize()).hasStringContent(MESSAGE_CONTENT),
-                    
hasName(MESSAGE_ID_2.serialize()).hasStringContent(MESSAGE_CONTENT));
+    @Nested
+    class NormalBehaviourTest {
+        @Test
+        void constructorShouldNotFailWhenCalledMultipleTimes() {
+            assertThatCode(() -> {
+                new DeletedMessageZipper();
+                new DeletedMessageZipper();
+            }).doesNotThrowAnyException();
         }
-    }
 
-    @Test
-    void zipShouldPutExtraFields() throws Exception {
-        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        @Test
+        void zipShouldPutEntriesToOutputStream() throws Exception {
+            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
-        zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE), outputStream);
+            zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE, 
DELETED_MESSAGE_2), outputStream);
 
-        try (ZipAssert zipAssert = assertThatZip(outputStream)) {
-            zipAssert.containsOnlyEntriesMatching(
-                    hasName(MESSAGE_ID.serialize())
-                        .containsExtraFields(new 
MessageIdExtraField(MESSAGE_ID))
-                        .containsExtraFields(new 
SizeExtraField(CONTENT.length)));
+            try (ZipAssert zipAssert = assertThatZip(outputStream)) {
+                zipAssert.containsOnlyEntriesMatching(
+                        
hasName(MESSAGE_ID.serialize()).hasStringContent(MESSAGE_CONTENT),
+                        
hasName(MESSAGE_ID_2.serialize()).hasStringContent(MESSAGE_CONTENT));
+            }
         }
-    }
 
-    @Test
-    void constructorShouldNotFailWhenCalledMultipleTimes() {
-        assertThatCode(() -> {
-                new DeletedMessageZipper();
-                new DeletedMessageZipper();
-            }).doesNotThrowAnyException();
-    }
+        @Test
+        void zipShouldPutExtraFields() throws Exception {
+            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
-    @Test
-    void zipShouldCloseAllResourcesStreamWhenFinishZipping() throws Exception {
-        Collection<InputStream> loadedContents = new ConcurrentLinkedQueue<>();
+            zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE), 
outputStream);
 
-        zipper.zip(spyLoadedContents(loadedContents), 
Stream.of(DELETED_MESSAGE, DELETED_MESSAGE_2), new ByteArrayOutputStream());
+            try (ZipAssert zipAssert = assertThatZip(outputStream)) {
+                zipAssert.containsOnlyEntriesMatching(
+                        hasName(MESSAGE_ID.serialize())
+                            .containsExtraFields(new 
MessageIdExtraField(MESSAGE_ID))
+                            .containsExtraFields(new 
SizeExtraField(CONTENT.length)));
+            }
+        }
 
-        assertThat(loadedContents)
-            .hasSize(2)
-            .allSatisfy(Throwing.consumer(content -> verify(content, 
times(1)).close()));
-    }
+        @Test
+        void zipShouldTerminateZipArchiveStreamWhenFinishZipping() throws 
Exception {
+            AtomicReference<ZipArchiveOutputStream> zipOutputStreamReference = 
new AtomicReference<>();
+            
when(zipper.newZipArchiveOutputStream(any())).thenAnswer(spyZipOutPutStream(zipOutputStreamReference));
 
-    @Test
-    void zipShouldTerminateZipArchiveStreamWhenFinishZipping() throws 
Exception {
-        AtomicReference<ZipArchiveOutputStream> zipOutputStreamReference = new 
AtomicReference<>();
-        
when(zipper.newZipArchiveOutputStream(any())).thenAnswer(spyZipOutPutStream(zipOutputStreamReference));
+            zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE, 
DELETED_MESSAGE_2), new ByteArrayOutputStream());
 
-        zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE, 
DELETED_MESSAGE_2), new ByteArrayOutputStream());
+            verify(zipOutputStreamReference.get(), times(1)).finish();
+            verify(zipOutputStreamReference.get(), times(1)).close();
+        }
 
-        verify(zipOutputStreamReference.get(), times(1)).finish();
-        verify(zipOutputStreamReference.get(), times(1)).close();
-    }
+        @Test
+        void zipShouldCloseAllResourcesStreamWhenFinishZipping() throws 
Exception {
+            Collection<InputStream> loadedContents = new 
ConcurrentLinkedQueue<>();
 
-    @Test
-    void zipShouldThrowWhenCreateEntryGetException() throws Exception {
-        doThrow(new IOException("mocked 
exception")).when(zipper).createEntry(any(), any());
+            zipper.zip(spyLoadedContents(loadedContents), 
Stream.of(DELETED_MESSAGE, DELETED_MESSAGE_2), new ByteArrayOutputStream());
 
-        assertThatThrownBy(() -> zipper.zip(CONTENT_LOADER, 
Stream.of(DELETED_MESSAGE, DELETED_MESSAGE_2), new ByteArrayOutputStream()))
-            .isInstanceOf(IOException.class);
+            assertThat(loadedContents)
+                .hasSize(2)
+                .allSatisfy(Throwing.consumer(content -> verify(content, 
times(1)).close()));
+        }
     }
 
-    @Test
-    void zipShouldThrowWhenPutMessageToEntryGetException() throws Exception {
-        doThrow(new IOException("mocked 
exception")).when(zipper).putMessageToEntry(any(), any(), any());
+    @Nested
+    class FailingBehaviourTest {
+        @Test
+        void zipShouldThrowWhenCreateEntryGetException() throws Exception {
+            doThrow(new IOException("mocked 
exception")).when(zipper).createEntry(any(), any());
 
-        assertThatThrownBy(() -> zipper.zip(CONTENT_LOADER, 
Stream.of(DELETED_MESSAGE, DELETED_MESSAGE_2), new ByteArrayOutputStream()))
-            .isInstanceOf(IOException.class);
-    }
+            assertThatThrownBy(() -> zipper.zip(CONTENT_LOADER, 
Stream.of(DELETED_MESSAGE, DELETED_MESSAGE_2), new ByteArrayOutputStream()))
+                .isInstanceOf(IOException.class);
+        }
 
-    @Test
-    void zipShouldStopLoadingResourcesWhenGettingException() throws Exception {
-        doThrow(new IOException("mocked 
exception")).when(zipper).createEntry(any(), any());
-        DeletedMessageContentLoader contentLoader = spy(new 
DeletedMessageContentLoader() {
-            // lambdas are final and thus can't be spied
-            @Override
-            public InputStream load(DeletedMessage deletedMessage) {
-                return new ByteArrayInputStream(CONTENT);
-            }
-        });
+        @Test
+        void zipShouldThrowWhenPutMessageToEntryGetException() throws 
Exception {
+            doThrow(new IOException("mocked 
exception")).when(zipper).putMessageToEntry(any(), any(), any());
 
-        try {
-            zipper.zip(contentLoader, Stream.of(DELETED_MESSAGE, 
DELETED_MESSAGE_2), new ByteArrayOutputStream());
-        } catch (Exception e) {
-            // ignored
+            assertThatThrownBy(() -> zipper.zip(CONTENT_LOADER, 
Stream.of(DELETED_MESSAGE, DELETED_MESSAGE_2), new ByteArrayOutputStream()))
+                .isInstanceOf(IOException.class);
         }
 
-        verify(contentLoader, times(1)).load(any());
-    }
+        @Test
+        void zipShouldTerminateZipArchiveStreamWhenGettingException() throws 
Exception {
+            doThrow(new IOException("mocked 
exception")).when(zipper).putMessageToEntry(any(), any(), any());
+            AtomicReference<ZipArchiveOutputStream> zipOutputStreamReference = 
new AtomicReference<>();
+            
when(zipper.newZipArchiveOutputStream(any())).thenAnswer(spyZipOutPutStream(zipOutputStreamReference));
 
-    @Test
-    void zipShouldCloseParameterOutputStreamWhenGettingException() throws 
Exception {
-        doThrow(new IOException("mocked 
exception")).when(zipper).putMessageToEntry(any(), any(), any());
-        ByteArrayOutputStream outputStream = spy(new ByteArrayOutputStream());
+            try {
+                zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE, 
DELETED_MESSAGE_2), new ByteArrayOutputStream());
+            } catch (Exception e) {
+                // ignored
+            }
 
-        try {
-            zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE), 
outputStream);
-        } catch (Exception e) {
-            // ignored
+            verify(zipOutputStreamReference.get(), times(1)).finish();
+            verify(zipOutputStreamReference.get(), times(1)).close();
         }
 
-        verify(outputStream, times(1)).close();
-    }
+        @Test
+        void zipShouldCloseParameterOutputStreamWhenGettingException() throws 
Exception {
+            doThrow(new IOException("mocked 
exception")).when(zipper).putMessageToEntry(any(), any(), any());
+            ByteArrayOutputStream outputStream = spy(new 
ByteArrayOutputStream());
 
-    @Test
-    void zipShouldTerminateZipArchiveStreamWhenGettingException() throws 
Exception {
-        doThrow(new IOException("mocked 
exception")).when(zipper).putMessageToEntry(any(), any(), any());
-        AtomicReference<ZipArchiveOutputStream> zipOutputStreamReference = new 
AtomicReference<>();
-        
when(zipper.newZipArchiveOutputStream(any())).thenAnswer(spyZipOutPutStream(zipOutputStreamReference));
+            try {
+                zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE), 
outputStream);
+            } catch (Exception e) {
+                // ignored
+            }
 
-        try {
-            zipper.zip(CONTENT_LOADER, Stream.of(DELETED_MESSAGE, 
DELETED_MESSAGE_2), new ByteArrayOutputStream());
-        } catch (Exception e) {
-            // ignored
+            verify(outputStream, times(1)).close();
         }
 
-        verify(zipOutputStreamReference.get(), times(1)).finish();
-        verify(zipOutputStreamReference.get(), times(1)).close();
+        @Test
+        void zipShouldStopLoadingResourcesWhenGettingException() throws 
Exception {
+            doThrow(new IOException("mocked 
exception")).when(zipper).createEntry(any(), any());
+            DeletedMessageContentLoader contentLoader = spy(new 
DeletedMessageContentLoader() {
+                // lambdas are final and thus can't be spied
+                @Override
+                public InputStream load(DeletedMessage deletedMessage) {
+                    return new ByteArrayInputStream(CONTENT);
+                }
+            });
+
+            try {
+                zipper.zip(contentLoader, Stream.of(DELETED_MESSAGE, 
DELETED_MESSAGE_2), new ByteArrayOutputStream());
+            } catch (Exception e) {
+                // ignored
+            }
+
+            verify(contentLoader, times(1)).load(any());
+        }
     }
 
     private DeletedMessageZipper.DeletedMessageContentLoader 
spyLoadedContents(Collection<InputStream> loadedContents) {


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

Reply via email to