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.awt.Dimension; |
19 | import java.io.File; |
20 | import java.util.Arrays; |
21 | |
22 | import org.apache.commons.logging.Log; |
23 | import org.apache.commons.logging.LogFactory; |
24 | |
25 | import junit.framework.TestCase; |
26 | import net.sf.jomic.tools.FileArchive; |
27 | import net.sf.jomic.tools.FileTools; |
28 | import net.sf.jomic.tools.ImageTools; |
29 | import net.sf.jomic.tools.ProgressChangeListener; |
30 | import net.sf.jomic.tools.ProgressFrame; |
31 | import net.sf.jomic.tools.RegExFileFilter; |
32 | import net.sf.jomic.tools.Task; |
33 | import net.sf.jomic.tools.TestTools; |
34 | |
35 | /** |
36 | * Abstract TestCase for CreateXxxComicTasks. |
37 | * |
38 | * @author Thomas Aglassinger |
39 | */ |
40 | public abstract class AbstractCreateComicTaskTest extends TestCase implements ProgressChangeListener |
41 | { |
42 | private String comicSuffix; |
43 | private FileTools fileTools; |
44 | private ImageTools imageTools; |
45 | private Log logger; |
46 | private ProgressFrame progressFrame; |
47 | private TestTools testTools; |
48 | |
49 | protected void setUp() |
50 | throws Exception { |
51 | super.setUp(); |
52 | testTools = TestTools.instance(); |
53 | fileTools = FileTools.instance(); |
54 | imageTools = ImageTools.instance(); |
55 | logger = LogFactory.getLog(AbstractCreateComicTaskTest.class); |
56 | testTools.setupCache(); |
57 | comicSuffix = getComicFormat().toLowerCase(); |
58 | progressFrame = new ProgressFrame(); |
59 | progressFrame.setTitle(getCallerClass().getName()); |
60 | } |
61 | |
62 | /** |
63 | * The calling test class (required to derive sensible target file names). |
64 | */ |
65 | protected abstract Class getCallerClass(); |
66 | |
67 | /** |
68 | * The target comic format the created test comics should have. |
69 | */ |
70 | protected abstract String getComicFormat(); |
71 | |
72 | public void progressChanged(Task source) { |
73 | progressFrame.setProgress(source.getProgress()); |
74 | } |
75 | |
76 | public void testCreateComicFromWrongImageSuffix() |
77 | throws Exception { |
78 | File wrongPngImage = testTools.getTestFile(TestTools.TEST_IMAGE_DISGUISED_PNG); |
79 | File wrongJpgImage = testTools.getTestFile(TestTools.TEST_IMAGE_DISGUISED_JPG); |
80 | String targetComicName = testTools.getTestFileName( |
81 | getCallerClass(), "testCreateComicFromWrongImageSuffix", null, comicSuffix); |
82 | File targetComicFile = testTools.getTestOutputFile(targetComicName); |
83 | File baseDir = wrongPngImage.getParentFile(); |
84 | Task createComicTask = createCreateComicTask( |
85 | baseDir, new File[]{wrongPngImage, wrongJpgImage}, targetComicFile); |
86 | |
87 | try { |
88 | progressFrame.setNote("testCreateComicFromWrongImageSuffix"); |
89 | progressFrame.setVisible(true); |
90 | createComicTask.start(); |
91 | assertTrue(targetComicFile.exists()); |
92 | } finally { |
93 | createComicTask.removeProgressChangeListener(this); |
94 | progressFrame.setVisible(false); |
95 | } |
96 | } |
97 | |
98 | public void testCreateComicTask() |
99 | throws Exception { |
100 | |
101 | String targetComicName = testTools.getTestFileName( |
102 | getCallerClass(), "testCreateComicTask", null, comicSuffix); |
103 | File targetComicFile = testTools.getTestOutputFile(targetComicName); |
104 | File testImageDir = testTools.getTestImageFile().getParentFile(); |
105 | File[] allTestImageFiles = testImageDir.listFiles(new RegExFileFilter("\\d\\d(\\+\\d\\d)?\\.png")); |
106 | File[] testImageFiles; |
107 | |
108 | assert allTestImageFiles.length > 0; |
109 | Arrays.sort(allTestImageFiles); |
110 | |
111 | // Use only the first few images to save time. |
112 | testImageFiles = new File[Math.min(8, allTestImageFiles.length)]; |
113 | System.arraycopy(allTestImageFiles, 0, testImageFiles, 0, testImageFiles.length); |
114 | |
115 | Task createComicTask = createCreateComicTask(testImageDir, testImageFiles, targetComicFile); |
116 | |
117 | try { |
118 | progressFrame.setNote("testCreateComicTask"); |
119 | progressFrame.setVisible(true); |
120 | fileTools.deleteOrSkip(targetComicFile, logger); |
121 | assertFalse(targetComicFile.exists()); |
122 | createComicTask.start(); |
123 | assertTrue(targetComicFile.exists()); |
124 | } finally { |
125 | createComicTask.removeProgressChangeListener(this); |
126 | progressFrame.setVisible(false); |
127 | } |
128 | } |
129 | |
130 | public void testCreateComicWithTrimmedImage() |
131 | throws Exception { |
132 | File portraitImageFile = testTools.getTestFile(TestTools.TEST_IMAGE_PORTRAIT_NAME); |
133 | File landscapeImageFile = testTools.getTestFile(TestTools.TEST_IMAGE_LANDSCAPE_NAME); |
134 | File baseDir = portraitImageFile.getParentFile(); |
135 | String targetComicName = testTools.getTestFileName( |
136 | getCallerClass(), "testCreateComicWithTrimmedImage", |
137 | null, comicSuffix); |
138 | File targetComicFile = testTools.getTestOutputFile(targetComicName); |
139 | Dimension imageSize = imageTools.getImageDimension(portraitImageFile); |
140 | Conversion trimImageConversion = new Conversion(); |
141 | |
142 | trimImageConversion.setComicFormat(getComicFormat()); |
143 | trimImageConversion.setLimitImageSize(true); |
144 | trimImageConversion.setMaxImageWidth(imageSize.width); |
145 | trimImageConversion.setMaxImageHeight(imageSize.height / 2); |
146 | |
147 | Task createComicTask = createCreateComicTask( |
148 | targetComicFile, baseDir, new File[]{portraitImageFile, landscapeImageFile}, trimImageConversion); |
149 | |
150 | try { |
151 | progressFrame.setNote("testCreateComicWithTrimmedImage"); |
152 | progressFrame.setVisible(true); |
153 | createComicTask.start(); |
154 | assertTrue(targetComicFile.exists()); |
155 | } finally { |
156 | createComicTask.removeProgressChangeListener(this); |
157 | progressFrame.setVisible(false); |
158 | } |
159 | |
160 | ComicModel comic = new ComicModel(targetComicFile); |
161 | |
162 | assertTrue(comic.getImageCount() == 2); |
163 | |
164 | int portraitImageHeight = comic.getImage(0).getHeight(); |
165 | int landscapeImageHeight = comic.getImage(1).getHeight(); |
166 | |
167 | assertEquals(trimImageConversion.getMaxImageHeight(), portraitImageHeight); |
168 | assertEquals(trimImageConversion.getMaxImageHeight(), landscapeImageHeight); |
169 | } |
170 | |
171 | public void testCreateNoComicFromNoImages() |
172 | throws Exception { |
173 | File notAnImageFile = testTools.getTestFile(TestTools.TEST_TEXT_NAME); |
174 | String targetComicName = testTools.getTestFileName( |
175 | getCallerClass(), "testCreateNoComicFromNoImages", null, comicSuffix); |
176 | File targetComicFile = testTools.getTestOutputFile(targetComicName); |
177 | File baseDir = notAnImageFile.getParentFile(); |
178 | Task createComicTask = createCreateComicTask(baseDir, |
179 | new File[]{notAnImageFile}, targetComicFile); |
180 | |
181 | fileTools.deleteOrSkip(targetComicFile, logger); |
182 | if (targetComicFile.exists()) { |
183 | throw new IllegalStateException("target file must be removed because " |
184 | + "it should never have existed in the first place: " + targetComicFile); |
185 | } |
186 | try { |
187 | progressFrame.setNote("testCreateComicTask"); |
188 | progressFrame.setVisible(true); |
189 | createComicTask.start(); |
190 | assertTrue("file must not exist: " + targetComicFile, !targetComicFile.exists()); |
191 | } finally { |
192 | createComicTask.removeProgressChangeListener(this); |
193 | progressFrame.setVisible(false); |
194 | } |
195 | } |
196 | |
197 | public void testCreateTiffComic() |
198 | throws Exception { |
199 | File tiffImageFile = testTools.getTestFile(TestTools.TEST_IMAGE_MAC_TIFF); |
200 | File baseDir = tiffImageFile.getParentFile(); |
201 | String targetComicName = testTools.getTestFileName( |
202 | getCallerClass(), "testCreateTiffComic", |
203 | null, comicSuffix); |
204 | File targetComicFile = testTools.getTestOutputFile(targetComicName); |
205 | Task createComicTask = createCreateComicTask( |
206 | baseDir, new File[]{tiffImageFile}, targetComicFile); |
207 | |
208 | try { |
209 | progressFrame.setNote("testCreateTiffComic"); |
210 | progressFrame.setVisible(true); |
211 | createComicTask.start(); |
212 | assertTrue(targetComicFile.exists()); |
213 | } finally { |
214 | createComicTask.removeProgressChangeListener(this); |
215 | progressFrame.setVisible(false); |
216 | } |
217 | |
218 | ComicModel comic = new ComicModel(targetComicFile); |
219 | |
220 | assertTrue(comic.getImageCount() == 1); |
221 | } |
222 | |
223 | protected abstract Task createNewComicTask( |
224 | File baseDir, File[] testImageFiles, File targetComicFile, Conversion conversion); |
225 | |
226 | protected void tearDown() |
227 | throws Exception { |
228 | imageTools = null; |
229 | testTools = null; |
230 | super.tearDown(); |
231 | } |
232 | |
233 | private Task createCreateComicTask(File baseDir, File[] testImageFiles, File targetComicFile) { |
234 | return createCreateComicTask(targetComicFile, baseDir, testImageFiles, null); |
235 | } |
236 | |
237 | private Task createCreateComicTask( |
238 | File targetComicFile, File baseDir, File[] testImageFiles, Conversion conversion) { |
239 | assert baseDir != null; |
240 | |
241 | Conversion actualConversion; |
242 | |
243 | if (conversion == null) { |
244 | actualConversion = new Conversion(getComicFormat()); |
245 | } else { |
246 | actualConversion = conversion; |
247 | } |
248 | assertEquals(getComicFormat(), actualConversion.getComicFormat()); |
249 | |
250 | Task result = createNewComicTask(baseDir, testImageFiles, targetComicFile, actualConversion); |
251 | |
252 | progressFrame.setMaximum(result.getMaxProgress()); |
253 | result.addProgressChangeListener(this); |
254 | return result; |
255 | } |
256 | } |