From da55c5807011350f872628655dd996601f31600c Mon Sep 17 00:00:00 2001 From: Srikanth Reddy Lingala Date: Mon, 24 May 2021 07:28:09 +0200 Subject: [PATCH] #313 Add and adjust tests --- .../net/lingala/zip4j/AddFilesToZipIT.java | 14 ++++++++++ .../java/net/lingala/zip4j/ZipFileTest.java | 26 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java b/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java index f48dd7d9..89943abc 100644 --- a/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java +++ b/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java @@ -255,6 +255,7 @@ public void testAddFileProgressMonitorThrowsExceptionWhenPerformingActionInBusyS expectedException.expect(ZipException.class); ZipFile zipFile = new ZipFile(generatedZipFile); + zipFile.setRunInThread(true); ProgressMonitor progressMonitor = zipFile.getProgressMonitor(); progressMonitor.setState(ProgressMonitor.State.BUSY); @@ -610,6 +611,19 @@ public void testAddFolderWithProgressMonitor() throws IOException, InterruptedEx ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, 13); } + @Test + public void testAddFolderProgressMonitorThrowsExceptionWhenPerformingActionInBusyState() throws ZipException { + expectedException.expectMessage("invalid operation - Zip4j is in busy state"); + expectedException.expect(ZipException.class); + + ZipFile zipFile = new ZipFile(generatedZipFile); + zipFile.setRunInThread(true); + ProgressMonitor progressMonitor = zipFile.getProgressMonitor(); + progressMonitor.setState(ProgressMonitor.State.BUSY); + + zipFile.addFile(TestUtils.getTestFileFromResources("")); + } + @Test public void testAddFolderWithNotNormalizedPath() throws IOException { ZipFile zipFile = new ZipFile(generatedZipFile); diff --git a/src/test/java/net/lingala/zip4j/ZipFileTest.java b/src/test/java/net/lingala/zip4j/ZipFileTest.java index 3b8b2aab..9a59c6a1 100644 --- a/src/test/java/net/lingala/zip4j/ZipFileTest.java +++ b/src/test/java/net/lingala/zip4j/ZipFileTest.java @@ -155,6 +155,7 @@ public void testAddFileAsFileThrowsExceptionWhenParametersIsNull() throws ZipExc @Test public void testAddFileAsFileThrowsExceptionWhenProgressMonitorStateIsBusy() throws ZipException { File fileToAdd = mockFile(true); + zipFile.setRunInThread(true); zipFile.getProgressMonitor().setState(ProgressMonitor.State.BUSY); expectedException.expect(ZipException.class); @@ -182,6 +183,7 @@ public void testAddFilesThrowsExceptionWhenInputFilesIsEmpty() throws ZipExcepti @Test public void testAddFilesThrowsExceptionWhenProgressMonitorStateIsBusy() throws ZipException { zipFile.getProgressMonitor().setState(ProgressMonitor.State.BUSY); + zipFile.setRunInThread(true); expectedException.expect(ZipException.class); expectedException.expectMessage("invalid operation - Zip4j is in busy state"); @@ -216,6 +218,7 @@ public void testAddFilesWithParametersThrowsExceptionWhenParametersAreNull() thr @Test public void testAddFilesWithParametersThrowsExceptionWhenProgressMonitorStateIsBusy() throws ZipException { + zipFile.setRunInThread(true); zipFile.getProgressMonitor().setState(ProgressMonitor.State.BUSY); expectedException.expect(ZipException.class); @@ -318,6 +321,18 @@ public void testAddFolderWithParametersThrowsExceptionWhenInputParametersAreNull zipFile.addFolder(folderToAdd, null); } + @Test + public void testAddFolderThrowsExceptionWhenProgressMonitorStateIsBusy() throws ZipException { + File folderToAdd = mockFolder(); + zipFile.setRunInThread(true); + zipFile.getProgressMonitor().setState(ProgressMonitor.State.BUSY); + + expectedException.expect(ZipException.class); + expectedException.expectMessage("invalid operation - Zip4j is in busy state"); + + zipFile.addFolder(folderToAdd, new ZipParameters()); + } + @Test public void testAddStreamThrowsExceptionWhenInputStreamIsNull() throws ZipException { expectedException.expectMessage("inputstream is null, cannot add file to zip"); @@ -376,6 +391,7 @@ public void testExtractFileWithFileHeaderWhenDestinationIsEmptyThrowsException() @Test public void testExtractFileWithFileHeaderWhenBusyStateThrowsException() throws ZipException { + zipFile.setRunInThread(true); zipFile.getProgressMonitor().setState(ProgressMonitor.State.BUSY); expectedException.expectMessage("invalid operation - Zip4j is in busy state"); @@ -625,4 +641,12 @@ private File mockFile(boolean fileExists) { return file; } -} \ No newline at end of file + private File mockFolder() { + File folder = mock(File.class); + when(folder.exists()).thenReturn(true); + when(folder.toString()).thenReturn("SOME_PATH"); + when(folder.isDirectory()).thenReturn(true); + when(folder.canRead()).thenReturn(true); + return folder; + } +}