| 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.tools; |
| 17 | |
| 18 | import java.awt.image.RenderedImage; |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | |
| 22 | import org.apache.commons.logging.Log; |
| 23 | import org.apache.commons.logging.LogFactory; |
| 24 | |
| 25 | import junit.framework.TestCase; |
| 26 | |
| 27 | /** |
| 28 | * TestCase for ImageCache. |
| 29 | * |
| 30 | * @author Thomas Aglassinger |
| 31 | */ |
| 32 | public class ImageCacheTest extends TestCase |
| 33 | { |
| 34 | private FileTools fileTools; |
| 35 | private Log logger; |
| 36 | private TestTools testTools; |
| 37 | |
| 38 | protected void setUp() |
| 39 | throws Exception { |
| 40 | super.setUp(); |
| 41 | testTools = TestTools.instance(); |
| 42 | fileTools = FileTools.instance(); |
| 43 | logger = LogFactory.getLog(ImageCacheTest.class); |
| 44 | } |
| 45 | |
| 46 | public void testBigImageCache() |
| 47 | throws IOException { |
| 48 | ImageCache cache = new ImageCache("big", Integer.MAX_VALUE); |
| 49 | File imageFile1 = testTools.getTestGeneratedInputFile("01.png"); |
| 50 | File imageFile2 = testTools.getTestGeneratedInputFile("02.png"); |
| 51 | RenderedImage image1 = cache.get(imageFile1); |
| 52 | |
| 53 | assertNotNull(image1); |
| 54 | assertEquals(1, cache.getEntryCount()); |
| 55 | |
| 56 | RenderedImage image2 = cache.get(imageFile2); |
| 57 | |
| 58 | assertNotNull(image2); |
| 59 | assertEquals(2, cache.getEntryCount()); |
| 60 | assertTrue(cache.has(imageFile1)); |
| 61 | assertTrue(cache.has(imageFile2)); |
| 62 | } |
| 63 | |
| 64 | public void testSmallImageCache() |
| 65 | throws IOException { |
| 66 | ImageCache cache = new ImageCache("small", 1); |
| 67 | File imageFile1 = testTools.getTestGeneratedInputFile("01.png"); |
| 68 | File imageFile2 = testTools.getTestGeneratedInputFile("02.png"); |
| 69 | RenderedImage image1 = cache.get(imageFile1); |
| 70 | |
| 71 | assertNotNull(image1); |
| 72 | assertEquals(1, cache.getEntryCount()); |
| 73 | assertTrue(cache.has(imageFile1)); |
| 74 | assertFalse(cache.has(imageFile2)); |
| 75 | |
| 76 | RenderedImage image2 = cache.get(imageFile2); |
| 77 | |
| 78 | assertNotNull(image2); |
| 79 | assertEquals(1, cache.getEntryCount()); |
| 80 | assertFalse(cache.has(imageFile1)); |
| 81 | assertTrue(cache.has(imageFile2)); |
| 82 | } |
| 83 | |
| 84 | public void testWithALotOfImages() |
| 85 | throws IOException { |
| 86 | ImageCache cache = new ImageCache("testWithALotOfImages", |
| 87 | Runtime.getRuntime().maxMemory() * 1 / 4); |
| 88 | File imageFile = testTools.getTestImageFile(); |
| 89 | File tempDir = File.createTempFile(ImageCacheTest.class.getName(), |
| 90 | ".tmp"); |
| 91 | |
| 92 | fileTools.delete(tempDir); |
| 93 | fileTools.mkdirs(tempDir); |
| 94 | try { |
| 95 | for (int index = 0; index < 1000; index += 1) { |
| 96 | String nextName = Integer.toString(index) + ".png"; |
| 97 | File nextFile = new File(tempDir, nextName); |
| 98 | |
| 99 | fileTools.copyFile(imageFile, nextFile); |
| 100 | cache.get(nextFile); |
| 101 | } |
| 102 | } finally { |
| 103 | fileTools.attemptToDeleteAll(tempDir, logger); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | protected void tearDown() |
| 108 | throws Exception { |
| 109 | fileTools = null; |
| 110 | testTools = null; |
| 111 | super.tearDown(); |
| 112 | } |
| 113 | } |