1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.tools; |
17 | |
18 | import java.io.File; |
19 | import java.util.zip.ZipException; |
20 | |
21 | import junit.framework.TestCase; |
22 | import org.apache.commons.logging.Log; |
23 | import org.apache.commons.logging.LogFactory; |
24 | |
25 | /** |
26 | * TestCase for ExtractZipTask. |
27 | * |
28 | * @author Thomas Aglassinger |
29 | */ |
30 | public class ExtractZipTaskTest extends TestCase |
31 | { |
32 | private FileTools fileTools; |
33 | private ImageTools imageTools; |
34 | private Log logger; |
35 | private ProgressFrame progressFrame; |
36 | private TestTools testTools; |
37 | |
38 | protected void setUp() |
39 | throws Exception { |
40 | super.setUp(); |
41 | testTools = TestTools.instance(); |
42 | logger = LogFactory.getLog(ExtractZipTaskTest.class); |
43 | fileTools = FileTools.instance(); |
44 | imageTools = ImageTools.instance(); |
45 | progressFrame = new ProgressFrame(); |
46 | progressFrame.setTitle(getClass().getName()); |
47 | } |
48 | |
49 | public void testExtract() |
50 | throws Exception { |
51 | String method = "testExtract"; |
52 | File zipFile = testTools.getTestFile(TestTools.THREE_PAGE_COMIC); |
53 | File targetFolder = testTools.getCleanTestOutputFolder(getClass().getName() + "." + method); |
54 | |
55 | progressFrame.setVisible(true); |
56 | progressFrame.setNote(method); |
57 | |
58 | Task extractZipTask = new ExtractZipTask(zipFile, targetFolder); |
59 | |
60 | try { |
61 | extractZipTask.start(); |
62 | assertTrue(targetFolder.exists()); |
63 | |
64 | File[] imageFiles = fileTools.listFilesRecursively(targetFolder); |
65 | |
66 | assertEquals("number of files extracted to " + targetFolder, 3, imageFiles.length); |
67 | // Make sure the extracted images can be read. |
68 | for (int i = 0; i < imageFiles.length; i += 1) { |
69 | imageTools.readImage(imageFiles[i]); |
70 | } |
71 | } finally { |
72 | progressFrame.setVisible(false); |
73 | } |
74 | } |
75 | |
76 | public void testExtractInternalError() |
77 | throws Exception { |
78 | String method = "testExtractInternalError"; |
79 | File zipFile = testTools.getTestFile(TestTools.TEST_COMIC_INTERNAL_ERROR); |
80 | File targetFolder = testTools.getCleanTestOutputFolder(getClass().getName() + "." + method); |
81 | |
82 | progressFrame.setVisible(true); |
83 | progressFrame.setNote(method); |
84 | |
85 | Task extractZipTask = new ExtractZipTask(zipFile, targetFolder); |
86 | |
87 | try { |
88 | extractZipTask.start(); |
89 | fail("broken archive must cause error: " + zipFile); |
90 | } catch (ZipException error) { |
91 | if (logger.isDebugEnabled()) { |
92 | logger.debug("ignoring expected error", error); |
93 | } |
94 | } finally { |
95 | progressFrame.setVisible(false); |
96 | } |
97 | } |
98 | |
99 | protected void tearDown() |
100 | throws Exception { |
101 | super.tearDown(); |
102 | } |
103 | } |