| 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.tests; |
| 17 | |
| 18 | import java.awt.Color; |
| 19 | import java.awt.GradientPaint; |
| 20 | import java.awt.Graphics2D; |
| 21 | import java.awt.geom.Rectangle2D; |
| 22 | import java.awt.image.BufferedImage; |
| 23 | import java.awt.image.RenderedImage; |
| 24 | import java.io.File; |
| 25 | import java.io.FileInputStream; |
| 26 | import java.io.FileOutputStream; |
| 27 | import java.io.FileWriter; |
| 28 | import java.io.IOException; |
| 29 | import java.io.OutputStream; |
| 30 | import java.util.Enumeration; |
| 31 | import java.util.Iterator; |
| 32 | import java.util.LinkedList; |
| 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | import java.util.TreeMap; |
| 36 | import java.util.zip.ZipEntry; |
| 37 | import java.util.zip.ZipFile; |
| 38 | import java.util.zip.ZipOutputStream; |
| 39 | |
| 40 | import javax.imageio.ImageIO; |
| 41 | import javax.imageio.ImageWriteParam; |
| 42 | import javax.imageio.ImageWriter; |
| 43 | import javax.imageio.stream.ImageOutputStream; |
| 44 | |
| 45 | import net.sf.jomic.comic.Conversion; |
| 46 | import net.sf.jomic.comic.CreateComicTask; |
| 47 | import net.sf.jomic.jaiunit.Tools; |
| 48 | import net.sf.jomic.tools.FileTools; |
| 49 | import net.sf.jomic.tools.ImageTools; |
| 50 | import net.sf.jomic.tools.RegExFileFilter; |
| 51 | import net.sf.jomic.tools.StringTools; |
| 52 | import net.sf.jomic.tools.Task; |
| 53 | import net.sf.jomic.tools.TestTools; |
| 54 | import org.apache.commons.logging.Log; |
| 55 | import org.apache.commons.logging.LogFactory; |
| 56 | |
| 57 | import org.apache.pdfbox.exceptions.COSVisitorException; |
| 58 | import org.apache.pdfbox.pdfwriter.COSWriter; |
| 59 | import org.apache.pdfbox.pdmodel.PDDocument; |
| 60 | import org.apache.pdfbox.pdmodel.PDPage; |
| 61 | |
| 62 | /** |
| 63 | * Tool to create test data, in particular images for comics. |
| 64 | * |
| 65 | * @author Thomas Aglassinger |
| 66 | */ |
| 67 | public class TestDataCreator |
| 68 | { |
| 69 | /** |
| 70 | * Prefix used for test files that are broken and cannot be opened. |
| 71 | */ |
| 72 | public static final String BROKEN_PREFIX = "broken_"; |
| 73 | |
| 74 | /** |
| 75 | * Prefix used for test comics that are broken and cannot be opened. |
| 76 | */ |
| 77 | public static final String BROKEN_ARCHIVE_PREFIX = BROKEN_PREFIX + "archive_"; |
| 78 | |
| 79 | /** |
| 80 | * Prefix used for test images that are broken and cannot be opened. |
| 81 | */ |
| 82 | public static final String BROKEN_IMAGE_PREFIX = BROKEN_PREFIX + "image_"; |
| 83 | |
| 84 | private static final int BUFFER_SIZE = 16384; |
| 85 | private static final double DIFFERENT_RATIO = 0.8; |
| 86 | |
| 87 | private static final int HEIGHT = 640; |
| 88 | |
| 89 | /** |
| 90 | * Default suffix used by generated test images. |
| 91 | */ |
| 92 | private static final String TEST_IMAGE_SUFFIX = "png"; |
| 93 | private static final int WIDTH = 480; |
| 94 | |
| 95 | private FileTools fileTools; |
| 96 | private ImageTools imageTools; |
| 97 | private Tools jaiUnitTools; |
| 98 | private Log logger; |
| 99 | private String[] standardTestNames; |
| 100 | private StringTools stringTools; |
| 101 | private Map testComics; |
| 102 | private List testImageNames; |
| 103 | private TestTools testTools; |
| 104 | |
| 105 | TestDataCreator() { |
| 106 | logger = LogFactory.getLog(TestDataCreator.class); |
| 107 | testTools = TestTools.instance(); |
| 108 | fileTools = FileTools.instance(); |
| 109 | stringTools = StringTools.instance(); |
| 110 | jaiUnitTools = Tools.instance(); |
| 111 | imageTools = ImageTools.instance(); |
| 112 | testImageNames = new LinkedList(); |
| 113 | testComics = new TreeMap(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Create all test data. |
| 118 | */ |
| 119 | public static void main(String[] args) |
| 120 | throws Exception { |
| 121 | TestDataCreator creator = new TestDataCreator(); |
| 122 | |
| 123 | creator.create(); |
| 124 | } |
| 125 | |
| 126 | void create() |
| 127 | throws Exception { |
| 128 | createTestImages(); |
| 129 | createTestTexts(); |
| 130 | createTestComics(); |
| 131 | createBrokenTestComics(); |
| 132 | |
| 133 | Iterator comicRider = testComics.entrySet().iterator(); |
| 134 | |
| 135 | while (comicRider.hasNext()) { |
| 136 | Map.Entry entry = (Map.Entry) comicRider.next(); |
| 137 | File comicFile = (File) entry.getKey(); |
| 138 | String description = (String) entry.getValue(); |
| 139 | |
| 140 | // TODO: Write test comic description to HTML/XML file. |
| 141 | logger.info("" + comicFile.getName() + ": " + description); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private void addTestComic(File comicFile, String xhtmlDescription) { |
| 146 | assert comicFile != null; |
| 147 | assert xhtmlDescription != null; |
| 148 | testComics.put(comicFile, xhtmlDescription); |
| 149 | } |
| 150 | |
| 151 | private void assertCannotBeLoaded(File imageFile) { |
| 152 | assert imageFile != null; |
| 153 | try { |
| 154 | imageTools.readImage(imageFile); |
| 155 | // TODO: actually break the image: |
| 156 | // assert false |
| 157 | // : "image file " + stringTools.sourced(imageFile.getAbsolutePath() + " must be unparseable"); |
| 158 | } catch (Throwable expectedError) { |
| 159 | if (logger.isDebugEnabled()) { |
| 160 | logger.debug("ignoring expected error on unparseable image: " + expectedError); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private File copyTestFiles(String source, String target) |
| 166 | throws IOException { |
| 167 | assert source != null; |
| 168 | assert target != null; |
| 169 | File result = testTools.getTestGeneratedInputFile(target); |
| 170 | |
| 171 | fileTools.copyFile(testTools.getTestFile(source), result); |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | private void copyTestImage(String sourceName, String targetName) |
| 176 | throws IOException { |
| 177 | assert sourceName != null; |
| 178 | assert targetName != null; |
| 179 | assert !sourceName.equals(targetName); |
| 180 | testTools.copyTestFile(sourceName, targetName); |
| 181 | testImageNames.add(targetName); |
| 182 | } |
| 183 | |
| 184 | private void createBrokenNoImagesPdf() |
| 185 | throws COSVisitorException, IOException { |
| 186 | File emptyPdf = testTools.getTestGeneratedInputFile(TestTools.BROKEN_NO_IMAGES_PDF); |
| 187 | PDDocument document = new PDDocument(); |
| 188 | |
| 189 | try { |
| 190 | //Every document requires at least one page, so we will add one |
| 191 | //blank page. |
| 192 | PDPage blankPage = new PDPage(); |
| 193 | |
| 194 | document.addPage(blankPage); |
| 195 | |
| 196 | FileOutputStream output = new FileOutputStream(emptyPdf); |
| 197 | COSWriter writer = new COSWriter(output); |
| 198 | |
| 199 | try { |
| 200 | writer.write(document.getDocument()); |
| 201 | } finally { |
| 202 | writer.close(); |
| 203 | } |
| 204 | } finally { |
| 205 | document.close(); |
| 206 | } |
| 207 | addTestComic(emptyPdf, "A (broken) PDF comic that does not contain any images, " |
| 208 | + "although it is a valid document."); |
| 209 | } |
| 210 | |
| 211 | private void createBrokenTestComics() |
| 212 | throws COSVisitorException, IOException { |
| 213 | File brokenImageIncompleteCbz = testTools.getTestGeneratedInputFile(TestTools.BROKEN_IMAGE_INCOMPLETE_CBZ); |
| 214 | |
| 215 | testTools.createTestZipArchive(brokenImageIncompleteCbz, |
| 216 | new String[]{"01.png", "incomplete.jpg", "03.png"}, null); |
| 217 | addTestComic(brokenImageIncompleteCbz, "A comic that contains a broken image."); |
| 218 | |
| 219 | File corruptedCbr = copyTestFiles(TestTools.TEST_TEXT_NAME, BROKEN_ARCHIVE_PREFIX + "corrupted.cbr"); |
| 220 | |
| 221 | addTestComic(corruptedCbr, "A broken CBR comic that is actually a text file."); |
| 222 | |
| 223 | File corruptedCbz = copyTestFiles(TestTools.TEST_TEXT_NAME, BROKEN_ARCHIVE_PREFIX + "corrupted.cbz"); |
| 224 | |
| 225 | addTestComic(corruptedCbz, "A broken CBZ comic that is actually a text file."); |
| 226 | |
| 227 | File corruptedPdf = copyTestFiles(TestTools.TEST_TEXT_NAME, BROKEN_ARCHIVE_PREFIX + "corrupted.pdf"); |
| 228 | |
| 229 | addTestComic(corruptedPdf, "A broken PDF comic that is actually a text file."); |
| 230 | |
| 231 | File brokenArchive0PagesCbz = testTools.getTestGeneratedInputFile(BROKEN_ARCHIVE_PREFIX + "0pages.cbz"); |
| 232 | |
| 233 | testTools.createTestZipArchive(brokenArchive0PagesCbz, new String[]{TestTools.TEST_TEXT_NAME}, null); |
| 234 | addTestComic(brokenArchive0PagesCbz, "A broken comic that does not contain any images, only a text file."); |
| 235 | |
| 236 | File brokenByCut = testTools.getTestFile(TestTools.TEST_IMAGE_BROKEN_BY_CUTTING_NAME); |
| 237 | File brokenByFill = testTools.getTestFile(TestTools.TEST_IMAGE_BROKEN_BY_FILLING_NAME); |
| 238 | |
| 239 | assertCannotBeLoaded(brokenByCut); |
| 240 | assertCannotBeLoaded(brokenByFill); |
| 241 | testTools.createTestZipArchive( |
| 242 | testTools.getTestGeneratedInputFile(BROKEN_ARCHIVE_PREFIX + "various.cbz"), |
| 243 | new String[]{ |
| 244 | "01." + TEST_IMAGE_SUFFIX, |
| 245 | "02." + TEST_IMAGE_SUFFIX, brokenByCut.getName(), |
| 246 | "03." + TEST_IMAGE_SUFFIX, brokenByFill.getName()}, null); |
| 247 | |
| 248 | createBrokenNoImagesPdf(); |
| 249 | } |
| 250 | |
| 251 | private File createHugeComic() |
| 252 | throws IOException { |
| 253 | File zipFile = testTools.getTestGeneratedInputFile(TestTools.HUGO_COMIC); |
| 254 | |
| 255 | if (logger.isInfoEnabled()) { |
| 256 | logger.info("create zip archive: " + zipFile); |
| 257 | } |
| 258 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 259 | ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); |
| 260 | File imageFile = testTools.getTestImageFile(); |
| 261 | |
| 262 | try { |
| 263 | for (int page = 0; page < TestTools.HUGE_PAGE_COUNT; page += 1) { |
| 264 | |
| 265 | ZipEntry zipEntry = new ZipEntry(Integer.toString(page) + ".png"); |
| 266 | |
| 267 | // set constant dummy time so version control does not consider |
| 268 | // the archives changed |
| 269 | zipEntry.setTime(0); |
| 270 | out.putNextEntry(zipEntry); |
| 271 | |
| 272 | FileInputStream in = new FileInputStream(imageFile); |
| 273 | |
| 274 | try { |
| 275 | while (in.read(buffer) > 0) { |
| 276 | out.write(buffer); |
| 277 | } |
| 278 | } finally { |
| 279 | in.close(); |
| 280 | } |
| 281 | out.closeEntry(); |
| 282 | } |
| 283 | } finally { |
| 284 | out.close(); |
| 285 | } |
| 286 | return zipFile; |
| 287 | } |
| 288 | |
| 289 | private void createTestComicPdf() |
| 290 | throws Exception { |
| 291 | File testPdf = testTools.getTestGeneratedInputFile(TestTools.TEST_COMIC_PDF); |
| 292 | File sourceImageFolder = testTools.getTestImageFile().getParentFile(); |
| 293 | File[] allSourceImageFiles = sourceImageFolder.listFiles(new RegExFileFilter("\\d\\d(\\+\\d\\d)?\\.png")); |
| 294 | String[] sourceImageNames = fileTools.getRelativePaths(sourceImageFolder, allSourceImageFiles); |
| 295 | Conversion conversion = new Conversion(Conversion.COMIC_FORMAT_PDF); |
| 296 | Task createPdfTask = new CreateComicTask(sourceImageFolder, sourceImageNames, testPdf, conversion); |
| 297 | |
| 298 | createPdfTask.start(); |
| 299 | addTestComic(testPdf, "A simple comic in PDF containing a few pages. " |
| 300 | + "This is useful to test that a viewer can process PDF. " |
| 301 | + "No fancy PDF features are used, " |
| 302 | + "these are just simple pages containing a single image and nothing else."); |
| 303 | } |
| 304 | |
| 305 | private void createTestComics() |
| 306 | throws Exception { |
| 307 | assert standardTestNames != null; |
| 308 | assert standardTestNames.length > 0; |
| 309 | assert testTools.getTestInputFile(TestTools.TEST_COMIC_CBR).exists(); |
| 310 | createTestComicPdf(); |
| 311 | |
| 312 | File testCbz = testTools.getTestComicFile(); |
| 313 | |
| 314 | testTools.createTestZipArchive(testCbz, standardTestNames, null); |
| 315 | addTestComic(testCbz, "A collection of test pages that showcase various special cases " |
| 316 | + "the two page mode can face. Among others: " |
| 317 | + "different sizes and aspect ratios of facing paces, " |
| 318 | + "different color models of facing pages, " |
| 319 | + "landscape images."); |
| 320 | |
| 321 | File testEmptyImagesCbz = testTools.getTestGeneratedInputFile(TestTools.TEST_EMPTY_IMAGES_CBZ); |
| 322 | |
| 323 | createTestEmpyImagesCbz(testEmptyImagesCbz); |
| 324 | addTestComic(testEmptyImagesCbz, "A comic containing empty image files with 0 bytes. " |
| 325 | + "A viewer should show them as broken."); |
| 326 | |
| 327 | File onePageCbz = testTools.getTestGeneratedInputFile(TestTools.SINGLE_PAGE_COMIC); |
| 328 | |
| 329 | testTools.createTestZipArchive(onePageCbz, |
| 330 | new String[]{"01." + TEST_IMAGE_SUFFIX}, null); |
| 331 | addTestComic(onePageCbz, "A comic with a single page. This is an interesting boundary case."); |
| 332 | |
| 333 | File onePageWithInfoCbz = testTools.getTestGeneratedInputFile("1page_with_info.cbz"); |
| 334 | |
| 335 | testTools.createTestZipArchive(onePageWithInfoCbz, |
| 336 | new String[]{"01." + TEST_IMAGE_SUFFIX, TestTools.TEST_TEXT_NAME}, null); |
| 337 | addTestComic(onePageWithInfoCbz, "A comic with one (single) page and a \"test.txt\" text file. " |
| 338 | + "A Viewer might choose to show the text file when opening the comic."); |
| 339 | |
| 340 | File twoPagesCbz = testTools.getTestGeneratedInputFile(TestTools.TWO_PAGE_COMIC); |
| 341 | |
| 342 | testTools.createTestZipArchive(twoPagesCbz, |
| 343 | new String[]{"01." + TEST_IMAGE_SUFFIX, "02." + TEST_IMAGE_SUFFIX}, null); |
| 344 | addTestComic(twoPagesCbz, "A comic with two (single) pages. " |
| 345 | + "This is an interesting boundary case, in particular for viewers that always " |
| 346 | + "show the title page seperately."); |
| 347 | |
| 348 | File threePagesCbz = testTools.getTestGeneratedInputFile(TestTools.THREE_PAGE_COMIC); |
| 349 | |
| 350 | testTools.createTestZipArchive(threePagesCbz, |
| 351 | new String[]{ |
| 352 | "01." + TEST_IMAGE_SUFFIX, |
| 353 | "02." + TEST_IMAGE_SUFFIX, |
| 354 | "03." + TEST_IMAGE_SUFFIX}, null); |
| 355 | addTestComic(threePagesCbz, "A comic with three (single) pages. " |
| 356 | + "This is an interesting boundary case."); |
| 357 | |
| 358 | File threePagesNotSortedCbz = testTools.getTestGeneratedInputFile("3pages_not_sorted.cbz"); |
| 359 | |
| 360 | testTools.createTestZipArchive(threePagesNotSortedCbz, |
| 361 | new String[]{ |
| 362 | "03." + TEST_IMAGE_SUFFIX, |
| 363 | "01." + TEST_IMAGE_SUFFIX, |
| 364 | "02." + TEST_IMAGE_SUFFIX}, null); |
| 365 | addTestComic(threePagesNotSortedCbz, "A comic with three (single) pages. " |
| 366 | + "In the archive, the images are stored in the order: 3, 1, 2. " |
| 367 | + "A viewer should be able to show them in the order 1, 2, 3."); |
| 368 | |
| 369 | File testMoronicNumberingCbz = testTools.getTestGeneratedInputFile(TestTools.TEST_MORONIC_NUMBERING_CBZ); |
| 370 | |
| 371 | testTools.createTestZipArchive(testMoronicNumberingCbz, |
| 372 | new String[]{ |
| 373 | "01." + TEST_IMAGE_SUFFIX, |
| 374 | "02." + TEST_IMAGE_SUFFIX, |
| 375 | "03." + TEST_IMAGE_SUFFIX, |
| 376 | "0405." + TEST_IMAGE_SUFFIX, |
| 377 | "06." + TEST_IMAGE_SUFFIX}, null); |
| 378 | addTestComic(testMoronicNumberingCbz, "In this comic, one file is named 0405.png, intending to " |
| 379 | + "contain pages 4 and 5. " |
| 380 | + "Many viewers treat the page number as 405, and will show it after page 6. " |
| 381 | + "This showcases \"moronic numbering\" as often used by inexperienced comic creators." |
| 382 | + "A proper naming for the image would have been \"04+05.png\"."); |
| 383 | |
| 384 | File logoTitleCbz = testTools.getTestGeneratedInputFile(TestTools.TEST_COMIC_WITH_LOGO_TITLE); |
| 385 | |
| 386 | testTools.createTestZipArchive(logoTitleCbz, |
| 387 | new String[]{"logo." + TEST_IMAGE_SUFFIX, "01." + TEST_IMAGE_SUFFIX, "02." + TEST_IMAGE_SUFFIX}, |
| 388 | new String[]{ |
| 389 | "logo." + TEST_IMAGE_SUFFIX, "xyz_01." + TEST_IMAGE_SUFFIX, |
| 390 | "xyz_02." + TEST_IMAGE_SUFFIX}); |
| 391 | addTestComic(logoTitleCbz, |
| 392 | "A comic with an ad as title page. This should still show the first comic page as preview image."); |
| 393 | |
| 394 | File disguisedZipCbr = copyTestFiles(TestTools.SINGLE_PAGE_COMIC, TestTools.DISGUISED_ZIP); |
| 395 | |
| 396 | addTestComic(disguisedZipCbr, "A ZIP comic that is improperly named as CBR. " |
| 397 | + "A viewer should still be able to properly identify the archive by looking at " |
| 398 | + "the \"magic bytes\" in the file header."); |
| 399 | |
| 400 | File disguisedRarCbz = copyTestFiles(TestTools.TEST_COMIC_CBR, TestTools.DISGUISED_RAR); |
| 401 | |
| 402 | addTestComic(disguisedRarCbz, "A RAR comic that is improperly named as CBZ. " |
| 403 | + "A viewer should still be able to properly identify the archive by looking at " |
| 404 | + "the \"magic bytes\" in the file header."); |
| 405 | |
| 406 | File comicsInArchiveZip = testTools.getTestGeneratedInputFile("special_comics_in_archive.cbz"); |
| 407 | |
| 408 | testTools.createTestZipArchive(comicsInArchiveZip, |
| 409 | new String[]{ |
| 410 | "1page.cbz", |
| 411 | "2pages.cbz", |
| 412 | TestTools.TEST_TEXT_NAME, |
| 413 | "test.cbz"}, null); |
| 414 | addTestComic(comicsInArchiveZip, "An archive that a user might think is a comic, but actually is " |
| 415 | + "a collection of comics. As it does not contain images directly, a viewer cannot show it. " |
| 416 | + "However, a viewer can explain the situation and offer to extract the archive in order to " |
| 417 | + "show the comics inside it."); |
| 418 | |
| 419 | File hugeComic = createHugeComic(); |
| 420 | |
| 421 | addTestComic(hugeComic, "An archive with many pages. Just a simple stress test."); |
| 422 | |
| 423 | File disguiseImagesZip = testTools.getTestGeneratedInputFile( |
| 424 | TestTools.IMAGES_WITH_WRONG_SUFFIX_COMIC); |
| 425 | |
| 426 | testTools.createTestZipArchive(disguiseImagesZip, |
| 427 | new String[]{TestTools.TEST_IMAGE_PNG_NAME, TestTools.TEST_IMAGE_PNG_NAME}, |
| 428 | new String[]{"01.jpg", "02.png"}); |
| 429 | addTestComic(comicsInArchiveZip, "An archive with a JPEG image having a \".png\" suffix ." |
| 430 | + " a PNG image having a \".jpg\" suffix. A viewer should still be able to view it " |
| 431 | + " without any problems. A PDF converter should detect the PNG image properly " |
| 432 | + " and convert it to a JPEG before including it in the PDF."); |
| 433 | |
| 434 | File landscapeOnlyComic = testTools.getTestGeneratedInputFile( |
| 435 | TestTools.TEST_COMIC_LANDSCAPE_ONLY); |
| 436 | |
| 437 | testTools.createTestZipArchive(landscapeOnlyComic, |
| 438 | new String[]{TestTools.TEST_IMAGE_LANDSCAPE_NAME, TestTools.TEST_IMAGE_LANDSCAPE_NAME}, |
| 439 | new String[]{"01.jpg", "02.png"}); |
| 440 | addTestComic(comicsInArchiveZip, "An which contains only landscape images. A viewer should" |
| 441 | + " be able to handle a landscape title page correctly, in particular the page" |
| 442 | + " numbers of the title and the following page must be correct (1+2 and 3+4)."); |
| 443 | |
| 444 | assert hasInternalError(testTools.getTestFile(TestTools.TEST_COMIC_INTERNAL_ERROR)); |
| 445 | } |
| 446 | |
| 447 | private void createTestEmpyImagesCbz(File testEmptyImagesCbz) |
| 448 | throws IOException { |
| 449 | for (int emptyNameIndex = 0; emptyNameIndex < TestTools.EMPTY_IMAGE_NAMES.length; emptyNameIndex += 1) { |
| 450 | String emptyImageToWriteName = TestTools.EMPTY_IMAGE_NAMES[emptyNameIndex]; |
| 451 | File emptyImageToWriteFile = testTools.getTestGeneratedInputFile(emptyImageToWriteName); |
| 452 | OutputStream emptyImageToWriteStream = new FileOutputStream(emptyImageToWriteFile); |
| 453 | |
| 454 | emptyImageToWriteStream.close(); |
| 455 | } |
| 456 | testTools.createTestZipArchive(testEmptyImagesCbz, TestTools.EMPTY_IMAGE_NAMES, null); |
| 457 | } |
| 458 | |
| 459 | private void createTestImages() |
| 460 | throws IOException { |
| 461 | writeTestImage(WIDTH, HEIGHT, "logo", "some cool logo"); |
| 462 | writeTestImage(WIDTH, HEIGHT, "01", "front single"); |
| 463 | writeTestImage(WIDTH, HEIGHT, "02", "single 1"); |
| 464 | writeTestImage(WIDTH, HEIGHT, "03", "single 2"); |
| 465 | writeTestImage(2 * WIDTH, HEIGHT, "04+05", "double"); |
| 466 | writeTestImage(WIDTH, HEIGHT, "06", "single 1 after double"); |
| 467 | writeTestImage(WIDTH, (int) (DIFFERENT_RATIO * HEIGHT), "07", "single 2 after double"); |
| 468 | writeTestImage(WIDTH, HEIGHT, "08", "lone single before double"); |
| 469 | writeTestFiller("09+10", "double after lone single"); |
| 470 | writeTestImage(WIDTH, HEIGHT, "11", "same ratio, normal"); |
| 471 | writeTestImage(2 * WIDTH, 2 * HEIGHT, "12", "same ratio, big"); |
| 472 | writeTestImage(2 * WIDTH, 2 * HEIGHT, "13", "same ratio, big"); |
| 473 | writeTestImage(WIDTH, HEIGHT, "14", "same ratio, normal"); |
| 474 | writeTestImage(WIDTH, HEIGHT, "15", "different ratio, normal"); |
| 475 | writeTestImage(WIDTH, (int) (DIFFERENT_RATIO * HEIGHT), "16", "different ratio, normal"); |
| 476 | writeTestImage(2 * WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_GRAY, "17+18", "gray"); |
| 477 | writeTestImage(WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_GRAY, "19", "gray"); |
| 478 | writeTestImage(WIDTH, HEIGHT, "20", "color"); |
| 479 | writeTestImage(WIDTH, HEIGHT, "21", "color"); |
| 480 | writeTestImage(WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_GRAY, "22", "gray"); |
| 481 | writeTestImage(WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_GRAY, "23", "gray, different ratio, normal"); |
| 482 | writeTestImage(WIDTH, (int) (DIFFERENT_RATIO * HEIGHT), |
| 483 | BufferedImage.TYPE_BYTE_GRAY, "24", "gray, different ratio, normal"); |
| 484 | writeTestImage(12 * WIDTH / 10, HEIGHT, "25", "same hight, wide"); |
| 485 | writeTestImage(8 * WIDTH / 10, HEIGHT, "26", "same hight, narrow"); |
| 486 | |
| 487 | writeTestImage(WIDTH, HEIGHT, "27.jpg", "JFIF/JPEG"); |
| 488 | // TODO: generate these files using a proper ImageWriter |
| 489 | copyTestImage("01.jp2", "28.jp2"); |
| 490 | copyTestImage("01.87a.gif", "29.gif"); |
| 491 | copyTestImage("01.89a.gif", "30.gif"); |
| 492 | copyTestImage("01.mac.tiff", "31.tiff"); |
| 493 | copyTestImage("01.ibm.tiff", "32.tiff"); |
| 494 | |
| 495 | // TODO: create images with 1, 4, and 6 bits depth |
| 496 | // TODO: create incomplete images with different formats |
| 497 | |
| 498 | writeTestFiller("97+98", "double before single at end"); |
| 499 | writeTestImage(WIDTH, HEIGHT, "99", "single at end"); |
| 500 | standardTestNames = (String[]) testImageNames.toArray(new String[0]); |
| 501 | copyTestImage("04+05." + TEST_IMAGE_SUFFIX, "0405." + TEST_IMAGE_SUFFIX); |
| 502 | copyTestImage(TestTools.TEST_IMAGE_PNG_NAME, TestTools.TEST_IMAGE_DISGUISED_PNG); |
| 503 | copyTestImage(TestTools.TEST_IMAGE_JPG_NAME, TestTools.TEST_IMAGE_DISGUISED_JPG); |
| 504 | |
| 505 | writeHugeImage(); |
| 506 | writeEmptyFile(TestTools.TEST_IMAGE_BROKEN_EMPTY_NAME); |
| 507 | } |
| 508 | |
| 509 | private void createTestTexts() |
| 510 | throws IOException { |
| 511 | File testTxtFile = testTools.getTestGeneratedInputFile(TestTools.TEST_TEXT_NAME); |
| 512 | FileWriter writer = new FileWriter(testTxtFile); |
| 513 | |
| 514 | try { |
| 515 | writer.write("dummy description of a comic\n"); |
| 516 | } finally { |
| 517 | writer.close(); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | private boolean hasInternalError(File brokenZipFile) |
| 522 | throws IOException { |
| 523 | boolean result = false; |
| 524 | ZipFile zipFile = null; |
| 525 | |
| 526 | try { |
| 527 | zipFile = new ZipFile(brokenZipFile, ZipFile.OPEN_READ); |
| 528 | |
| 529 | Enumeration zipEntries = zipFile.entries(); |
| 530 | |
| 531 | if (zipEntries.hasMoreElements()) { |
| 532 | zipEntries.nextElement(); |
| 533 | } |
| 534 | } catch (InternalError bug) { |
| 535 | result = true; |
| 536 | } catch (Exception error) { |
| 537 | if (logger.isDebugEnabled()) { |
| 538 | logger.debug("ignoring broken archive while checking for internal error", error); |
| 539 | } |
| 540 | } finally { |
| 541 | if (zipFile != null) { |
| 542 | zipFile.close(); |
| 543 | } |
| 544 | } |
| 545 | return result; |
| 546 | } |
| 547 | |
| 548 | private void writeEmptyFile(String name) |
| 549 | throws IOException { |
| 550 | File targetFile = testTools.getTestGeneratedInputFile(name); |
| 551 | OutputStream out = new FileOutputStream(targetFile); |
| 552 | |
| 553 | out.close(); |
| 554 | } |
| 555 | |
| 556 | private void writeHugeImage() |
| 557 | throws IOException { |
| 558 | String suffix = "jpg"; |
| 559 | BufferedImage image = new BufferedImage(3 * WIDTH, 3 * HEIGHT, BufferedImage.TYPE_INT_ARGB); |
| 560 | Graphics2D g2d = image.createGraphics(); |
| 561 | |
| 562 | try { |
| 563 | GradientPaint gradient = new GradientPaint(0, 0, Color.BLUE, |
| 564 | image.getWidth() / 6, image.getHeight() / 6, Color.RED, true); |
| 565 | |
| 566 | //g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f)); |
| 567 | g2d.setPaint(gradient); |
| 568 | g2d.fill(new Rectangle2D.Double(image.getWidth() / 4, image.getHeight() / 4, |
| 569 | image.getWidth() / 2, image.getHeight() / 2)); |
| 570 | } finally { |
| 571 | g2d.dispose(); |
| 572 | } |
| 573 | |
| 574 | |
| 575 | File file = testTools.getTestGeneratedInputFile("huge." + suffix); |
| 576 | Iterator writers = ImageIO.getImageWritersByFormatName(suffix); |
| 577 | ImageWriter writer = (ImageWriter) writers.next(); |
| 578 | ImageWriteParam writeParameters = writer.getDefaultWriteParam(); |
| 579 | |
| 580 | writeParameters.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); |
| 581 | writeParameters.setCompressionQuality(1); |
| 582 | if (logger.isInfoEnabled()) { |
| 583 | logger.info("write test image " + stringTools.sourced(file.getAbsolutePath())); |
| 584 | } |
| 585 | ImageOutputStream ios = ImageIO.createImageOutputStream(file); |
| 586 | |
| 587 | try { |
| 588 | writer.setOutput(ios); |
| 589 | writer.write(image); |
| 590 | } finally { |
| 591 | ios.close(); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | private void writeTestFiller(String baseFileName, String description) |
| 596 | throws IOException { |
| 597 | writeTestImage(2 * WIDTH, HEIGHT, baseFileName, "filler: " + description); |
| 598 | } |
| 599 | |
| 600 | private void writeTestImage(int width, int height, String baseFileName, String details) |
| 601 | throws IOException { |
| 602 | writeTestImage(width, height, BufferedImage.TYPE_INT_RGB, baseFileName, details); |
| 603 | } |
| 604 | |
| 605 | private void writeTestImage(int width, int height, int imageType, String baseFileName, String details) |
| 606 | throws IOException { |
| 607 | assert baseFileName != null; |
| 608 | assert width > 0; |
| 609 | assert height > 0; |
| 610 | String suffix = fileTools.getSuffix(baseFileName); |
| 611 | String label; |
| 612 | |
| 613 | if (suffix.length() == 0) { |
| 614 | label = baseFileName; |
| 615 | suffix = TEST_IMAGE_SUFFIX; |
| 616 | baseFileName += "." + suffix; |
| 617 | } else { |
| 618 | label = baseFileName.substring(0, baseFileName.length() - suffix.length() - 1); |
| 619 | } |
| 620 | if (logger.isDebugEnabled()) { |
| 621 | logger.debug("label=" + stringTools.sourced(label)); |
| 622 | } |
| 623 | RenderedImage image = jaiUnitTools.createTestImage(width, height, imageType, label, details); |
| 624 | Iterator writers = ImageIO.getImageWritersByFormatName(suffix); |
| 625 | ImageWriter writer = (ImageWriter) writers.next(); |
| 626 | File file = testTools.getTestGeneratedInputFile(baseFileName); |
| 627 | |
| 628 | if (logger.isInfoEnabled()) { |
| 629 | logger.info("write test image " + stringTools.sourced(file.getAbsolutePath())); |
| 630 | } |
| 631 | ImageOutputStream ios = ImageIO.createImageOutputStream(file); |
| 632 | |
| 633 | try { |
| 634 | writer.setOutput(ios); |
| 635 | writer.write(image); |
| 636 | } finally { |
| 637 | ios.close(); |
| 638 | } |
| 639 | testImageNames.add(baseFileName); |
| 640 | } |
| 641 | } |