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