KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.commons.logging.LogFactory;
15 import org.jboss.cache.CacheImpl;
16 import org.jboss.cache.Fqn;
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
28 /**
29  * Test marshalling for async mode.
30  *
31  * @author Ben Wang
32  * @version $Revision: 1.13 $
33  */

34 public class AsyncReplTest extends RegionBasedMarshallingTestBase
35 {
36    CacheImpl cache1, cache2;
37    String JavaDoc props = null;
38    Person ben_;
39    Address addr_;
40    Throwable JavaDoc ex_;
41
42    public void setUp() throws Exception JavaDoc
43    {
44       super.setUp();
45
46       log("creating cache1");
47       cache1 = createCache("TestCache");
48
49       log("creating cache2");
50
51       cache2 = createCache("TestCache");
52
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       cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replAsync-service.xml"));
67       cache.getConfiguration().setClusterName(name);
68       // Use marshaller
69
cache.getConfiguration().setUseRegionBasedMarshalling(true);
70       cache.create();
71       cache.start();
72       return cache;
73    }
74
75    public void tearDown() throws Exception JavaDoc
76    {
77       super.tearDown();
78       cache1.remove("/");
79       if (cache1 != null)
80       {
81          log("stopping cache1");
82          cache1.stop();
83       }
84
85       if (cache2 != null)
86       {
87          log("stopping cache2");
88          cache2.stop();
89       }
90    }
91
92    /**
93     * Test replication with classloaders.
94     *
95     * @throws Exception
96     */

97    public void testCLSet2() throws Exception JavaDoc
98    {
99       ClassLoader JavaDoc cla = getClassLoader();
100       cache1.registerClassLoader("/aop", cla);
101       ClassLoader JavaDoc clb = getClassLoader();
102       cache2.registerClassLoader("/aop", clb);
103
104       cache1.put("/aop", "person", ben_);
105       cache1.put("alias", "person", ben_);
106
107       TestingUtil.sleepThread(1000);
108       Object JavaDoc ben2 = null;
109       try
110       {
111          // Can't cast it to Person. CCE will resutl.
112
ben2 = cache2.get("/aop", "person");
113          assertNotNull(ben2);
114          assertEquals(ben_.toString(), ben2.toString());
115       }
116       catch (Exception JavaDoc ex)
117       {
118          LogFactory.getLog("TEST").debug("Ex:", ex);
119          fail("Test fails with exception " + ex);
120       }
121
122       Class JavaDoc claz = clb.loadClass(ADDRESS_CLASSNAME);
123       Object JavaDoc add = claz.newInstance();
124       {
125          Class JavaDoc[] types = {String JavaDoc.class};
126          Method JavaDoc setValue = claz.getMethod("setCity", types);
127          Object JavaDoc[] margs = {"Sunnyvale"};
128          setValue.invoke(add, margs);
129       }
130
131       {
132          Class JavaDoc clasz1 = clb.loadClass(PERSON_CLASSNAME);
133          Class JavaDoc[] types = {claz};
134          Method JavaDoc setValue = clasz1.getMethod("setAddress", types);
135          Object JavaDoc[] margs = {add};
136          setValue.invoke(ben2, margs);
137       }
138
139       // Set it back to the cache
140
try
141       {
142          // Can't cast it to Person. CCE will resutl.
143
cache2.put("/aop", "person", ben2);
144          TestingUtil.sleepThread(1000);
145          Object JavaDoc ben3 = cache1.get("/aop", "person");
146          assertEquals(ben2.toString(), ben3.toString());
147       }
148       catch (Exception JavaDoc ex)
149       {
150          ex.printStackTrace();
151          fail("Test fails with exception " + ex);
152       }
153
154    }
155
156    public void testPuts() throws Exception JavaDoc
157    {
158       ClassLoader JavaDoc cl = getClassLoader();
159       cache1.registerClassLoader("/aop", cl);
160       // Create an empty Person loaded by this classloader
161
Object JavaDoc scopedBen1 = getPersonFromClassloader(cl);
162
163       cl = getClassLoader();
164       cache2.registerClassLoader("/aop", cl);
165       // Create another empty Person loaded by this classloader
166
Object JavaDoc scopedBen2 = getPersonFromClassloader(cl);
167
168       cache1.put("/aop/1", "person", ben_);
169       cache1.put("/aop/2", "person", scopedBen1);
170       TestingUtil.sleepThread(1000);
171
172       Object JavaDoc ben2 = null;
173       try
174       {
175          // Can't cast it to Person. CCE will resutl.
176
ben2 = cache2.get("/aop/1", "person");
177          assertEquals(ben_.toString(), ben2.toString());
178
179          ben2 = cache2.get("/aop/2", "person");
180          assertFalse("cache2 deserialized with scoped classloader", ben2 instanceof Person);
181          assertFalse("cache2 deserialized with cache2 classloader", scopedBen1.equals(ben2));
182          assertEquals("scopedBen deserialized properly", scopedBen2, ben2);
183       }
184       catch (Exception JavaDoc ex)
185       {
186          fail("Test fails with exception " + ex);
187       }
188
189    }
190
191    public void testTxPut() throws Exception JavaDoc
192    {
193       Transaction JavaDoc tx = beginTransaction();
194       cache1.put("/aop", "person", ben_);
195       cache1.put("/aop", "person1", ben_);
196       tx.commit();
197       TestingUtil.sleepThread(1000);
198       Person ben2 = (Person) cache2.get("/aop", "person");
199       assertNotNull("Person from 2nd cache should not be null ", ben2);
200       assertEquals(ben_.toString(), ben2.toString());
201    }
202
203    public void testTxCLSet2() throws Exception JavaDoc
204    {
205       ClassLoader JavaDoc cla = getClassLoader();
206       cache1.registerClassLoader("/aop", cla);
207       ClassLoader JavaDoc clb = getClassLoader();
208       cache2.registerClassLoader("/aop", clb);
209
210       Transaction JavaDoc tx = beginTransaction();
211       cache1.put("/aop", "person", ben_);
212       tx.commit();
213       TestingUtil.sleepThread(1000);
214
215       Object JavaDoc ben2 = null;
216       try
217       {
218          // Can't cast it to Person. CCE will resutl.
219
ben2 = cache2.get("/aop", "person");
220          assertEquals(ben_.toString(), ben2.toString());
221       }
222       catch (Exception JavaDoc ex)
223       {
224          fail("Test fails with exception " + ex);
225       }
226
227       Class JavaDoc claz = clb.loadClass(ADDRESS_CLASSNAME);
228       Object JavaDoc add = claz.newInstance();
229       {
230          Class JavaDoc[] types = {String JavaDoc.class};
231          Method JavaDoc setValue = claz.getMethod("setCity", types);
232          Object JavaDoc[] margs = {"Sunnyvale"};
233          setValue.invoke(add, margs);
234       }
235
236       {
237          Class JavaDoc clasz1 = clb.loadClass(PERSON_CLASSNAME);
238          Class JavaDoc[] types = {claz};
239          Method JavaDoc setValue = clasz1.getMethod("setAddress", types);
240          Object JavaDoc[] margs = {add};
241          setValue.invoke(ben2, margs);
242       }
243
244       // Set it back to the cache
245
try
246       {
247          // Can't cast it to Person. CCE will resutl.
248
cache2.put("/aop", "person", ben2);
249          TestingUtil.sleepThread(1000);
250          Object JavaDoc ben3 = cache1.get("/aop", "person");
251          assertEquals(ben2.toString(), ben3.toString());
252       }
253       catch (Exception JavaDoc ex)
254       {
255          fail("Test fails with exception " + ex);
256       }
257
258    }
259
260    public void testStateTransfer() throws Exception JavaDoc
261    {
262       // Need to test out if app is not registered with beforehand??
263
}
264
265    public void testCustomFqn() throws Exception JavaDoc
266    {
267       // FIXME work via the Cache API
268

269       FooClassLoader cl1 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
270       cache1.registerClassLoader("/aop", cl1);
271       FooClassLoader cl2 = new FooClassLoader(Thread.currentThread().getContextClassLoader());
272       cache2.registerClassLoader("/aop", cl2);
273
274       Class JavaDoc clazz = cl1.loadFoo();
275       Object JavaDoc custom1 = clazz.newInstance();
276
277       clazz = cl2.loadFoo();
278       Object JavaDoc custom2 = clazz.newInstance();
279
280       Fqn base = Fqn.fromString("/aop");
281       cache1.put(new Fqn(base, custom1), "key", "value");
282       TestingUtil.sleepThread(1000);
283
284       try
285       {
286          Object JavaDoc val = cache2.get(new Fqn(base, custom2), "key");
287          assertEquals("value", val);
288       }
289       catch (Exception JavaDoc ex)
290       {
291          fail("Test fails with exception " + ex);
292       }
293    }
294
295    Transaction JavaDoc beginTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc
296    {
297       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
298       mgr.begin();
299       return mgr.getTransaction();
300    }
301
302    protected Object JavaDoc getPersonFromClassloader(ClassLoader JavaDoc cl) throws Exception JavaDoc
303    {
304       Class JavaDoc clazz = cl.loadClass(PERSON_CLASSNAME);
305       return clazz.newInstance();
306    }
307
308    void log(String JavaDoc msg)
309    {
310       System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
311    }
312
313
314    public static Test suite()
315    {
316       return new TestSuite(AsyncReplTest.class);
317    }
318
319    public static void main(String JavaDoc[] args)
320    {
321       junit.textui.TestRunner.run(suite());
322    }
323
324 }
325
Popular Tags