KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > CacheLoaderPurgingTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.loader;
8
9 import org.jboss.cache.CacheException;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.config.Configuration;
13 import org.jboss.cache.factories.XmlConfigurationParser;
14 import org.jboss.cache.xml.XmlHelper;
15 import org.w3c.dom.Element JavaDoc;
16
17 /**
18  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
19  */

20 public class CacheLoaderPurgingTest extends AbstractCacheLoaderTestBase
21 {
22    private CacheImpl cache;
23    private String JavaDoc key = "key", value = "value";
24    private Fqn fqn = Fqn.fromString("/a/b/c");
25
26    public void tearDown() throws CacheException
27    {
28       if (cache != null)
29       {
30          cache.remove(Fqn.ROOT);
31          cache.stop();
32          cache = null;
33       }
34    }
35
36    public void testSingleLoaderNoPurge() throws Exception JavaDoc
37    {
38       cache = new CacheImpl();
39       Configuration c = new Configuration();
40       cache.setConfiguration(c);
41       c.setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "location=" + System.getProperty("java.io.tmpdir", "/tmp") + "/" + "CacheLoaderPurgingTest", false, false, false));
42       cache.start();
43
44       cache.put(fqn, key, value);
45
46       CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
47
48       assertEquals(value, cache.get(fqn, key));
49       assertEquals(value, loader.get(fqn).get(key));
50
51       cache.evict(fqn);
52       cache.stop();
53       assertEquals(value, loader.get(fqn).get(key));
54
55       cache.start();
56       assertEquals(value, cache.get(fqn, key));
57       assertEquals(value, loader.get(fqn).get(key));
58    }
59
60    public void testSingleLoaderPurge() throws Exception JavaDoc
61    {
62       cache = new CacheImpl();
63       Configuration c = new Configuration();
64       cache.setConfiguration(c);
65       c.setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "location=" + System.getProperty("java.io.tmpdir", "/tmp") + "/" + "CacheLoaderPurgingTest", false, false, false, true));
66       cache.start();
67
68       cache.put(fqn, key, value);
69
70       CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
71
72       assertEquals(value, cache.get(fqn, key));
73       assertEquals(value, loader.get(fqn).get(key));
74
75       cache.evict(fqn);
76       cache.stop();
77       assertEquals(value, loader.get(fqn).get(key));
78
79       cache.start();
80
81       assertTrue(cache.getCacheLoaderManager().getCacheLoaderConfig().getFirstCacheLoaderConfig().isPurgeOnStartup());
82
83       assertNull(cache.get(fqn));
84       assertNull(loader.get(fqn));
85    }
86
87    public void testTwoLoadersPurge() throws Exception JavaDoc
88    {
89       cache = new CacheImpl();
90
91       String JavaDoc xml = "<config>\n" +
92               "<passivation>false</passivation>\n" +
93               "<preload></preload>\n" +
94               "<cacheloader>\n" +
95               "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
96               "<properties>" +
97               " location=" + System.getProperty("java.io.tmpdir", "/tmp") + "/" + "CacheLoaderPurgingTest_1" + "\n" +
98               "</properties>\n" +
99               "<async>false</async>\n" +
100               "<fetchPersistentState>true</fetchPersistentState>\n" +
101               "<purgeOnStartup>" + true + "</purgeOnStartup>\n" +
102               "</cacheloader>\n" +
103               "<cacheloader>\n" +
104               "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
105               "<properties>" +
106               " location=" + System.getProperty("java.io.tmpdir", "/tmp") + "/" + "CacheLoaderPurgingTest_2" + "\n" +
107               "</properties>\n" +
108               "<async>false</async>\n" +
109               "<fetchPersistentState>false</fetchPersistentState>\n" +
110               "<purgeOnStartup>" + false + "</purgeOnStartup>\n" +
111               "</cacheloader>\n" +
112               "</config>";
113
114       Configuration c = new Configuration();
115       cache.setConfiguration(c);
116       Element JavaDoc element = XmlHelper.stringToElement(xml);
117       c.setCacheLoaderConfig(XmlConfigurationParser.parseCacheLoaderConfig(element));
118       cache.start();
119
120       cache.put(fqn, key, value);
121
122       CacheLoader loader[] = ((ChainingCacheLoader) cache.getCacheLoaderManager().getCacheLoader()).getCacheLoaders().toArray(new CacheLoader[]{});
123
124       assertEquals(value, cache.get(fqn, key));
125       assertEquals(value, loader[0].get(fqn).get(key));
126       assertEquals(value, loader[1].get(fqn).get(key));
127
128       cache.evict(fqn);
129       cache.stop();
130       assertEquals(value, loader[0].get(fqn).get(key));
131       assertEquals(value, loader[1].get(fqn).get(key));
132
133       cache.start();
134       assertTrue(!cache.exists(fqn));
135       assertNull(loader[0].get(fqn));
136       assertNotNull(loader[1].get(fqn));
137       assertEquals(value, cache.get(fqn, key));
138    }
139
140 }
141
Popular Tags