package org.jboss.test.cache.bean;
import junit.framework.Test;
import org.jboss.test.JBossTestCase;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.transaction.UserTransaction;
import java.util.Properties;
public class MBeanUnitTestCase extends JBossTestCase
{
TreeCacheMBeanTesterHome cache_home;
TreeCacheMBeanTester cache1 = null, cache2 = null;
Properties p_ = new Properties();
public MBeanUnitTestCase(String name)
{
super(name);
}
public void setUp() throws Exception
{
super.setUp();
mySetup();
}
public void tearDown() throws Exception
{
super.tearDown();
if (cache2 != null)
cache2.remove(); if (cache1 != null)
cache1.remove();
}
public void mySetup() throws Exception
{
Object obj;
p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
p_.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
p_.put(Context.PROVIDER_URL, "localhost:1099");
obj = new InitialContext(p_).lookup(TreeCacheMBeanTesterHome.JNDI_NAME);
cache_home = (TreeCacheMBeanTesterHome) PortableRemoteObject.narrow(obj, TreeCacheMBeanTesterHome.class);
}
public void testSetup()
{
assertNotNull("TreeCacheMBeanTesterHome ", cache_home);
}
public void testPutTx()
{
UserTransaction tx = null;
try {
tx = (UserTransaction) new InitialContext(p_).lookup("UserTransaction");
assertNotNull("UserTransaction should not be null ", tx);
cache1 = cache_home.create();
tx.begin();
cache1.put("/a/b/c", "age", new Integer(38));
assertEquals(new Integer(38), cache1.get("/a/b/c", "age"));
cache1.put("/a/b/c", "age", new Integer(39));
tx.commit();
tx.begin();
assertEquals(new Integer(39), cache1.get("/a/b/c", "age"));
tx.commit();
tx.begin();
cache1.remove("/a/b/c");
cache1.remove("/a/b");
cache1.remove("/a");
tx.commit();
} catch (Throwable t) {
fail(t.toString());
try {
tx.rollback();
} catch (Throwable t2) {
;
}
fail(t.toString());
}
}
public void testRollbackTx()
{
UserTransaction tx = null;
try {
tx = (UserTransaction) new InitialContext(p_).lookup("UserTransaction");
assertNotNull("UserTransaction should not be null ", tx);
cache1 = cache_home.create();
tx.begin();
cache1.put("/a/b/c", "age", new Integer(38));
cache1.put("/a/b/c", "age", new Integer(39));
tx.rollback();
tx.begin();
Integer val = (Integer) cache1.get("/a/b/c", "age");
tx.commit();
assertNull(val);
} catch (Throwable t) {
fail(t.toString());
try {
tx.rollback();
} catch (Throwable t2) {
;
}
fail(t.toString());
}
}
public void testReplicatedPutTx()
{
UserTransaction tx = null;
try {
tx = (UserTransaction) new InitialContext(p_).lookup("UserTransaction");
assertNotNull("UserTransaction should not be null ", tx);
cache1 = cache_home.create();
cache2 = cache_home.create();
tx.begin();
cache1.put("/a/b/c", "age", new Integer(38));
assertEquals(new Integer(38), cache1.get("/a/b/c", "age"));
cache1.put("/a/b/c", "age", new Integer(39));
tx.commit();
tx.begin();
assertEquals(new Integer(39), cache2.get("/a/b/c", "age"));
tx.commit();
tx.begin();
cache1.remove("/a/b/c");
cache1.remove("/a/b");
cache1.remove("/a");
tx.commit();
} catch (Throwable t) {
fail(t.toString());
try {
tx.rollback();
} catch (Throwable t2) {
;
}
fail(t.toString());
}
}
void log(String msg)
{
getLog().info("-- [" + Thread.currentThread() + "]: " + msg);
}
public static Test suite() throws Exception
{
return getDeploySetup(getDeploySetup(MBeanUnitTestCase.class, "cachetest.jar"),
"cachetest.sar");
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}