package org.jboss.cache.tests;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.CacheException;
import org.jboss.cache.Fqn;
import org.jboss.cache.TreeCache;
import org.jboss.cache.TreeCacheListener;
import org.jboss.cache.lock.IsolationLevel;
import org.jboss.cache.transaction.DummyTransactionManager;
import org.jgroups.View;
import javax.transaction.NotSupportedException;
import javax.transaction.Transaction;
public class CallbackTest extends TestCase {
TreeCache cache=null, cache2;
Transaction tx=null;
final Fqn FQN=Fqn.fromString("/myNode");
final String KEY="key";
final String VALUE="value";
Listener exListener=new Listener() {
public void nodeCreated(Fqn fqn) {
throw new RuntimeException("this will cause the TX to rollback");
}
};
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
if(cache != null) {
cache.stopService();
cache.destroyService();
cache=null;
}
if(tx != null) {
tx.commit();
tx=null;
}
}
public void testLocalCallbackWithoutTransaction() throws Exception, NotSupportedException {
cache=createCache(TreeCache.LOCAL, IsolationLevel.SERIALIZABLE);
cache.addTreeCacheListener(new MyListener(cache));
cache.put("/a", null);
assertTrue(cache.exists(FQN));
}
public void testLocalCallbackWithTransaction() throws Exception, NotSupportedException {
cache=createCache(TreeCache.LOCAL, IsolationLevel.SERIALIZABLE);
cache.addTreeCacheListener(new MyListener(cache));
tx=startTransaction();
cache.put("/a", null);
tx.commit();
assertTrue(cache.exists(FQN));
}
public void testLocalCallbackWithException() throws Exception, NotSupportedException {
cache=createCache(TreeCache.LOCAL, IsolationLevel.SERIALIZABLE);
cache.addTreeCacheListener(exListener);
tx=startTransaction();
try {
cache.put("/a", null);
tx.rollback();
}
catch(RuntimeException ex) {
tx.rollback();
}
assertFalse(cache.exists(FQN));
}
TreeCache createCache(int mode, IsolationLevel level) throws Exception {
TreeCache c=new TreeCache();
c.setCacheMode(mode);
c.setIsolationLevel(level);
c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
c.createService();
c.startService();
return c;
}
Transaction startTransaction() {
DummyTransactionManager mgr=DummyTransactionManager.getInstance();
try {
mgr.begin();
return mgr.getTransaction();
}
catch(Throwable t) {
return null;
}
}
class Listener implements TreeCacheListener {
public void nodeCreated(Fqn fqn) {
}
public void nodeRemoved(Fqn fqn) {
}
public void nodeLoaded(Fqn fqn) {
}
public void nodeEvicted(Fqn fqn) {
}
public void nodeModified(Fqn fqn) {
}
public void nodeVisited(Fqn fqn) {
}
public void cacheStarted(TreeCache cache) {
}
public void cacheStopped(TreeCache cache) {
}
public void viewChange(View new_view) { }
}
class MyListener extends Listener {
TreeCache c;
public MyListener(TreeCache c) {
this.c=c;
}
public void nodeCreated(Fqn fqn) {
try {
if(!c.exists(FQN)) {
System.out.println("MyListener: creating node " + FQN);
c.put(FQN, KEY, VALUE);
System.out.println("MyListener: created node " + FQN);
}
}
catch(CacheException e) {
fail("listener was unable to update cache during callback: " + e);
}
}
}
public static Test suite() {
return new TestSuite(CallbackTest.class);
}
}