| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2008 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 net.sf.wraplog.Logger; |
| 24 | |
| 25 | /** |
| 26 | * TestCase for ComicModel. |
| 27 | * |
| 28 | * @author Thomas Aglassinger |
| 29 | */ |
| 30 | public class ComicModelTest extends AbstractComicArchiveTest |
| 31 | { |
| 32 | private File cacheDir; |
| 33 | private ComicCache comicCache; |
| 34 | private FileTools fileTools; |
| 35 | private Logger logger; |
| 36 | |
| 37 | protected void setUp() |
| 38 | throws Exception { |
| 39 | super.setUp(); |
| 40 | logger = Logger.getLogger(ComicModelTest.class); |
| 41 | fileTools = FileTools.instance(); |
| 42 | cacheDir = File.createTempFile(ComicModelTest.class.getName(), ".tmp"); |
| 43 | fileTools.delete(cacheDir); |
| 44 | comicCache = ComicCache.instance(); |
| 45 | comicCache.setUp(cacheDir); |
| 46 | } |
| 47 | |
| 48 | public final void testCreate() |
| 49 | throws Exception { |
| 50 | new ComicModel(getTestTools().getTestComicFile()); |
| 51 | } |
| 52 | |
| 53 | public final void testGetContents() |
| 54 | throws Exception { |
| 55 | ComicModel model = new ComicModel(getTestTools().getTestComicFile()); |
| 56 | |
| 57 | assertTrue(model.getImageCount() > 0); |
| 58 | |
| 59 | model.dispose(); |
| 60 | } |
| 61 | |
| 62 | public final void testReExtract() |
| 63 | throws IOException, InterruptedException { |
| 64 | // Extract the test comic. |
| 65 | File testComicFile = getTestTools().getTestComicFile(); |
| 66 | ComicModel comicModel = new ComicModel(testComicFile); |
| 67 | File archiveCacheDir = comicModel.getCacheDir(); |
| 68 | File[] allImageFiles = fileTools.listFilesRecursively(archiveCacheDir); |
| 69 | File someFile = allImageFiles[allImageFiles.length / 2]; |
| 70 | |
| 71 | // Close the comic but leave it in the cache. |
| 72 | comicModel.dispose(); |
| 73 | |
| 74 | // Be nasty and invalidate the cache by removing an image from it. |
| 75 | assertTrue(someFile.exists()); |
| 76 | fileTools.delete(someFile); |
| 77 | assertFalse(someFile.exists()); |
| 78 | |
| 79 | // Re-open the comic. |
| 80 | ComicModel againComicModel = new ComicModel(testComicFile); |
| 81 | |
| 82 | // Ensure that the missing file was detected, and the comic re-extracted. |
| 83 | assertTrue(someFile.exists()); |
| 84 | |
| 85 | againComicModel.dispose(); |
| 86 | } |
| 87 | |
| 88 | public final void testUnrar() |
| 89 | throws Exception { |
| 90 | testExtract("test.cbr"); |
| 91 | } |
| 92 | |
| 93 | public final void testUnzip() |
| 94 | throws Exception { |
| 95 | testExtract(TestTools.HUGO_COMIC); |
| 96 | } |
| 97 | |
| 98 | protected void tearDown() |
| 99 | throws Exception { |
| 100 | if (cacheDir != null) { |
| 101 | if (comicCache != null) { |
| 102 | comicCache.dispose(); |
| 103 | } |
| 104 | fileTools.attemptToDeleteAll(cacheDir, logger); |
| 105 | fileTools = null; |
| 106 | } |
| 107 | super.tearDown(); |
| 108 | } |
| 109 | } |