1 5 package com.opensymphony.oscache.plugins.diskpersistence; 6 7 import com.opensymphony.oscache.base.CacheEntry; 8 import com.opensymphony.oscache.base.Config; 9 import com.opensymphony.oscache.base.persistence.CachePersistenceException; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 15 import java.io.File ; 16 import java.io.FilenameFilter ; 17 18 import java.util.HashSet ; 19 import java.util.Properties ; 20 import java.util.Set ; 21 22 30 public final class TestDiskPersistenceListener extends TestCase { 31 34 public static final String CACHEDIR = "/tmp/diskcache"; 35 36 39 private DiskPersistenceListener listener = null; 40 41 44 private final String CONTENT = "Disk persistance content"; 45 46 49 private final String GROUP = "test group"; 50 51 54 private final String KEY = "Test disk persistance listener key"; 55 private CacheFileFilter cacheFileFilter = new CacheFileFilter(); 56 57 public TestDiskPersistenceListener(String str) { 58 super(str); 59 } 60 61 66 public static Test suite() { 67 return new TestSuite(TestDiskPersistenceListener.class); 68 } 69 70 74 public void setUp() { 75 listener = new DiskPersistenceListener(); 77 78 Properties p = new Properties (); 79 p.setProperty("cache.path", CACHEDIR); 80 p.setProperty("cache.memory", "false"); 81 p.setProperty("cache.persistence.class", "com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener"); 82 listener.configure(new Config(p)); 83 } 84 85 88 public void testClear() { 89 testStoreRetrieve(); 91 92 try { 94 listener.clear(); 95 assertTrue(!listener.isStored(KEY)); 96 } catch (CachePersistenceException cpe) { 97 cpe.printStackTrace(); 98 fail("Exception thrown in test clear!"); 99 } 100 } 101 102 105 public void testIsStored() { 106 try { 107 listener.store(KEY, CONTENT); 108 109 assertTrue(listener.isStored(KEY)); 111 112 assertTrue(!listener.isStored(KEY + "fake")); 114 } catch (Exception e) { 115 e.printStackTrace(); 116 fail("testIsStored raised an exception"); 117 } 118 } 119 120 123 public void testRemove() { 124 try { 126 if (!listener.isStored(KEY)) { 127 listener.store(KEY, CONTENT); 128 } 129 130 listener.remove(KEY); 132 } catch (CachePersistenceException cpe) { 133 cpe.printStackTrace(); 134 fail("Exception thrown in test remove!"); 135 } 136 } 137 138 141 public void testStoreRetrieve() { 142 CacheEntry entry = new CacheEntry(KEY); 144 entry.setContent(CONTENT); 145 146 try { 147 listener.store(KEY, entry); 148 149 CacheEntry newEntry = (CacheEntry) listener.retrieve(KEY); 151 assertTrue(entry.getContent().equals(newEntry.getContent())); 152 assertEquals(entry.getCreated(), newEntry.getCreated()); 153 assertTrue(entry.getKey().equals(newEntry.getKey())); 154 155 assertNull(listener.retrieve("doesn't exist")); 157 } catch (Exception ex) { 158 ex.printStackTrace(); 159 fail("Exception raised!"); 160 } 161 } 162 163 166 public void testStoreRetrieveGroups() { 167 Set groupSet = new HashSet (); 169 groupSet.add("1"); 170 groupSet.add("2"); 171 172 try { 173 listener.storeGroup(GROUP, groupSet); 174 175 groupSet = listener.retrieveGroup(GROUP); 177 assertNotNull(groupSet); 178 179 assertTrue(groupSet.contains("1")); 180 assertTrue(groupSet.contains("2")); 181 assertFalse(groupSet.contains("3")); 182 183 assertNull(listener.retrieveGroup("abc")); 185 } catch (Exception ex) { 186 ex.printStackTrace(); 187 fail("Exception raised!"); 188 } 189 } 190 191 protected void tearDown() throws Exception { 192 listener.clear(); 193 assertTrue("Cache not cleared", new File (CACHEDIR).list(cacheFileFilter).length == 0); 194 } 195 196 private static class CacheFileFilter implements FilenameFilter { 197 public boolean accept(File dir, String name) { 198 return !"__groups__".equals(name); 199 } 200 } 201 } 202 | Popular Tags |