KickJava   Java API By Example, From Geeks To Geeks.

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


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.CacheImpl;
10 import org.jboss.cache.interceptors.CacheStoreInterceptor;
11 import org.jboss.cache.interceptors.Interceptor;
12
13 import java.util.Iterator JavaDoc;
14
15 /**
16  * See http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3919374#3919374
17  *
18  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
19  */

20 public class SharedCacheLoaderTest extends AbstractCacheLoaderTestBase
21 {
22    private CacheImpl cache1, cache2;
23    private DummyCacheLoader dummyCacheLoader;
24
25    protected void setUp() throws Exception JavaDoc
26    {
27       if (cache1 != null || cache2 != null) tearDown();
28
29       // set up 2 instances of CacheImpl with shared CacheLoaders.
30
cache1 = new CacheImpl();
31       cache2 = new CacheImpl();
32
33       cache1.getConfiguration().setCacheMode("REPL_SYNC");
34       cache2.getConfiguration().setCacheMode("REPL_SYNC");
35
36       cache1.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", DummyCacheLoader.class.getName(), "", false, false, true));
37       cache2.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", DummyCacheLoader.class.getName(), "", false, false, true));
38
39       cache1.start();
40       cache2.start();
41
42       // force setting up the same cache loader class
43
dummyCacheLoader = new DummyCacheLoader();
44
45       cache1.setCacheLoader(dummyCacheLoader);
46       cache2.setCacheLoader(dummyCacheLoader);
47       findCacheStoreInterceptor(cache1).setCache(cache1);
48       findCacheStoreInterceptor(cache2).setCache(cache2);
49    }
50
51    protected CacheStoreInterceptor findCacheStoreInterceptor(CacheImpl cache)
52    {
53       Iterator JavaDoc ints = cache.getInterceptors().iterator();
54       CacheStoreInterceptor csi = null;
55       while (ints.hasNext())
56       {
57          Interceptor i = (Interceptor) ints.next();
58          if (i instanceof CacheStoreInterceptor)
59          {
60             csi = (CacheStoreInterceptor) i;
61             break;
62          }
63       }
64       return csi;
65    }
66
67    protected void tearDown()
68    {
69       if (cache1 != null) cache1.stop();
70       if (cache2 != null) cache2.stop();
71       cache1 = null;
72       cache2 = null;
73    }
74
75    public void testReplicationWithSharedCL() throws Exception JavaDoc
76    {
77       cache1.put("/test", "one", "two");
78
79       // should have replicated
80
assertEquals("two", cache1.get("/test", "one"));
81       assertEquals("two", cache2.get("/test", "one"));
82
83       // only a single put() should have happened on the cache loader though.
84
assertEquals(1, dummyCacheLoader.getPutCount());
85    }
86 }
87
Popular Tags