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