1 5 11 package com.opensymphony.oscache.plugins.diskpersistence; 12 13 14 18 import com.opensymphony.oscache.base.CacheEntry; 19 import com.opensymphony.oscache.base.Config; 20 import com.opensymphony.oscache.base.persistence.CachePersistenceException; 21 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 26 import java.io.File ; 27 import java.io.FilenameFilter ; 28 29 import java.util.HashSet ; 30 import java.util.Properties ; 31 import java.util.Set ; 32 33 41 public final class TestHashDiskPersistenceListener extends TestCase { 42 45 private HashDiskPersistenceListener listener = null; 46 47 50 private final String CONTENT = "Disk persistance content"; 51 52 55 private final String GROUP = "test group"; 56 57 60 private final String KEY = "Test disk persistance listener key"; 61 private CacheFileFilter cacheFileFilter = new CacheFileFilter(); 62 63 public TestHashDiskPersistenceListener(String str) { 64 super(str); 65 } 66 67 72 public static Test suite() { 73 return new TestSuite(TestHashDiskPersistenceListener.class); 74 } 75 76 80 public void setUp() { 81 listener = new HashDiskPersistenceListener(); 83 84 Properties p = new Properties (); 85 p.setProperty("cache.path", TestDiskPersistenceListener.CACHEDIR); 86 p.setProperty("cache.memory", "false"); 87 p.setProperty("cache.persistence.class", "com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener"); 88 p.setProperty("cache.persistence.disk.hash.algorithm", "MD5"); 89 listener.configure(new Config(p)); 90 } 91 92 95 public void testClear() { 96 testStoreRetrieve(); 98 99 try { 101 listener.clear(); 102 assertTrue(!listener.isStored(KEY)); 103 } catch (CachePersistenceException cpe) { 104 cpe.printStackTrace(); 105 fail("Exception thrown in test clear!"); 106 } 107 } 108 109 112 public void testIsStored() { 113 try { 114 listener.store(KEY, CONTENT); 115 116 assertTrue(listener.isStored(KEY)); 118 119 assertTrue(!listener.isStored(KEY + "fake")); 121 } catch (Exception e) { 122 e.printStackTrace(); 123 fail("testIsStored raised an exception"); 124 } 125 } 126 127 130 public void testRemove() { 131 try { 133 if (!listener.isStored(KEY)) { 134 listener.store(KEY, CONTENT); 135 } 136 137 listener.remove(KEY); 139 } catch (CachePersistenceException cpe) { 140 cpe.printStackTrace(); 141 fail("Exception thrown in test remove!"); 142 } 143 } 144 145 148 public void testStoreRetrieve() { 149 CacheEntry entry = new CacheEntry(KEY); 151 entry.setContent(CONTENT); 152 153 try { 154 listener.store(KEY, entry); 155 156 CacheEntry newEntry = (CacheEntry) listener.retrieve(KEY); 158 assertTrue(entry.getContent().equals(newEntry.getContent())); 159 assertEquals(entry.getCreated(), newEntry.getCreated()); 160 assertTrue(entry.getKey().equals(newEntry.getKey())); 161 162 assertNull(listener.retrieve("doesn't exist")); 164 } catch (Exception ex) { 165 ex.printStackTrace(); 166 fail("Exception raised!"); 167 } 168 } 169 170 173 public void testStoreRetrieveGroups() { 174 Set groupSet = new HashSet (); 176 groupSet.add("1"); 177 groupSet.add("2"); 178 179 try { 180 listener.storeGroup(GROUP, groupSet); 181 182 groupSet = listener.retrieveGroup(GROUP); 184 assertNotNull(groupSet); 185 186 assertTrue(groupSet.contains("1")); 187 assertTrue(groupSet.contains("2")); 188 assertFalse(groupSet.contains("3")); 189 190 assertNull(listener.retrieveGroup("abc")); 192 } catch (Exception ex) { 193 ex.printStackTrace(); 194 fail("Exception raised!"); 195 } 196 } 197 198 protected void tearDown() throws Exception { 199 listener.clear(); 200 assertTrue("Cache not cleared", new File (TestDiskPersistenceListener.CACHEDIR).list(cacheFileFilter).length == 0); 201 } 202 203 private static class CacheFileFilter implements FilenameFilter { 204 public boolean accept(File dir, String name) { 205 return !"__groups__".equals(name); 206 } 207 } 208 } 209 | Popular Tags |