1 5 package com.opensymphony.oscache.base.algorithm; 6 7 import com.opensymphony.oscache.base.Config; 8 import com.opensymphony.oscache.base.persistence.PersistenceListener; 9 import com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener; 10 import com.opensymphony.oscache.plugins.diskpersistence.TestDiskPersistenceListener; 11 12 import java.util.Iterator ; 13 import java.util.Properties ; 14 15 24 public abstract class TestQueueCache extends TestAbstractCache { 25 28 protected final String CONTENT = "Test Queue Cache content"; 29 30 33 protected final String KEY = "Test Queue Cache key"; 34 35 40 public TestQueueCache(String str) { 41 super(str); 42 } 43 44 47 public abstract void testRemoveItem(); 48 49 52 public void testClear() { 53 getCache().clear(); 54 assertEquals(0, getCache().size()); 55 } 56 57 60 public void testContainsKey() { 61 getCache().put(KEY, CONTENT); 62 assertTrue(getCache().containsKey(KEY)); 63 getCache().clear(); 64 } 65 66 69 public void testGet() { 70 getCache().put(KEY, CONTENT); 72 assertTrue(getCache().get(KEY).equals(CONTENT)); 73 74 try { 76 getCache().get(null); 77 fail("Get called with null parameters!"); 78 } catch (Exception e) { 79 } 80 81 getCache().clear(); 82 } 83 84 87 public void testGetSetMaxEntries() { 88 for (int count = 0; count < MAX_ENTRIES; count++) { 91 getCache().put(KEY + count, CONTENT + count); 92 } 93 94 assertEquals(MAX_ENTRIES, getCache().size()); 95 getCache().setMaxEntries(MAX_ENTRIES - 1); 96 assertEquals(MAX_ENTRIES - 1, getCache().getMaxEntries()); 97 assertEquals(MAX_ENTRIES - 1, getCache().size()); 98 99 try { 101 getCache().setMaxEntries(INVALID_MAX_ENTRIES); 102 fail("Cache capacity set with an invalid argument"); 103 } catch (Exception e) { 104 } 106 107 getCache().clear(); 108 } 109 110 113 public void testIterator() { 114 int nbEntries = getCache().size(); 116 Iterator iterator = getCache().entrySet().iterator(); 117 assertNotNull(iterator); 118 119 for (int count = 0; count < nbEntries; count++) { 120 assertNotNull(iterator.next()); 121 } 122 123 assertTrue(!iterator.hasNext()); 124 } 125 126 129 public void testPut() { 130 for (int count = 0; count < MAX_ENTRIES; count++) { 132 getCache().put(KEY + count, CONTENT + count); 133 } 134 135 try { 137 getCache().put(null, null); 138 fail("Put called with null parameters!"); 139 } catch (Exception e) { 140 } 141 142 getCache().clear(); 143 } 144 145 148 public void testPutOverflow() { 149 PersistenceListener listener = new DiskPersistenceListener(); 151 152 Properties p = new Properties (); 153 p.setProperty("cache.path", TestDiskPersistenceListener.CACHEDIR); 154 p.setProperty("cache.memory", "true"); 155 p.setProperty("cache.persistence.overflow.only", "true"); 156 p.setProperty("cache.persistence.class", "com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener"); 157 listener.configure(new Config(p)); 158 getCache().setPersistenceListener(listener); 159 getCache().clear(); 160 getCache().setMaxEntries(MAX_ENTRIES); 161 getCache().setOverflowPersistence(true); 162 163 if (getCache() instanceof UnlimitedCache) { 164 return; } 166 167 for (int count = 0; count <= MAX_ENTRIES; count++) { 169 getCache().put(KEY + count, CONTENT + count); 170 } 171 172 try { 173 int numPersisted = 0; 174 175 for (int count = 0; count <= MAX_ENTRIES; count++) { 178 if (getCache().getPersistenceListener().isStored(KEY + count)) { 179 numPersisted++; 180 } 181 } 182 183 if (getCache().isOverflowPersistence()) { 184 assertTrue("Only 1 element should have been persisted ", numPersisted == 1); 185 } else { 186 assertTrue("All elements should have been persisted ", numPersisted == (MAX_ENTRIES + 1)); 187 } 188 } catch (Exception e) { 189 fail(); 190 } 191 192 getCache().clear(); 193 } 194 195 198 public void testRemove() { 199 getCache().put(KEY, CONTENT); 200 201 assertNotNull(getCache().remove(KEY)); 203 getCache().clear(); 204 } 205 } 206 | Popular Tags |