KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.jboss.cache.CacheImpl;
13 import org.jboss.cache.Fqn;
14 import org.jboss.cache.config.Configuration;
15
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 /**
20  * Tests ClusteredCacheLoader
21  *
22  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
23  */

24 public class ClusteredCacheLoaderTest extends AbstractCacheLoaderTestBase
25 {
26    private static Log log = LogFactory.getLog(ClusteredCacheLoaderTest.class);
27    private CacheImpl cache1, cache2;
28    private CacheLoader loader1, loader2;
29    private Fqn fqn = Fqn.fromString("/a");
30    private String JavaDoc key = "key";
31
32    protected void setUp() throws Exception JavaDoc
33    {
34       if (cache1 != null || cache2 != null) tearDown();
35       cache1 = new CacheImpl();
36       cache2 = new CacheImpl();
37
38       cache1.getConfiguration().setClusterName("CCL-Test");
39       cache1.getConfiguration().setInitialStateRetrievalTimeout(2000);
40       cache2.getConfiguration().setClusterName("CCL-Test");
41       cache2.getConfiguration().setInitialStateRetrievalTimeout(2000);
42       cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
43       cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
44
45       cache1.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.ClusteredCacheLoader", "timeout=500", false, false, false));
46       cache2.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.ClusteredCacheLoader", "timeout=500", false, false, false));
47
48       cache1.start();
49       cache2.start();
50
51       loader1 = cache1.getCacheLoaderManager().getCacheLoader();
52       loader2 = cache2.getCacheLoaderManager().getCacheLoader();
53    }
54
55    protected void tearDown()
56    {
57       if (cache1 != null)
58       {
59          cache1.stop();
60          cache1 = null;
61          loader1 = null;
62       }
63
64       if (cache2 != null)
65       {
66          cache2.stop();
67          cache2 = null;
68          loader2 = null;
69       }
70    }
71
72    public void testGetKeyValue() throws Exception JavaDoc
73    {
74       cache1.put(fqn, key, "value");
75
76       log.info("Finished put");
77       // test that this has propagated.
78
Assert.assertEquals("value", loader1.get(fqn).get(key));
79       Assert.assertEquals("value", loader2.get(fqn).get(key));
80
81       cache1.evict(fqn);
82
83       // now cache 1 should not have this but cache 2 should.
84
// loader1 looks at cache2 while loader2 looks at cache1
85
Assert.assertEquals("value", loader1.get(fqn).get(key));
86       Assert.assertNull("Expecting null", loader2.get(fqn));
87 // // calling a get on cache1 should cause the loader to retrieve the node from cache2
88
Assert.assertEquals("value", cache1.get(fqn, key));
89 // // and now loader2 should see it
90
// Assert.assertEquals("value", loader2.get(fqn).get(key));
91
}
92
93    public void testGet() throws Exception JavaDoc
94    {
95       cache1.put(fqn, key, "value");
96
97       // test that this has propagated.
98
Map JavaDoc map = loader1.get(fqn);
99       Assert.assertTrue("Should contain key", map.containsKey(key));
100       Assert.assertEquals("value", map.get(key));
101       Assert.assertEquals(1, map.size());
102
103       map = loader2.get(fqn);
104       Assert.assertTrue("Should contain key", map.containsKey(key));
105       Assert.assertEquals("value", map.get(key));
106       Assert.assertEquals(1, map.size());
107
108       cache1.evict(fqn);
109
110       // now cache 1 should not have this but cache 2 should.
111
// loader1 looks at cache2 while loader2 looks at cache1
112
map = loader1.get(fqn);
113       Assert.assertTrue(map.containsKey(key));
114       Assert.assertEquals("value", map.get(key));
115       Assert.assertEquals(1, map.size());
116
117       Assert.assertNull("Expecting null", loader2.get(fqn));
118       map = loader2.get(fqn);
119       Assert.assertNull("Should be null", map);
120
121       // calling a get on cache1 should cause the loader to retrieve the node from cache2
122
Assert.assertEquals("value", cache1.get(fqn, key));
123       // and now loader2 should see it
124
map = loader2.get(fqn);
125       Assert.assertTrue(map.containsKey(key));
126       Assert.assertEquals("value", map.get(key));
127       Assert.assertEquals(1, map.size());
128    }
129
130    public void testGetChildrenNames() throws Exception JavaDoc
131    {
132       cache1.put(fqn, key, "value");
133       Fqn child1 = new Fqn(fqn, "child1");
134       Fqn child2 = new Fqn(fqn, "child2");
135       Fqn child3 = new Fqn(fqn, "child3");
136       cache1.put(child1, key, "value");
137       cache1.put(child2, key, "value");
138       cache1.put(child3, key, "value");
139
140       // test that this has propagated.
141
Set JavaDoc childNames = loader1.getChildrenNames(fqn);
142       Assert.assertEquals(3, childNames.size());
143       childNames = loader2.getChildrenNames(fqn);
144       Assert.assertEquals(3, childNames.size());
145
146       cache1.evict(child1);
147       cache1.evict(child2);
148       cache1.evict(child3);
149       cache1.evict(fqn);
150
151       // now cache 1 should not have this but cache 2 should.
152
// loader1 looks at cache2 while loader2 looks at cache1
153
childNames = loader1.getChildrenNames(fqn);
154       Assert.assertEquals(3, childNames.size());
155
156       childNames = loader2.getChildrenNames(fqn);
157       Assert.assertNull("should be null", childNames);
158       // calling a get on cache1 should cause the loader to retrieve the node from cache2
159
Assert.assertEquals("value", cache1.get(fqn, key));
160       // load up children
161
Assert.assertEquals("value", cache1.get(child1, key));
162       Assert.assertEquals("value", cache1.get(child2, key));
163       Assert.assertEquals("value", cache1.get(child3, key));
164       // and now loader2 should see it
165
childNames = loader2.getChildrenNames(fqn);
166       Assert.assertEquals(3, childNames.size());
167    }
168
169    public void testExists() throws Exception JavaDoc
170    {
171       cache1.put(fqn, key, "value");
172
173       // test that this has propagated.
174
Assert.assertTrue("should exist", loader1.exists(fqn));
175       Assert.assertTrue("should exist", loader2.exists(fqn));
176
177       cache1.evict(fqn);
178
179       // now cache 1 should not have this but cache 2 should.
180
// loader1 looks at cache2 while loader2 looks at cache1
181
Assert.assertTrue("should exist", loader1.exists(fqn));
182       Assert.assertTrue("should not exist", !loader2.exists(fqn));
183       // calling a get on cache1 should cause the loader to retrieve the node from cache2
184
Assert.assertEquals("value", cache1.get(fqn, key));
185       // and now loader2 should see it
186
Assert.assertTrue("should exist", loader2.exists(fqn));
187    }
188 }
189
Popular Tags