KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > replicated > SyncCacheListenerTest


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

8 package org.jboss.cache.tests.replicated;
9
10 import EDU.oswego.cs.dl.util.concurrent.FIFOSemaphore;
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.jboss.cache.CacheException;
17 import org.jboss.cache.Fqn;
18 import org.jboss.cache.TreeCache;
19 import org.jboss.cache.TreeCacheListener;
20 import org.jboss.cache.lock.IsolationLevel;
21 import org.jboss.cache.transaction.DummyTransactionManager;
22 import org.jgroups.View;
23
24 import javax.naming.Context JavaDoc;
25 import javax.transaction.NotSupportedException JavaDoc;
26 import javax.transaction.SystemException JavaDoc;
27 import javax.transaction.Transaction JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 /**
33  * Test out the TreeCacheListener
34  *
35  * @version $Revision: 1.2 $
36  */

37 public class SyncCacheListenerTest extends TestCase {
38    TreeCache cache1, cache2;
39    int caching_mode=TreeCache.REPL_SYNC;
40    final String JavaDoc group_name="TreeCacheTestGroup";
41    String JavaDoc props=
42          "UDP(ip_mcast=true;ip_ttl=64;loopback=false;mcast_addr=228.1.2.3;" +
43          "mcast_port=45566;mcast_recv_buf_size=80000;mcast_send_buf_size=150000;" +
44          "ucast_recv_buf_size=80000;ucast_send_buf_size=150000):" +
45          "PING(down_thread=true;num_initial_members=2;timeout=500;up_thread=true):" +
46          "MERGE2(max_interval=20000;min_interval=10000):" +
47          "FD(down_thread=true;shun=true;up_thread=true):" +
48          "VERIFY_SUSPECT(down_thread=true;timeout=1500;up_thread=true):" +
49          "pbcast.NAKACK(down_thread=true;gc_lag=50;retransmit_timeout=600,1200,2400,4800;" +
50          "up_thread=true):" +
51          "pbcast.STABLE(desired_avg_gossip=20000;down_thread=true;up_thread=true):" +
52          "UNICAST(down_thread=true;min_threshold=10;timeout=600,1200,2400;window_size=100):" +
53          "FRAG(down_thread=true;frag_size=8192;up_thread=true):" +
54          "pbcast.GMS(join_retry_timeout=2000;join_timeout=5000;print_local_addr=true;shun=true):" +
55          "pbcast.STATE_TRANSFER(down_thread=true;up_thread=true)";
56
57    final static Log log_=LogFactory.getLog(SyncCacheListenerTest.class);
58    String JavaDoc old_factory=null;
59    final String JavaDoc FACTORY="org.jboss.cache.transaction.DummyContextFactory";
60    FIFOSemaphore lock=new FIFOSemaphore(1);
61    DummyTransactionManager tx_mgr;
62    Throwable JavaDoc t1_ex, t2_ex, ex=null;
63
64
65
66    public SyncCacheListenerTest(String JavaDoc name) {
67       super(name);
68    }
69
70    public void setUp() throws Exception JavaDoc {
71       super.setUp();
72       old_factory=System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
73       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
74       tx_mgr=DummyTransactionManager.getInstance();
75       t1_ex=t2_ex=ex=null;
76    }
77
78    public void tearDown() throws Exception JavaDoc {
79       super.tearDown();
80       DummyTransactionManager.destroy();
81       destroyCaches();
82       if(old_factory != null) {
83          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
84          old_factory=null;
85       }
86    }
87
88    Transaction JavaDoc beginTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc {
89       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
90       mgr.begin();
91       Transaction JavaDoc tx=mgr.getTransaction();
92       return tx;
93    }
94
95    void initCaches(int caching_mode) throws Exception JavaDoc {
96       this.caching_mode=caching_mode;
97       cache1=new TreeCache();
98       cache2=new TreeCache();
99       cache1.setCacheMode(caching_mode);
100       cache2.setCacheMode(caching_mode);
101       cache1.setIsolationLevel(IsolationLevel.SERIALIZABLE);
102       cache2.setIsolationLevel(IsolationLevel.SERIALIZABLE);
103
104       cache1.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
105       cache2.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
106       /*
107       cache1.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
108       cache2.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
109 */

110       cache1.setLockAcquisitionTimeout(5000);
111       cache2.setLockAcquisitionTimeout(5000);
112       cache1.start();
113       cache2.start();
114    }
115
116    void destroyCaches() throws Exception JavaDoc {
117       if(cache1 != null)
118          cache1.stop();
119       if(cache2 != null)
120          cache2.stop();
121       cache1=null;
122       cache2=null;
123    }
124
125    public void testSyncTxRepl() throws Exception JavaDoc {
126       Integer JavaDoc age;
127       Transaction JavaDoc tx;
128
129       try {
130          initCaches(TreeCache.REPL_SYNC);
131
132          tx=beginTransaction();
133          Listener lis = new Listener();
134          cache1.addTreeCacheListener(lis);
135          lis.put("/a/b/c", "age", new Integer JavaDoc(38));
136          assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age"));
137          tx.commit();
138
139          // value on cache2 must be 38
140
age=(Integer JavaDoc)cache2.get("/a/b/c", "age");
141          assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
142          assertTrue("\"age\" must be 38", age.intValue() == 38);
143       }
144       catch(Exception JavaDoc e) {
145          fail(e.toString());
146       }
147    }
148
149
150    public void testSyncRepl() throws Exception JavaDoc {
151       Integer JavaDoc age;
152       try {
153          initCaches(TreeCache.REPL_SYNC);
154
155          Listener lis = new Listener();
156          cache1.addTreeCacheListener(lis);
157          lis.put("/a/b/c", "age", new Integer JavaDoc(38));
158
159          // value on cache2 must be 38
160
age=(Integer JavaDoc)cache2.get("/a/b/c", "age");
161          assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
162          assertTrue("\"age\" must be 38", age.intValue() == 38);
163       }
164       catch(Exception JavaDoc e) {
165          fail(e.toString());
166       }
167    }
168
169    public void testSyncTxReplMap() throws Exception JavaDoc {
170       Integer JavaDoc age;
171       Transaction JavaDoc tx;
172
173       try {
174          initCaches(TreeCache.REPL_SYNC);
175
176          tx=beginTransaction();
177          Listener lis = new Listener();
178          cache1.addTreeCacheListener(lis);
179          Map JavaDoc map = new HashMap JavaDoc();
180          map.put("age", new Integer JavaDoc(38));
181          map.put("name", "Ben");
182          lis.put("/a/b/c", map);
183          assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age"));
184          tx.commit();
185
186          // value on cache2 must be 38
187
age=(Integer JavaDoc)cache2.get("/a/b/c", "age");
188          assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
189          assertTrue("\"age\" must be 38", age.intValue() == 38);
190       }
191       catch(Exception JavaDoc e) {
192          fail(e.toString());
193       }
194    }
195
196
197    public void testSyncReplMap() throws Exception JavaDoc {
198       Integer JavaDoc age;
199       try {
200          initCaches(TreeCache.REPL_SYNC);
201
202          Listener lis = new Listener();
203          cache1.addTreeCacheListener(lis);
204          Map JavaDoc map = new HashMap JavaDoc();
205          map.put("age", new Integer JavaDoc(38));
206          map.put("name", "Ben");
207          lis.put("/a/b/c", map);
208
209          // value on cache2 must be 38
210
age=(Integer JavaDoc)cache2.get("/a/b/c", "age");
211          assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
212          assertTrue("\"age\" must be 38", age.intValue() == 38);
213       }
214       catch(Exception JavaDoc e) {
215          fail(e.toString());
216       }
217    }
218
219
220    class Listener implements TreeCacheListener
221    {
222       Object JavaDoc key_ = null;
223       public void put(String JavaDoc fqn, Object JavaDoc key, Object JavaDoc val) throws Exception JavaDoc
224       {
225          key_ = key;
226          cache1.put(fqn, key, val);
227       }
228
229       public void put(String JavaDoc fqn, Map JavaDoc map) throws Exception JavaDoc
230       {
231          if(map.size() == 0) fail("put(): map size can't be 0");
232          Set JavaDoc set = map.keySet();
233          key_ = set.iterator().next(); // take anyone
234
cache1.put(fqn, map);
235       }
236
237
238       public void nodeCreated(Fqn fqn) {
239          //To change body of implemented methods use File | Settings | File Templates.
240
}
241
242       public void nodeRemoved(Fqn fqn) {
243          //To change body of implemented methods use File | Settings | File Templates.
244
}
245
246       public void nodeLoaded(Fqn fqn) {
247          //To change body of implemented methods use File | Settings | File Templates.
248
}
249
250       public void nodeEvicted(Fqn fqn) {
251          //To change body of implemented methods use File | Settings | File Templates.
252
}
253
254       public void nodeModified(Fqn fqn)
255       {
256          log_.debug("nodeModified visited with fqn: " +fqn);
257          try {
258             // test out if we can get the read lock since there is a write lock going as well.
259
cache1.get(fqn, key_);
260          } catch (CacheException e) {
261             e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
262
fail("nodeModified: test failed with exception: " +e);
263          }
264       }
265
266       public void nodeVisited(Fqn fqn) {
267          //To change body of implemented methods use File | Settings | File Templates.
268
}
269
270       public void cacheStarted(TreeCache cache) {
271          //To change body of implemented methods use File | Settings | File Templates.
272
}
273
274       public void cacheStopped(TreeCache cache) {
275          //To change body of implemented methods use File | Settings | File Templates.
276
}
277
278       public void viewChange(View new_view) // might be MergeView after merging
279
{
280          //To change body of implemented methods use File | Settings | File Templates.
281
}
282    }
283
284    static void _pause(long millis) {
285       try {
286          Thread.sleep(millis);
287       }
288       catch(Exception JavaDoc t) {
289       }
290    }
291
292    public static Test suite() throws Exception JavaDoc {
293 // return getDeploySetup(SyncTxUnitTestCase.class, "cachetest.jar");
294
return new TestSuite(SyncCacheListenerTest.class);
295    }
296
297
298 }
299
Popular Tags