KickJava   Java API By Example, From Geeks To Geeks.

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


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

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 JavaDoc;
16 import java.io.FilenameFilter JavaDoc;
17
18 import java.util.HashSet JavaDoc;
19 import java.util.Properties JavaDoc;
20 import java.util.Set JavaDoc;
21
22 /**
23  * Test all the public methods of the disk persistance listener and assert the
24  * return values
25  *
26  * $Id: TestDiskPersistenceListener.java,v 1.2 2005/10/16 18:28:26 ltorunski Exp $
27  * @version $Revision: 1.2 $
28  * @author <a HREF="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
29  */

30 public final class TestDiskPersistenceListener extends TestCase {
31     /**
32      * Cache dir to persist to
33      */

34     public static final String JavaDoc CACHEDIR = "/tmp/diskcache";
35
36     /**
37      * The persistance listener used for the tests
38      */

39     private DiskPersistenceListener listener = null;
40
41     /**
42      * Object content
43      */

44     private final String JavaDoc CONTENT = "Disk persistance content";
45
46     /**
47      * Cache group
48      */

49     private final String JavaDoc GROUP = "test group";
50
51     /**
52      * Object key
53      */

54     private final String JavaDoc KEY = "Test disk persistance listener key";
55     private CacheFileFilter cacheFileFilter = new CacheFileFilter();
56
57     public TestDiskPersistenceListener(String JavaDoc str) {
58         super(str);
59     }
60
61     /**
62      * This methods returns the name of this test class to JUnit
63      * <p>
64      * @return The test for this class
65      */

66     public static Test suite() {
67         return new TestSuite(TestDiskPersistenceListener.class);
68     }
69
70     /**
71      * This method is invoked before each testXXXX methods of the
72      * class. It set ups the variables required for each tests.
73      */

74     public void setUp() {
75         // At first invocation, create a listener
76
listener = new DiskPersistenceListener();
77
78         Properties JavaDoc p = new Properties JavaDoc();
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     /**
86      * Test the cache directory removal
87      */

88     public void testClear() {
89         // Create an new element since we removed it at the last test
90
testStoreRetrieve();
91
92         // Remove the directory, and assert that we have no more entry
93
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     /**
103      * Test that the previouly created file exists
104      */

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

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

141     public void testStoreRetrieve() {
142         // Create a cache entry and store it
143
CacheEntry entry = new CacheEntry(KEY);
144         entry.setContent(CONTENT);
145
146         try {
147             listener.store(KEY, entry);
148
149             // Retrieve our entry and validate the values
150
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             // Try to retrieve a non-existent object
156
assertNull(listener.retrieve("doesn't exist"));
157         } catch (Exception JavaDoc ex) {
158             ex.printStackTrace();
159             fail("Exception raised!");
160         }
161     }
162
163     /**
164      * Test the storing and retrieving of groups
165      */

166     public void testStoreRetrieveGroups() {
167         // Store a group
168
Set JavaDoc groupSet = new HashSet JavaDoc();
169         groupSet.add("1");
170         groupSet.add("2");
171
172         try {
173             listener.storeGroup(GROUP, groupSet);
174
175             // Retrieve it and validate its contents
176
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             // Try to retrieve a non-existent group
184
assertNull(listener.retrieveGroup("abc"));
185         } catch (Exception JavaDoc ex) {
186             ex.printStackTrace();
187             fail("Exception raised!");
188         }
189     }
190
191     protected void tearDown() throws Exception JavaDoc {
192         listener.clear();
193         assertTrue("Cache not cleared", new File JavaDoc(CACHEDIR).list(cacheFileFilter).length == 0);
194     }
195
196     private static class CacheFileFilter implements FilenameFilter JavaDoc {
197         public boolean accept(File JavaDoc dir, String JavaDoc name) {
198             return !"__groups__".equals(name);
199         }
200     }
201 }
202
Popular Tags