KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > CallbackTest


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

7 package org.jboss.cache.tests;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.jboss.cache.CacheException;
13 import org.jboss.cache.Fqn;
14 import org.jboss.cache.TreeCache;
15 import org.jboss.cache.TreeCacheListener;
16 import org.jboss.cache.lock.IsolationLevel;
17 import org.jboss.cache.transaction.DummyTransactionManager;
18 import org.jgroups.View;
19
20 import javax.transaction.NotSupportedException JavaDoc;
21 import javax.transaction.Transaction JavaDoc;
22
23 /**
24  * Tests whether modifications within callbacks (TreeCacheListener) are handled correctly
25  * @author Bela Ban
26  * @version $Id: CallbackTest.java,v 1.4.2.2 2005/04/06 21:07:03 starksm Exp $
27  */

28 public class CallbackTest extends TestCase {
29    TreeCache cache=null, cache2;
30    Transaction JavaDoc tx=null;
31    final Fqn FQN=Fqn.fromString("/myNode");
32    final String JavaDoc KEY="key";
33    final String JavaDoc VALUE="value";
34
35
36
37
38
39    Listener exListener=new Listener() {
40       public void nodeCreated(Fqn fqn) {
41          throw new RuntimeException JavaDoc("this will cause the TX to rollback");
42       }
43    };
44
45    protected void setUp() throws Exception JavaDoc {
46       super.setUp();
47    }
48
49    protected void tearDown() throws Exception JavaDoc {
50       super.tearDown();
51       if(cache != null) {
52          cache.stopService();
53          cache.destroyService();
54          cache=null;
55       }
56       if(tx != null) {
57          tx.commit();
58          tx=null;
59       }
60    }
61
62
63
64
65
66    public void testLocalCallbackWithoutTransaction() throws Exception JavaDoc, NotSupportedException JavaDoc {
67       cache=createCache(TreeCache.LOCAL, IsolationLevel.SERIALIZABLE);
68       cache.addTreeCacheListener(new MyListener(cache));
69
70       cache.put("/a", null);
71       assertTrue(cache.exists(FQN));
72    }
73
74
75    public void testLocalCallbackWithTransaction() throws Exception JavaDoc, NotSupportedException JavaDoc {
76       cache=createCache(TreeCache.LOCAL, IsolationLevel.SERIALIZABLE);
77       cache.addTreeCacheListener(new MyListener(cache));
78       tx=startTransaction();
79       cache.put("/a", null);
80       tx.commit();
81       assertTrue(cache.exists(FQN));
82    }
83
84
85    public void testLocalCallbackWithException() throws Exception JavaDoc, NotSupportedException JavaDoc {
86       cache=createCache(TreeCache.LOCAL, IsolationLevel.SERIALIZABLE);
87       cache.addTreeCacheListener(exListener);
88       tx=startTransaction();
89       try {
90          cache.put("/a", null);
91          tx.rollback();
92       }
93       catch(RuntimeException JavaDoc ex) {
94         tx.rollback();
95       }
96       assertFalse(cache.exists(FQN));
97       // assertFalse(cache.exists("/a")); -- fix: reason is that CreateIfNotExists interceptor is *before*
98
// LockInterceptor, which registers for TX completion, we don't even get that far
99
}
100
101    /*public void testSyncReplicationWith2Caches() throws Exception, NotSupportedException {
102       cache=createCache(TreeCache.REPL_SYNC, IsolationLevel.SERIALIZABLE);
103       cache2=createCache(TreeCache.REPL_SYNC, IsolationLevel.SERIALIZABLE);
104       cache.setSyncCommitPhase(true);
105       cache2.setSyncCommitPhase(true);
106
107       // cache.setSyncReplTimeout(60000); // todo: remove again, just for debugging
108       // cache2.setSyncReplTimeout(60000); // todo: remove again, just for debugging
109
110       assertEquals(2, cache.getMembers().size());
111       System.out.println("view is correct: " + cache.getMembers() + " (2 members)");
112       cache2.addTreeCacheListener(new MyListener(cache2));
113
114       tx=startTransaction();
115       cache.put("/a", null);
116       tx.commit();
117
118       // 1. cache: put("/a")
119       // 2. tx.commit() in cache causes 2PC to cache2
120       // 3. cache2 is updated with "/a"
121       // 4. listener in cache2 is called, creates FQN
122       // 5. cache2 replicates FQN over to cache as part of its own 2PC protocol
123
124       // Util.sleep(1000000);
125
126       assertTrue(cache.exists("/a")); // original modification to cache
127       assertTrue(cache.exists(FQN)); // result of replication from cache
128       assertTrue(cache2.exists("/a")); // result of replication from cache
129       assertTrue(cache2.exists(FQN)); // created by listener, triggered by replication of "/a" from cache
130       TransactionTable tx_table1, tx_table2;
131       tx_table1=cache.getTransactionTable();
132       tx_table2=cache2.getTransactionTable();
133       System.out.println("tx_table1=" + tx_table1 + ", tx_table2=" + tx_table2);
134       assertEquals(0, tx_table1.getNumLocalTransactions());
135       assertEquals(0, tx_table2.getNumLocalTransactions());
136       assertEquals(0, tx_table1.getNumGlobalTransactions());
137       assertEquals(0, tx_table2.getNumGlobalTransactions());
138    }*/

139
140
141
142 /* public static void main(String[] args) {
143       try {
144          new CallbackTest().testSyncReplicationWith2Caches();
145       }
146       catch(Exception e) {
147          e.printStackTrace();
148       }
149    }*/

150
151
152    TreeCache createCache(int mode, IsolationLevel level) throws Exception JavaDoc {
153       TreeCache c=new TreeCache();
154       c.setCacheMode(mode);
155       c.setIsolationLevel(level);
156       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
157       c.createService();
158       c.startService();
159       return c;
160    }
161
162    Transaction JavaDoc startTransaction() {
163       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
164       try {
165          mgr.begin();
166          return mgr.getTransaction();
167       }
168       catch(Throwable JavaDoc t) {
169          return null;
170       }
171    }
172
173
174
175    class Listener implements TreeCacheListener {
176
177       public void nodeCreated(Fqn fqn) {
178       }
179
180       public void nodeRemoved(Fqn fqn) {
181       }
182
183       public void nodeLoaded(Fqn fqn) {
184       }
185
186       public void nodeEvicted(Fqn fqn) {
187       }
188
189       public void nodeModified(Fqn fqn) {
190       }
191
192       public void nodeVisited(Fqn fqn) {
193       }
194
195       public void cacheStarted(TreeCache cache) {
196       }
197
198       public void cacheStopped(TreeCache cache) {
199       }
200
201       public void viewChange(View new_view) { // might be MergeView after merging
202
}
203    }
204
205
206
207    class MyListener extends Listener {
208       TreeCache c;
209
210       public MyListener(TreeCache c) {
211          this.c=c;
212       }
213
214       public void nodeCreated(Fqn fqn) {
215          try {
216             if(!c.exists(FQN)) {
217                System.out.println("MyListener: creating node " + FQN);
218                c.put(FQN, KEY, VALUE);
219                System.out.println("MyListener: created node " + FQN);
220             }
221          }
222          catch(CacheException e) {
223             fail("listener was unable to update cache during callback: " + e);
224          }
225       }
226
227    }
228
229    public static Test suite() {
230       return new TestSuite(CallbackTest.class);
231    }
232
233    //public static void main(String[] args) {
234
// junit.textui.TestRunner.run(suite());
235
//}
236

237 }
238
Popular Tags