KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.tests.basic;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.CacheException;
7 import org.jboss.cache.Fqn;
8 import org.jboss.cache.TreeCache;
9 import org.jboss.cache.TreeCacheListener;
10 import org.jboss.cache.lock.IsolationLevel;
11 import org.jboss.cache.transaction.DummyTransactionManager;
12 import org.jgroups.View;
13
14 import javax.transaction.NotSupportedException JavaDoc;
15 import javax.transaction.Transaction JavaDoc;
16
17 /**
18  * Tests whether modifications within callbacks (TreeCacheListener) are handled correctly
19  * @author Bela Ban
20  * @version $Id: CallbackTest.java,v 1.1.1.1 2005/03/31 10:15:08 belaban Exp $
21  */

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

133
134
135
136 /* public static void main(String[] args) {
137       try {
138          new CallbackTest().testSyncReplicationWith2Caches();
139       }
140       catch(Exception e) {
141          e.printStackTrace();
142       }
143    }*/

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

231 }
232
Popular Tags