| 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.comic; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.io.IOException; |
| 20 | |
| 21 | import net.sf.jomic.tools.FileTools; |
| 22 | import net.sf.jomic.tools.TestTools; |
| 23 | import org.apache.commons.logging.Log; |
| 24 | import org.apache.commons.logging.LogFactory; |
| 25 | |
| 26 | /** |
| 27 | * TestCase for ComicModel. |
| 28 | * |
| 29 | * @author Thomas Aglassinger |
| 30 | */ |
| 31 | public class ComicModelTest extends AbstractComicArchiveTest |
| 32 | { |
| 33 | private File cacheDir; |
| 34 | private ComicCache comicCache; |
| 35 | private FileTools fileTools; |
| 36 | private Log logger; |
| 37 | |
| 38 | protected void setUp() |
| 39 | throws Exception { |
| 40 | super.setUp(); |
| 41 | logger = LogFactory.getLog(ComicModelTest.class); |
| 42 | fileTools = FileTools.instance(); |
| 43 | cacheDir = File.createTempFile(ComicModelTest.class.getName(), ".tmp"); |
| 44 | fileTools.delete(cacheDir); |
| 45 | comicCache = ComicCache.instance(); |
| 46 | comicCache.setUp(cacheDir); |
| 47 | } |
| 48 | |
| 49 | public final void testCanIgnoreMacCompressResourceForkPseudoImages() |
| 50 | throws Exception { |
| 51 | File macCompressedComicFile = getTestTools().getTestInputFile("test_mac_compress.cbz"); |
| 52 | ComicModel comic = new ComicModel(macCompressedComicFile); |
| 53 | |
| 54 | for (int imageIndex = 0; imageIndex < comic.getImageCount(); imageIndex += 1) { |
| 55 | ComicImage comicImage = comic.getComicImage(imageIndex); |
| 56 | String imageName = comicImage.getName(); |
| 57 | |
| 58 | assert imageName.length() > 0; |
| 59 | assertTrue("image name starting with dot must be ignored: " + imageName, imageName.charAt(0) != '.'); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public final void testCreate() |
| 64 | throws Exception { |
| 65 | new ComicModel(getTestTools().getTestComicFile()); |
| 66 | } |
| 67 | |
| 68 | public final void testGetContents() |
| 69 | throws Exception { |
| 70 | ComicModel model = new ComicModel(getTestTools().getTestComicFile()); |
| 71 | |
| 72 | assertTrue(model.getImageCount() > 0); |
| 73 | |
| 74 | model.dispose(); |
| 75 | } |
| 76 | |
| 77 | public final void testReExtract() |
| 78 | throws IOException, InterruptedException { |
| 79 | // Extract the test comic. |
| 80 | File testComicFile = getTestTools().getTestComicFile(); |
| 81 | ComicModel comicModel = new ComicModel(testComicFile); |
| 82 | File archiveCacheDir = comicModel.getCacheDir(); |
| 83 | File[] allImageFiles = fileTools.listFilesRecursively(archiveCacheDir); |
| 84 | File someFile = allImageFiles[allImageFiles.length / 2]; |
| 85 | |
| 86 | // Close the comic but leave it in the cache. |
| 87 | comicModel.dispose(); |
| 88 | |
| 89 | // Be nasty and invalidate the cache by removing an image from it. |
| 90 | assertTrue(someFile.exists()); |
| 91 | fileTools.delete(someFile); |
| 92 | assertFalse(someFile.exists()); |
| 93 | |
| 94 | // Re-open the comic. |
| 95 | ComicModel againComicModel = new ComicModel(testComicFile); |
| 96 | |
| 97 | // Ensure that the missing file was detected, and the comic re-extracted. |
| 98 | assertTrue(someFile.exists()); |
| 99 | |
| 100 | againComicModel.dispose(); |
| 101 | } |
| 102 | |
| 103 | public final void testUnrar() |
| 104 | throws Exception { |
| 105 | testExtract("test.cbr"); |
| 106 | } |
| 107 | |
| 108 | public final void testUnzip() |
| 109 | throws Exception { |
| 110 | testExtract(TestTools.HUGO_COMIC); |
| 111 | } |
| 112 | |
| 113 | protected void tearDown() |
| 114 | throws Exception { |
| 115 | if (cacheDir != null) { |
| 116 | if (comicCache != null) { |
| 117 | comicCache.dispose(); |
| 118 | } |
| 119 | fileTools.attemptToDeleteAll(cacheDir, logger); |
| 120 | fileTools = null; |
| 121 | } |
| 122 | super.tearDown(); |
| 123 | } |
| 124 | } |