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.FileInputStream; |
20 | import java.io.FileOutputStream; |
21 | import java.io.IOException; |
22 | import java.util.Iterator; |
23 | import java.util.Map; |
24 | import java.util.Random; |
25 | |
26 | import junit.framework.TestCase; |
27 | import org.apache.commons.logging.Log; |
28 | import org.apache.commons.logging.LogFactory; |
29 | |
30 | /** |
31 | * TestCase for FileTools. |
32 | * |
33 | * @author Thomas Aglassinger |
34 | */ |
35 | public class FileToolsTest extends TestCase |
36 | { |
37 | /** |
38 | * Size of automatically created test files in byte. |
39 | */ |
40 | private static final int TEST_FILE_SIZE = 123456; |
41 | |
42 | private FileTools fileTools; |
43 | private Log logger; |
44 | private TestTools testTools; |
45 | |
46 | protected void setUp() |
47 | throws Exception { |
48 | super.setUp(); |
49 | logger = LogFactory.getLog(FileToolsTest.class); |
50 | testTools = TestTools.instance(); |
51 | fileTools = FileTools.instance(); |
52 | } |
53 | |
54 | private Map getFlattenedFolderNames(String[] filePaths) { |
55 | Map folderMap = fileTools.getFolderMap(createFileArray(filePaths)); |
56 | Map folderNames = fileTools.getFolderNames(folderMap); |
57 | Map result = fileTools.getFlattenedFolderNames(folderNames); |
58 | |
59 | return result; |
60 | } |
61 | |
62 | public void testCopyDir() |
63 | throws IOException { |
64 | File rootDir = testTools.getTestInputFile("testCopyDir"); |
65 | File targetDir = testTools.getTestOutputFile("testCopyDir"); |
66 | File someSubDir = new File(rootDir, "subDir"); |
67 | File emptySubDir = new File(rootDir, "emptyDir"); |
68 | File fileInRootDir = new File(rootDir, "fileInRootDir.txt"); |
69 | File fileInSubDir = new File(someSubDir, "fileInSubDir.txt"); |
70 | byte[] manyData = createRandomData(TEST_FILE_SIZE); |
71 | byte[] fewData = createRandomData(1); |
72 | |
73 | // Create test data. |
74 | fileTools.mkdirs(targetDir); |
75 | fileTools.mkdirs(rootDir); |
76 | fileTools.mkdirs(someSubDir); |
77 | fileTools.mkdirs(emptySubDir); |
78 | createFile(fileInRootDir, fewData); |
79 | createFile(fileInSubDir, manyData); |
80 | |
81 | fileTools.copyDir(rootDir, targetDir); |
82 | // TODO: Compare directory structure and all file contents. |
83 | |
84 | // Clean up. |
85 | fileTools.attemptToDeleteAll(rootDir, logger); |
86 | fileTools.attemptToDeleteAll(targetDir, logger); |
87 | } |
88 | |
89 | public void testCopyFile() |
90 | throws IOException { |
91 | File source = testTools.getTestGeneratedInputFile("testCopyFile.source"); |
92 | File target = testTools.getTestOutputFile("testCopyFile.target"); |
93 | byte[] sourceData = createRandomData(TEST_FILE_SIZE); |
94 | |
95 | createFile(source, sourceData); |
96 | fileTools.copyFile(source, target); |
97 | |
98 | assertEquals("target.length", source.length(), target.length()); |
99 | |
100 | byte[] targetData = new byte[TEST_FILE_SIZE]; |
101 | FileInputStream in = new FileInputStream(target); |
102 | |
103 | try { |
104 | in.read(targetData); |
105 | testTools.assertEquals(sourceData, targetData); |
106 | |
107 | int c = in.read(); |
108 | |
109 | assertEquals("target file must have ended already: " + target.getAbsolutePath(), -1, c); |
110 | } finally { |
111 | in.close(); |
112 | } |
113 | fileTools.delete(target); |
114 | fileTools.delete(source); |
115 | } |
116 | |
117 | public void testCreateTempDir() |
118 | throws IOException { |
119 | File tempDir = fileTools.createTempDir(FileToolsTest.class.getName() + "-"); |
120 | |
121 | try { |
122 | assertNotNull(tempDir); |
123 | assertTrue(tempDir.exists()); |
124 | assertTrue(tempDir.isDirectory()); |
125 | } finally { |
126 | fileTools.attemptToDeleteAll(tempDir, logger); |
127 | } |
128 | } |
129 | |
130 | public void testDelete() |
131 | throws IOException { |
132 | String testFileName = testTools.getTestFileName(FileToolsTest.class, "testDelete", null, "tmp"); |
133 | File file = testTools.getTestOutputFile(testFileName); |
134 | |
135 | // test failed delete |
136 | if (file.exists()) { |
137 | throw new IllegalStateException("file must be deleted before test can run: " + file); |
138 | } |
139 | try { |
140 | fileTools.delete(file); |
141 | assertTrue("delete() of non-existent file must throw IOException", false); |
142 | } catch (IOException expectedError) { |
143 | logger.debug("cannot delete non-existent file as expected", expectedError); |
144 | } |
145 | |
146 | // test proper delete |
147 | byte[] data = createRandomData(TEST_FILE_SIZE); |
148 | |
149 | createFile(file, data); |
150 | fileTools.delete(file); |
151 | assertTrue("file must have been deleted: " + file, !file.exists()); |
152 | } |
153 | |
154 | public void testFailedCopyFile() { |
155 | File testSourceFile = testTools.getTestImageFile(); |
156 | File testTargetDir = testTools.getTestOutputFile("non-existent-dir"); |
157 | File testTargetFile = new File(testTargetDir, testSourceFile.getName()); |
158 | |
159 | assertFalse(testTargetDir.exists()); |
160 | try { |
161 | fileTools.copyFile(testSourceFile, testTargetFile); |
162 | fail("copy to non-existent directory must fail"); |
163 | } catch (IOException error) { |
164 | logger.debug("ignore expected error", error); |
165 | } |
166 | assertFalse(testTargetFile.exists()); |
167 | } |
168 | |
169 | public void testGetAdjustedComicFile() |
170 | throws IOException { |
171 | testGetAdjustedComicFile(TestTools.TEST_COMIC_CBR, "cbr"); |
172 | testGetAdjustedComicFile(TestTools.TEST_COMIC_CBZ, "cbz"); |
173 | testGetAdjustedComicFile(TestTools.TEST_COMIC_PDF, "pdf"); |
174 | testGetAdjustedComicFile(TestTools.DISGUISED_RAR, "cbr"); |
175 | testGetAdjustedComicFile(TestTools.DISGUISED_ZIP, "cbz"); |
176 | } |
177 | |
178 | public void testGetFlattenedFolderNames() { |
179 | String sep = File.separator; |
180 | String[] filePaths; |
181 | Map flattenedFolderNames; |
182 | |
183 | // Test without name clash. |
184 | filePaths = new String[]{ |
185 | "hugo.png", |
186 | "issue_01" + sep + "01.png", |
187 | "issue_01" + sep + "02.png", |
188 | "issue_01" + sep + "03.png", |
189 | "issue_02" + sep + "01.png", |
190 | "issue_02" + sep + "02-03.png", |
191 | "issue_02" + sep + "04.png", |
192 | }; |
193 | flattenedFolderNames = getFlattenedFolderNames(filePaths); |
194 | logFlattenedFolderNames(flattenedFolderNames); |
195 | assertNotNull(flattenedFolderNames); |
196 | assertTrue(flattenedFolderNames.containsKey(FileTools.NO_FOLDER_NAME)); |
197 | assertTrue(flattenedFolderNames.containsKey("issue_01")); |
198 | assertTrue(flattenedFolderNames.containsKey("issue_02")); |
199 | assertEquals(3, flattenedFolderNames.keySet().size()); |
200 | |
201 | // Test with name clash. |
202 | filePaths = new String[]{ |
203 | "hugo" + sep + "issue_01" + sep + "hugo.png", |
204 | "sepp" + sep + "issue_01" + sep + "sepp.png", |
205 | "sepp" + sep + "issue_02" + sep + "sepp2.png", |
206 | "resi" + sep + "issue_01" + sep + "resi.png", |
207 | }; |
208 | flattenedFolderNames = getFlattenedFolderNames(filePaths); |
209 | logFlattenedFolderNames(flattenedFolderNames); |
210 | assertNotNull(flattenedFolderNames); |
211 | assertTrue(flattenedFolderNames.containsKey("issue_01")); |
212 | assertTrue(flattenedFolderNames.containsKey("issue_01_1")); |
213 | assertTrue(flattenedFolderNames.containsKey("issue_01_2")); |
214 | assertTrue(flattenedFolderNames.containsKey("issue_02")); |
215 | assertEquals(4, flattenedFolderNames.keySet().size()); |
216 | } |
217 | |
218 | public void testGetFolderMap() |
219 | throws IOException { |
220 | // Create test data. |
221 | File rootFolder = testTools.getTestInputFile("testGetFolderMap"); |
222 | File someSubFolder = new File(rootFolder, "subFolder"); |
223 | File emptySubFolder = new File(rootFolder, "emptyFolder"); |
224 | File fileInRootFolder = new File(rootFolder, "fileInRootFolder.txt"); |
225 | File fileInSubFolder = new File(someSubFolder, "fileInSubFolder.txt"); |
226 | byte[] manyData = createRandomData(TEST_FILE_SIZE); |
227 | byte[] fewData = createRandomData(1); |
228 | |
229 | fileTools.mkdirs(rootFolder); |
230 | fileTools.mkdirs(someSubFolder); |
231 | fileTools.mkdirs(emptySubFolder); |
232 | createFile(fileInRootFolder, fewData); |
233 | createFile(fileInSubFolder, manyData); |
234 | |
235 | // Perform test. |
236 | Map folderMap = fileTools.getFolderMap(rootFolder); |
237 | String emptyFolderPath = emptySubFolder.getPath(); |
238 | String someSubFolderPath = someSubFolder.getPath(); |
239 | |
240 | assertNotNull(folderMap); |
241 | assertFalse(folderMap.isEmpty()); |
242 | assertFalse(folderMap.containsKey(emptyFolderPath)); |
243 | assertTrue(folderMap.containsKey(someSubFolderPath)); |
244 | |
245 | // Clean up. |
246 | fileTools.attemptToDeleteAll(rootFolder, logger); |
247 | } |
248 | |
249 | public void testGetFolderNames() |
250 | throws IOException { |
251 | // Create test data. |
252 | File rootFolder = testTools.getTestInputFile("testGetFolderNames"); |
253 | File someSubFolder = new File(rootFolder, "subFolder"); |
254 | File emptySubFolder = new File(rootFolder, "emptyFolder"); |
255 | File fileInRootFolder = new File(rootFolder, "fileInRootFolder.txt"); |
256 | File fileInSubFolder = new File(someSubFolder, "fileInSubFolder.txt"); |
257 | byte[] manyData = createRandomData(TEST_FILE_SIZE); |
258 | byte[] fewData = createRandomData(1); |
259 | |
260 | fileTools.mkdirs(rootFolder); |
261 | fileTools.mkdirs(someSubFolder); |
262 | fileTools.mkdirs(emptySubFolder); |
263 | createFile(fileInRootFolder, fewData); |
264 | createFile(fileInSubFolder, manyData); |
265 | |
266 | // Perform test. |
267 | Map folderMap = fileTools.getFolderMap(rootFolder); |
268 | Map folderNames = fileTools.getFolderNames(folderMap); |
269 | String emptyFolderName = emptySubFolder.getName(); |
270 | String someSubFolderName = someSubFolder.getName(); |
271 | |
272 | assertNotNull(folderNames); |
273 | assertFalse(folderNames.isEmpty()); |
274 | assertFalse(folderNames.containsKey(emptyFolderName)); |
275 | assertTrue(folderNames.containsKey(someSubFolderName)); |
276 | |
277 | // Clean up. |
278 | fileTools.attemptToDeleteAll(rootFolder, logger); |
279 | } |
280 | |
281 | public void testGetHomeDir() { |
282 | assertNotNull(fileTools.getHomeDir()); |
283 | } |
284 | |
285 | public void testGetIcon() { |
286 | assertNotNull(fileTools.getIconFor(testTools.getTestComicFile())); |
287 | assertNotNull(fileTools.getIconFor(testTools.getTestGeneratedInputDir())); |
288 | } |
289 | |
290 | public void testGetRelativePath() { |
291 | File testFile = testTools.getTestComicFile(); |
292 | |
293 | assertEquals(testFile.getName(), fileTools.getRelativePath(testFile.getParentFile(), testFile)); |
294 | } |
295 | |
296 | public void testGetSuffix() { |
297 | assertEquals("", fileTools.getSuffix(new File("hugo"))); |
298 | assertEquals("gz", fileTools.getSuffix(new File("hugo.gz"))); |
299 | assertEquals("gz", fileTools.getSuffix(new File("hugo.tar.gz"))); |
300 | assertEquals("gz", fileTools.getSuffix(new File("sepp", "hugo.tar.gz"))); |
301 | assertEquals("gz", fileTools.getSuffix(new File("sepp.resi", "hugo.tar.gz"))); |
302 | assertEquals("", fileTools.getSuffix(new File("sepp.resi", "hugo"))); |
303 | } |
304 | |
305 | public void testGetWithoutLastSuffix() { |
306 | testGetWithoutLastSuffix("hugo", "hugo"); |
307 | testGetWithoutLastSuffix("hugo.tar", "hugo"); |
308 | testGetWithoutLastSuffix("hugo.tar.gz", "hugo.tar"); |
309 | testGetWithoutLastSuffix(".", ""); |
310 | testGetWithoutLastSuffix("", ""); |
311 | } |
312 | |
313 | public void testListFilesRecursively() |
314 | throws IOException { |
315 | File tempDir = fileTools.createTempDir( |
316 | FileToolsTest.class.getName() + ".testListFilesRecursively-"); |
317 | File[] noFiles = fileTools.listFilesRecursively(tempDir); |
318 | |
319 | assertEquals(0, noFiles.length); |
320 | |
321 | // Create a nested directory structure with 2 files in it. |
322 | File nestedDir = new File(new File(new File(tempDir, "a"), "b"), "c"); |
323 | |
324 | fileTools.mkdirs(nestedDir); |
325 | fileTools.copyFile(testTools.getTestGeneratedInputFile(TestTools.SINGLE_PAGE_COMIC), |
326 | new File(tempDir, "a.tmp")); |
327 | fileTools.copyFile(testTools.getTestGeneratedInputFile(TestTools.SINGLE_PAGE_COMIC), |
328 | new File(nestedDir, "b.tmp")); |
329 | fileTools.mkdirs(new File(nestedDir, "c.dir")); |
330 | |
331 | File[] twoFiles = fileTools.listFilesRecursively(tempDir); |
332 | |
333 | assertEquals(2, twoFiles.length); |
334 | |
335 | // Test error handling. |
336 | try { |
337 | File nonExistentFolder = new File(nestedDir, "must_not_exist"); |
338 | |
339 | fileTools.listFilesRecursively(nonExistentFolder); |
340 | fail("folder must not be listed: " + nonExistentFolder.getAbsolutePath()); |
341 | } catch (TunneledIOException expectedError) { |
342 | if (logger.isDebugEnabled()) { |
343 | logger.debug("ignore expected error", expectedError); |
344 | } |
345 | } |
346 | |
347 | // Clean up. |
348 | fileTools.attemptToDeleteAll(tempDir, logger); |
349 | } |
350 | |
351 | public void testObtainBrokenComicFormat() { |
352 | File comicFile = testTools.getTestFile(TestTools.TEST_TEXT_NAME); |
353 | |
354 | try { |
355 | String comicFormat = fileTools.obtainComicFormat(comicFile); |
356 | |
357 | fail("comic format must not be detected but was: " + comicFormat); |
358 | } catch (IOException expectedError) { |
359 | if (logger.isDebugEnabled()) { |
360 | logger.debug("ignore expected error", expectedError); |
361 | } |
362 | } |
363 | } |
364 | |
365 | public void testObtainComicFormat() |
366 | throws IOException { |
367 | testObtainComicFormat(FileTools.FORMAT_PDF, TestTools.TEST_COMIC_PDF); |
368 | testObtainComicFormat(FileTools.FORMAT_RAR, TestTools.TEST_COMIC_CBR); |
369 | testObtainComicFormat(FileTools.FORMAT_ZIP, TestTools.TEST_COMIC_CBZ); |
370 | } |
371 | |
372 | public void testPortableFileNames() { |
373 | testPortableFileNames("hugo.txt", "hugo.txt"); |
374 | testPortableFileNames("h?go.txt", "h-go.txt"); |
375 | testPortableFileNames("???.txt", "-.txt"); |
376 | testPortableFileNames("a--b", "a-b"); |
377 | testPortableFileNames("?-b", "-b"); |
378 | testPortableFileNames("", ""); |
379 | } |
380 | |
381 | public void testPortableFileNames(String name, String expected) { |
382 | assert name != null; |
383 | assert expected != null; |
384 | |
385 | String actual = fileTools.getPortableFileName(name); |
386 | |
387 | assertEquals(expected, actual); |
388 | } |
389 | |
390 | public void testSortSmart() { |
391 | testSortSmart(new String[]{}, new String[]{}); |
392 | testSortSmart(new String[]{"p/n01.s", "p/n0203.s", "p/n04.s"}, new String[]{"p/n01.s", "p/n0203.s", "p/n04.s"}); |
393 | testSortSmart(new String[]{"p/n1.s", "p/n2.s"}, new String[]{"p/n1.s", "p/n2.s"}); |
394 | testSortSmart(new String[]{"p/n2.s", "p/n1.s"}, new String[]{"p/n1.s", "p/n2.s"}); |
395 | testSortSmart(new String[]{"p/n01.s", "p/n0203.s", "p/n04.s"}, new String[]{"p/n01.s", "p/n0203.s", "p/n04.s"}); |
396 | testSortSmart(new String[]{"p/n01.s", "p/n02+03.s", "p/n04.s"}, |
397 | new String[]{"p/n01.s", "p/n02+03.s", "p/n04.s"}); |
398 | } |
399 | |
400 | public void testSortSmartSeparatedDoublePage() { |
401 | testSortSmart(new String[]{"p/n1.s", "p/n2-3.s", "p/n4.s"}, new String[]{"p/n1.s", "p/n2-3.s", "p/n4.s"}); |
402 | } |
403 | |
404 | public void testSortWithFrontPage() { |
405 | testSortSmart(new String[]{"p/n1.s", "p/n0front.s", "p/n2.s"}, new String[]{"p/n0front.s", "p/n1.s", "p/n2.s"}); |
406 | } |
407 | |
408 | public void testWriteLines() |
409 | throws IOException { |
410 | testWriteLines("empty", new String[]{}); |
411 | testWriteLines("oneEmptyLine", new String[]{""}); |
412 | testWriteLines("oneLine", new String[]{"one"}); |
413 | testWriteLines("someLines", new String[]{"one", "two", "three four five"}); |
414 | } |
415 | |
416 | private void createFile(File source, byte[] data) |
417 | throws IOException { |
418 | assert source != null; |
419 | assert data != null; |
420 | FileOutputStream out = new FileOutputStream(source); |
421 | boolean written = false; |
422 | |
423 | try { |
424 | out.write(data); |
425 | written = true; |
426 | } finally { |
427 | out.close(); |
428 | if (!written) { |
429 | fileTools.deleteOrWarn(source, logger); |
430 | } |
431 | } |
432 | } |
433 | |
434 | private File[] createFileArray(String[] filePaths) { |
435 | File[] result = new File[filePaths.length]; |
436 | |
437 | for (int i = 0; i < filePaths.length; i += 1) { |
438 | result[i] = new File(filePaths[i]); |
439 | } |
440 | return result; |
441 | } |
442 | |
443 | private byte[] createRandomData(int size) { |
444 | assert size > 0; |
445 | byte[] data = new byte[size]; |
446 | Random randomizer = new Random(0); |
447 | |
448 | randomizer.nextBytes(data); |
449 | return data; |
450 | } |
451 | |
452 | private void logFlattenedFolderNames(Map flattenedFolderNames) { |
453 | Iterator rider = flattenedFolderNames.entrySet().iterator(); |
454 | |
455 | while (rider.hasNext()) { |
456 | Map.Entry nameAndFileEntry = (Map.Entry) rider.next(); |
457 | String name = (String) nameAndFileEntry.getKey(); |
458 | File folder = (File) nameAndFileEntry.getValue(); |
459 | |
460 | if (logger.isInfoEnabled()) { |
461 | logger.info(name + " <- " + folder); |
462 | } |
463 | } |
464 | } |
465 | |
466 | private void testGetAdjustedComicFile(String fileName, String expectedSuffix) |
467 | throws IOException { |
468 | File comicFile = testTools.getTestFile(fileName); |
469 | File adjustedComicFile = fileTools.getAdjustedComicFile(comicFile); |
470 | |
471 | assertNotNull(adjustedComicFile); |
472 | |
473 | String adjustedSuffix = fileTools.getSuffix(adjustedComicFile); |
474 | |
475 | assertEquals(expectedSuffix, adjustedSuffix); |
476 | } |
477 | |
478 | private void testGetWithoutLastSuffix(String fileName, String expected) { |
479 | assert fileName != null; |
480 | assert expected != null; |
481 | String actual = fileTools.getWithoutLastSuffix(fileName); |
482 | |
483 | assertEquals(expected, actual); |
484 | } |
485 | |
486 | private void testObtainComicFormat(String expectedFormat, String testComicName) |
487 | throws IOException { |
488 | File comicFile = testTools.getTestFile(testComicName); |
489 | String actualFormat = fileTools.obtainComicFormat(comicFile); |
490 | |
491 | assertEquals(testComicName, expectedFormat, actualFormat); |
492 | } |
493 | |
494 | private void testSortSmart(String[] source, String[] expected) { |
495 | assert source != null; |
496 | assert expected != null; |
497 | assert source.length == expected.length |
498 | : "expected.length must be " + source.length + " but is " + expected.length; |
499 | String[] actual = fileTools.sort(source, FileTools.SORT_SMART); |
500 | |
501 | testTools.assertEquals(expected, actual); |
502 | } |
503 | |
504 | private void testWriteLines(String testName, String[] testLines) |
505 | throws IOException { |
506 | String testFileName = testTools.getTestFileName(FileToolsTest.class, "testWriteLines", testName, "txt"); |
507 | File testFileToWrite = testTools.getTestOutputFile(testFileName); |
508 | File expectedFile = testTools.getTestExpectedFile(testFileName); |
509 | |
510 | fileTools.writeLines(testFileToWrite, testLines); |
511 | testTools.assertFilesEqual(expectedFile, testFileToWrite); |
512 | } |
513 | } |