KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > replicated > AsyncUnitTestCase


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.test.cache.test.replicated;
10
11
12 import junit.framework.Test;
13 import junit.framework.TestCase;
14 import junit.framework.TestSuite;
15 import org.jboss.cache.PropertyConfigurator;
16 import org.jboss.cache.TreeCache;
17
18 /**
19  * Unit test for replicated async TreeCache. Use locking and multiple threads to test
20  * concurrent access to the tree.
21  *
22  * @version $Revision: 1.12 $
23  */

24 public class AsyncUnitTestCase extends TestCase {
25    TreeCache cache1, cache2;
26    String JavaDoc props=null;
27
28    public AsyncUnitTestCase(String JavaDoc name) {
29       super(name);
30    }
31
32    public void setUp() throws Exception JavaDoc {
33       super.setUp();
34
35       log("creating cache1");
36       cache1=createCache("CacheGroup");
37
38       log("creating cache2");
39       cache2=createCache("CacheGroup");
40    }
41
42    private TreeCache createCache(String JavaDoc name) throws Exception JavaDoc {
43       TreeCache tree=new TreeCache();
44       PropertyConfigurator config=new PropertyConfigurator();
45       config.configure(tree, "META-INF/replAsync-service.xml"); // read in generic replAsync xml
46
tree.setClusterName(name);
47       tree.createService();
48       tree.startService();
49       return tree;
50    }
51
52    public void tearDown() throws Exception JavaDoc {
53       super.tearDown();
54       if(cache1 != null) {
55          log("stopping cache1");
56          cache1.stopService();
57       }
58
59       if(cache2 != null) {
60          log("stopping cache2");
61          cache2.stopService();
62       }
63    }
64
65
66    public void testPutShouldNotReplicateToDifferentCluster() {
67       TreeCache cache3=null;
68       try {
69          cache3=createCache("DifferentGroup");
70          cache1.put("/a/b/c", "age", new Integer JavaDoc(38));
71          // because we use async repl, modfication may not yet have been propagated to cache2, so
72
// we have to wait a little
73
sleep(2000);
74          assertNull("Should not have replicated", cache3.get("/a/b/c", "age"));
75       }
76       catch(Exception JavaDoc e) {
77          fail(e.toString());
78       }
79       finally {
80          if(cache3 != null)
81             cache3.stopService();
82       }
83    }
84
85    public void testStateTransfer() {
86       TreeCache cache4=null;
87       try {
88          cache1.put("a/b/c", "age", new Integer JavaDoc(38));
89          cache4=createCache("CacheGroup");
90          assertEquals(3, cache4.getMembers().size()); // cache1, cache2 and cache4
91
assertEquals("\"age\" should be 38", new Integer JavaDoc(38), cache4.get("/a/b/c", "age"));
92       }
93       catch(Exception JavaDoc e) {
94          fail(e.toString());
95       }
96       finally {
97          if(cache4 != null) {
98             System.out.println("cache4's view: " + cache4.getMembers());
99             cache4.stopService();
100          }
101       }
102    }
103
104
105    public void testAsyncReplDelay() {
106       Integer JavaDoc age;
107
108       try {
109          cache1.put("/a/b/c", "age", new Integer JavaDoc(38));
110
111          // value on cache2 may be 38 or not yet replicated
112
age=(Integer JavaDoc)cache2.get("/a/b/c", "age");
113          log("attr \"age\" of \"/a/b/c\" on cache2=" + age);
114          assertTrue("should be either null or 38", age == null || age.intValue() == 38);
115       }
116       catch(Exception JavaDoc e) {
117          fail(e.toString());
118       }
119    }
120
121    public void testSyncRepl() throws Exception JavaDoc {
122       Integer JavaDoc age;
123       cache1.setCacheMode(TreeCache.REPL_SYNC);
124       cache2.setCacheMode(TreeCache.REPL_SYNC);
125
126       try {
127          cache1.put("/a/b/c", "age", new Integer JavaDoc(38));
128
129          // value on cache2 must be 38
130
age=(Integer JavaDoc)cache2.get("/a/b/c", "age");
131          log("attr \"age\" of \"/a/b/c\" on cache2=" + age);
132          assertNotNull("\"age\" obtained from cache2 is null ", age);
133          assertTrue("\"age\" must be 38", age.intValue() == 38);
134       }
135       catch(Exception JavaDoc e) {
136          fail(e.toString());
137       }
138    }
139
140
141    private void sleep(long timeout) {
142       try {
143          Thread.sleep(timeout);
144       }
145       catch(Throwable JavaDoc t) {
146          ;
147       }
148    }
149
150    void log(String JavaDoc msg) {
151       System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
152    }
153
154
155    public static Test suite() {
156       return new TestSuite(AsyncUnitTestCase.class);
157    }
158
159    public static void main(String JavaDoc[] args) {
160       junit.textui.TestRunner.run(suite());
161    }
162 }
163
Popular Tags