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