KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > plugins > diskpersistence > TestHashDiskPersistenceListener


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 /*
6  * Created on Aug 30, 2004
7  *
8  * TODO To change the template for this generated file go to
9  * Window - Preferences - Java - Code Style - Code Templates
10  */

11 package com.opensymphony.oscache.plugins.diskpersistence;
12
13
14 /*
15  * Copyright (c) 2002-2003 by OpenSymphony
16  * All rights reserved.
17  */

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 JavaDoc;
27 import java.io.FilenameFilter JavaDoc;
28
29 import java.util.HashSet JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Set JavaDoc;
32
33 /**
34  * Test all the public methods of the disk persistance listener and assert the
35  * return values
36  *
37  * $Id: TestHashDiskPersistenceListener.java,v 1.1 2005/06/17 05:07:04 dres Exp $
38  * @version $Revision: 1.1 $
39  * @author <a HREF="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
40  */

41 public final class TestHashDiskPersistenceListener extends TestCase {
42     /**
43      * The persistance listener used for the tests
44      */

45     private HashDiskPersistenceListener listener = null;
46
47     /**
48      * Object content
49      */

50     private final String JavaDoc CONTENT = "Disk persistance content";
51
52     /**
53      * Cache group
54      */

55     private final String JavaDoc GROUP = "test group";
56
57     /**
58      * Object key
59      */

60     private final String JavaDoc KEY = "Test disk persistance listener key";
61     private CacheFileFilter cacheFileFilter = new CacheFileFilter();
62
63     public TestHashDiskPersistenceListener(String JavaDoc str) {
64         super(str);
65     }
66
67     /**
68      * This methods returns the name of this test class to JUnit
69      * <p>
70      * @return The test for this class
71      */

72     public static Test suite() {
73         return new TestSuite(TestHashDiskPersistenceListener.class);
74     }
75
76     /**
77      * This method is invoked before each testXXXX methods of the
78      * class. It set ups the variables required for each tests.
79      */

80     public void setUp() {
81         // At first invocation, create a listener
82
listener = new HashDiskPersistenceListener();
83
84         Properties JavaDoc p = new Properties JavaDoc();
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     /**
93      * Test the cache directory removal
94      */

95     public void testClear() {
96         // Create an new element since we removed it at the last test
97
testStoreRetrieve();
98
99         // Remove the directory, and assert that we have no more entry
100
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     /**
110      * Test that the previouly created file exists
111      */

112     public void testIsStored() {
113         try {
114             listener.store(KEY, CONTENT);
115
116             // Retrieve the previously created file
117
assertTrue(listener.isStored(KEY));
118
119             // Check that the fake key returns false
120
assertTrue(!listener.isStored(KEY + "fake"));
121         } catch (Exception JavaDoc e) {
122             e.printStackTrace();
123             fail("testIsStored raised an exception");
124         }
125     }
126
127     /**
128      * Test the cache removal
129      */

130     public void testRemove() {
131         // Create an entry if it doesn't exists
132
try {
133             if (!listener.isStored(KEY)) {
134                 listener.store(KEY, CONTENT);
135             }
136
137             // Remove the previously created file
138
listener.remove(KEY);
139         } catch (CachePersistenceException cpe) {
140             cpe.printStackTrace();
141             fail("Exception thrown in test remove!");
142         }
143     }
144
145     /**
146      * Test the disk store and retrieve
147      */

148     public void testStoreRetrieve() {
149         // Create a cache entry and store it
150
CacheEntry entry = new CacheEntry(KEY);
151         entry.setContent(CONTENT);
152
153         try {
154             listener.store(KEY, entry);
155
156             // Retrieve our entry and validate the values
157
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             // Try to retrieve a non-existent object
163
assertNull(listener.retrieve("doesn't exist"));
164         } catch (Exception JavaDoc ex) {
165             ex.printStackTrace();
166             fail("Exception raised!");
167         }
168     }
169
170     /**
171      * Test the storing and retrieving of groups
172      */

173     public void testStoreRetrieveGroups() {
174         // Store a group
175
Set JavaDoc groupSet = new HashSet JavaDoc();
176         groupSet.add("1");
177         groupSet.add("2");
178
179         try {
180             listener.storeGroup(GROUP, groupSet);
181
182             // Retrieve it and validate its contents
183
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             // Try to retrieve a non-existent group
191
assertNull(listener.retrieveGroup("abc"));
192         } catch (Exception JavaDoc ex) {
193             ex.printStackTrace();
194             fail("Exception raised!");
195         }
196     }
197
198     protected void tearDown() throws Exception JavaDoc {
199         listener.clear();
200         assertTrue("Cache not cleared", new File JavaDoc(TestDiskPersistenceListener.CACHEDIR).list(cacheFileFilter).length == 0);
201     }
202
203     private static class CacheFileFilter implements FilenameFilter JavaDoc {
204         public boolean accept(File JavaDoc dir, String JavaDoc name) {
205             return !"__groups__".equals(name);
206         }
207     }
208 }
209
Popular Tags