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.io.FileNotFoundException; |
20 | import java.io.FileOutputStream; |
21 | import java.io.IOException; |
22 | import java.util.Arrays; |
23 | |
24 | import junit.framework.TestCase; |
25 | |
26 | import org.apache.commons.logging.Log; |
27 | import org.apache.commons.logging.LogFactory; |
28 | |
29 | /** |
30 | * TestCase for FileArchive. |
31 | * |
32 | * @author Thomas Aglassinger |
33 | */ |
34 | public class FileArchiveTest extends TestCase |
35 | { |
36 | private static final int ANY_NUMBER = -1; |
37 | private static Log logger = LogFactory.getLog(FileArchiveTest.class); |
38 | |
39 | private StringTools stringTools; |
40 | private TestTools testTools; |
41 | |
42 | public FileArchiveTest(final String argument) { |
43 | super(argument); |
44 | } |
45 | |
46 | protected void setUp() |
47 | throws Exception { |
48 | super.setUp(); |
49 | testTools = TestTools.instance(); |
50 | stringTools = StringTools.instance(); |
51 | } |
52 | |
53 | public final void testExtract1FromTestCbr() |
54 | throws IOException { |
55 | testExtract(TestTools.TEST_COMIC_CBR, new String[]{"testdata/01.jpg"}); |
56 | } |
57 | |
58 | public final void testExtract1FromTestCbz() |
59 | throws IOException { |
60 | testExtract(TestTools.TEST_COMIC_FILE_NAME, new String[]{"data/01.png"}); |
61 | } |
62 | |
63 | public final void testExtract1FromTestPdf() |
64 | throws IOException { |
65 | try { |
66 | testExtract(TestTools.TEST_COMIC_PDF, new String[]{"01.png"}); |
67 | fail("testExtract1FromTestPdf must be adjusted to properly implemented " |
68 | + "FileArchive.extractPdf()"); |
69 | } catch (ArrayIndexOutOfBoundsException knownBug) { |
70 | logger.warn("ignore known bug", knownBug); |
71 | } |
72 | } |
73 | |
74 | public final void testExtractAllFromTestCbr() |
75 | throws IOException { |
76 | testExtract(TestTools.TEST_COMIC_CBR, null); |
77 | } |
78 | |
79 | public final void testExtractAllFromTestCbz() |
80 | throws IOException { |
81 | testExtract(TestTools.TEST_COMIC_FILE_NAME, null); |
82 | } |
83 | |
84 | public final void testExtractAllFromTestPdf() |
85 | throws IOException { |
86 | testExtract(TestTools.TEST_COMIC_PDF, null); |
87 | } |
88 | |
89 | public final void testList1PageCbz() |
90 | throws IOException { |
91 | testList("1page.cbz", 1); |
92 | } |
93 | |
94 | public final void testList2PagesCbz() |
95 | throws IOException { |
96 | testList("2pages.cbz", 2); |
97 | } |
98 | |
99 | public final void testList3PagesCbz() |
100 | throws IOException { |
101 | testList("3pages.cbz", 3); |
102 | } |
103 | |
104 | public final void testListTestCbr() |
105 | throws IOException { |
106 | testList(TestTools.TEST_COMIC_CBR, ANY_NUMBER); |
107 | } |
108 | |
109 | public final void testListTestCbz() |
110 | throws IOException { |
111 | testList(TestTools.TEST_COMIC_FILE_NAME, ANY_NUMBER); |
112 | } |
113 | |
114 | public void testExtractBrokenIncompleteCbr() { |
115 | try { |
116 | testExtract("broken_incomplete.cbr", null); |
117 | fail("broken incomplete file must not extract"); |
118 | } catch (IOException expectedError) { |
119 | logger.debug("ignoring expected error: ", expectedError); |
120 | } |
121 | } |
122 | |
123 | public void testExtractBrokenNonExistentCbr() |
124 | throws IOException { |
125 | try { |
126 | File nonExistentFile = new File("nonExistent.cbr"); |
127 | |
128 | assertFalse(nonExistentFile.exists()); |
129 | new FileArchive(nonExistentFile); |
130 | fail("non-existent file name must fail: " + stringTools.sourced(nonExistentFile)); |
131 | } catch (FileNotFoundException expectedError) { |
132 | logger.debug("ignoring expected error: ", expectedError); |
133 | } |
134 | } |
135 | |
136 | public void testInvalidFileName(String fileName) { |
137 | assert fileName != null; |
138 | try { |
139 | File file = new File(fileName); |
140 | |
141 | new FileOutputStream(file); |
142 | file.delete(); |
143 | fail("invalid file name must fail: " + stringTools.sourced(fileName)); |
144 | } catch (FileNotFoundException error) { |
145 | logger.debug("cannot create file as expected because name is invalid"); |
146 | } |
147 | } |
148 | |
149 | public void testInvalidFileNames() { |
150 | testInvalidFileName("/"); |
151 | // testInvalidFileName(new String(new byte[] {(byte) 0xa4}, "iso-8859-1")); |
152 | } |
153 | |
154 | protected void tearDown() |
155 | throws Exception { |
156 | testTools = null; |
157 | stringTools = null; |
158 | super.tearDown(); |
159 | } |
160 | |
161 | private void testExtract(String archiveName, String[] fileNames) |
162 | throws IOException { |
163 | assert archiveName != null; |
164 | File zipFile = testTools.getTestFile(archiveName); |
165 | FileArchive archive = new FileArchive(zipFile); |
166 | |
167 | String[] filesInArchive = archive.list(); |
168 | String[] filesToExtract = fileNames; |
169 | |
170 | |
171 | if (filesToExtract == null) { |
172 | filesToExtract = filesInArchive; |
173 | } |
174 | Arrays.sort(filesToExtract); |
175 | |
176 | int actualNumberOfFiles = filesInArchive.length; |
177 | |
178 | if (logger.isDebugEnabled()) { |
179 | for (int i = 0; i < actualNumberOfFiles; i += 1) { |
180 | logger.debug("- " + filesInArchive[i]); |
181 | } |
182 | } |
183 | File targetDir = new File(System.getProperty("java.io.tmpdir"), FileArchiveTest.class.getName()); |
184 | |
185 | targetDir.mkdir(); |
186 | |
187 | archive.extract(targetDir, filesToExtract); |
188 | for (int i = 0; i < filesInArchive.length; i += 1) { |
189 | String fileNameInArchive = filesInArchive[i]; |
190 | File file = new File(targetDir, fileNameInArchive); |
191 | |
192 | if (file.isFile()) { |
193 | String fileName = file.getAbsolutePath(); |
194 | |
195 | if (Arrays.binarySearch(filesToExtract, fileNameInArchive) < 0) { |
196 | assertTrue("file must not exist: \"" + fileName + "\"", !file.exists()); |
197 | } else { |
198 | assertTrue("file must exist: \"" + fileName + "\"", file.exists()); |
199 | |
200 | boolean deleted = file.delete(); |
201 | |
202 | assertTrue("file must be deletable: \"" + fileName + "\"", deleted); |
203 | } |
204 | } |
205 | } |
206 | FileTools.instance().attemptToDeleteAll(targetDir, logger); |
207 | assertTrue("target directory must be deletable: \"" + targetDir.getAbsolutePath() + "\"", !targetDir.exists()); |
208 | } |
209 | |
210 | private void testList(String fileName, int expectedNumberOfFiles) |
211 | throws IOException { |
212 | assert fileName != null; |
213 | assert (expectedNumberOfFiles == ANY_NUMBER) || (expectedNumberOfFiles > 0); |
214 | if (logger.isInfoEnabled()) { |
215 | logger.info("list \"" + fileName + "\""); |
216 | } |
217 | File zipFile = testTools.getTestFile(fileName); |
218 | FileArchive archive = new FileArchive(zipFile); |
219 | |
220 | String[] files = archive.list(); |
221 | int actualNumberOfFiles = files.length; |
222 | |
223 | if (logger.isDebugEnabled()) { |
224 | for (int i = 0; i < actualNumberOfFiles; i += 1) { |
225 | logger.debug("- " + files[i]); |
226 | } |
227 | } |
228 | if (expectedNumberOfFiles != ANY_NUMBER) { |
229 | assertEquals(expectedNumberOfFiles, actualNumberOfFiles); |
230 | } |
231 | } |
232 | } |