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 | |
20 | import junit.framework.TestCase; |
21 | import net.sf.jomic.tools.FileArchive; |
22 | import net.sf.jomic.tools.FileTools; |
23 | import net.sf.jomic.tools.ProgressChangeListener; |
24 | import net.sf.jomic.tools.ProgressFrame; |
25 | import net.sf.jomic.tools.Task; |
26 | import net.sf.jomic.tools.TestTools; |
27 | import org.apache.commons.logging.Log; |
28 | import org.apache.commons.logging.LogFactory; |
29 | |
30 | /** |
31 | * TestCase for ConvertComicTask. |
32 | * |
33 | * @author Thomas Aglassinger |
34 | */ |
35 | public class ConvertComicTaskTest extends TestCase implements ProgressChangeListener |
36 | { |
37 | private static final String SUFFIX_TO_GET_RID_OF = ".png"; |
38 | private FileTools fileTools; |
39 | private Log logger; |
40 | private ProgressFrame progressFrame; |
41 | private TestTools testTools; |
42 | |
43 | protected void setUp() |
44 | throws Exception { |
45 | super.setUp(); |
46 | testTools = TestTools.instance(); |
47 | fileTools = FileTools.instance(); |
48 | logger = LogFactory.getLog(ConvertComicTaskTest.class); |
49 | progressFrame = new ProgressFrame(); |
50 | progressFrame.setTitle(getClass().getName()); |
51 | } |
52 | |
53 | public void progressChanged(Task source) { |
54 | progressFrame.setProgress(source.getProgress()); |
55 | } |
56 | |
57 | public void testConvertCbzToPdf() |
58 | throws Exception { |
59 | String methodName = "testConvertCbzToPdf"; |
60 | File sourceComicFile = testTools.getTestFile(TestTools.TEST_COMIC_CBZ); |
61 | File targetComicFile = testTools.getTestOutputFile(getClass().getName() + "." + methodName + ".pdf"); |
62 | Conversion conversion = new Conversion(Conversion.COMIC_FORMAT_PDF); |
63 | final Task convertCbzToPdfTask = new ConvertComicTask(sourceComicFile, targetComicFile, conversion); |
64 | |
65 | convertCbzToPdfTask.addProgressChangeListener(this); |
66 | progressFrame.setMaximum(convertCbzToPdfTask.getMaxProgress()); |
67 | progressFrame.setNote(methodName); |
68 | progressFrame.setVisible(true); |
69 | try { |
70 | convertCbzToPdfTask.start(); |
71 | } finally { |
72 | convertCbzToPdfTask.removeProgressChangeListener(this); |
73 | progressFrame.setVisible(false); |
74 | } |
75 | } |
76 | |
77 | public void testConvertNothingToNotYetExistentFolder() |
78 | throws Exception { |
79 | String methodName = "testConvertNothing"; |
80 | File sourceComicFile = testTools.getTestFile(TestTools.TEST_COMIC_CBZ); |
81 | File targetComicFolder = testTools.getTestOutputFile("newSubFolder"); |
82 | |
83 | if (targetComicFolder.exists()) { |
84 | fileTools.attemptToDeleteAll(targetComicFolder, logger); |
85 | if (targetComicFolder.exists()) { |
86 | throw new IllegalStateException("supposedly non-existent folder must be removed: " + targetComicFolder); |
87 | } |
88 | } |
89 | File targetComicFile = new File(targetComicFolder, getClass().getName() + "." + methodName + ".cbz"); |
90 | Conversion conversion = new Conversion(Conversion.COMIC_FORMAT_CBZ); |
91 | final Task convertNothingTask = new ConvertComicTask(sourceComicFile, targetComicFile, conversion); |
92 | |
93 | convertNothingTask.addProgressChangeListener(this); |
94 | progressFrame.setMaximum(convertNothingTask.getMaxProgress()); |
95 | progressFrame.setNote(methodName); |
96 | progressFrame.setVisible(true); |
97 | try { |
98 | convertNothingTask.start(); |
99 | } finally { |
100 | convertNothingTask.removeProgressChangeListener(this); |
101 | progressFrame.setVisible(false); |
102 | } |
103 | } |
104 | |
105 | public void testConvertPngToJpeg() |
106 | throws Exception { |
107 | String methodName = "testConvertPngToJpeg"; |
108 | File sourceComicFile = testTools.getTestFile(TestTools.TEST_COMIC_CBZ); |
109 | |
110 | // Make sure the test comic contains at least one PNG image. |
111 | FileArchive sourceZipArchiv = new FileArchive(sourceComicFile); |
112 | String[] sourceImageFileNames = sourceZipArchiv.list(); |
113 | boolean hasPng = false; |
114 | |
115 | for (int i = 0; !hasPng && (i < sourceImageFileNames.length); i += 1) { |
116 | String imageName = sourceImageFileNames[i]; |
117 | |
118 | if (imageName.endsWith(SUFFIX_TO_GET_RID_OF)) { |
119 | hasPng = true; |
120 | } |
121 | } |
122 | if (!hasPng) { |
123 | throw new IllegalArgumentException( |
124 | "comic must contain at least one image file ending in \"" |
125 | + SUFFIX_TO_GET_RID_OF + "\": " + sourceComicFile); |
126 | } |
127 | |
128 | File targetComicFile = testTools.getTestOutputFile(getClass().getName() + "." + methodName + ".cbz"); |
129 | Conversion conversion = new Conversion(Conversion.COMIC_FORMAT_CBZ); |
130 | |
131 | conversion.setImageFormat(Conversion.IMAGE_FORMAT_JFIF); |
132 | |
133 | final Task convertPngToJpegTask = new ConvertComicTask(sourceComicFile, targetComicFile, conversion); |
134 | |
135 | convertPngToJpegTask.addProgressChangeListener(this); |
136 | progressFrame.setMaximum(convertPngToJpegTask.getMaxProgress()); |
137 | progressFrame.setNote(methodName); |
138 | progressFrame.setVisible(true); |
139 | try { |
140 | convertPngToJpegTask.start(); |
141 | } finally { |
142 | convertPngToJpegTask.removeProgressChangeListener(this); |
143 | progressFrame.setVisible(false); |
144 | } |
145 | |
146 | // Assert that there are no more PNG images in the created comic. |
147 | FileArchive zipArchiv = new FileArchive(targetComicFile); |
148 | String[] targetImageFileNames = zipArchiv.list(); |
149 | |
150 | for (int i = 0; i < targetImageFileNames.length; i += 1) { |
151 | String imageName = targetImageFileNames[i]; |
152 | |
153 | assertFalse("image must not have suffix \"" + SUFFIX_TO_GET_RID_OF + "\": " + imageName, |
154 | imageName.endsWith(SUFFIX_TO_GET_RID_OF)); |
155 | } |
156 | } |
157 | |
158 | protected void tearDown() |
159 | throws Exception { |
160 | progressFrame.dispose(); |
161 | super.tearDown(); |
162 | } |
163 | } |