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.image.RenderedImage; |
19 | import java.io.File; |
20 | import java.io.IOException; |
21 | |
22 | import junit.framework.TestCase; |
23 | import net.sf.jomic.common.Settings; |
24 | import net.sf.jomic.tools.FileTools; |
25 | import net.sf.jomic.tools.TestTools; |
26 | import org.apache.commons.logging.Log; |
27 | import org.apache.commons.logging.LogFactory; |
28 | |
29 | /** |
30 | * TestCase for Thumbnail. |
31 | * |
32 | * @author Thomas Aglassinger |
33 | */ |
34 | public class ThumbnailTest extends TestCase |
35 | { |
36 | static final int MAX_THUMB_HEIGHT = 100; |
37 | static final int MAX_THUMB_WIDTH = 135; |
38 | |
39 | private Log logger; |
40 | private TestTools testTools; |
41 | |
42 | /* |
43 | * @see TestCase#setUp() |
44 | */ |
45 | protected void setUp() |
46 | throws Exception { |
47 | super.setUp(); |
48 | logger = LogFactory.getLog(ThumbnailTest.class); |
49 | testTools = TestTools.instance(); |
50 | } |
51 | |
52 | /** |
53 | * Regression test for bug 1991777: NullPointerException when no ImageReader for thumb nail. |
54 | */ |
55 | public void testCanObtainThumbForEmptyImage() |
56 | throws IOException { |
57 | File cacheDir = Settings.instance().getCacheDir(); |
58 | |
59 | clearComicCaches(cacheDir); |
60 | |
61 | ComicCache comicCache = ComicCache.instance(); |
62 | |
63 | comicCache.setUp(cacheDir); |
64 | |
65 | for (int emptyImageNameIndex = 0; emptyImageNameIndex < TestTools.EMPTY_IMAGE_NAMES.length; |
66 | emptyImageNameIndex += 1) { |
67 | String emptyImageName = TestTools.EMPTY_IMAGE_NAMES[emptyImageNameIndex]; |
68 | File imageFile = testTools.getTestFile(emptyImageName); |
69 | RenderedImage thumbImage = comicCache.getThumbnail(imageFile); |
70 | |
71 | assertNotNull(thumbImage); |
72 | |
73 | RenderedImage againThumbImage = comicCache.getThumbnail(imageFile); |
74 | |
75 | assertNotNull(againThumbImage); |
76 | } |
77 | } |
78 | |
79 | public void testCanObtainThumbFromCache() |
80 | throws IOException { |
81 | File cacheDir = Settings.instance().getCacheDir(); |
82 | |
83 | clearComicCaches(cacheDir); |
84 | |
85 | ComicCache comicCache = ComicCache.instance(); |
86 | |
87 | comicCache.setUp(cacheDir); |
88 | |
89 | File imageFile = testTools.getTestImageFile(); |
90 | RenderedImage thumbImage = comicCache.getThumbnail(imageFile); |
91 | |
92 | assertNotNull(thumbImage); |
93 | |
94 | RenderedImage againThumbImage = comicCache.getThumbnail(imageFile); |
95 | |
96 | assertNotNull(againThumbImage); |
97 | } |
98 | |
99 | |
100 | /* |
101 | * @see TestCase#tearDown() |
102 | */ |
103 | protected void tearDown() |
104 | throws Exception { |
105 | testTools = null; |
106 | super.tearDown(); |
107 | } |
108 | |
109 | // TODO: find a dencent way to clear the files in the cache |
110 | private void clearComicCaches(File cacheDir) { |
111 | FileTools.instance().attemptToDeleteAll(cacheDir, logger); |
112 | } |
113 | } |
114 | |