[jboss-cvs] JBossCache/old/tests/functional/org/jboss/cache/aop ...
Ben Wang
bwang at jboss.com
Tue Oct 31 03:01:13 EST 2006
User: bwang
Date: 06/10/31 03:01:13
Added: old/tests/functional/org/jboss/cache/aop
NewReplicatedTxAopTest.java
TreeCacheAopCompatibilityAopTest.java
ReplicatedSerializableAopTest.java
ReplicatedPutWithBulkRemoveAopTest.java
CacheLoaderTestsBase.java NonAspectizedAopTest.java
ReplicatedTxAopTest.java
ReplicatedObjectGraphAopTest.java
LocalConcurrentTest.java TreeCacheAopTester.java
LocalAopTest.java NewLocalAopTest.java
ObjectGraphAopTest.java
SharedRefConcurrentTest.java
NewReplicatedAopTest.java RecursiveRefAopTest.java
ReplicatedAopTest.java
Log:
Deprecated files moved to old dir.
Revision Changes Path
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/NewReplicatedTxAopTest.java
Index: NewReplicatedTxAopTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.IdObject;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.misc.TestingUtil;
import org.jboss.cache.transaction.DummyTransactionManager;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.UserTransaction;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* PojoCache replicated test cases with tx.
*
* @author Ben Wang
*/
public class NewReplicatedTxAopTest extends TestCase
{
Log log= LogFactory.getLog(NewReplicatedTxAopTest.class);
PojoCache cache_;
PojoCache cache1_;
final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
DummyTransactionManager tx_mgr;
Throwable t1_ex, t2_ex;
long start=0;
public NewReplicatedTxAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
super.setUp();
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
cache_ = new PojoCache();
cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
cache1_ = new PojoCache();
cache1_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
cache_.start();
cache1_.start();
tx_mgr = DummyTransactionManager.getInstance();
t1_ex = t2_ex = null;
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
cache1_.stop();
DummyTransactionManager.destroy();
}
UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
{
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.cache.transaction.DummyContextFactory");
return (UserTransaction)new InitialContext(prop).lookup("UserTransaction");
}
public void testSimpleTxWithRollback() throws Exception
{
log.info("testSimpleTxWithRollback() ....");
UserTransaction tx = getTransaction();
Person joe = new Person();
joe.setName("Joe");
Address add = new Address();
add.setZip(104);
add.setCity("Taipei");
joe.setAddress(add);
tx.begin();
cache_.putObject("/person/joe", joe);
tx.commit();
Person p = (Person)cache1_.getObject("/person/joe");
assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
// test rollback
Person ben = new Person();
ben.setName("Ben");
add = new Address();
add.setZip(104);
add.setCity("Taipei");
joe.setAddress(add);
tx.begin();
cache_.putObject("/person/ben", ben);
tx.rollback();
assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
}
public void testCollectionTxWithRollback() throws Exception
{
log.info("testCollectionTxWithRollback() ....");
UserTransaction tx = getTransaction();
Person joe = new Person();
joe.setName("Joe");
Address add = new Address();
add.setZip(104);
add.setCity("Taipei");
joe.setAddress(add);
ArrayList lang = new ArrayList();
lang.add("English");
lang.add("Taiwanese");
lang.add("Mandirin");
joe.setLanguages(lang);
tx.begin();
cache_.putObject("/person/joe", joe);
tx.commit();
Person p = (Person)cache1_.getObject("/person/joe");
assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
// test rollback
Person ben = new Person();
ben.setName("Ben");
add = new Address();
add.setZip(104);
add.setCity("Taipei");
ben.setAddress(add);
tx.begin();
cache_.putObject("/person/ben", ben);
tx.rollback();
assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
assertEquals("Langue 1 should be: ", "English", (String)joe.getLanguages().get(0));
}
public void testConcurrentPutsWithRollback() throws Exception
{
Thread t1 = new Thread()
{
public void run()
{
Person p = new Person();
p.setName("Ben");
Address add = new Address();
add.setCity("Taipei");
p.setAddress(add);
List lang = new ArrayList();
lang.add("English");
lang.add("Taiwanese");
lang.add("Japanese");
p.setLanguages(lang);
try {
cache_.putObject("/test/ben", p);
}
catch(Exception ex) {
try {
cache_.putObject("/test/ben", p);
} catch (Exception ex1)
{
t1_ex = ex1;
}
}
}
};
Thread t2 = new Thread()
{
Transaction tx;
public void run()
{
try {
UserTransaction tx = getTransaction();
tx.begin();
Person p = new Person();
p.setName("Ben");
Address add = new Address();
add.setCity("Taipei");
p.setAddress(add);
cache1_.putObject("/test/ben", p);
TestingUtil.sleepThread(1000);
tx.commit();
}
catch(RollbackException rollback) {
// t2_ex = rollback;
}
catch (Exception ex) {
t2_ex = ex;
}
}
};
t1.start();
t2.start();
t1.join();
t2.join();
// t2 should rollback due to timeout while t2 should succeed
if(t1_ex != null)
fail("Thread1 failed: " + t1_ex);
if(t2_ex != null)
fail("Thread2 failed: " + t2_ex);
}
public void testConcurrentTxPutsWithRollback() throws Exception
{
Thread t1 = new Thread()
{
Transaction tx;
public void run()
{
UserTransaction tx = null;
Person p = new Person();
p.setName("Ben");
Address add = new Address();
add.setCity("Taipei");
p.setAddress(add);
List lang = new ArrayList();
lang.add("English");
lang.add("Taiwanese");
lang.add("Japanese");
p.setLanguages(lang);
try {
tx = getTransaction();
tx.begin();
cache_.putObject("/test/ben", p);
TestingUtil.sleepThread(1000);
tx.commit();
}
catch(RollbackException rollback) {
try {
tx = getTransaction();
tx.begin();
cache_.putObject("/test/ben", p);
tx.commit();
} catch (Exception ex)
{
t1_ex = ex;
ex.printStackTrace();
}
}
catch (Exception ex) {
t1_ex = ex;
ex.printStackTrace();
}
}
};
Thread t2 = new Thread()
{
Transaction tx;
public void run()
{
try {
UserTransaction tx = getTransaction();
tx.begin();
Person p = new Person();
p.setName("Ben");
Address add = new Address();
add.setCity("Taipei");
p.setAddress(add);
cache1_.putObject("/test/ben", p);
TestingUtil.sleepThread(1000);
tx.commit();
}
catch(RollbackException rollback) {
// t2_ex = rollback;
}
catch (Exception ex) {
t2_ex = ex;
}
}
};
t1.start();
t2.start();
t1.join();
t2.join();
// t2 should rollback due to timeout while t2 should succeed
if(t1_ex != null)
fail("Thread1 failed: " + t1_ex);
if(t2_ex != null)
fail("Thread2 failed: " + t2_ex);
}
public void testConcurrentTxPutsWithRollbackField() throws Exception
{
Person p = new Person();
p.setName("Ben");
Address add = new Address();
add.setCity("Taipei");
p.setAddress(add);
List lang = new ArrayList();
IdObject id1 = new IdObject("1");
lang.add(id1);
p.setLanguages(lang);
cache_.putObject("/test/ben", p);
Thread t1 = new Thread()
{
Transaction tx;
public void run()
{
UserTransaction tx = null;
List lang = null;
IdObject id2 = new IdObject("2");
IdObject id3 = new IdObject("3");
try {
Person ben = (Person)cache_.getObject("/test/ben");
lang = ben.getLanguages();
tx = getTransaction();
tx.begin();
lang.add(id2);
lang.add(id3);
lang.remove(0);
TestingUtil.sleepThread(1000);
tx.commit();
}
catch(RollbackException rollback) {
try {
tx = getTransaction();
tx.begin();
lang.add(id2);
lang.add(id3);
lang.remove(0);
tx.commit();
} catch (Exception ex)
{
t1_ex = ex;
}
}
catch (Exception ex) {
t1_ex = ex;
}
}
};
Thread t2 = new Thread()
{
Transaction tx;
public void run()
{
List lang = null;
UserTransaction tx = null;
IdObject id1 = null;
try {
Person ben = (Person)cache1_.getObject("/test/ben");
lang = ben.getLanguages();
tx = getTransaction();
tx.begin();
id1 = new IdObject("2");
lang.add(id1);
TestingUtil.sleepThread(1000);
tx.commit();
}
catch(RollbackException rollback) {
TestingUtil.sleepThread(1000);
try {
tx = getTransaction();
tx.begin();
lang.add(id1);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
catch (Exception ex) {
t2_ex = ex;
}
}
};
t1.start();
t2.start();
t1.join();
t2.join();
// t2 should rollback due to timeout while t2 should succeed
if(t1_ex != null)
fail("Thread1 failed: " + t1_ex);
if(t2_ex != null)
fail("Thread2 failed: " + t2_ex);
}
public static Test suite() throws Exception
{
return new TestSuite(NewReplicatedTxAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(NewReplicatedTxAopTest.suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/TreeCacheAopCompatibilityAopTest.java
Index: TreeCacheAopCompatibilityAopTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.util.Map;
/**
* Test for use of old TreeCacheAop api.
*
* @author Ben Wang
*/
public class TreeCacheAopCompatibilityAopTest extends TestCase
{
Log log_= LogFactory.getLog(TreeCacheAopCompatibilityAopTest.class);
PojoCache cache_;
public TreeCacheAopCompatibilityAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log_.info("setUp() ....");
String configFile = "META-INF/local-service.xml";
cache_ = new PojoCache();
cache_.setConfiguration(new XmlConfigurationParser().parseFile(configFile));
cache_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
}
// public void testDummy() {}
public void testBadFqn() throws Exception
{
log_.info("testBadFqn() ....");
Person test = new Person();
test.setName("Ben");
test.setAge(10);
cache_.putObject("/a", test);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", test, result);
result.setAge(20);
try {
cache_.putObject(InternalDelegate.JBOSS_INTERNAL, test);
fail("putObject under JBoss_Internal should fail");
} catch (IllegalArgumentException iex)
{
// ok
}
try {
cache_.removeObject(InternalDelegate.JBOSS_INTERNAL);
fail("putObject under JBoss_Internal should fail");
} catch (IllegalArgumentException iex)
{
// ok
}
}
public void testPutRemove() throws Exception
{
log_.info("testPutRemove() ....");
Person test = new Person();
test.setName("Ben");
test.setAge(10);
cache_.putObject("/a", test);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", test, result);
result.setAge(20);
cache_.removeObject("/a");
assertNull("Object should be null ", cache_.getObject("/a"));
assertEquals("Age should be updated as ", 20, test.getAge());
}
public void testPutRemoveNodeExistence() throws Exception
{
log_.info("testPutRemove() ....");
Person test = new Person();
test.setName("Ben");
test.setAge(10);
cache_.putObject("/a", test);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", test, result);
result.setAge(20);
cache_.removeObject("/a");
assertNull("Object should be null ", cache_.getObject("/a"));
assertEquals("Age should be updated as ", 20, test.getAge());
assertNull("DataNode should not exisit ", cache_.get("/a"));
}
public void testFindObjects() throws Exception
{
log_.info("testFindObjects() ....");
Map map = cache_.findObjects("/");
assertEquals("Objects size should be ", 0, map.size());
Person ben = new Person();
ben.setName("Ben");
ben.setAge(10);
cache_.putObject("/a/b/c", ben);
cache_.putObject("/e", ben); // multiple keys, same pojo
Person joe = new Person();
joe.setName("Joe");
joe.setAge(10);
cache_.putObject("/f/joe", joe);
map = cache_.findObjects("/");
assertEquals("Objects size should be ", 3, map.size());
map = cache_.findObjects("/a");
assertEquals("Objects size should be ", 1, map.size());
cache_.removeObject("/e");
map = cache_.findObjects("/");
assertEquals("Objects size should be ", 2, map.size());
}
public static Test suite() throws Exception
{
return new TestSuite(TreeCacheAopCompatibilityAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(TreeCacheAopCompatibilityAopTest.suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/ReplicatedSerializableAopTest.java
Index: ReplicatedSerializableAopTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.test.CacheObject;
import org.jboss.cache.aop.test.NonSerializableObject;
import org.jboss.cache.aop.test.Student;
import org.jboss.cache.factories.XmlConfigurationParser;
import javax.naming.Context;
import java.util.Properties;
/**
* New NewReplicatedAopTest that doesn't use TreeCacheAopTester.
*
* @author Ben Wang
*/
public class ReplicatedSerializableAopTest extends TestCase
{
Log log_= LogFactory.getLog(ReplicatedSerializableAopTest.class);
PojoCache cache_;
PojoCache cache1_;
public ReplicatedSerializableAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
cache_ = new PojoCache();
cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
cache1_ = new PojoCache();
cache1_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
cache_.start();
cache1_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
cache1_.stop();
}
public void testSeriazableSubObject() throws Exception
{
log_.info("testSerializableSubObject() ....");
Student ben = new Student();
ben.setName("Ben");
ben.setYear("9th grade");
CacheObject co1 = new CacheObject("1");
ben.setCO1(co1);
CacheObject co2 = new CacheObject("2");
ben.setCO2(co2);
cache_.putObject("/test", ben);
Student be = (Student)cache1_.getObject("/test");
assertNotNull("CacheObject should not be null", be.getCO1());
assertNotNull("CacheObject should not be null", be.getCO2());
assertEquals("1", be.getCO1().getId());
assertEquals("2", be.getCO2().getId());
}
/**
* We don;t currently support Serializable with relationship now.
* @throws Exception
*/
public void XtestSeriazableSubObjectRelation() throws Exception
{
log_.info("testSerializableSubObjectRelation() ....");
Student ben = new Student();
ben.setName("Ben");
ben.setYear("9th grade");
CacheObject co1 = new CacheObject("1");
ben.setCO1(co1);
Student elynne = new Student();
elynne.setName("Elynne");
elynne.setYear("9th grade");
// Same co object
elynne.setCO1(co1);
cache_.putObject("/ben", ben);
cache_.putObject("/elynne", elynne);
Student be = (Student)cache1_.getObject("/ben");
Student el = (Student)cache1_.getObject("/elynne");
assertNotNull("CacheObject should not be null", be.getCO1());
assertNotNull("CacheObject should not be null", el.getCO1());
assertEquals("Both co object should be the same", be.getCO1().getId(), el.getCO1().getId());
assertTrue("Both co should be the same reference", be.getCO1() == el.getCO1());
}
public void testPlainSeriazable() throws Exception
{
log_.info("testPlainSerializable() ....");
// First the flag is set to false
CacheObject co = new CacheObject("1");
cache_.putObject("/test", co);
CacheObject co1 = (CacheObject)cache1_.getObject("/test");
assertNotNull("co on remote cache should not be null", co1);
assertEquals("co should be the same", co.getId(), co1.getId());
}
public void testNonSeriazable1() throws Exception
{
log_.info("testNonSerializable1() ....");
// First the flag is set to false
NonSerializableObject nso = new NonSerializableObject();
nso.setId("2");
try {
cache_.putObject("/test", nso);
fail("should fail becuase vo is not Serializable");
}
catch (RuntimeException e) {
}
// Then we set the flag
cache_.setMarshallNonSerializable(true);
cache1_.setMarshallNonSerializable(true);
cache_.putObject("/test", nso);
NonSerializableObject nso1 = (NonSerializableObject)cache1_.getObject("/test");
assertNotNull("nso on remote cache should not be null", nso1);
assertEquals("VO should be the same", nso, nso1);
}
public void testNonSeriazable2() throws Exception
{
log_.info("testNonSerializable2() ....");
// First the flag is set to false
NonSerializableObject nso = new NonSerializableObject();
nso.setId("2");
// Then we set the flag
cache_.setMarshallNonSerializable(true);
cache1_.setMarshallNonSerializable(true);
cache_.putObject("/test", nso);
NonSerializableObject nso1 = (NonSerializableObject)cache1_.getObject("/test");
assertNotNull("nso on remote cache should not be null", nso1);
assertEquals("VO should be the same", nso, nso1);
nso1 = new NonSerializableObject();
nso1.setId("4");
cache1_.putObject("/test", nso1);
nso = (NonSerializableObject)cache_.getObject("/test");
assertNotNull("nso on remote cache should not be null", nso);
assertEquals("VO should be the same", nso, nso1);
}
public static Test suite() throws Exception
{
return new TestSuite(ReplicatedSerializableAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(ReplicatedSerializableAopTest.suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/ReplicatedPutWithBulkRemoveAopTest.java
Index: ReplicatedPutWithBulkRemoveAopTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.factories.XmlConfigurationParser;
import javax.naming.Context;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Test for putObject with existing pojo for bulkd remove for speed.
*
* @author Ben Wang
*/
public class ReplicatedPutWithBulkRemoveAopTest extends TestCase
{
Log log_= LogFactory.getLog(ReplicatedPutWithBulkRemoveAopTest.class);
PojoCache cache_;
PojoCache cache1_;
public ReplicatedPutWithBulkRemoveAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
cache_ = new PojoCache();
cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
cache1_ = new PojoCache();
cache1_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
cache_.start();
cache1_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
cache1_.stop();
}
public void testPutPutLocal() throws Exception
{
log_.info("testPutPut() ....");
Person test = new Person();
test.setName("Ben");
test.setAge(10);
Address addr = new Address();
addr.setZip(95123);
addr.setCity("Sunnyvale");
test.setAddress(addr);
cache_.putObject("/a", test);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", test, result);
Person joe = new Person();
joe.setName("joe");
joe.setAge(20);
cache_.putObject("/a", joe);
Person joe1 = (Person)cache_.getObject("/a");
assertEquals("Age should be ", 20, joe1.getAge());
assertEquals("Age should be ", 10, result.getAge());
assertEquals("Zip should be ", 95123, result.getAddress().getZip());
// Try to re-use the pojo
cache_.putObject("/a", test);
Person result1 = (Person)cache_.getObject("/a");
assertEquals("Zip should be ", 95123, result1.getAddress().getZip());
}
public void testPutPut() throws Exception
{
log_.info("testPutPut() ....");
Person test = new Person();
test.setName("Ben");
test.setAge(10);
Address addr = new Address();
addr.setZip(95123);
addr.setCity("Sunnyvale");
test.setAddress(addr);
cache_.putObject("/a", test);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", test, result);
Person result1 = (Person)cache1_.getObject("/a");
assertEquals("Age should be ", 10, result1.getAge());
assertEquals("Zip should be ", 95123, result1.getAddress().getZip());
Person joe = new Person();
joe.setName("joe");
joe.setAge(20);
cache_.putObject("/a", joe);
Person joe1 = (Person)cache_.getObject("/a");
assertEquals("Age should be ", 20, joe1.getAge());
assertEquals("Age should be ", 10, result.getAge());
assertEquals("Zip should be ", 95123, result.getAddress().getZip());
}
Address getAddress(String city)
{
Address addr = new Address();
addr.setCity(city);
addr.setZip(95123);
addr.setStreet("Sunnyvale");
return addr;
}
public void testPutCollection() throws Exception
{
log_.info("testPutCollection() ....");
Person p1 = new Person();
p1.setName("Ben");
p1.setAge(10);
p1.setAddress(getAddress("Sunnyvale"));
List lang = new ArrayList();
lang.add(getAddress("Taipei"));
lang.add(getAddress("Tainan"));
p1.setLanguages(lang);
cache_.putObject("/a", p1);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", p1, result);
Person result1 = (Person)cache1_.getObject("/a");
assertEquals("Age should be ", 10, result1.getAge());
assertEquals("Zip should be ", 95123, result1.getAddress().getZip());
Person p2 = new Person();
p2.setName("joe");
p2.setAge(20);
cache_.putObject("/a", p2);
Person joe1 = (Person)cache_.getObject("/a");
assertEquals("Age should be ", 20, joe1.getAge());
// put p1 again
cache_.putObject("/a", p1);
// remove p1
Person p3 = (Person)cache_.removeObject("/a");
assertEquals("Zip should be ", 95123, p3.getAddress().getZip());
assertEquals("Age should be ", 10, result.getAge());
assertEquals("Zip should be ", 95123, result.getAddress().getZip());
}
public static Test suite() throws Exception
{
return new TestSuite(ReplicatedPutWithBulkRemoveAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(ReplicatedPutWithBulkRemoveAopTest.suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/CacheLoaderTestsBase.java
Index: CacheLoaderTestsBase.java
===================================================================
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.Fqn;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.loader.CacheLoader;
import javax.transaction.Transaction;
/**
* Commons tests for all CacheLoaders. Aop version.
*
* @author bwang
* @version $Id: CacheLoaderTestsBase.java,v 1.1 2006/10/31 08:01:12 bwang Exp $
*/
abstract public class CacheLoaderTestsBase extends TestCase
{
PojoCache cache;
CacheLoader loader = null;
Transaction tx = null;
static final Fqn FQN = new Fqn("key");
protected void setUp() throws Exception
{
super.setUp();
cache = new PojoCache();
cache.getConfiguration().setCacheMode("local");
configureCache();
cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
cache.create();
cache.start();
loader = cache.getCacheLoader();
}
abstract protected void configureCache() throws Exception;
protected void tearDown() throws Exception
{
super.tearDown();
if (tx != null)
{
try
{
tx.commit();
}
catch (Throwable e)
{
e.printStackTrace();
}
}
cache.remove("/");
loader.remove(Fqn.fromString("/"));
cache.stop();
cache.destroy();
}
protected void addDelay()
{
; // returns immediately in this case. Subclasses may override where a delay is needed.
}
public void testEvictionWithCacheLoader() throws Exception
{
cache.putObject("/first/second/third", "val1"); // stored in cache loader
cache.evict(Fqn.fromString("/first/second/third")); // removes node, because there are no children
addDelay();
assertFalse(cache.exists("/first/second/third"));
assertTrue(cache.exists("/first/second"));
assertTrue(cache.exists("/first"));
String val = (String) cache.getObject("/first/second/third"); // should be loaded from cache loader
assertEquals("val1", val);
assertTrue(cache.exists("/first/second/third"));
assertTrue(cache.exists("/first/second"));
assertTrue(cache.exists("/first"));
}
public void testPojoEvictionWithCacheLoader() throws Exception
{
Person test = new Person();
test.setName("Ben");
test.setAge(10);
cache.putObject("/first/second/third", test); // stored in cache loader
cache.evict(Fqn.fromString("/first/second/third")); // removes node, because there are no children
addDelay();
assertFalse(cache.exists("/first/second/third"));
assertTrue(cache.exists("/first/second"));
assertTrue(cache.exists("/first"));
Person val = (Person) cache.getObject("/first/second/third"); // should be loaded from cache loader
assertNotNull(val);
assertEquals(test.getName(), val.getName());
assertTrue(cache.exists("/first/second/third"));
assertTrue(cache.exists("/first/second"));
assertTrue(cache.exists("/first"));
}
public void testPojoRemoveWithCacheLoader() throws Exception
{
Person test = new Person();
test.setName("Ben");
test.setAge(10);
cache.putObject("/first/second/third", test); // stored in cache loader
cache.remove(Fqn.fromString("/first/second/third")); // removes node, because there are no children
addDelay();
assertFalse(cache.exists("/first/second/third"));
assertTrue(cache.exists("/first/second"));
assertTrue(cache.exists("/first"));
Person val = (Person) cache.getObject("/first/second/third"); // should be loaded from cache loader
assertNull(val);
}
public static Test suite()
{
return new TestSuite(CacheLoaderTestsBase.class);
}
public static void main(String[] args)
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/NonAspectizedAopTest.java
Index: NonAspectizedAopTest.java
===================================================================
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.test.SerializedAddress;
import org.jboss.cache.factories.XmlConfigurationParser;
/**
* Test for Serializable pojos
*
* @author Ben Wang
*/
public class NonAspectizedAopTest extends TestCase
{
Log log_=LogFactory.getLog(NonAspectizedAopTest.class);
PojoCache cache_;
public NonAspectizedAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log_.info("setUp() ....");
String configFile = "META-INF/local-service.xml";
cache_ = new PojoCache();
cache_.setConfiguration(new XmlConfigurationParser().parseFile(configFile));
cache_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
}
// public void testDummy() {}
public void testPutPrimitive() throws Exception
{
log_.info("testPutPrimitive() ....");
String test = "test";
cache_.putObject("/a", test);
String result = (String)cache_.getObject("/a");
assertEquals("test string ", "test", result);
cache_.removeObject("/a");
assertNull("Object should be null ", cache_.getObject("/a"));
}
public void testPutSerializable() throws Exception
{
log_.info("testPutSerializable() ....");
SerializedAddress test = new SerializedAddress();
test.setCity("Sunnyvale");
test.setZip(94086);
cache_.putObject("/a", test);
SerializedAddress result = (SerializedAddress)cache_.getObject("/a");
assertEquals("test SerializedAddress ", test, result);
cache_.removeObject("/a");
assertNull("Object should be null ", cache_.getObject("/a"));
}
public static Test suite() throws Exception
{
return new TestSuite(NonAspectizedAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/ReplicatedTxAopTest.java
Index: ReplicatedTxAopTest.java
===================================================================
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.transaction.DummyTransactionManager;
import org.jboss.cache.misc.TestingUtil;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.*;
import java.util.Properties;
//import org.jboss.test.JBossTestCase;
/**
* LocalTestCase.java that uses standalone aop package.
* <p/>
* <p/>
* Created: Mon May 05 17:30:11 2003
*/
public class ReplicatedTxAopTest extends TestCase
{
// Category log = getLog();
Log log=LogFactory.getLog(ReplicatedTxAopTest.class);
TreeCacheAopTester tester, tester1;
final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
DummyTransactionManager tx_mgr;
Throwable t1_ex, t2_ex;
long start=0;
public ReplicatedTxAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
String configFile = "META-INF/replSync-service.xml";
tester = new TreeCacheAopTester(configFile);
tester1 = new TreeCacheAopTester(configFile);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
tx_mgr = DummyTransactionManager.getInstance();
t1_ex = t2_ex = null;
}
protected void tearDown() throws Exception
{
super.tearDown();
tester.stop();
tester1.stop();
tester = null;
tester1 = null;
DummyTransactionManager.destroy();
}
// public void testDummy() {}
public void testSetup()
{
log.info("testSetup() ....");
try {
tester.testSetup();
tester1.testSetup();
} catch (Exception ex) {
ex.printStackTrace();
fail("testSetup(): fails. " + ex.toString());
}
}
UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
{
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.cache.transaction.DummyContextFactory");
return (UserTransaction)new InitialContext(prop).lookup("UserTransaction");
}
public void testSimple() throws Exception
{
log.info("testSimple() ....");
UserTransaction tx = getTransaction();
tx.begin();
tester.createPerson("/person/test1", "Harald Gliebe", 32);
tx.commit();
tx.begin();
tester.setName("/person/test1", "Benoit");
tx.commit();
assertEquals((Object) "Benoit", (Object) tester.getName("/person/test1"));
assertEquals((Object) "Benoit", (Object) tester1.getName("/person/test1"));
tx.begin();
tester.setAge("/person/test1", 61);
tx.commit();
assertEquals(61, tester.getAge("/person/test1"));
assertEquals(61, tester1.getAge("/person/test1"));
}
public void testModification() throws Exception
{
UserTransaction tx = getTransaction();
tx.begin();
tester.createPerson("/person/test2", "Harald", 32);
tester.setName("/person/test2", "Harald Gliebe");
tx.commit();
tx.begin();
tester1.setName("/person/test2", "Benoit");
tx.commit();
assertEquals((Object) tester.getName("/person/test2"), (Object) "Benoit");
assertEquals((Object) tester1.getName("/person/test2"), (Object) "Benoit");
tx.begin();
tester1.setName("/person/test2", "Harald");
tx.rollback();
assertEquals((Object) tester.getName("/person/test2"), (Object) "Benoit");
assertEquals((Object) tester1.getName("/person/test2"), (Object) "Benoit");
tester.removePerson("/person/test2");
}
public void testConcurrentPuts() throws Exception
{
tester.setSyncCommitPhase(true);
Thread t1 = new Thread()
{
Transaction tx;
public void run()
{
try {
tester.createPerson("/person/test6", "p6", 50);
UserTransaction tx = getTransaction();
tx.begin();
tester.addLanguage("/person/test6", "German");
TestingUtil.sleepThread(4000);
tx.commit();
}
catch(RollbackException rollback) {
;
}
catch (Exception ex) {
t1_ex = ex;
}
}
};
Thread t2 = new Thread()
{
Transaction tx;
public void run()
{
try {
TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
UserTransaction tx = getTransaction();
tx.begin();
tester.addLanguage("/person/test6", "English");
tx.commit();
}
catch(RollbackException rollback) {
;
}
catch (Exception ex) {
t2_ex = ex;
}
}
};
t1.start();
t2.start();
t1.join();
t2.join();
// t2 should rollback due to timeout while t2 should succeed
if(t2_ex != null)
fail("Thread1 failed: " + t2_ex);
if(t1_ex != null)
fail("Thread2 failed: " + t1_ex);
int size = tester.getLanguagesSize("/person/test6");
assertEquals("number of languages should be 2, but is " + size + " (" +
tester.getLanguages("/person/test6") + ")",
2, size);
System.out.println("tester: " + tester.getLanguages("/person/test6"));
size = tester1.getLanguagesSize("/person/test6");
assertEquals("number of languages should be 2, but is " + size + " (" +
tester.getLanguages("/person/test6") + ")",
2, size);
System.out.println("tester1: " + tester1.getLanguages("/person/test6"));
}
void log(String s) {
long now;
if(start == 0)
start=System.currentTimeMillis();
now=System.currentTimeMillis();
System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
}
public static Test suite() throws Exception
{
return new TestSuite(ReplicatedTxAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/ReplicatedObjectGraphAopTest.java
Index: ReplicatedObjectGraphAopTest.java
===================================================================
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.Link;
import org.jboss.cache.aop.test.NodeManager;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.misc.TestingUtil;
import java.util.ArrayList;
import java.util.List;
/**
* Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
*
* @author Ben Wang
*/
public class ReplicatedObjectGraphAopTest extends TestCase
{
Log log = LogFactory.getLog(ReplicatedObjectGraphAopTest.class);
PojoCache cache1;
PojoCache cache2;
public ReplicatedObjectGraphAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
cache1 = createCache("CacheGroup");
cache2 = createCache("CacheGroup");
}
protected void tearDown() throws Exception
{
super.tearDown();
cache1.remove("/");
cache1.stop();
cache2.stop();
}
private PojoCache createCache(String name) throws Exception
{
PojoCache tree = new PojoCache();
tree.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
tree.getConfiguration().setClusterName(name);
tree.create();
tree.start();
return tree;
}
// public void testDummy() {}
protected Person createPerson(String name, int age)
{
Person p = new Person();
p.setName(name);
p.setAge(age);
return p;
}
private void stage0() throws Exception
{
cache1.putObject("/person/joe", createPerson("Joe Black", 31));
Person joe = (Person) cache1.getObject("/person/joe");
cache1.putObject("/person/ben", createPerson("Ben Hogan", 51));
Person ben = (Person) cache1.getObject("/person/ben");
Address addr = new Address();
addr.setStreet("123 Albert Ave.");
addr.setCity("Sunnyvale");
addr.setZip(94087);
cache1.putObject("/address", addr);
// They share the sub-object: address
joe.setAddress(addr);
ben.setAddress(addr);
assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity());
assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
}
private void stage1() throws Exception
{
cache1.putObject("/person/joe", createPerson("Joe Black", 31));
Person joe = (Person) cache1.getObject("/person/joe");
cache1.putObject("/person/ben", createPerson("Ben Hogan", 51));
Person ben = (Person) cache1.getObject("/person/ben");
Address addr = new Address();
addr.setStreet("123 Albert Ave.");
addr.setCity("Sunnyvale");
addr.setZip(94087);
// They share the sub-object: address
joe.setAddress(addr);
ben.setAddress(addr);
assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity());
assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
}
private void stage2(PojoCache cache) throws Exception
{
//
cache.removeObject("/person/joe");
Person ben = (Person) cache.getObject("/person/ben");
assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
Address addr = ben.getAddress();
addr.setCity("Santa Clara");
assertEquals("Ben's address should be changed ", "Santa Clara", ben.getAddress().getCity());
}
/**
* Test whether repeated update on the ref count will change the replicated aop instances
*
* @throws Exception
*/
public void XtestCheckReplInstance() throws Exception
{
log.info("testCheckReplInstance() ...");
stage0();
TestingUtil.sleepThread(100);
Person joe = (Person) cache1.getObject("/person/joe");
Person ben = (Person) cache1.getObject("/person/ben");
assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(),
ben.getAddress().getCity());
Address joe1 = (Address) cache2.getObject("/address");
assertEquals("Ben's address should not be changed ", joe.getAddress().getCity(), joe1.getCity());
cache1.removeObject("/person/ben");
Address joe2 = (Address) cache2.getObject("/address");
assertEquals("Joe's reference should be the same.", joe1, joe2);
}
public void testRefCountCheckRepl() throws Exception
{
log.info("testRefCountCheckRepl() ...");
stage1();
TestingUtil.sleepThread(100);
Person joe = (Person) cache1.getObject("/person/joe");
Person ben = (Person) cache1.getObject("/person/ben");
assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(),
ben.getAddress().getCity());
TestingUtil.sleepThread(100);
stage2(cache2);
assertEquals("Ben's address should be changed on cache1 as well ", "Santa Clara", ben.getAddress().getCity());
cache2.removeObject("/person/ben");
}
public void testCircularReference1() throws Exception
{
// try {Thread.sleep(10000); } catch (Exception e) {};
log.info("testCircularReference1() ...");
Link parent = new Link("parent");
Link child = new Link("child");
parent.setLink(child);
child.setLink(parent);
cache1.putObject("/link/parent", parent);
TestingUtil.sleepThread(100);
assertEquals("parent", ((Link) cache1.getObject("/link/parent")).getName());
assertEquals("child", ((Link) cache1.getObject("/link/parent")).getLink().getName());
assertEquals("parent", ((Link) cache2.getObject("/link/parent")).getName());
assertEquals("child", ((Link) cache2.getObject("/link/parent")).getLink().getName());
((Link) cache2.getObject("/link/parent")).setLink(null);
assertNull("Child should be null", ((Link) cache2.getObject("/link/parent")).getLink());
Link link = (Link) cache1.removeObject("/link/parent");
assertNotNull("Link should not be null ", link);
System.out.println("Link: " + link);
}
public void testCircularReference2() throws Exception
{
// try {Thread.sleep(10000); } catch (Exception e) {};
log.info("testCircularReference2() ...");
Link parent = new Link("parent");
Link child = new Link("child");
cache1.putObject("/link/parent", parent);
parent.setLink(child);
child.setLink(parent);
assertEquals("parent", ((Link) cache1.getObject("/link/parent")).getName());
assertEquals("child", ((Link) cache1.getObject("/link/parent")).getLink().getName());
assertEquals("parent", ((Link) cache2.getObject("/link/parent")).getName());
assertEquals("child", ((Link) cache2.getObject("/link/parent")).getLink().getName());
((Link) cache2.getObject("/link/parent")).setLink(null);
assertNull("Child should be null", ((Link) cache2.getObject("/link/parent")).getLink());
Link link = (Link) cache1.removeObject("/link/parent");
assertNotNull("Link should not be null ", link);
}
public void testCircularReference3() throws Exception
{
// try {Thread.sleep(10000); } catch (Exception e) {};
log.info("testCircularReference3() ...");
Link parent = new Link("parent");
Link child = new Link("child");
cache1.putObject("/link/parent", parent);
cache1.putObject("/link/child", child);
TestingUtil.sleepThread(100);
parent.setLink(child);
child.setLink(parent);
Link p1 = (Link) cache1.getObject("/link/parent");
Link c1 = (Link) cache1.getObject("/link/child");
assertEquals("parent", p1.getName());
assertEquals("child", p1.getLink().getName());
assertEquals("child", c1.getName());
assertEquals("parent", c1.getLink().getName());
Link p2 = (Link) cache1.getObject("/link/parent");
Link c2 = (Link) cache1.getObject("/link/child");
assertEquals("parent", p2.getName());
assertEquals("child", p2.getLink().getName());
assertEquals("child", c2.getName());
assertEquals("parent", c2.getLink().getName());
p2.setLink(null);
assertNull("Child should be null", p2.getLink());
Link link = (Link) cache1.removeObject("/link/parent");
assertNotNull("Link should not be null ", link);
}
/**
* Setting the circular relationship and also as a shared object.
*
* @throws Exception
*/
public void testCircularReference4() throws Exception
{
// try {Thread.sleep(10000); } catch (Exception e) {};
log.info("testCircularReference3() ...");
Link parent = new Link("parent");
Link child = new Link("child");
parent.setLink(child);
child.setLink(parent);
List list = new ArrayList();
list.add(parent);
cache1.putObject("/list", list);
cache1.putObject("/alias", list);
TestingUtil.sleepThread(100);
List list1 = (List) cache2.getObject("/list");
List list2 = (List) cache2.getObject("/alias");
assertEquals("parent", ((Link) list1.get(0)).getName());
assertEquals("child", ((Link) list2.get(0)).getLink().getName());
}
public void testCircularAndSharedReferences() throws Exception
{
log.info("testCircularAndSharedReferences() ...");
NodeManager pm_ = new NodeManager();
pm_.setRootNode("root");
pm_.addNode("root", "kanto");
pm_.addNode("root.kanto", "tokyo");
pm_.addNode("root.kanto", "kanagawa");
cache1.putObject("/propagation", pm_);
assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN());
pm_.addNode("root.kanto.tokyo", "hadanshita");
assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
NodeManager pm2_ = (NodeManager) cache2.getObject("/propagation");
assertEquals("kanagawa", pm2_.findNode("root.kanto.kanagawa").getNodeRDN());
assertEquals("hadanshita", pm2_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
/*
System.out.println("\n\n");
System.out.println("---------------------------------------------");
System.out.println("Initial pm state");
System.out.println("---------------------------------------------");
pm_.printNodes();
System.out.println("\n\n");
System.out.println("---------------------------------------------");
System.out.println("Initial cache content");
System.out.println(cache_.printDetails());
System.out.println("---------------------------------------------");
*/
}
public void testRemoveObject1() throws Exception
{
log.info("testRemoveObject1() ...");
cache1.putObject("/person/joe", createPerson("Joe Black", 31));
Person joe = (Person) cache1.getObject("/person/joe");
cache1.putObject("/person/ben", createPerson("Ben Hogan", 51));
Person ben = (Person) cache1.getObject("/person/ben");
Address addr = new Address();
addr.setStreet("123 Albert Ave.");
addr.setCity("Sunnyvale");
addr.setZip(94087);
// They share the sub-object: address
log.info("testMultipleReference(): set Joe address");
joe.setAddress(addr);
log.info("testMultipleReference(): set Ben address");
ben.setAddress(addr);
Address add1 = (Address) ((Person) cache2.getObject("/person/joe")).getAddress();
Address add2 = (Address) ((Person) cache2.getObject("/person/ben")).getAddress();
assertEquals(add1.getCity(), add2.getCity());
addr.setCity("Santa Clara");
assertEquals(add1.getCity(), add2.getCity());
// Remove pojo joe will relocate the address field to ben's
cache2.removeObject("/person/joe");
add2 = (Address) ((Person) cache2.getObject("/person/ben")).getAddress();
System.out.println("*** Cache content *** " + cache2.printDetails());
assertEquals("City ", "Santa Clara", add2.getCity());
}
public void testRemoveObject2() throws Exception
{
log.info("testRemoveObject2() ...");
cache1.putObject("/person/joe", createPerson("Joe Black", 31));
Person joe = (Person) cache1.getObject("/person/joe");
cache1.putObject("/person/ben", createPerson("Ben Hogan", 51));
Person ben = (Person) cache1.getObject("/person/ben");
cache1.putObject("/person/john", createPerson("John Daly", 41));
Person john = (Person) cache1.getObject("/person/john");
Address addr = new Address();
addr.setStreet("123 Albert Ave.");
addr.setCity("Sunnyvale");
addr.setZip(94087);
Address addr1 = new Address();
addr1.setStreet("123 Albert Ave.");
addr1.setCity("San Jose");
addr1.setZip(94087);
// They share the sub-object: address
log.info("testMultipleReference(): set Joe address");
joe.setAddress(addr);
log.info("testMultipleReference(): set Ben address");
ben.setAddress(addr);
john.setAddress(addr);
Address add1 = (Address) ((Person) cache2.getObject("/person/joe")).getAddress();
Address add2 = (Address) ((Person) cache2.getObject("/person/ben")).getAddress();
assertEquals(add1.getCity(), add2.getCity());
addr.setCity("Santa Clara");
assertEquals(add1.getCity(), add2.getCity());
// Remove pojo joe will relocate the address field to ben's
joe.setAddress(addr1);
add2 = (Address) ((Person) cache2.getObject("/person/joe")).getAddress();
assertEquals("City ", "San Jose", add2.getCity());
add2 = (Address) ((Person) cache2.getObject("/person/ben")).getAddress();
assertEquals("City ", "Santa Clara", add2.getCity());
add2 = (Address) ((Person) cache2.getObject("/person/john")).getAddress();
assertEquals("City ", "Santa Clara", add2.getCity());
}
public static Test suite() throws Exception
{
return new TestSuite(ReplicatedObjectGraphAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/LocalConcurrentTest.java
Index: LocalConcurrentTest.java
===================================================================
/*
*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.CacheException;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.lock.IsolationLevel;
import org.jboss.cache.lock.LockStrategyFactory;
import org.jboss.cache.lock.UpgradeException;
import org.jboss.cache.misc.TestingUtil;
import org.jboss.cache.transaction.DummyTransactionManager;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Random;
/**
* Local mode stress test for PojoCache. Test getObject and putObject under load
* and concurrency.
*
* @version $Revision: 1.1 $
* @author<a href="mailto:bwang at jboss.org">Ben Wang</a> December 2004
*/
public class LocalConcurrentTest extends TestCase
{
static PojoCache cache_;
Configuration.CacheMode cachingMode_ = Configuration.CacheMode.LOCAL;
Properties p_;
String oldFactory_ = null;
final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
static ArrayList nodeList_;
static final int depth_ = 2;
static final int children_ = 2;
static final int MAX_LOOP = 100;
static final int SLEEP_TIME = 50;
static Exception thread_ex = null;
UserTransaction tx_ = null;
public LocalConcurrentTest(String name)
{
super(name);
}
public void setUp() throws Exception
{
super.setUp();
oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
DummyTransactionManager.getInstance();
if (p_ == null)
{
p_ = new Properties();
p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
}
tx_ = (UserTransaction) new InitialContext(p_).lookup("UserTransaction");
initCaches(Configuration.CacheMode.LOCAL);
nodeList_ = nodeGen(depth_, children_);
log("LocalConcurrentTestCase: cacheMode=TRANSIENT, one cache");
}
public void tearDown() throws Exception
{
super.tearDown();
thread_ex = null;
DummyTransactionManager.destroy();
destroyCaches();
if (oldFactory_ != null)
{
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
oldFactory_ = null;
}
}
void initCaches(Configuration.CacheMode caching_mode) throws Exception
{
cachingMode_ = caching_mode;
cache_ = new PojoCache();
cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-service.xml"));
cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
cache_.create();
cache_.start();
}
void destroyCaches() throws Exception
{
cache_.stop();
cache_ = null;
}
protected void setLevelRW()
{
log("set lock level to RWUpgrade ...");
LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
cache_.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
}
protected void setLevelSerial()
{
log("set lock level to SimpleLock ...");
LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE);
cache_.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
}
public void testAll_RWLock() throws Exception
{
try
{
setLevelRW();
all();
}
catch (UpgradeException ue)
{
log("Upgrade exception. Can ingore for repeatable read. " + ue);
}
catch (Exception ex)
{
log("Exception: " + ex);
throw ex;
}
}
public void XtestAll_SimpleLock() throws Exception
{
setLevelSerial();
all();
}
private void all() throws Exception
{
RunThread t1 = new RunThread(1);
RunThread t2 = new RunThread(2);
RunThread t3 = new RunThread(3);
RunThread t4 = new RunThread(4);
t1.start();
TestingUtil.sleepThread(100);
t2.start();
TestingUtil.sleepThread(100);
t3.start();
TestingUtil.sleepThread(100);
t4.start();
t1.join(60000); // wait for 20 secs
t2.join(60000); // wait for 20 secs
t3.join(60000); // wait for 20 secs
t4.join(60000); // wait for 20 secs
if (thread_ex != null)
throw thread_ex;
}
class RunThread extends Thread
{
final int seed_;
Random random_;
Person person_;
public RunThread(int seed)
{
seed_ = seed;
random_ = new Random(seed);
}
private void createPerson()
{
person_ = new Person();
person_.setName("Ben");
person_.setAge(18);
ArrayList lang = new ArrayList();
lang.add("English");
lang.add("French");
lang.add("Mandarin");
person_.setLanguages(lang);
Address addr = new Address();
addr.setZip(95123);
addr.setStreet("Almeria");
addr.setCity("San Jose");
person_.setAddress(addr);
}
public void run()
{
try
{
_run();
}
catch (Exception e)
{
thread_ex = e;
}
}
/**
*/
public void _run() throws Exception
{
for (int loop = 0; loop < MAX_LOOP; loop++)
{
createPerson(); // create a new person instance every loop.
TestingUtil.sleepThread(random_.nextInt(50));
try
{
op1();
}
catch (CacheException ex)
{
throw ex;
}
}
}
// Operation 1
private void op1() throws CacheException
{
int i = random_.nextInt(nodeList_.size() - 1);
if (i == 0) return; // it is meaningless to test root
String node = (String) nodeList_.get(i) + "/aop";
cache_.putObject(node, person_);
TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
// getObject is not thread safe since there is no way to lock the fqn atomically without triggering
// the replication.
// cache_.getObject(node);
TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
cache_.removeObject(node);
}
}
/**
* Generate the tree nodes quasi-exponentially. I.e., depth is the level
* of the hierarchy and children is the number of children under each node.
* This strucutre is used to add, get, and remove for each node.
*/
private ArrayList nodeGen(int depth, int children)
{
ArrayList strList = new ArrayList();
ArrayList oldList = new ArrayList();
ArrayList newList = new ArrayList();
// Skip root node
oldList.add("/");
newList.add("/");
strList.add("/");
while (depth > 0)
{
// Trying to produce node name at this depth.
newList = new ArrayList();
for (int i = 0; i < oldList.size(); i++)
{
for (int j = 0; j < children; j++)
{
String tmp = (String) oldList.get(i);
tmp += Integer.toString(j);
if (depth != 1)
{
tmp += "/";
}
newList.add(tmp);
}
}
strList.addAll(newList);
oldList = newList;
depth--;
}
// let's prune out root node
for (int i = 0; i < strList.size(); i++)
{
if (strList.get(i).equals("/"))
{
strList.remove(i);
break;
}
}
log("Nodes generated: " + strList.size());
return strList;
}
public static Test suite() throws Exception
{
return new TestSuite(LocalConcurrentTest.class);
}
private static void log(String str)
{
System.out.println("Thread: " + Thread.currentThread() + ": " + str);
// System.out.println(str);
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/TreeCacheAopTester.java
Index: TreeCacheAopTester.java
===================================================================
/*
*/
package org.jboss.cache.aop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.aop.Advised;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.aop.test.Student;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Proxy to the PojoCache.
* The AOP framework requires that classes are loaded by special classloaders (e.g UCL).
*/
public class TreeCacheAopTester
{
PojoCache cache;
Log logger_ = LogFactory.getLog(TreeCacheAopTester.class);
public TreeCacheAopTester(String configFile)
{
try
{
/*
cache=new PojoCache(cluster_name, props, 10000);
cache.start();
cache2=new PojoCache(cluster_name, props, 10000);
cache2.start();
*/
cache = new PojoCache();
XmlConfigurationParser parser = new XmlConfigurationParser();
Configuration c = parser.parseFile(configFile);
cache.setConfiguration(c);
cache.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void stop()
{
cache.stop();
}
public void testSetup()
{
Person p = new Person();
if (!(p instanceof Advised))
{
logger_.error("testSetup(): p is not an instance of Advised");
throw new RuntimeException("Person must be advised!");
}
Address a = new Address();
if (!(a instanceof Advised))
{
logger_.error("testSetup(): a is not an instance of Advised");
throw new RuntimeException("Address must be advised!");
}
}
public void setSyncCommitPhase(boolean bool)
{
cache.getConfiguration().setSyncCommitPhase(bool);
}
public void createPerson(String key, String name, int age)
{
Person p = new Person();
p.setName(name);
p.setAge(age);
p.setAddress(new Address());
try
{
cache.putObject(key, p);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public void removePerson(String key)
{
try
{
cache.removeObject(key);
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
Object getPerson(String key)
{
try
{
return (Person) cache.getObject(key);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public void createStudent(String key, String name, int age, String year)
{
Student p = new Student();
p.setName(name);
p.setAge(age);
p.setAddress(new Address());
p.setYear(year);
try
{
cache.putObject(key, p);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public void removeStudent(String key)
{
try
{
cache.removeObject(key);
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
Object getStudent(String key)
{
try
{
return (Student) cache.getObject(key);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public void setYear(String key, String year)
{
((Student) getStudent(key)).setYear(year);
}
public String getYear(String key)
{
return ((Student) getStudent(key)).getYear();
}
public void setAddress(Person person, Address addr)
{
person.setAddress(addr);
}
public Address createAddress()
{
return new Address();
}
public void setName(String key, String name)
{
((Person) getPerson(key)).setName(name);
}
public String getName(String key)
{
return ((Person) getPerson(key)).getName();
}
public void setCurrentStatus(String key, String status)
{
((Person) getPerson(key)).setCurrentStatus(status);
}
public String getCurrentStatus(String key)
{
return ((Person) getPerson(key)).getCurrentStatus();
}
public void setAge(String key, int age)
{
((Person) getPerson(key)).setAge(age);
}
public int getAge(String key)
{
return ((Person) getPerson(key)).getAge();
}
public void setStreet(String key, String street)
{
((Person) getPerson(key)).getAddress().setStreet(street);
}
public String getStreet(String key)
{
return ((Person) getPerson(key)).getAddress().getStreet();
}
public void setCity(String key, String city)
{
((Person) getPerson(key)).getAddress().setCity(city);
}
public String getCity(String key)
{
return ((Person) getPerson(key)).getAddress().getCity();
}
public void setZip(String key, int zip)
{
((Person) getPerson(key)).getAddress().setZip(zip);
}
public int getZip(String key)
{
return ((Person) getPerson(key)).getAddress().getZip();
}
// Map operations
public Object getHobby(String key, Object hobbyKey)
{
Map hobbies = ((Person) getPerson(key)).getHobbies();
return hobbies == null ? null : hobbies.get(hobbyKey);
}
public void setHobby(String key, Object hobbyKey, Object value)
{
Person person = ((Person) getPerson(key));
Map hobbies = person.getHobbies();
if (hobbies == null)
{
hobbies = new HashMap();
person.setHobbies(hobbies);
// NB: it is neccessary to get hobbies again to get advised version
hobbies = person.getHobbies();
}
hobbies.put(hobbyKey, value);
}
public void setHobbies(String key, Map map)
{
Person person = ((Person) getPerson(key));
person.setHobbies(map);
}
public Map getHobbies(String key)
{
return ((Person) getPerson(key)).getHobbies();
}
// List operations
public Object getLanguage(String key, int index)
{
List languages = ((Person) getPerson(key)).getLanguages();
return languages == null ? null : languages.get(index);
}
public Object getLanguages(String key)
{
List languages = ((Person) getPerson(key)).getLanguages();
return languages;
}
public void addLanguage(String key, Object language)
{
Person person = ((Person) getPerson(key));
List languages = person.getLanguages();
if (languages == null)
{
person.setLanguages(new ArrayList());
languages = person.getLanguages();
}
languages.add(language);
}
public void removeLanguage(String key, Object language)
{
List languages = ((Person) getPerson(key)).getLanguages();
if (languages == null) return;
languages.remove(language);
}
public int getLanguagesSize(String key)
{
List languages = ((Person) getPerson(key)).getLanguages();
return languages == null ? 0 : languages.size();
}
public Set getSkills(String key)
{
return new HashSet(((Person) getPerson(key)).getSkills());
}
public void addSkill(String key, String skill)
{
Person person = ((Person) getPerson(key));
Set skills = person.getSkills();
if (skills == null)
{
person.setSkills(new HashSet());
skills = person.getSkills();
}
skills.add(skill);
}
public void removeSkill(String key, String skill)
{
Person person = ((Person) getPerson(key));
Set skills = person.getSkills();
if (skills != null)
{
skills.remove(skill);
}
}
public Object testSerialization()
{
try
{
Person p = new Person();
/*
if (!(p instanceof Externalizable)) {
throw new RuntimeException("p not Externalizable");
}
*/
p.setName("Joe Black");
Address address = new Address();
address.setCity("Mannheim");
p.setAddress(address);
cache.putObject("/person/joe", p);
return (Person) cache.getObject("/person/joe");
}
catch (Throwable t)
{
throw new RuntimeException(t);
}
}
public void testDeserialization(String key, Object value)
{
try
{
cache.putObject(key, value);
}
catch (Throwable t)
{
throw new RuntimeException(t);
}
}
public void printPerson(String key)
{
System.out.println(getPerson(key));
}
public void printCache()
{
System.out.println(cache.toString());
}
public String printCacheDetails()
{
return cache.printDetails();
}
public Object getFieldValue(String key, String name)
{
try
{
Object object = cache.getObject(key);
Field f = object.getClass().getDeclaredField(name);
f.setAccessible(true);
return f.get(object);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public PojoCache getCache()
{
return cache;
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/LocalAopTest.java
Index: LocalAopTest.java
===================================================================
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.aop.proxy.ClassProxy;
import org.jboss.cache.aop.test.Person;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
//import org.jboss.test.JBossTestCase;
/**
* LocalTestCase.java that uses standalone aop package.
* @author Ben Wang
*/
public class LocalAopTest extends TestCase
{
// TODO No need to use the TreeCacheAopTester now.
// Category log = getLog();
Log log=LogFactory.getLog(LocalAopTest.class);
TreeCacheAopTester tester;
public LocalAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
String configFile = "META-INF/local-service.xml";
tester = new TreeCacheAopTester(configFile);
}
protected void tearDown() throws Exception
{
super.tearDown();
tester.stop();
tester = null;
}
// public void testDummy() {}
public void testSetup()
{
log.info("testSetup() ....");
try {
tester.testSetup();
} catch (Exception ex) {
ex.printStackTrace();
fail("testSetup(): fails. " + ex.toString());
}
}
public void testSimple() throws Exception
{
log.info("testSimple() ....");
tester.createPerson("/person/test1", "Joe Black", 32);
assertEquals((Object) "Joe Black", (Object) tester.getName("/person/test1"));
// assertTrue(tester.getAge("/person/test1") == 32);
// tester.removePerson("/person/test1");
}
public void testMap() throws Exception
{
log.info("testMap() ....");
tester.createPerson("/person/test1", "Ben Wang", 40);
assertEquals((Object) "Ben Wang", (Object) tester.getName("/person/test1"));
Person ben = (Person)tester.getPerson("/person/test1");
// Map skills = new HashMap();
Map hobbies = ben.getHobbies();
if (hobbies == null) {
hobbies = new HashMap();
ben.setHobbies(hobbies);
// NB: it is neccessary to get hobbies again to get advised version
hobbies = ben.getHobbies();
}
hobbies.put("1", "English");
hobbies.put("2", "French");
if( !(hobbies instanceof ClassProxy)) {
fail("Hobbies is not an instance of ClassProxy");
}
// ben.setHobbies(skills);
// skills = ben.getHobbies();
hobbies = ben.getHobbies();
assertEquals("Hobbies size", 2, hobbies.size());
log.debug("Hobbies is " + hobbies.toString());
}
public void testModification() throws Exception
{
tester.createPerson("/person/test2", "Joe", 32);
tester.setName("/person/test2", "Joe Black");
assertEquals((Object) tester.getName("/person/test2"), (Object) "Joe Black");
tester.removePerson("/person/test2");
}
public void testRemove() throws Exception
{
tester.createPerson("/person/test3", "Joe", 32);
tester.removePerson("/person/test3");
try {
tester.getName("/person/test3");
fail("Object wasn't removed");
} catch (Exception e) {
// should be thrown
}
}
public void testDynamicRefSwapping() throws Exception
{
tester.createPerson("/person/test3", "Joe", 32);
Person person = (Person)tester.getPerson("/person/test3");
try {
person.setAge(30);
List med = person.getMedication();
assertNull("Medication should be null ", med);
person.setAge(60);
med = person.getMedication();
assertEquals("Medication ", (Object)"Lipitor", (Object)med.get(0));
} catch (Exception e) {
// should be thrown
}
}
public void testDependent() throws Exception
{
tester.createPerson("/person/test4", "Joe Black", 32);
tester.setCity("/person/test4", "Mannheim");
assertEquals((Object) tester.getCity("/person/test4"), (Object) "Mannheim");
}
public void testSerialization() throws Throwable
{
Person p = (Person) tester.testSerialization();
/*
if (p instanceof Advised)
{
InstanceAdvisor advisor = ((Advised) p)._getInstanceAdvisor();
Interceptor[] interceptors = advisor.getInterceptors();
for (int i = 0; i < interceptors.length; i++)
{
assertTrue("CacheInterceptor shouldn't be serialized",
!(interceptors[i] instanceof CacheInterceptor));
}
}
*/
assertEquals((Object) "Joe Black", (Object) p.getName());
assertEquals((Object) "Mannheim", (Object) p.getAddress().getCity());
}
public void testDeserialization() throws Throwable
{
Person p = new Person();
p.setName("test6");
tester.testDeserialization("/person/test6", p);
String name = tester.getName("/person/test6");
assertEquals((Object) "test6", (Object) name);
}
public void testMap2() throws Throwable
{
tester.createPerson("/person/test5", "Joe Black", 32);
tester.setHobby("/person/test5", "music", "guitar");
Object val = tester.getHobby("/person/test5", "music");
assertEquals("guitar", val);
tester.setHobby("/person/test5", "a", "b");
tester.getHobby("/person/test5", "a");
tester.printPerson("/person/test5");
}
public void testList() throws Throwable
{
tester.createPerson("/person/test6", "p6", 50);
tester.addLanguage("/person/test6", "German");
tester.addLanguage("/person/test6", "English");
tester.addLanguage("/person/test6", "French");
int size = tester.getLanguagesSize("/person/test6");
assertTrue(size == 3);
tester.printCache();
tester.addLanguage("/person/test6", "asdf");
tester.printCache();
tester.removeLanguage("/person/test6", "asdf");
tester.printCache();
size = tester.getLanguagesSize("/person/test6");
// assertTrue(size==3);
for (int i = 0; i < size; i++) {
log.debug("" + i + " : " + tester.getLanguage("/person/test6", i));
}
assertEquals(new Integer(3), new Integer(size));
String language = (String) tester.getLanguage("/person/test6", 1);
assertEquals((Object) "English", (Object) language);
}
public void testSet() throws Throwable
{
tester.createPerson("/person/test7", "p7", 27);
tester.addSkill("/person/test7", "Java");
tester.addSkill("/person/test7", "Java");
tester.addSkill("/person/test7", "Java");
Set skills = tester.getSkills("/person/test7");
assertEquals(new Integer(1), new Integer(skills.size()));
tester.removeSkill("/person/test7", "Java");
skills = tester.getSkills("/person/test7");
assertTrue(skills.isEmpty());
tester.addSkill("/person/test7", "Java");
tester.addSkill("/person/test7", "J2EE");
tester.addSkill("/person/test7", "JBoss");
skills = tester.getSkills("/person/test7");
assertEquals(new Integer(3), new Integer(skills.size()));
}
public void testFieldSynchronization() throws Throwable
{
String key = "/person/test8";
tester.createPerson(key, "p8", 8);
assertEquals(tester.getName(key), tester.getFieldValue(key, "name"));
assertEquals(new Integer(tester.getAge(key)), tester.getFieldValue(key, "age"));
tester.setName(key, "p8x");
assertEquals(tester.getName(key), tester.getFieldValue(key, "name"));
tester.setAge(key, 18);
assertEquals(new Integer(tester.getAge(key)), tester.getFieldValue(key, "age"));
}
public void testInheritance() throws Exception
{
tester.createStudent("/person/joe", "Joe", 32, "Senior");
tester.setName("/person/joe", "Joe Black");
assertEquals((Object) tester.getName("/person/joe"), (Object) "Joe Black");
tester.setYear("/person/joe", "Junior");
assertEquals((Object) tester.getYear("/person/joe"), (Object) "Junior");
tester.removePerson("/person/joe");
}
public static Test suite() throws Exception
{
return new TestSuite(LocalAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/NewLocalAopTest.java
Index: NewLocalAopTest.java
===================================================================
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.util.Map;
/**
* New LocalAopTest that doesn't use TreeCacheAopTester. There should have no need for it not.
*
* @author Ben Wang
*/
public class NewLocalAopTest extends TestCase
{
Log log_=LogFactory.getLog(NewLocalAopTest.class);
PojoCache cache_;
public NewLocalAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log_.info("setUp() ....");
String configFile = "META-INF/local-service.xml";
cache_ = new PojoCache();
cache_.setConfiguration(new XmlConfigurationParser().parseFile(configFile));
cache_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
}
// public void testDummy() {}
public void testBadFqn() throws Exception
{
log_.info("testBadFqn() ....");
Person test = new Person();
test.setName("Ben");
test.setAge(10);
cache_.putObject("/a", test);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", test, result);
result.setAge(20);
try {
cache_.putObject(InternalDelegate.JBOSS_INTERNAL, test);
fail("putObject under JBoss_Internal should fail");
} catch (IllegalArgumentException iex)
{
// ok
}
try {
cache_.removeObject(InternalDelegate.JBOSS_INTERNAL);
fail("putObject under JBoss_Internal should fail");
} catch (IllegalArgumentException iex)
{
// ok
}
}
public void testPutRemove() throws Exception
{
log_.info("testPutRemove() ....");
Person test = new Person();
test.setName("Ben");
test.setAge(10);
cache_.putObject("/a", test);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", test, result);
result.setAge(20);
cache_.removeObject("/a");
assertNull("Object should be null ", cache_.getObject("/a"));
assertEquals("Age should be updated as ", 20, test.getAge());
}
public void testPutRemoveNodeExistence() throws Exception
{
log_.info("testPutRemove() ....");
Person test = new Person();
test.setName("Ben");
test.setAge(10);
cache_.putObject("/a", test);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", test, result);
result.setAge(20);
cache_.removeObject("/a");
assertNull("Object should be null ", cache_.getObject("/a"));
assertEquals("Age should be updated as ", 20, test.getAge());
assertNull("DataNode should not exisit ", cache_.get("/a"));
}
public void testFindObjects() throws Exception
{
log_.info("testFindObjects() ....");
Map map = cache_.findObjects("/");
assertEquals("Objects size should be ", 0, map.size());
Person ben = new Person();
ben.setName("Ben");
ben.setAge(10);
cache_.putObject("/a/b/c", ben);
cache_.putObject("/e", ben); // multiple keys, same pojo
Person joe = new Person();
joe.setName("Joe");
joe.setAge(10);
cache_.putObject("/f/joe", joe);
map = cache_.findObjects("/");
assertEquals("Objects size should be ", 3, map.size());
map = cache_.findObjects("/a");
assertEquals("Objects size should be ", 1, map.size());
cache_.removeObject("/e");
map = cache_.findObjects("/");
assertEquals("Objects size should be ", 2, map.size());
}
public static Test suite() throws Exception
{
return new TestSuite(NewLocalAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/ObjectGraphAopTest.java
Index: ObjectGraphAopTest.java
===================================================================
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.DataNode;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.Link;
import org.jboss.cache.aop.test.NodeManager;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.util.ArrayList;
import java.util.List;
/**
* Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
* @author Ben Wang
*/
public class ObjectGraphAopTest extends TestCase
{
Log log=LogFactory.getLog(ObjectGraphAopTest.class);
PojoCache cache_;
public ObjectGraphAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
// String configFile = "META-INF/local-service.xml";
String configFile = "META-INF/local-service.xml";
cache_ = new PojoCache();
cache_.setConfiguration(new XmlConfigurationParser().parseFile(configFile));
cache_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
}
// public void testDummy() {}
protected Person createPerson(String name, int age)
{
Person p = new Person();
p.setName(name);
p.setAge(age);
return p;
}
/**
* Test shared sub-object. In diagram, it is a forest
* where the sub-object is connected by two tree parents.
*/
public void testMultipleReference() throws Exception
{
log.info("testMultipleReference() ...");
cache_.putObject("/person/joe", createPerson("Joe Black", 31));
Person joe = (Person) cache_.getObject("/person/joe");
cache_.putObject("/person/ben", createPerson("Ben Hogan", 51));
Person ben = (Person) cache_.getObject("/person/ben");
Address addr = new Address();
addr.setStreet("123 Albert Ave.");
addr.setCity("Sunnyvale");
addr.setZip(94087);
// They share the sub-object: address
log.info("testMultipleReference(): set Joe address");
joe.setAddress(addr);
log.info("testMultipleReference(): set Ben address");
ben.setAddress(addr);
log.info("testMultipleReference(): verify");
Address add1 = (Address) ((Person)cache_.getObject("/person/joe")).getAddress();
Address add2 = (Address) ((Person)cache_.getObject("/person/ben")).getAddress();
assertEquals(add1.getCity(), add2.getCity());
addr.setCity("Santa Clara");
assertEquals(add1.getCity(), add2.getCity());
}
public void testRefCount() throws Exception
{
// try {Thread.sleep(10000); } catch (Exception e) {};
log.info("testRefCount() ...");
stage1();
stage2();
cache_.removeObject("/person/ben");
}
private void stage1() throws Exception
{
Person joe = createPerson("Joe Black", 31);
Person ben = createPerson("Ben Hogan", 51);
cache_.putObject("/person/joe", joe);
cache_.putObject("/person/ben", ben);
Address addr = new Address();
addr.setStreet("123 Albert Ave.");
addr.setCity("Sunnyvale");
addr.setZip(94087);
joe.setAddress(addr);
ben.setAddress(addr);
// They share the sub-object: address
Address add1 = (Address) ((Person)cache_.getObject("/person/joe")).getAddress();
Address add2 = (Address) ((Person)cache_.getObject("/person/ben")).getAddress();
assertEquals("Joe's address should still be valid ", "Sunnyvale", add1.getCity());
assertEquals("Ben's address should still be valid ", "Sunnyvale", add2.getCity());
}
private void stage2() throws Exception
{
//
cache_.removeObject("/person/joe");
Person ben = (Person) cache_.getObject("/person/ben");
Address addr = ben.getAddress();
assertEquals("Ben's address should still be valid ", "Sunnyvale", addr.getCity());
addr.setCity("Santa Clara");
assertEquals("Ben's address should be changed ", "Santa Clara", addr.getCity());
}
/**
* Pure parent child relationsip
* @throws Exception
*/
public void testCircularReference1() throws Exception
{
// try {Thread.sleep(10000); } catch (Exception e) {};
log.info("testCircularReference1() ...");
Link parent = new Link("parent");
Link child = new Link("child");
parent.setLink(child);
child.setLink(parent);
cache_.putObject("/link/parent", parent);
assertEquals("parent", ((Link) cache_.getObject("/link/parent")).getName());
assertEquals("child", ((Link) cache_.getObject("/link/parent")).getLink().getName());
Link link = (Link)cache_.removeObject("/link/parent");
assertEquals("child", link.getLink().getName());
assertNull("Cache should be null ", ((DataNode)cache_.get("/parent")));
}
/**
* cache managed first before put in the relationsip.
* @throws Exception
*/
public void testCircularReference2() throws Exception
{
// try {Thread.sleep(10000); } catch (Exception e) {};
log.info("testCircularReference2() ...");
Link parent = new Link("parent");
Link child = new Link("child");
cache_.putObject("/link/parent", parent);
parent.setLink(child);
child.setLink(parent);
assertEquals("parent", ((Link) cache_.getObject("/link/parent")).getName());
assertEquals("child", ((Link) cache_.getObject("/link/parent")).getLink().getName());
Link link = (Link)cache_.removeObject("/link/parent");
assertEquals("child", link.getLink().getName());
assertNull("Cache should be null ", ((DataNode)cache_.get("/parent")));
}
/**
* Put first before settting the relationship
* @throws Exception
*/
public void testCircularReference3() throws Exception
{
// try {Thread.sleep(10000); } catch (Exception e) {};
log.info("testCircularReference3() ...");
Link parent = new Link("parent");
Link child = new Link("child");
cache_.putObject("/link/parent", parent);
cache_.putObject("/link/child", child);
parent.setLink(child);
child.setLink(parent);
assertEquals("parent", ((Link) cache_.getObject("/link/parent")).getName());
assertEquals("child", ((Link) cache_.getObject("/link/parent")).getLink().getName());
assertEquals("child", ((Link) cache_.getObject("/link/child")).getName());
assertEquals("parent", ((Link) cache_.getObject("/link/child")).getLink().getName());
Link link = (Link)cache_.removeObject("/link/parent");
assertEquals("child", link.getLink().getName());
assertNull("Cache should be null ", ((DataNode)cache_.get("/parent")));
}
/**
* Setting the circular relationship and also as a shared object.
* @throws Exception
*/
public void testCircularReference4() throws Exception
{
// try {Thread.sleep(10000); } catch (Exception e) {};
log.info("testCircularReference3() ...");
Link parent = new Link("parent");
Link child = new Link("child");
parent.setLink(child);
child.setLink(parent);
List list = new ArrayList();
list.add(parent);
cache_.putObject("/list", list);
cache_.putObject("/alias", list);
List list1 = (List)cache_.getObject("/list");
List list2 = (List)cache_.getObject("/alias");
assertEquals("parent", ((Link) list1.get(0)).getName());
assertEquals("child", ((Link) list2.get(0)).getLink().getName());
}
public void testCircularAndSharedReferences() throws Exception
{
log.info("testCircularAndSharedReferences() ...");
NodeManager pm_ = new NodeManager();
pm_.setRootNode("root");
pm_.addNode("root", "kanto");
pm_.addNode("root.kanto", "tokyo");
pm_.addNode("root.kanto", "kanagawa");
assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN());
cache_.putObject("/propagation", pm_);
pm_.addNode("root.kanto.tokyo", "hadanshita");
assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
List list = pm_.findNode("root").getChildren();
assertEquals("Root should have children of ", 1, list.size());
/*
System.out.println("\n\n");
System.out.println("---------------------------------------------");
System.out.println("Initial cache content");
System.out.println(cache_.printDetails());
System.out.println("---------------------------------------------");
System.out.println("\n\n");
System.out.println("---------------------------------------------");
System.out.println("Initial pm state");
System.out.println("---------------------------------------------");
*/
pm_.printNodes();
}
public void testRemoveObject1() throws Exception
{
log.info("testRemoveObject1() ...");
cache_.putObject("/person/joe", createPerson("Joe Black", 31));
Person joe = (Person) cache_.getObject("/person/joe");
cache_.putObject("/person/ben", createPerson("Ben Hogan", 51));
Person ben = (Person) cache_.getObject("/person/ben");
Address addr = new Address();
addr.setStreet("123 Albert Ave.");
addr.setCity("Sunnyvale");
addr.setZip(94087);
// They share the sub-object: address
log.info("testMultipleReference(): set Joe address");
joe.setAddress(addr);
log.info("testMultipleReference(): set Ben address");
ben.setAddress(addr);
Address add1 = (Address) ((Person)cache_.getObject("/person/joe")).getAddress();
Address add2 = (Address) ((Person)cache_.getObject("/person/ben")).getAddress();
assertEquals(add1.getCity(), add2.getCity());
addr.setCity("Santa Clara");
assertEquals(add1.getCity(), add2.getCity());
// Remove pojo joe will relocate the address field to ben's
cache_.removeObject("/person/joe");
add2 = (Address) ((Person)cache_.getObject("/person/ben")).getAddress();
assertEquals("City ", "Santa Clara", add2.getCity());
}
public void testRemoveObject2() throws Exception
{
log.info("testRemoveObject2() ...");
cache_.putObject("/person/joe", createPerson("Joe Black", 31));
Person joe = (Person) cache_.getObject("/person/joe");
cache_.putObject("/person/ben", createPerson("Ben Hogan", 51));
Person ben = (Person) cache_.getObject("/person/ben");
cache_.putObject("/person/john", createPerson("John Daly", 41));
Person john = (Person) cache_.getObject("/person/john");
Address addr = new Address();
addr.setStreet("123 Albert Ave.");
addr.setCity("Sunnyvale");
addr.setZip(94087);
Address addr1 = new Address();
addr1.setStreet("123 Albert Ave.");
addr1.setCity("San Jose");
addr1.setZip(94087);
// They share the sub-object: address
log.info("testMultipleReference(): set Joe address");
joe.setAddress(addr);
log.info("testMultipleReference(): set Ben address");
ben.setAddress(addr);
log.info("testMultipleReference(): set John address");
john.setAddress(addr);
Address add1 = (Address) ((Person)cache_.getObject("/person/joe")).getAddress();
Address add2 = (Address) ((Person)cache_.getObject("/person/ben")).getAddress();
assertEquals(add1.getCity(), add2.getCity());
addr.setCity("Santa Clara");
assertEquals(add1.getCity(), add2.getCity());
// Remove pojo joe will relocate the address field to ben's
joe.setAddress(addr1);
add2 = (Address) ((Person)cache_.getObject("/person/joe")).getAddress();
assertEquals("City ", "San Jose", add2.getCity());
add2 = (Address) ((Person)cache_.getObject("/person/ben")).getAddress();
assertEquals("City ", "Santa Clara", add2.getCity());
add2 = (Address) ((Person)cache_.getObject("/person/john")).getAddress();
assertEquals("City ", "Santa Clara", add2.getCity());
}
public void XtestObjectIdentity() throws Exception
{
}
public static Test suite() throws Exception
{
return new TestSuite(ObjectGraphAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/SharedRefConcurrentTest.java
Index: SharedRefConcurrentTest.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.aop;
import EDU.oswego.cs.dl.util.concurrent.Semaphore;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.CacheException;
import org.jboss.cache.TreeCache;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.misc.TestingUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
/**
* Tests concurrency of the __JBossInternal__ region of the cache.
*
* @author <a href="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
* @version $Revision: 1.1 $
*/
public class SharedRefConcurrentTest extends TestCase
{
private Map caches;
private static final int CACHE_COUNT = 2;
private static final int SUBTREE_SIZE = 4;
private static final int MAX_OPS = 20;
private static final int MAX_SLEEP = 200;
private static final int MIN_SLEEP = 5;
private Log log = LogFactory.getLog(SharedRefConcurrentTest.class);
/**
* Tests partial state transfer under heavy concurrent load and REPL_SYNC.
* See <code>concurrentUseTest</code> for details.
*
* @throws Exception
*/
public void testConcurrentUseSync() throws Exception
{
concurrentUseTest(true);
}
/**
* Tests partial state transfer under heavy concurrent load and REPL_ASYNC.
* See <code>concurrentUseTest</code> for details.
*
* @throws Exception
*/
public void testConcurrentUseAsync() throws Exception
{
if (true)
return;
concurrentUseTest(false);
}
/**
* Initiates 4 caches and has them concurrently do puts/removes that involve
* shared references. Each cache operates only in its own subtree, and
* all shared references are within that subtree. Basically we are testing
* for concurrency problems with the shared reference map.
*
* @param sync whether to use REPL_SYNC or REPL_ASYNCE
* @throws Exception
*/
private void concurrentUseTest(boolean sync) throws Exception
{
// String[] names = { "A", "B", "C", "D", "E" };
// String[] names = { "A", "B" };
// int CACHE_COUNT = names.length;
CacheUser[] cacheUsers = new CacheUser[CACHE_COUNT];
try
{
// Create a semaphore and take all its tickets
Semaphore semaphore = new Semaphore(CACHE_COUNT);
for (int i = 0; i < CACHE_COUNT; i++)
{
semaphore.acquire();
}
// Create activation threads that will block on the semaphore
TreeCache[] caches = new TreeCache[CACHE_COUNT];
for (int i = 0; i < CACHE_COUNT; i++)
{
cacheUsers[i] = new CacheUser(semaphore, String.valueOf(i), sync);
caches[i] = cacheUsers[i].getTreeCache();
}
// Make sure everyone is in sync
TestingUtil.blockUntilViewsReceived(caches, 60000);
// Release the semaphore to allow the threads to start work
semaphore.release(CACHE_COUNT);
// Sleep to ensure the threads get all the semaphore tickets
TestingUtil.sleepThread(1000);
// Reacquire the semaphore tickets; when we have them all
// we know the threads are done
for (int i = 0; i < CACHE_COUNT; i++)
{
boolean acquired = semaphore.attempt(60000);
if (!acquired)
fail("failed to acquire semaphore " + i);
}
// Sleep to allow any async calls to clear
if (!sync)
TestingUtil.sleepThread(500);
// Ensure the caches held by the cacheUsers see all the values
for (int i = 0; i < CACHE_COUNT; i++)
{
log.info("TEST: CacheUser " + i + " did " +
cacheUsers[i].getOpCount() + " operations");
assertEquals("CacheUser " + i + " saw no exceptions",
null, cacheUsers[i].getException());
}
}
catch (Exception ex)
{
fail(ex.getLocalizedMessage());
}
finally
{
for (int i = 0; i < CACHE_COUNT; i++)
cacheUsers[i].cleanup();
}
}
protected PojoCache createCache(String cacheID, boolean sync)
throws Exception
{
if (caches.get(cacheID) != null)
throw new IllegalStateException(cacheID + " already created");
PojoCache tree = new PojoCache();
String configFile = sync ? "META-INF/replSync-service.xml" : "META-INF/replAsync-service.xml";
tree.setConfiguration(new XmlConfigurationParser().parseFile(configFile));
tree.setDeadlockDetection(sync);
tree.getConfiguration().setClusterName("StateTransferTest");
tree.create();
tree.start();
caches.put(cacheID, tree);
return tree;
}
protected void setUp() throws Exception
{
super.setUp();
caches = new HashMap();
}
protected void tearDown() throws Exception
{
super.tearDown();
Set keys = caches.keySet();
String[] cacheIDs = new String[keys.size()];
cacheIDs = (String[]) keys.toArray(cacheIDs);
for (int i = 0; i < cacheIDs.length; i++)
{
stopCache((PojoCache) caches.get(cacheIDs[i]));
}
}
protected void stopCache(PojoCache cache)
{
if (cache != null)
{
try
{
cache.stop();
cache.destroy();
}
catch (Exception e)
{
log.error("Exception stopping cache " + e.getMessage(), e);
}
}
}
private class CacheUser implements Runnable
{
private Semaphore semaphore;
private PojoCache cache;
private String name;
private Exception exception;
private Thread thread;
private Person[] people;
private boolean[] loaded;
private int opCount;
CacheUser(Semaphore semaphore,
String name,
boolean sync)
throws Exception
{
this.cache = createCache(name, sync);
this.semaphore = semaphore;
this.name = name;
Address addr1 = new Address();
addr1.setStreet("1 Test Street");
addr1.setCity("TestOne, CA");
Address addr2 = new Address();
addr2.setStreet("2 Test Street");
addr2.setCity("TestTwo, CA");
people = new Person[SUBTREE_SIZE];
loaded = new boolean[SUBTREE_SIZE];
for (int j = 0; j < SUBTREE_SIZE; j++)
{
Person p = new Person();
p.setName("Person " + j);
p.setAge(j);
p.setAddress((j % 2 == 0) ? addr1 : addr2);
people[j] = p;
}
thread = new Thread(this);
thread.start();
}
public void run()
{
boolean acquired = false;
try
{
acquired = semaphore.attempt(60000);
if (!acquired)
throw new Exception(name + " cannot acquire semaphore");
log.info("TEST: " + name + " acquired semaphore");
useCache();
}
catch (Exception e)
{
log.error("TEST: " + name + ": (opCount " + opCount + ") " + e.getLocalizedMessage(), e);
// Save it for the test to check
exception = e;
}
finally
{
if (acquired)
semaphore.release();
}
}
void useCache() throws Exception
{
Random random = new Random(System.currentTimeMillis() + name.hashCode());
int factor;
String fqn;
for (opCount = 0; opCount < MAX_OPS; opCount++)
{
factor = random.nextInt(MAX_SLEEP);
int index = factor % SUBTREE_SIZE;
fqn = "/" + name + "/" + String.valueOf(index);
if (loaded[index] == false)
{
cache.putObject(fqn, people[index]);
loaded[index] = true;
log.info("TEST: " + name + " put Person at " + fqn);
}
else
{
cache.removeObject(fqn);
loaded[index] = false;
log.info("TEST: " + name + " removed Person at " + fqn);
}
int sleep = Math.max(MIN_SLEEP, factor);
log.info("TEST: " + name + " op " + opCount + " complete; sleeping " + sleep + " ms");
TestingUtil.sleepThread(sleep);
}
}
public Exception getException()
{
return exception;
}
public PojoCache getTreeCache()
{
return cache;
}
public Object getCacheValue(String fqn) throws CacheException
{
return cache.getObject(fqn);
}
public int getOpCount()
{
return opCount;
}
public void cleanup()
{
if (thread != null && thread.isAlive())
thread.interrupt();
}
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/NewReplicatedAopTest.java
Index: NewReplicatedAopTest.java
===================================================================
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.aop.test.SpecialSerializedAddress;
import org.jboss.cache.factories.XmlConfigurationParser;
import javax.naming.Context;
import java.util.Properties;
/**
* New NewReplicatedAopTest that doesn't use TreeCacheAopTester.
*
* @author Ben Wang
*/
public class NewReplicatedAopTest extends TestCase
{
Log log_=LogFactory.getLog(NewReplicatedAopTest.class);
PojoCache cache_;
PojoCache cache1_;
public NewReplicatedAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
cache_ = new PojoCache();
cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
cache1_ = new PojoCache();
cache1_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
cache_.start();
cache1_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
cache1_.stop();
}
/*
public void testObjectSizePut() throws Exception
{
org.jboss.cache.data.Student student = (org.jboss.cache.data.Student)constructObject();
cache_.putObject("/joe", student);
}
static Object constructObject()
{
org.jboss.cache.data.Student joe = new org.jboss.cache.data.Student();
joe.setName("Joe");
Address add = new Address();
add.setZip(94086);
add.setCity("Sunnyvale)");
add.setStreet("Albertson");
joe.setAddress(add);
String str;
for(int i=0; i < 100; i++)
{
Course course = new Course();
str = RandomString.randomstring(10,20);
course.setInstructor(str);
str = RandomString.randomstring(10,20);
course.setTitle(str);
str = RandomString.randomstring(10,20);
course.setRoom(str);
joe.addCourse(course);
}
return joe;
}
*/
public void testRemoteRemove() throws Exception
{
log_.info("testRemoteRemove() ....");
Person test = new Person();
test.setName("Ben");
test.setAge(10);
cache_.putObject("/a", test);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", test, result);
Person remote = (Person)cache1_.getObject("/a");
assertEquals("Age should be ", 10, remote.getAge());
// Remote remove
cache_.removeObject("/a");
assertNull("Object should be null ", cache_.getObject("/a"));
assertNull("Object should be null ", cache1_.getObject("/a"));
// It is 0 since it will be un-initialized.
assertEquals("Age should be ", 0, remote.getAge());
}
/**
* Test for pojo detachment and then serialization.
* @throws Exception
*/
public void testRemoteDetach() throws Exception
{
log_.info("testRemoteDetach() ....");
SpecialSerializedAddress addr = new SpecialSerializedAddress();
addr.setZip(95123);
addr.addResidents("Ben");
addr.addResidents("Joe");
cache_.putObject("/a", addr);
SpecialSerializedAddress result = (SpecialSerializedAddress)cache_.getObject("/a");
assertEquals(" ", addr, result);
// Remote remove
cache_.removeObject("/a");
assertNull("Object should be null ", cache_.getObject("/a"));
// Test serialization after detach
cache_.put("/plain", "test", addr);
SpecialSerializedAddress remote = (SpecialSerializedAddress)cache1_.get("/plain", "test");
assertEquals("Name should be ", 95123, remote.getZip());
}
/**
* JBCACHE-200.
* @throws Exception
*/
public void testStateTransfer() throws Exception
{
log_.info("testStateTransfer() ....");
Person test = new Person();
test.setName("Ben");
test.setAge(10);
cache_.putObject("/a", test);
Person result = (Person)cache_.getObject("/a");
assertEquals(" ", test, result);
// restart cache1_
cache1_.stop();
cache1_.remove("/a");
cache1_.start();
// Start from scratch for initial state transfer
Person remote = (Person)cache1_.getObject("/a");
assertEquals("Age should be ", 10, remote.getAge());
}
public static Test suite() throws Exception
{
return new TestSuite(NewReplicatedAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/RecursiveRefAopTest.java
Index: RecursiveRefAopTest.java
===================================================================
package org.jboss.cache.aop;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.test.IdObject;
import org.jboss.cache.aop.test.ValueObject;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.util.HashMap;
import java.util.Map;
public class RecursiveRefAopTest extends TestCase
{
private static final String CONFIG_FILENAME = "META-INF/local-service.xml";
private PojoCache treeCache;
Log log = LogFactory.getLog(ReplicatedAopTest.class);
private Map cachedMap;
public RecursiveRefAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
treeCache = new PojoCache();
treeCache.setConfiguration(new XmlConfigurationParser().parseFile(CONFIG_FILENAME));
treeCache.start();
treeCache.putObject("/aop/test", new HashMap());
cachedMap = (Map) treeCache.getObject("/aop/test");
}
protected void tearDown() throws Exception
{
super.tearDown();
}
public void XtestDummy()
{
}
/**
* This test feature is not currently supported now since we can't support key is non-primitive object
* that overrides the hashCode().
*/
public void testRecuriveMapKey()
{
try
{
IdObject id = new IdObject("1");
ValueObject value = new ValueObject(id, 3.0f);
cachedMap.put(id, value);
} // try
catch (Exception x)
{
x.printStackTrace();
fail("testFailed");
} // catch
} // initCache
} // class TestRunner
1.1 date: 2006/10/31 08:01:12; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/ReplicatedAopTest.java
Index: ReplicatedAopTest.java
===================================================================
package org.jboss.cache.aop;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.test.Person;
import java.util.List;
//import org.jboss.test.JBossTestCase;
/**
* Relpicated test that use a tester wrapper. Future new test should use NewReplicatedAopTest
* @author Ben Wang
*/
public class ReplicatedAopTest extends TestCase
{
// Category log = getLog();
Log log=LogFactory.getLog(ReplicatedAopTest.class);
TreeCacheAopTester tester, tester1;
public ReplicatedAopTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
String configFile = "META-INF/replSync-service.xml";
tester = new TreeCacheAopTester(configFile);
tester1 = new TreeCacheAopTester(configFile);
}
protected void tearDown() throws Exception
{
super.tearDown();
tester.stop();
tester1.stop();
tester = null;
tester1 = null;
}
// public void testDummy() {}
public void testSetup()
{
log.info("testSetup() ....");
try {
tester.testSetup();
tester1.testSetup();
} catch (Exception ex) {
ex.printStackTrace();
fail("testSetup(): fails. " + ex.toString());
}
}
public void testSimple() throws Exception
{
log.info("testSimple() ....");
tester.createPerson("/person/test1", "Ben Wang", 40);
assertEquals((Object) "Ben Wang", (Object) tester.getName("/person/test1"));
assertEquals((Object) "Ben Wang", (Object) tester1.getName("/person/test1"));
}
public void testDynamicRefSwapping() throws Exception
{
tester.createPerson("/person/test3", "Joe", 32);
Person person = (Person)tester.getPerson("/person/test3");
try {
person.setAge(30);
List med = person.getMedication();
assertNull("Medication should be null ", med);
person.setAge(61);
med = person.getMedication();
assertEquals("Medication ", (Object)"Lipitor", (Object)med.get(0));
assertEquals("Medication on cache1 ", (Object)"Lipitor",
(Object)((Person)tester1.getPerson("/person/test3")).getMedication().get(0));
person.setAge(71);
assertEquals("Medication ", (Object)"Vioxx", (Object)med.get(1));
assertEquals("Medication on cache1 ", (Object)"Vioxx",
(Object)((Person)tester1.getPerson("/person/test3")).getMedication().get(1));
} catch (Exception e) {
// should be thrown
}
}
public void testTransient() throws Exception
{
log.info("testTransient() ....");
tester.createPerson("/person/test1", "Ben Wang", 40);
tester.setCurrentStatus("/person/test1", "Idle");
assertEquals("Cache 1 ", (Object) "Idle", (Object) tester.getCurrentStatus("/person/test1"));
assertEquals("Cache 2 ", (Object) "Active", (Object) tester1.getCurrentStatus("/person/test1"));
}
public void testModification() throws Exception
{
tester.createPerson("/person/test2", "Ben Wang", 40);
tester.setName("/person/test2", "Harald Gliebe");
assertEquals((Object) tester.getName("/person/test2"), (Object) "Harald Gliebe");
assertEquals((Object) tester1.getName("/person/test2"), (Object) "Harald Gliebe");
tester.removePerson("/person/test2");
}
public void testInheritance() throws Exception
{
tester.createStudent("/person/joe", "Joe", 32, "Senior");
tester.setName("/person/joe", "Joe Black");
assertEquals((Object) tester.getName("/person/joe"), (Object) "Joe Black");
assertEquals((Object) tester1.getName("/person/joe"), (Object) "Joe Black");
tester1.setYear("/person/joe", "Junior");
assertEquals((Object) tester.getYear("/person/joe"), (Object) "Junior");
assertEquals((Object) tester1.getYear("/person/joe"), (Object) "Junior");
tester.removePerson("/person/joe");
}
public static Test suite() throws Exception
{
return new TestSuite(ReplicatedAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
More information about the jboss-cvs-commits
mailing list