001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.oscache.plugins.diskpersistence;
006:
007: import com.opensymphony.oscache.base.CacheEntry;
008: import com.opensymphony.oscache.base.Config;
009: import com.opensymphony.oscache.base.persistence.CachePersistenceException;
010:
011: import junit.framework.Test;
012: import junit.framework.TestCase;
013: import junit.framework.TestSuite;
014:
015: import java.io.File;
016: import java.io.FilenameFilter;
017:
018: import java.util.HashSet;
019: import java.util.Properties;
020: import java.util.Set;
021:
022: /**
023: * Test all the public methods of the disk persistance listener and assert the
024: * return values
025: *
026: * $Id: TestDiskPersistenceListener.java 422 2007-03-17 23:47:29Z larst $
027: * @version $Revision: 422 $
028: * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
029: */
030: public final class TestDiskPersistenceListener extends TestCase {
031: /**
032: * Cache dir to persist to
033: */
034: public static final String CACHEDIR = "/tmp/diskcache";
035:
036: /**
037: * The persistance listener used for the tests
038: */
039: private DiskPersistenceListener listener = null;
040:
041: /**
042: * Object content
043: */
044: private final String CONTENT = "Disk persistance content";
045:
046: /**
047: * Cache group
048: */
049: private final String GROUP = "test group";
050:
051: /**
052: * Object key
053: */
054: private final String KEY = "Test disk persistance listener key";
055: private CacheFileFilter cacheFileFilter = new CacheFileFilter();
056:
057: public TestDiskPersistenceListener(String str) {
058: super (str);
059: }
060:
061: /**
062: * This methods returns the name of this test class to JUnit
063: * <p>
064: * @return The test for this class
065: */
066: public static Test suite() {
067: return new TestSuite(TestDiskPersistenceListener.class);
068: }
069:
070: /**
071: * This method is invoked before each testXXXX methods of the
072: * class. It set ups the variables required for each tests.
073: */
074: public void setUp() {
075: // At first invocation, create a listener
076: listener = new DiskPersistenceListener();
077:
078: Properties p = new Properties();
079: p.setProperty("cache.path", CACHEDIR);
080: p.setProperty("cache.memory", "false");
081: p
082: .setProperty("cache.persistence.class",
083: "com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener");
084: listener.configure(new Config(p));
085: }
086:
087: /**
088: * Test the cache directory removal
089: */
090: public void testClear() {
091: // Create an new element since we removed it at the last test
092: testStoreRetrieve();
093:
094: // Remove the directory, and assert that we have no more entry
095: try {
096: listener.clear();
097: assertTrue(!listener.isStored(KEY));
098: } catch (CachePersistenceException cpe) {
099: cpe.printStackTrace();
100: fail("Exception thrown in test clear!");
101: }
102: }
103:
104: /**
105: * Test that the previouly created file exists
106: */
107: public void testIsStored() {
108: try {
109: listener.store(KEY, CONTENT);
110:
111: // Retrieve the previously created file
112: assertTrue(listener.isStored(KEY));
113:
114: // Check that the fake key returns false
115: assertTrue(!listener.isStored(KEY + "fake"));
116: } catch (Exception e) {
117: e.printStackTrace();
118: fail("testIsStored raised an exception");
119: }
120: }
121:
122: /**
123: * Test the cache removal
124: */
125: public void testRemove() {
126: // Create an entry if it doesn't exists
127: try {
128: if (!listener.isStored(KEY)) {
129: listener.store(KEY, CONTENT);
130: }
131:
132: // Remove the previously created file
133: listener.remove(KEY);
134: } catch (CachePersistenceException cpe) {
135: cpe.printStackTrace();
136: fail("Exception thrown in test remove!");
137: }
138: }
139:
140: /**
141: * Force CachePersistenceException to get a 100% in the unit test
142: */
143: public void testCachePersistenceException() {
144: try {
145: for (int i = 0; i < 2; i++) {
146: if (i == 1)
147: throw new CachePersistenceException("test");
148: }
149: fail("CachePersistenceException not thrown!");
150: } catch (CachePersistenceException cpe) {
151: // ignore
152: }
153: try {
154: for (int i = 0; i < 2; i++) {
155: if (i == 1)
156: throw new CachePersistenceException();
157: }
158: fail("CachePersistenceException not thrown!");
159: } catch (CachePersistenceException cpe) {
160: // ignore
161: }
162: }
163:
164: /**
165: * Test the disk store and retrieve
166: */
167: public void testStoreRetrieve() {
168: // Create a cache entry and store it
169: CacheEntry entry = new CacheEntry(KEY);
170: entry.setContent(CONTENT);
171:
172: try {
173: listener.store(KEY, entry);
174:
175: // Retrieve our entry and validate the values
176: CacheEntry newEntry = (CacheEntry) listener.retrieve(KEY);
177: assertTrue(entry.getContent().equals(newEntry.getContent()));
178: assertEquals(entry.getCreated(), newEntry.getCreated());
179: assertTrue(entry.getKey().equals(newEntry.getKey()));
180:
181: // Try to retrieve a non-existent object
182: assertNull(listener.retrieve("doesn't exist"));
183: } catch (Exception ex) {
184: ex.printStackTrace();
185: fail("Exception raised!");
186: }
187: }
188:
189: /**
190: * Test the storing and retrieving of groups
191: */
192: public void testStoreRetrieveGroups() {
193: // Store a group
194: Set groupSet = new HashSet();
195: groupSet.add("1");
196: groupSet.add("2");
197:
198: try {
199: listener.storeGroup(GROUP, groupSet);
200:
201: // Retrieve it and validate its contents
202: groupSet = listener.retrieveGroup(GROUP);
203: assertNotNull(groupSet);
204:
205: assertTrue(groupSet.contains("1"));
206: assertTrue(groupSet.contains("2"));
207: assertFalse(groupSet.contains("3"));
208:
209: // Try to retrieve a non-existent group
210: assertNull(listener.retrieveGroup("abc"));
211: } catch (Exception ex) {
212: ex.printStackTrace();
213: fail("Exception raised!");
214: }
215: }
216:
217: protected void tearDown() throws Exception {
218: listener.clear();
219: assertTrue("Cache not cleared", new File(CACHEDIR)
220: .list(cacheFileFilter).length == 0);
221: }
222:
223: private static class CacheFileFilter implements FilenameFilter {
224: public boolean accept(File dir, String name) {
225: return !"__groups__".equals(name);
226: }
227: }
228: }
|