| 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.Arrays; |
| 20 | |
| 21 | import net.sf.jomic.tools.FileTools; |
| 22 | import net.sf.jomic.tools.ProgressChangeListener; |
| 23 | import net.sf.jomic.tools.ProgressFrame; |
| 24 | import net.sf.jomic.tools.RegExFileFilter; |
| 25 | import net.sf.jomic.tools.StringTools; |
| 26 | import net.sf.jomic.tools.Task; |
| 27 | import net.sf.jomic.tools.TestTools; |
| 28 | import junit.framework.TestCase; |
| 29 | |
| 30 | /** |
| 31 | * TestCase for ConvertComicTask. |
| 32 | * |
| 33 | * @author Thomas Aglassinger |
| 34 | */ |
| 35 | public class CreateComicTaskTest extends TestCase implements ProgressChangeListener |
| 36 | { |
| 37 | private ProgressFrame progressFrame; |
| 38 | private StringTools stringTools; |
| 39 | private TestTools testTools; |
| 40 | |
| 41 | protected void setUp() |
| 42 | throws Exception { |
| 43 | super.setUp(); |
| 44 | testTools = TestTools.instance(); |
| 45 | stringTools = StringTools.instance(); |
| 46 | progressFrame = new ProgressFrame(); |
| 47 | progressFrame.setTitle(getClass().getName()); |
| 48 | } |
| 49 | |
| 50 | public void progressChanged(Task source) { |
| 51 | progressFrame.setProgress(source.getProgress()); |
| 52 | } |
| 53 | |
| 54 | public void testCreateCbz() |
| 55 | throws Exception { |
| 56 | testCreate(new Conversion(Conversion.COMIC_FORMAT_CBZ)); |
| 57 | } |
| 58 | |
| 59 | public void testCreatePdf() |
| 60 | throws Exception { |
| 61 | testCreate(new Conversion(Conversion.COMIC_FORMAT_PDF)); |
| 62 | } |
| 63 | |
| 64 | protected void tearDown() |
| 65 | throws Exception { |
| 66 | progressFrame.dispose(); |
| 67 | super.tearDown(); |
| 68 | } |
| 69 | |
| 70 | private void testCreate(Conversion conversion) |
| 71 | throws Exception { |
| 72 | |
| 73 | String comicSuffix = conversion.getComicFormat(); |
| 74 | String method = "testCreate" + stringTools.titled(comicSuffix); |
| 75 | String targetComicName = testTools.getTestFileName( |
| 76 | CreateComicTaskTest.class, method, null, comicSuffix); |
| 77 | File targetComicFile = testTools.getTestOutputFile(targetComicName); |
| 78 | File testImageDir = testTools.getTestImageFile().getParentFile(); |
| 79 | File[] allTestImageFiles = testImageDir.listFiles(new RegExFileFilter("\\d\\d(\\+\\d\\d)?\\.png")); |
| 80 | File[] testImageFiles; |
| 81 | |
| 82 | assert allTestImageFiles.length > 0; |
| 83 | Arrays.sort(allTestImageFiles); |
| 84 | |
| 85 | // Use only the first few images to save time. |
| 86 | testImageFiles = new File[Math.min(8, allTestImageFiles.length)]; |
| 87 | System.arraycopy(allTestImageFiles, 0, testImageFiles, 0, testImageFiles.length); |
| 88 | |
| 89 | FileTools fileTools = FileTools.instance(); |
| 90 | String[] sourceNames = fileTools.getRelativePaths(testImageDir, testImageFiles); |
| 91 | |
| 92 | Task createComicTask = new CreateComicTask(testImageDir, sourceNames, targetComicFile, conversion); |
| 93 | |
| 94 | progressFrame.setMaximum(createComicTask.getMaxProgress()); |
| 95 | createComicTask.addProgressChangeListener(this); |
| 96 | try { |
| 97 | progressFrame.setNote(method); |
| 98 | progressFrame.setVisible(true); |
| 99 | targetComicFile.delete(); |
| 100 | assertFalse(targetComicFile.exists()); |
| 101 | createComicTask.start(); |
| 102 | assertTrue(targetComicFile.exists()); |
| 103 | } finally { |
| 104 | createComicTask.removeProgressChangeListener(this); |
| 105 | progressFrame.setVisible(false); |
| 106 | } |
| 107 | } |
| 108 | } |