| 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.io.File; |
| 19 | import java.io.IOException; |
| 20 | import java.io.StringReader; |
| 21 | import java.io.StringWriter; |
| 22 | |
| 23 | import junit.framework.TestCase; |
| 24 | |
| 25 | import org.xml.sax.SAXException; |
| 26 | |
| 27 | /** |
| 28 | * TestCase for ArchiveCache and ArchiveCacheEntry. |
| 29 | * |
| 30 | * @author Thomas Aglassinger |
| 31 | */ |
| 32 | public class ArchiveCacheTest extends TestCase |
| 33 | { |
| 34 | private static final int TEST_CACHE_SIZE = 64 * 1024 * 1024; |
| 35 | private TestTools testTools; |
| 36 | |
| 37 | protected void setUp() |
| 38 | throws Exception { |
| 39 | super.setUp(); |
| 40 | testTools = TestTools.instance(); |
| 41 | testTools.setupCache(); |
| 42 | } |
| 43 | |
| 44 | public void testEntry() { |
| 45 | ArchiveCacheEntry entry = new ArchiveCacheEntry(testTools.getTestComicFile(), 0x12345678); |
| 46 | String sep = File.separator; |
| 47 | String expectedPathSuffix = "12" + sep + "34" + sep + "56" + sep + "78"; |
| 48 | String actualPath = entry.getCachedDirName(); |
| 49 | String actualPathSuffix = actualPath.substring(actualPath.length() - expectedPathSuffix.length()); |
| 50 | |
| 51 | assertEquals(expectedPathSuffix, actualPathSuffix); |
| 52 | } |
| 53 | |
| 54 | public void testSetMaxSize() |
| 55 | throws IOException { |
| 56 | ArchiveCache cache = createTestCache(); |
| 57 | int oldEntryCount = cache.getEntryCount(); |
| 58 | long oldUsedSize = cache.getUsedSize(); |
| 59 | |
| 60 | assertTrue("oldEntryCount=" + oldEntryCount, (oldEntryCount > 1)); |
| 61 | assertTrue("oldUsedSize=" + oldUsedSize, (oldUsedSize > 0)); |
| 62 | |
| 63 | // Increase the cache |
| 64 | cache.setMaxSize(2 * cache.getMaxSize()); |
| 65 | |
| 66 | int increasedEntryCount = cache.getEntryCount(); |
| 67 | long increasedUsedSize = cache.getUsedSize(); |
| 68 | |
| 69 | assertEquals("increasedEntryCount", oldEntryCount, increasedEntryCount); |
| 70 | assertEquals("increasedUsedSize", oldUsedSize, increasedUsedSize); |
| 71 | |
| 72 | cache.setMaxSize(0); |
| 73 | |
| 74 | int reducedEntryCount = cache.getEntryCount(); |
| 75 | long reducedUsedSize = cache.getUsedSize(); |
| 76 | |
| 77 | assertEquals("reducedEntryCount", 1, reducedEntryCount); |
| 78 | assertTrue("oldUsedSize=" + oldUsedSize + ", reducedUsedSize=" + reducedUsedSize, |
| 79 | (reducedUsedSize < oldUsedSize)); |
| 80 | } |
| 81 | |
| 82 | public void testWriteAndRead() |
| 83 | throws IOException, SAXException { |
| 84 | ArchiveCache cache = createTestCache(); |
| 85 | |
| 86 | int writtenEntryCount = cache.getEntryCount(); |
| 87 | |
| 88 | String textWritten = createCacheXmlString(cache); |
| 89 | |
| 90 | assertTrue(textWritten.length() > 0); |
| 91 | |
| 92 | StringReader reader = new StringReader(textWritten); |
| 93 | |
| 94 | try { |
| 95 | cache.readEntries(reader); |
| 96 | } finally { |
| 97 | reader.close(); |
| 98 | } |
| 99 | |
| 100 | int readEntryCount = cache.getEntryCount(); |
| 101 | |
| 102 | assertEquals(writtenEntryCount, readEntryCount); |
| 103 | |
| 104 | String textWrittenAgain = createCacheXmlString(cache); |
| 105 | |
| 106 | assertEquals(textWritten, textWrittenAgain); |
| 107 | |
| 108 | cache.clear(); |
| 109 | assertEquals(0, cache.getEntryCount()); |
| 110 | cache.attemptToReadEntries(); |
| 111 | assertEquals(0, cache.getEntryCount()); |
| 112 | cache.writeEntries(); |
| 113 | |
| 114 | cache = createTestCache(); |
| 115 | cache.writeEntries(); |
| 116 | } |
| 117 | |
| 118 | protected void tearDown() |
| 119 | throws Exception { |
| 120 | testTools = null; |
| 121 | super.tearDown(); |
| 122 | } |
| 123 | |
| 124 | private String createCacheXmlString(ArchiveCache cache) |
| 125 | throws IOException { |
| 126 | StringWriter writer = new StringWriter(); |
| 127 | String textWritten; |
| 128 | |
| 129 | try { |
| 130 | cache.writeEntries(writer); |
| 131 | } finally { |
| 132 | textWritten = writer.getBuffer().toString(); |
| 133 | writer.close(); |
| 134 | } |
| 135 | return textWritten; |
| 136 | } |
| 137 | |
| 138 | private ArchiveCache createTestCache() |
| 139 | throws IOException { |
| 140 | File cacheDir = new File(System.getProperty("java.io.tmpdir"), ArchiveCacheTest.class.getName()); |
| 141 | ArchiveCache result = new ArchiveCache(cacheDir, TEST_CACHE_SIZE); |
| 142 | |
| 143 | result.clear(); |
| 144 | |
| 145 | ArchiveCacheEntry entry1 = createTestCacheEntry(result, testTools.getTestFile("3pages.cbz")); |
| 146 | |
| 147 | assertEquals(0, entry1.getIndex()); |
| 148 | putTestCacheEntry(result, entry1); |
| 149 | assertEquals("usedSize", entry1.getSize(), result.getUsedSize()); |
| 150 | |
| 151 | ArchiveCacheEntry entry2 = createTestCacheEntry(result, testTools.getTestFile("2pages.cbz")); |
| 152 | |
| 153 | assertEquals(1, entry2.getIndex()); |
| 154 | putTestCacheEntry(result, entry2); |
| 155 | assertEquals("usedSize", entry1.getSize() + entry2.getSize(), result.getUsedSize()); |
| 156 | |
| 157 | return result; |
| 158 | } |
| 159 | |
| 160 | private ArchiveCacheEntry createTestCacheEntry(ArchiveCache cache, File archiveFile) |
| 161 | throws IOException { |
| 162 | assert cache != null; |
| 163 | assert archiveFile != null; |
| 164 | ArchiveCacheEntry result = cache.createCacheEntry(archiveFile); |
| 165 | FileArchive archive = new FileArchive(archiveFile); |
| 166 | |
| 167 | archive.extract(cache.getCacheEntryDir(result), archive.list()); |
| 168 | |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | private void putTestCacheEntry(ArchiveCache cache, ArchiveCacheEntry entry) { |
| 173 | assert cache != null; |
| 174 | assert entry != null; |
| 175 | |
| 176 | cache.put(entry); |
| 177 | cache.setCacheEntrySize(entry); |
| 178 | } |
| 179 | } |