KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  *
3  * JBoss, the OpenSource J2EE webOS
4  *
5  * Distributable under LGPL license.
6  * See terms of license at gnu.org.
7  */

8
9 package org.jboss.cache.marshall;
10
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import org.jboss.cache.CacheImpl;
15 import org.jboss.cache.Fqn;
16 import org.jboss.cache.config.Configuration;
17 import org.jboss.cache.factories.XmlConfigurationParser;
18 import org.jboss.cache.marshall.data.Address;
19 import org.jboss.cache.marshall.data.Person;
20 import org.jboss.cache.misc.TestingUtil;
21 import org.jboss.cache.transaction.DummyTransactionManager;
22
23 import javax.transaction.NotSupportedException JavaDoc;
24 import javax.transaction.SystemException JavaDoc;
25 import javax.transaction.Transaction JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.HashMap JavaDoc;
28
29 /**
30  * Test case for marshalling using Sync mode.
31  *
32  * @author Ben Wang
33  * @version $Revision: 1.14 $
34  */

35 public class SyncReplTest extends RegionBasedMarshallingTestBase
36 {
37    CacheImpl cache1, cache2;
38    String JavaDoc props = null;
39    Person ben_;
40    Address addr_;
41    Throwable JavaDoc ex_;
42
43    public void setUp() throws Exception JavaDoc
44    {
45       super.setUp();
46
47       log("creating cache1");
48       cache1 = createCache("TestCache");
49
50       log("creating cache2");
51
52       cache2 = createCache("TestCache");
53       addr_ = new Address();
54       addr_.setCity("San Jose");
55       ben_ = new Person();
56       ben_.setName("Ben");
57       ben_.setAddress(addr_);
58
59       // Pause to give caches time to see each other
60
TestingUtil.blockUntilViewsReceived(new CacheImpl[]{cache1, cache2}, 60000);
61    }
62
63    private CacheImpl createCache(String JavaDoc name) throws Exception JavaDoc
64    {
65       CacheImpl cache = new CacheImpl();
66
67       XmlConfigurationParser parser = new XmlConfigurationParser();
68       Configuration c = parser.parseFile("META-INF/replSync-service.xml");
69       cache.setConfiguration(c);
70
71       c.setClusterName(name);
72       // Use marshaller
73
c.setUseRegionBasedMarshalling(true);
74       cache.create();
75       cache.start();
76       return cache;
77    }
78
79    public void tearDown() throws Exception JavaDoc
80    {
81       super.tearDown();
82       cache1.remove("/");
83       if (cache1 != null)
84       {
85          log("stopping cache1");
86          cache1.stop();
87       }
88
89       if (cache2 != null)
90       {
91          log("stopping cache2");
92          cache2.stop();
93       }
94    }
95
96    public void testPlainPut() throws Exception JavaDoc
97    {
98       cache1.put("/aop", "person", ben_);
99       Person ben2 = (Person) cache2.get("/aop", "person");
100       assertNotNull("Person from 2nd cache should not be null ", ben2);
101       assertEquals(ben_.toString(), ben2.toString());
102    }
103
104    public void testCCE() throws Exception JavaDoc
105    {
106       ClassLoader JavaDoc cl = getClassLoader();
107       cache1.registerClassLoader("/aop", cl);
108       cl = getClassLoader();
109       cache2.registerClassLoader("/aop", cl);
110
111       cache1.put("/aop", "person", ben_);
112       try
113       {
114          Person ben2 = (Person) cache2.get("/aop", "person");
115       }
116       catch (ClassCastException JavaDoc ex)
117       {
118          // That's ok.
119
return;
120       }
121       fail("Should have thrown an exception");
122    }
123
124    public void testPut() throws Exception JavaDoc
125    {
126       ClassLoader JavaDoc cla = getClassLoader();
127       cache1.registerClassLoader("/aop", cla);
128       ClassLoader JavaDoc clb = getClassLoader();
129       cache2.registerClassLoader("/aop", clb);
130
131       cache1.put("/aop", "person", ben_);
132
133       Object JavaDoc ben2;
134       // Can't cast it to Person. CCE will resutl.
135
ben2 = cache2.get("/aop", "person");
136       assertEquals(ben_.toString(), ben2.toString());
137    }
138
139    public void testCLSet() throws Exception JavaDoc
140    {
141       ClassLoader JavaDoc cla = getClassLoader();
142       cache1.registerClassLoader("/aop", cla);
143       ClassLoader JavaDoc clb = getClassLoader();
144       cache2.registerClassLoader("/aop", clb);
145
146       cache1.put("/aop", "person", ben_);
147
148       Object JavaDoc ben2;
149       // Can't cast it to Person. CCE will resutl.
150
ben2 = cache2.get("/aop", "person");
151       assertEquals(ben_.toString(), ben2.toString());
152
153       Class JavaDoc claz = clb.loadClass(ADDRESS_CLASSNAME);
154       Object JavaDoc add = claz.newInstance();
155       {
156          Class JavaDoc[] types = {String JavaDoc.class};
157          Method JavaDoc setValue = claz.getMethod("setCity", types);
158          Object JavaDoc[] margs = {"Sunnyvale"};
159          setValue.invoke(add, margs);
160       }
161
162       {
163          Class JavaDoc clasz1 = clb.loadClass(PERSON_CLASSNAME);
164          Class JavaDoc[] types = {claz};
165          Method JavaDoc setValue = clasz1.getMethod("setAddress", types);
166          Object JavaDoc[] margs = {add};
167          setValue.invoke(ben2, margs);
168       }
169    }
170
171    /**
172     * Test replication with classloaders.
173     *
174     * @throws Exception
175     */

176    public void testCLSet2() throws Exception JavaDoc
177    {
178       ClassLoader JavaDoc cla = getClassLoader();
179       cache1.registerClassLoader("/aop", cla);
180       ClassLoader JavaDoc clb = getClassLoader();
181       cache2.registerClassLoader("/aop", clb);
182
183       cache1.put("/aop", "person", ben_);
184
185       Object JavaDoc ben2;
186       // Can't cast it to Person. CCE will resutl.
187
ben2 = cache2.get("/aop", "person");
188       assertEquals(ben_.toString(), ben2.toString());
189
190       Class JavaDoc claz = clb.loadClass(ADDRESS_CLASSNAME);
191       Object JavaDoc add = claz.newInstance();
192       {
193          Class JavaDoc[] types = {String JavaDoc.class};
194          Method JavaDoc setValue = claz.getMethod("setCity", types);
195          Object JavaDoc[] margs = {"Sunnyvale"};
196          setValue.invoke(add, margs);
197       }
198
199       {
200          Class JavaDoc clasz1 = clb.loadClass(PERSON_CLASSNAME);
201          Class JavaDoc[] types = {claz};
202          Method JavaDoc setValue = clasz1.getMethod("setAddress", types);
203          Object JavaDoc[] margs = {add};
204          setValue.invoke(ben2, margs);
205       }
206
207       // Set it back to the cache
208
// Can't cast it to Person. CCE will resutl.
209
cache2.put("/aop", "person", ben2);
210       Object JavaDoc ben3 = cache1.get("/aop", "person");
211       assertEquals(ben2.toString(), ben3.toString());
212
213    }
214
215    public void testPuts() throws Exception JavaDoc
216    {
217       ClassLoader JavaDoc cl = getClassLoader();
218       cache1.registerClassLoader("/aop", cl);
219       // Create an empty Person loaded by this classloader
220
Object JavaDoc scopedBen1 = getPersonFromClassloader(cl);
221
222       cl = getClassLoader();
223       cache2.registerClassLoader("/aop", cl);
224       // Create another empty Person loaded by this classloader
225
Object JavaDoc scopedBen2 = getPersonFromClassloader(cl);
226
227       cache1.put("/aop/1", "person", ben_);
228       cache1.put("/aop/2", "person", scopedBen1);
229
230       Object JavaDoc ben2 = null;
231       try
232       {
233          // Can't cast it to Person. CCE will resutl.
234
ben2 = cache2.get("/aop/1", "person");
235          assertEquals(ben_.toString(), ben2.toString());
236
237          ben2 = cache2.get("/aop/2", "person");
238          assertFalse("cache2 deserialized with scoped classloader", ben2 instanceof Person);
239          assertFalse("cache2 deserialized with cache2 classloader", scopedBen1.equals(ben2));
240          assertEquals("scopedBen deserialized properly", scopedBen2, ben2);
241       }
242       catch (Exception JavaDoc ex)
243       {
244          fail("Test fails with exception " + ex);
245       }
246
247    }
248
249    public void testMethodCall() throws Exception JavaDoc
250    {
251       ClassLoader JavaDoc cl = getClassLoader();
252       cache1.registerClassLoader("/aop", cl);
253       cl = getClassLoader();
254       cache2.registerClassLoader("/aop", cl);
255
256       cache1.put("/aop/1", "person", ben_);
257       cache1.remove("/aop/1", "person");
258       HashMap JavaDoc map = new HashMap JavaDoc();
259       map.put("1", "1");
260       map.put("2", "2");
261       cache1.put("/aop/2", map);
262       cache1.remove("/aop/2");
263
264       TestingUtil.sleepThread(1000);
265    }
266
267    public void testTxMethodCall() throws Exception JavaDoc
268    {
269       ClassLoader JavaDoc cl = getClassLoader();
270       cache1.registerClassLoader("/aop", cl);
271       cl = getClassLoader();
272       cache2.registerClassLoader("/aop", cl);
273
274       Transaction JavaDoc tx = beginTransaction();
275       cache1.put("/aop/1", "person", ben_);
276       cache1.remove("/aop/1", "person");
277       HashMap JavaDoc map = new HashMap JavaDoc();
278       map.put("1", "1");
279       map.put("2", "2");
280       cache1.put("/aop/2", map);
281       cache1.remove("/aop/2");
282       tx.commit();
283
284       TestingUtil.sleepThread(1000);
285    }
286
287    public void testTxPut() throws Exception JavaDoc
288    {
289       Transaction JavaDoc tx = beginTransaction();
290       cache1.put("/aop", "person", ben_);
291       cache1.put("/aop", "person1", ben_);
292       cache1.remove("/aop");
293       cache1.put("/aop", "person", ben_);
294       tx.commit();
295       Person ben2 = (Person) cache2.get("/aop", "person");
296       assertNotNull("Person from 2nd cache should not be null ", ben2);
297       assertEquals(ben_.toString(), ben2.toString());
298    }
299
300    public void testTxRollback() throws Exception JavaDoc
301    {
302       Transaction JavaDoc tx = beginTransaction();
303       cache1.put("/aop", "person", ben_);
304       cache1.put("/aop", "person1", ben_);
305       tx.rollback();
306       Person ben2 = (Person) cache2.get("/aop", "person");
307       assertNull("Person from 2nd cache should be null ", ben2);
308    }
309
310    public void testTxCLSet2() throws Exception JavaDoc
311    {
312       ClassLoader JavaDoc cla = getClassLoader();
313       cache1.registerClassLoader("/aop", cla);
314       ClassLoader JavaDoc clb = getClassLoader();
315       cache2.registerClassLoader("/aop", clb);
316
317       Transaction JavaDoc tx = beginTransaction();
318       cache1.put("/aop", "person", ben_);
319       tx.commit();
320
321       Object JavaDoc ben2;
322       // Can't cast it to Person. CCE will resutl.
323
ben2 = cache2.get("/aop", "person");
324       assertEquals(ben_.toString(), ben2.toString());
325
326       Class JavaDoc claz = clb.loadClass(ADDRESS_CLASSNAME);
327       Object JavaDoc add = claz.newInstance();
328       {
329          Class JavaDoc[] types = {String JavaDoc.class};
330          Method JavaDoc setValue = claz.getMethod("setCity", types);
331          Object JavaDoc[] margs = {"Sunnyvale"};
332          setValue.invoke(add, margs);
333       }
334
335       {
336          Class JavaDoc clasz1 = clb.loadClass(PERSON_CLASSNAME);
337          Class JavaDoc[] types = {claz};
338          Method JavaDoc setValue = clasz1.getMethod("setAddress", types);
339          Object JavaDoc[] margs = {add};
340          setValue.invoke(ben2, margs);
341       }
342
343       // Set it back to the cache
344
// Can't cast it to Person. CCE will resutl.
345
cache2.put("/aop", "person", ben2);
346       Object JavaDoc ben3 = cache1.get("/aop", "person");
347       assertEquals(ben2.toString(), ben3.toString());
348    }
349
350    public void testStateTransfer() throws Exception JavaDoc
351    {
352       // Need to test out if app is not registered with beforehand??
353
}
354
355    public void testCustomFqn() throws Exception JavaDoc
356    {
357       FooClassLoader cl1 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
358       cache1.registerClassLoader("/aop", cl1);
359       FooClassLoader cl2 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
360       cache2.registerClassLoader("/aop", cl2);
361
362       Class JavaDoc clazz = cl1.loadFoo();
363       Object JavaDoc custom1 = clazz.newInstance();
364
365       clazz = cl2.loadFoo();
366       Object JavaDoc custom2 = clazz.newInstance();
367
368       Fqn base = Fqn.fromString("/aop");
369       cache1.put(new Fqn(base, custom1), "key", "value");
370
371       try
372       {
373          Object JavaDoc val = cache2.get(new Fqn(base, custom2), "key");
374          assertEquals("value", val);
375       }
376       catch (Exception JavaDoc ex)
377       {
378          fail("Test fails with exception " + ex);
379       }
380    }
381
382    Transaction JavaDoc beginTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc
383    {
384       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
385       mgr.begin();
386       return mgr.getTransaction();
387    }
388
389    protected Object JavaDoc getPersonFromClassloader(ClassLoader JavaDoc cl) throws Exception JavaDoc
390    {
391       Class JavaDoc clazz = cl.loadClass(PERSON_CLASSNAME);
392       return clazz.newInstance();
393    }
394
395    void log(String JavaDoc msg)
396    {
397       System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
398    }
399
400
401    public static Test suite()
402    {
403       return new TestSuite(SyncReplTest.class);
404    }
405
406    public static void main(String JavaDoc[] args)
407    {
408       junit.textui.TestRunner.run(suite());
409    }
410
411 }
412
Popular Tags