KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > api > SyncReplTest


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

7
8 package org.jboss.cache.api;
9
10 import junit.framework.TestCase;
11 import org.jboss.cache.Cache;
12 import org.jboss.cache.CacheSPI;
13 import org.jboss.cache.Fqn;
14 import org.jboss.cache.InvocationContext;
15 import org.jboss.cache.Node;
16 import org.jboss.cache.config.Option;
17 import org.jboss.cache.factories.DefaultCacheFactory;
18 import org.jboss.cache.misc.TestingUtil;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 /**
24  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
25  */

26 public class SyncReplTest extends TestCase
27 {
28    private CacheSPI[] caches;
29
30    protected void setUp()
31    {
32       System.out.println("*** In setUp()");
33       caches = new CacheSPI[2];
34       caches[0] = (CacheSPI) DefaultCacheFactory.getInstance().createCache("META-INF/replSync-service.xml");
35       caches[1] = (CacheSPI) DefaultCacheFactory.getInstance().createCache("META-INF/replSync-service.xml");
36
37       TestingUtil.blockUntilViewsReceived(caches, 5000);
38       System.out.println("*** Finished setUp()");
39    }
40
41    protected void tearDown()
42    {
43       System.out.println("*** In tearDown()");
44       if (caches != null)
45       {
46          for (Cache c : caches)
47          {
48             c.stop();
49             c = null;
50          }
51          caches = null;
52       }
53       System.out.println("*** Finished tearDown()");
54    }
55
56    public void testBasicOperation()
57    {
58       assertClusterSize("Should only be 2 caches in the cluster!!!", 2);
59       assertInvocationContextInitState();
60
61       Fqn f = Fqn.fromString("/test/data");
62       String JavaDoc k = "key", v = "value";
63
64       assertNull("Should be null", caches[0].getRoot().getChild(f));
65       assertNull("Should be null", caches[1].getRoot().getChild(f));
66
67       Node node = caches[0].getRoot().addChild(f);
68
69       assertNotNull("Should not be null", node);
70
71       node.put(k, v);
72
73       assertEquals(v, node.get(k));
74       assertEquals(v, caches[0].get(f, k));
75       assertEquals("Should have replicated", v, caches[1].get(f, k));
76    }
77
78    public void testSyncRepl()
79    {
80       assertClusterSize("Should only be 2 caches in the cluster!!!", 2);
81       assertInvocationContextInitState();
82
83       Fqn fqn = Fqn.fromString("/JSESSIONID/1010.10.5:3000/1234567890/1");
84       caches[0].getConfiguration().setSyncCommitPhase(true);
85       caches[1].getConfiguration().setSyncCommitPhase(true);
86
87
88       caches[0].put(fqn, "age", 38);
89       assertEquals("Value should be set", 38, caches[0].get(fqn, "age"));
90       assertEquals("Value should have replicated", 38, caches[1].get(fqn, "age"));
91    }
92
93    public void testPutMap()
94    {
95       assertClusterSize("Should only be 2 caches in the cluster!!!", 2);
96       assertInvocationContextInitState();
97
98       Fqn fqn = Fqn.fromString("/JSESSIONID/10.10.10.5:3000/1234567890/1");
99       Fqn fqn1 = Fqn.fromString("/JSESSIONID/10.10.10.5:3000/1234567890/2");
100
101       Map JavaDoc map = new HashMap JavaDoc();
102       map.put("1", "1");
103       map.put("2", "2");
104       caches[0].getInvocationContext().getOptionOverrides().setSuppressLocking(true);
105       caches[0].getRoot().addChild(fqn).put(map);
106       caches[0].getInvocationContext().getOptionOverrides().setSuppressLocking(true);
107       assertEquals("Value should be set", "1", caches[0].get(fqn, "1"));
108
109       map = new HashMap JavaDoc();
110       map.put("3", "3");
111       map.put("4", "4");
112       caches[0].getInvocationContext().getOptionOverrides().setSuppressLocking(true);
113       caches[0].getRoot().addChild(fqn1).put(map);
114
115       caches[0].getInvocationContext().getOptionOverrides().setSuppressLocking(true);
116       assertEquals("Value should be set", "2", caches[0].get(fqn, "2"));
117    }
118
119
120    private void assertClusterSize(String JavaDoc message, int size)
121    {
122       for (Cache c : caches)
123       {
124          assertClusterSize(message, size, c);
125       }
126    }
127
128    private void assertClusterSize(String JavaDoc message, int size, Cache c)
129    {
130       assertEquals(message, size, c.getMembers().size());
131    }
132
133    private void assertInvocationContextInitState()
134    {
135       for (Cache c : caches)
136       {
137          assertInvocationContextInitState(c);
138       }
139    }
140
141    private void assertInvocationContextInitState(Cache c)
142    {
143       InvocationContext ctx = c.getInvocationContext();
144       InvocationContext control = null;
145       try
146       {
147          control = ctx.clone();
148       }
149       catch (CloneNotSupportedException JavaDoc e)
150       {
151          fail("Unable to clone InvocationContext");
152       }
153
154       control.reset();
155       control.setOptionOverrides(new Option());
156
157       assertEquals("Should be equal", control, ctx);
158    }
159
160
161 }
162
Popular Tags