1 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 ; 21 import javax.transaction.Transaction ; 22 23 28 public class CallbackTest extends TestCase { 29 TreeCache cache=null, cache2; 30 Transaction tx=null; 31 final Fqn FQN=Fqn.fromString("/myNode"); 32 final String KEY="key"; 33 final String VALUE="value"; 34 35 36 37 38 39 Listener exListener=new Listener() { 40 public void nodeCreated(Fqn fqn) { 41 throw new RuntimeException ("this will cause the TX to rollback"); 42 } 43 }; 44 45 protected void setUp() throws Exception { 46 super.setUp(); 47 } 48 49 protected void tearDown() throws Exception { 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 , NotSupportedException { 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 , NotSupportedException { 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 , NotSupportedException { 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 ex) { 94 tx.rollback(); 95 } 96 assertFalse(cache.exists(FQN)); 97 } 100 101 139 140 141 142 150 151 152 TreeCache createCache(int mode, IsolationLevel level) throws Exception { 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 startTransaction() { 163 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 164 try { 165 mgr.begin(); 166 return mgr.getTransaction(); 167 } 168 catch(Throwable 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) { } 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 237 } 238 | Popular Tags |