| 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.util.zip.ZipException; |
| 20 | |
| 21 | import net.sf.jomic.tools.FileTools; |
| 22 | import net.sf.jomic.tools.ProgressFrame; |
| 23 | import net.sf.jomic.tools.StringTools; |
| 24 | import net.sf.jomic.tools.Task; |
| 25 | import net.sf.jomic.tools.TestTools; |
| 26 | import org.apache.commons.logging.Log; |
| 27 | import org.apache.commons.logging.LogFactory; |
| 28 | |
| 29 | import junit.framework.TestCase; |
| 30 | |
| 31 | /** |
| 32 | * TestCase for ExtractZipTask. |
| 33 | * |
| 34 | * @author Thomas Aglassinger |
| 35 | */ |
| 36 | public class ExtractComicTaskTest extends TestCase |
| 37 | { |
| 38 | private FileTools fileTools; |
| 39 | private Log logger; |
| 40 | private ProgressFrame progressFrame; |
| 41 | private StringTools stringTools; |
| 42 | private TestTools testTools; |
| 43 | |
| 44 | protected void setUp() |
| 45 | throws Exception { |
| 46 | super.setUp(); |
| 47 | logger = LogFactory.getLog(ExtractComicTaskTest.class); |
| 48 | testTools = TestTools.instance(); |
| 49 | fileTools = FileTools.instance(); |
| 50 | stringTools = StringTools.instance(); |
| 51 | progressFrame = new ProgressFrame(); |
| 52 | progressFrame.setTitle(getClass().getName()); |
| 53 | } |
| 54 | |
| 55 | public void testExtractCbr() |
| 56 | throws Exception { |
| 57 | testExtract(testTools.getTestFile(TestTools.TEST_COMIC_CBR)); |
| 58 | } |
| 59 | |
| 60 | public void testExtractCbz() |
| 61 | throws Exception { |
| 62 | testExtract(testTools.getTestFile(TestTools.TEST_COMIC_CBZ)); |
| 63 | } |
| 64 | |
| 65 | public void testExtractInternalError() |
| 66 | throws Exception { |
| 67 | String method = "testExtractInternalError"; |
| 68 | File zipFile = testTools.getTestFile(TestTools.TEST_COMIC_INTERNAL_ERROR); |
| 69 | File targetFolder = testTools.getTestOutputFile(getClass().getName() + "." + method); |
| 70 | |
| 71 | progressFrame.setVisible(true); |
| 72 | progressFrame.setNote(method); |
| 73 | |
| 74 | Task extractZipTask = new ExtractComicTask(zipFile, targetFolder); |
| 75 | |
| 76 | try { |
| 77 | extractZipTask.start(); |
| 78 | fail("broken archive must cause error: " + zipFile); |
| 79 | } catch (ZipException error) { |
| 80 | if (logger.isDebugEnabled()) { |
| 81 | logger.debug("ignoring expected error", error); |
| 82 | } |
| 83 | } finally { |
| 84 | progressFrame.setVisible(false); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public void testExtractPdf() |
| 89 | throws Exception { |
| 90 | testExtract(testTools.getTestFile(TestTools.TEST_COMIC_PDF)); |
| 91 | } |
| 92 | |
| 93 | protected void tearDown() |
| 94 | throws Exception { |
| 95 | progressFrame.dispose(); |
| 96 | super.tearDown(); |
| 97 | } |
| 98 | |
| 99 | private void testExtract(File comicFile) |
| 100 | throws Exception { |
| 101 | String comicSuffix = fileTools.getSuffix(comicFile); |
| 102 | String method = "testExtract" + stringTools.titled(comicSuffix); |
| 103 | File targetFolder = testTools.getTestOutputFile(getClass().getName() + "." + method); |
| 104 | |
| 105 | progressFrame.setVisible(true); |
| 106 | progressFrame.setNote(method); |
| 107 | |
| 108 | Task extractZipTask = new ExtractComicTask(comicFile, targetFolder); |
| 109 | |
| 110 | fileTools.attemptToDeleteAll(targetFolder, logger); |
| 111 | try { |
| 112 | extractZipTask.start(); |
| 113 | assertTrue(targetFolder.exists()); |
| 114 | } finally { |
| 115 | progressFrame.setVisible(false); |
| 116 | } |
| 117 | } |
| 118 | } |