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 ; 15 import javax.transaction.Transaction ; 16 17 22 public class CallbackTest extends TestCase { 23 TreeCache cache=null, cache2; 24 Transaction tx=null; 25 final Fqn FQN=Fqn.fromString("/myNode"); 26 final String KEY="key"; 27 final String VALUE="value"; 28 29 30 31 32 33 Listener exListener=new Listener() { 34 public void nodeCreated(Fqn fqn) { 35 throw new RuntimeException ("this will cause the TX to rollback"); 36 } 37 }; 38 39 protected void setUp() throws Exception { 40 super.setUp(); 41 } 42 43 protected void tearDown() throws Exception { 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 , NotSupportedException { 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 , NotSupportedException { 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 , NotSupportedException { 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 ex) { 88 tx.rollback(); 89 } 90 assertFalse(cache.exists(FQN)); 91 } 94 95 133 134 135 136 144 145 146 TreeCache createCache(int mode, IsolationLevel level) throws Exception { 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 startTransaction() { 157 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 158 try { 159 mgr.begin(); 160 return mgr.getTransaction(); 161 } 162 catch(Throwable 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) { } 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 231 } 232 | Popular Tags |