KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.Assert;
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.jboss.cache.CacheImpl;
14 import org.jboss.cache.Fqn;
15 import org.jboss.cache.config.CacheLoaderConfig;
16 import org.jboss.cache.factories.XmlConfigurationParser;
17 import org.jboss.cache.xml.XmlHelper;
18 import org.w3c.dom.Element JavaDoc;
19
20 import javax.transaction.TransactionManager JavaDoc;
21 import java.io.File JavaDoc;
22
23 /**
24  * Tests ignoreModifications and tests contents of individual loaders
25  *
26  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
27  */

28 public class ChainingCacheLoaderFullTest extends TestCase
29 {
30    private String JavaDoc loc1 = System.getProperty("java.io.tmpdir", ".") + File.separator + "JBossCache-ChainingCacheLoaderBasicTest-1";
31    private String JavaDoc loc2 = System.getProperty("java.io.tmpdir", ".") + File.separator + "JBossCache-ChainingCacheLoaderBasicTest-2";
32
33    private CacheImpl cache;
34    private ChainingCacheLoader chainingCacheLoader;
35    private CacheLoader loader1, loader2;
36    private Fqn fqn = Fqn.fromString("/a/b");
37    private String JavaDoc key = "key";
38    private String JavaDoc value = "value";
39
40
41    public ChainingCacheLoaderFullTest()
42    {
43       File JavaDoc dir1 = new File JavaDoc(loc1);
44       File JavaDoc dir2 = new File JavaDoc(loc2);
45
46       if (!dir1.exists()) dir1.mkdirs();
47       if (!dir2.exists()) dir2.mkdirs();
48    }
49
50    protected void startCache(boolean ignoreMods1, boolean ignoreMods2) throws Exception JavaDoc
51    {
52       cache = new CacheImpl();
53       cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig(loc1, loc2, ignoreMods1, ignoreMods2));
54       cache.getConfiguration().setCacheMode("local");
55       cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
56       cache.start();
57
58       chainingCacheLoader = (ChainingCacheLoader) cache.getCacheLoaderManager().getCacheLoader();
59       loader1 = chainingCacheLoader.getCacheLoaders().get(0);
60       loader2 = chainingCacheLoader.getCacheLoaders().get(1);
61    }
62
63    protected void cleanup() throws Exception JavaDoc
64    {
65       cache.remove("/");
66       cache.stop();
67       cache = null;
68    }
69
70    protected CacheLoaderConfig getCacheLoaderConfig(String JavaDoc loc1, String JavaDoc loc2, boolean ignoreMods1, boolean ignoreMods2) throws Exception JavaDoc
71    {
72       String JavaDoc xml = "<config>\n" +
73               "<passivation>false</passivation>\n" +
74               "<preload></preload>\n" +
75               "<cacheloader>\n" +
76               "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
77               "<properties>" +
78               " location=" + loc1 + "\n" +
79               "</properties>\n" +
80               "<async>false</async>\n" +
81               "<fetchPersistentState>true</fetchPersistentState>\n" +
82               "<ignoreModifications>" + ignoreMods1 + "</ignoreModifications>\n" +
83               "</cacheloader>\n" +
84               "<cacheloader>\n" +
85               "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
86               "<properties>" +
87               " location=" + loc2 + "\n" +
88               "</properties>\n" +
89               "<async>false</async>\n" +
90               "<fetchPersistentState>false</fetchPersistentState>\n" +
91               "<ignoreModifications>" + ignoreMods2 + "</ignoreModifications>\n" +
92               "</cacheloader>\n" +
93               "</config>";
94       Element JavaDoc element = XmlHelper.stringToElement(xml);
95       return XmlConfigurationParser.parseCacheLoaderConfig(element);
96    }
97
98    public void testCruds() throws Exception JavaDoc
99    {
100       startCache(false, false);
101
102       // put something in the cache.
103
cache.put(fqn, key, value);
104
105       Assert.assertEquals(value, cache.get(fqn, key));
106       Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
107       Assert.assertEquals(value, loader1.get(fqn).get(key));
108       Assert.assertEquals(value, loader2.get(fqn).get(key));
109
110       // evict
111
cache.evict(fqn);
112       Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
113       Assert.assertEquals(value, loader1.get(fqn).get(key));
114       Assert.assertEquals(value, loader2.get(fqn).get(key));
115       Assert.assertEquals(value, cache.get(fqn, key));
116
117       // remove
118
cache.remove(fqn);
119       Assert.assertNull(value, cache.get(fqn, key));
120       Assert.assertNull(value, chainingCacheLoader.get(fqn));
121       Assert.assertNull(value, loader1.get(fqn));
122       Assert.assertNull(value, loader2.get(fqn));
123
124       cleanup();
125    }
126
127    public void testGets() throws Exception JavaDoc
128    {
129       startCache(false, false);
130
131       cache.put(fqn, key, value);
132
133       // test that loader1 is always looked up first.
134
cache.evict(fqn);
135       loader1.put(fqn, key, value + 2);
136       Assert.assertEquals(value, loader2.get(fqn).get(key));
137       Assert.assertEquals(value + 2, loader1.get(fqn).get(key));
138       Assert.assertEquals(value + 2, chainingCacheLoader.get(fqn).get(key));
139       Assert.assertEquals(value + 2, cache.get(fqn, key));
140
141       cache.remove("/");
142       cache.put(fqn, key, value);
143
144       // test that loader2 is NOT checked if loader1 has the value
145
cache.evict(fqn);
146       loader2.put(fqn, key, value + 2);
147       Assert.assertEquals(value + 2, loader2.get(fqn).get(key));
148       Assert.assertEquals(value, loader1.get(fqn).get(key));
149       Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
150       Assert.assertEquals(value, cache.get(fqn, key));
151
152       cache.remove("/");
153       cache.put(fqn, key, value);
154
155       // test that loader2 is checked if loader1 returns a null
156
cache.evict(fqn);
157       loader1.remove(fqn);
158       Assert.assertNull(loader1.get(fqn));
159       Assert.assertEquals(value, loader2.get(fqn).get(key));
160       Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
161       Assert.assertEquals(value, cache.get(fqn, key));
162
163       cleanup();
164    }
165
166    public void testIgnoreMods() throws Exception JavaDoc
167    {
168       startCache(false, true);
169
170       // initialise the loaders
171
loader1.put(fqn, key, value);
172       loader2.put(fqn, key, value);
173
174       // check contents
175
Assert.assertEquals(value, cache.get(fqn, key));
176       Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
177       Assert.assertEquals(value, loader1.get(fqn).get(key));
178       Assert.assertEquals(value, loader2.get(fqn).get(key));
179
180       // do a put
181
cache.put(fqn, key, value + 2);
182       Assert.assertEquals(value + 2, cache.get(fqn, key));
183       Assert.assertEquals(value + 2, chainingCacheLoader.get(fqn).get(key));
184       Assert.assertEquals(value + 2, loader1.get(fqn).get(key));
185       Assert.assertEquals(value, loader2.get(fqn).get(key));
186
187       // remove
188
cache.remove(fqn);
189       Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
190       Assert.assertNull(loader1.get(fqn));
191       Assert.assertEquals(value, loader2.get(fqn).get(key));
192       Assert.assertEquals(value, cache.get(fqn, key));
193
194       cleanup();
195    }
196
197    public void testIgnoreModsTransactional() throws Exception JavaDoc
198    {
199       startCache(false, true);
200       TransactionManager JavaDoc mgr = cache.getTransactionManager();
201
202       // initialise the loaders
203
loader1.put(fqn, key, value);
204       loader2.put(fqn, key, value);
205
206       // check contents
207
Assert.assertEquals(value, cache.get(fqn, key));
208       Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
209       Assert.assertEquals(value, loader1.get(fqn).get(key));
210       Assert.assertEquals(value, loader2.get(fqn).get(key));
211
212       // do a put
213
mgr.begin();
214       cache.put(fqn, key, value + 2);
215       Assert.assertEquals(value + 2, cache.get(fqn, key));
216       Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
217       Assert.assertEquals(value, loader1.get(fqn).get(key));
218       Assert.assertEquals(value, loader2.get(fqn).get(key));
219       mgr.commit();
220       Assert.assertEquals(value + 2, cache.get(fqn, key));
221       Assert.assertEquals(value + 2, chainingCacheLoader.get(fqn).get(key));
222       Assert.assertEquals(value + 2, loader1.get(fqn).get(key));
223       Assert.assertEquals(value, loader2.get(fqn).get(key));
224
225       // remove - not in a tx, see http://jira.jboss.com/jira/browse/JBCACHE-352
226
// mgr.begin();
227
cache.remove(fqn);
228
229 // Assert.assertNull(cache.get(fqn, key));
230
// Assert.assertEquals(value + 2, chainingCacheLoader.get(fqn).get(key));
231
// Assert.assertEquals(value + 2, loader1.get(fqn).get(key));
232
// Assert.assertEquals(value, loader2.get(fqn).get(key));
233
// mgr.commit();
234
Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
235       Assert.assertNull(loader1.get(fqn));
236       Assert.assertEquals(value, loader2.get(fqn).get(key));
237       Assert.assertEquals(value, cache.get(fqn, key));
238
239       cleanup();
240    }
241
242    public void testCrudsTransactional() throws Exception JavaDoc
243    {
244       startCache(false, false);
245       TransactionManager JavaDoc mgr = cache.getTransactionManager();
246
247       // assert that the loaders ae empty
248
chainingCacheLoader.remove(cache.getRoot().getFqn());
249       Assert.assertNull(loader1.get(fqn));
250       Assert.assertNull(loader2.get(fqn));
251       Assert.assertNull(chainingCacheLoader.get(fqn));
252
253       // put something in the cache.
254
mgr.begin();
255       cache.put(fqn, key, value);
256       Assert.assertEquals(value, cache.get(fqn, key));
257       Assert.assertNull(chainingCacheLoader.get(fqn));
258       Assert.assertNull(loader1.get(fqn));
259       Assert.assertNull(loader2.get(fqn));
260       mgr.commit();
261       Assert.assertEquals(value, cache.get(fqn, key));
262       Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
263       Assert.assertEquals(value, loader1.get(fqn).get(key));
264       Assert.assertEquals(value, loader2.get(fqn).get(key));
265
266       // evict
267
cache.evict(fqn);
268       Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
269       Assert.assertEquals(value, loader1.get(fqn).get(key));
270       Assert.assertEquals(value, loader2.get(fqn).get(key));
271       Assert.assertEquals(value, cache.get(fqn, key));
272
273       // remove - not in a tx, see http://jira.jboss.com/jira/browse/JBCACHE-352
274
// mgr.begin();
275
cache.remove(fqn);
276 // Assert.assertEquals(value, chainingCacheLoader.get(fqn).get(key));
277
// Assert.assertEquals(value, loader1.get(fqn).get(key));
278
// Assert.assertEquals(value, loader2.get(fqn).get(key));
279
// Assert.assertNull(value, cache.get(fqn, key));
280
// mgr.commit();
281
Assert.assertNull(value, cache.get(fqn, key));
282       Assert.assertNull(value, chainingCacheLoader.get(fqn));
283       Assert.assertNull(value, loader1.get(fqn));
284       Assert.assertNull(value, loader2.get(fqn));
285
286       cleanup();
287    }
288
289    public static Test suite()
290    {
291       return new TestSuite(ChainingCacheLoaderFullTest.class);
292    }
293
294    public static void main(String JavaDoc[] args)
295    {
296       junit.textui.TestRunner.run(suite());
297    }
298 }
299
Popular Tags