[jboss-cvs] JBossCache/old/tests/functional/org/jboss/cache/aop/collection ...
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/collection
ReplicatedSyncSetTest.java
CachedMapAopNullTest.java
CachedListImplAopTest.java CachedListAopTest.java
ReplicatedSyncListTest.java
ReplicatedSyncMapTest.java CachedSetAopTest.java
CachedMapAopTest.java ObjectGraphAopTest.java
CollectionAopTest.java CachedListAopTxTest.java
Log:
Deprecated files moved to old dir.
Revision Changes Path
1.1 date: 2006/10/31 08:01:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/ReplicatedSyncSetTest.java
Index: ReplicatedSyncSetTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.PojoCache;
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 java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
*
* @author Ben Wang
*/
public class ReplicatedSyncSetTest extends TestCase
{
Log log = LogFactory.getLog(ReplicatedSyncSetTest.class);
PojoCache cache1;
PojoCache cache2;
public ReplicatedSyncSetTest(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();
XmlConfigurationParser parser = new XmlConfigurationParser();
Configuration c = parser.parseFile("META-INF/replSync-service.xml"); // read in generic replAsync xml
tree.setConfiguration(c);
c.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;
}
/**
* Test attachment and then detachment and attachment.
*
* @throws Exception
*/
public void testAttachDetach() throws Exception
{
log.info("testAttachDetach() ....");
Set set1 = new HashSet();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
set1.add(addr);
Address addr2 = new Address();
addr2.setCity("Santa Clara");
addr2.setZip(95131);
Address addr3 = new Address();
addr3.setCity("Sunnyvale");
addr3.setZip(94086);
// Pure list
cache1.putObject("/set", set1);
set1 = (Set) cache1.getObject("/set");
set1.add(addr2);
cache1.removeObject("/set");
assertEquals("Detached set should still be", 2, set1.size());
set1.add(addr3);
cache1.putObject("/set", set1);
Set set2 = (Set) cache2.getObject("/set");
assertEquals("Set size should be ", 3, set2.size());
}
public void testRelationshipWithSharedSet1() throws Exception
{
log.info("testRelationshipWithSet() ....");
Set set1 = new HashSet();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
set1.add(addr);
// Pure set
cache1.putObject("/set", set1);
// We specifically need to use Proxy otherwise it won't work with multiple references
set1 = (Set) cache1.getObject("/set");
cache1.putObject("/alias", set1);
Set set2 = (Set) cache1.getObject("/alias");
Address add1 = (Address) set2.iterator().next();
assertNotNull("Address should not be null", add1);
assertEquals("Zip ", 95123, add1.getZip());
set1 = (Set) cache2.getObject("/set");
set2 = (Set) cache2.getObject("/alias");
assertTrue("Set size should not be 0 ", (set2.size() != 0));
assertEquals("Both sets should be equal ", set1, set2);
add1 = (Address) set2.iterator().next();
assertNotNull("Address should not be null", add1);
assertEquals("Zip ", 95123, add1.getZip());
}
public void testRelationshipWithSharedSet2() throws Exception
{
log.info("testRelationshipWithSet2() ....");
Set set1 = new HashSet();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
set1.add(addr);
Set set2 = new HashSet();
set2.add(addr);
cache1.putObject("/set1", set1);
cache1.putObject("/set2", set2);
Address add2 = (Address) ((Set) cache2.getObject("/set2")).iterator().next();
Address add1 = (Address) ((Set) cache2.getObject("/set1")).iterator().next();
assertEquals("Address should be the same", add1, add2);
assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
}
public void testNullWithSharedSet1() throws Exception
{
log.info("testNullWithSharedSet1() ....");
Set set1 = new HashSet();
set1.add("element 0");
set1.add(null); // element 1
set1.add("element 2");
assertTrue("contains test for null value", set1.contains(null));
Object a1[] = set1.toArray();
for (int looper = 0; looper < a1.length; looper++)
{
System.out.println("contained values:" + a1[looper]);
}
// Pure set
cache1.putObject("/set", set1);
// We specifically need to use Proxy otherwise it won't work with multiple references
set1 = (Set) cache1.getObject("/set");
cache1.putObject("/alias", set1);
Set set2 = (Set) cache1.getObject("/alias");
set1 = (Set) cache2.getObject("/set");
set2 = (Set) cache2.getObject("/alias");
assertTrue("Set size should not be 0 ", (set2.size() != 0));
assertEquals("Both sets should be equal ", set1, set2);
a1 = set1.toArray();
for (int looper = 0; looper < a1.length; looper++)
{
System.out.println("contained values:" + a1[looper]);
}
assertTrue("contains test for null value", set1.contains(null));
assertTrue("contains test for null value", set2.contains(null));
Iterator iter = set1.iterator();
while (iter.hasNext())
{
Object val = iter.next();
if ("element 2".equals(val))
{
iter.remove(); // remove element 2
}
}
assertFalse("element 2 is removed", set2.contains("element 2"));
}
public static Test suite() throws Exception
{
return new TestSuite(ReplicatedSyncSetTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/CachedMapAopNullTest.java
Index: CachedMapAopNullTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.PojoCache;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
//import org.jboss.test.JBossTestCase;
/**
* Set interface testing.
* @author Scott Marlow
*/
public class CachedMapAopNullTest extends TestCase
{
Log log=LogFactory.getLog(CachedMapAopNullTest.class);
PojoCache cache_;
Map hobbies;
public CachedMapAopNullTest(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();
stage();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
}
static final int NUMBER_OF_STAGED_HOBBIES = 5;
protected void stage() throws Exception
{
hobbies = new HashMap();
hobbies.put( "1", "golf");
hobbies.put("2", "tennis");
hobbies.put("3", "polo");
hobbies.put(null,"Non-null value but the key is null");
hobbies.put("key is non-null but value is null", null);
cache_.putObject("/person/test7", hobbies);
hobbies = (Map)cache_.getObject("/person/test7");
assertEquals("Map size", NUMBER_OF_STAGED_HOBBIES, hobbies.size());
if( !(hobbies instanceof ClassProxy || hobbies instanceof Map) ) {
fail("testPut(): hobbies is not instance of ClassProxy nor Map");
}
}
/**
* Test simple put
* @throws Throwable
*/
public void testPut() throws Throwable {
int size = hobbies.size();
assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES, size);
hobbies.put("6", "baseball");
size = hobbies.size();
assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES + 1, size);
}
public void testAddAndRemoveIndex() throws Throwable
{
hobbies.put("4", "baseball");
int size = hobbies.size();
assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES +1, size);
assertTrue("Skill contain Golf ", hobbies.containsKey("3"));
hobbies.remove("3");
size = hobbies.size();
assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES, size);
assertFalse("Skill does not contain "+NUMBER_OF_STAGED_HOBBIES+" anymore ", hobbies.containsKey("3"));
assertTrue("search for null key returned non-null value " + hobbies.get(null) ,hobbies.get(null) != null);
hobbies.remove(null);
size = hobbies.size();
assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES-1, size);
assertFalse("Skill does not contain "+(NUMBER_OF_STAGED_HOBBIES-1)+" ", hobbies.containsKey(null));
hobbies.clear();
size = hobbies.size();
assertEquals("Size is ", 0, size);
assertTrue("Should be empty", hobbies.isEmpty());
}
public void testPutAllEtc() throws Throwable {
Map map = new HashMap();
map.put("4", "pingpong");
map.put("5", "handball");
hobbies.putAll(map);
int size = hobbies.size();
assertEquals("Size is ", NUMBER_OF_STAGED_HOBBIES+2, size);
assertTrue("Key is ", hobbies.containsKey("4"));
Set keys = hobbies.keySet();
assertEquals("Key size ", NUMBER_OF_STAGED_HOBBIES+2, keys.size());
Set entries = hobbies.entrySet();
assertEquals("Entry size ", NUMBER_OF_STAGED_HOBBIES+2, entries.size());
}
public void testEntrySet() throws Throwable {
System.out.println("Map "+ hobbies.toString());
for (Iterator i = hobbies.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
System.out.println("Entry key and value "+ entry.getKey()+ " "+ entry.getValue());
}
}
public void testValues() throws Throwable {
System.out.println("Map "+ hobbies.toString());
Set correct = new HashSet();
correct.add("golf");
correct.add("tennis");
correct.add("polo");
correct.add("Non-null value but the key is null");
correct.add(null);
Collection values = hobbies.values();
assertEquals("Correct number of elements in value collection",
correct.size(), values.size());
Iterator iter = null;
for (iter = correct.iterator(); iter.hasNext();)
assertTrue(values.contains(iter.next()));
for (iter = values.iterator(); iter.hasNext();) {
Object value = iter.next();
assertTrue( value + " expected", correct.remove(value));
}
assertTrue("No missing elements from iterator", correct.size() == 0);
iter.remove();
assertTrue("2 elements left after remove via iter", values.size() == NUMBER_OF_STAGED_HOBBIES-1);
assertTrue("Iter removal reflected in map", hobbies.size() == NUMBER_OF_STAGED_HOBBIES-1);
Object[] data = values.toArray();
assertTrue("2 elements in values array", data.length == NUMBER_OF_STAGED_HOBBIES - 1);
values.remove(data[0]);
assertTrue("1 element left after remove", values.size() == NUMBER_OF_STAGED_HOBBIES-2);
assertTrue("Removal reflected in map", hobbies.size() == NUMBER_OF_STAGED_HOBBIES-2);
values.clear();
assertTrue("0 elements left after clear", values.size() == 0);
assertTrue("Clear reflected in map", hobbies.size() == 0);
}
public void testContainsValue() throws Throwable {
System.out.println("Map "+ hobbies.toString());
assertTrue("contains golf", hobbies.containsValue("golf"));
assertTrue("contains tennis", hobbies.containsValue("tennis"));
assertTrue("contains polo", hobbies.containsValue("polo"));
assertFalse("does not contain squash", hobbies.containsValue("squash"));
}
public void testEquals() throws Throwable {
Map map = new HashMap();
map.put("1", "test");
map.put("4", "test");
map.put("2", "tennis");
assertFalse("Map should not be the same ", map.equals(hobbies));
map.clear();
map.put( "1", "golf");
map.put("2", "tennis");
map.put("3", "polo");
map.put(null,"Non-null value but the key is null");
map.put("key is non-null but value is null", null);
assertTrue("Map should be the same ", map.equals(hobbies));
assertTrue("Map should be the same, hobbies=" + hobbies.toString() + ", map=" + map.toString(), hobbies.equals(map));
assertTrue("Map should be the same ", hobbies.equals(hobbies));
}
public void testAttachAndDetach() throws Exception
{
Map map = new HashMap();
map.put("1", "English");
map.put("2", "French");
map.put("3", "Taiwanese");
cache_.putObject("/test", map); // attach
map = (Map)cache_.getObject("/test");
assertEquals("Size ", 3, map.size());
map = (Map)cache_.removeObject("/test"); // detach
assertEquals("Size ", 3, map.size());
System.out.println(cache_.printDetails());
System.out.println("**** End of cache content **** ");
map.remove("2");
map.put("2", "Hoklo");
assertEquals("Size ", 3, map.size());
assertEquals("Content ", "Hoklo", map.get("2"));
// Try to re-attach
cache_.putObject("/test", map);
map.remove("3");
assertEquals("Size ", 2, map.size());
System.out.println(cache_.printDetails());
}
public void testPojoAttachAndDetach() throws Exception
{
Address add1 = new Address();
add1.setCity("San Jose");
add1.setZip(95123);
Address add2 = new Address();
add1.setCity("Sunnyvale");
add1.setZip(94086);
Address add3 = new Address();
add1.setCity("Santa Clara");
add1.setZip(951131);
Map map = new HashMap();
map.put("1", add1);
map.put("2", add2);
map.put("3", add3);
cache_.putObject("/test", map); // attach
map = (Map)cache_.getObject("/test");
assertEquals("Size ", 3, map.size());
map = (Map)cache_.removeObject("/test");
assertEquals("Size ", 3, map.size());
System.out.println(cache_.printDetails());
System.out.println("**** End of cache content **** ");
map.remove("2");
map.put("2", add2);
assertEquals("Size ", 3, map.size());
assertEquals("Content ", add2, map.get("2"));
// Try to re-attach
cache_.putObject("/test", map);
map.remove("2");
assertEquals("Size ", 2, map.size());
System.out.println(cache_.printDetails());
}
public static Test suite() throws Exception
{
return new TestSuite(CachedMapAopNullTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/CachedListImplAopTest.java
Index: CachedListImplAopTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.PojoCache;
import org.jboss.cache.factories.XmlConfigurationParser;
//import org.jboss.test.JBossTestCase;
/**
* List implementation testing.
*/
public class CachedListImplAopTest extends TestCase
{
Log log=LogFactory.getLog(CachedListImplAopTest.class);
PojoCache cache_;
public CachedListImplAopTest(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 static Test suite() throws Exception
{
return new TestSuite(CachedListImplAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/CachedListAopTest.java
Index: CachedListAopTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.PojoCache;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
//import org.jboss.test.JBossTestCase;
/**
* List interface testing.
*/
public class CachedListAopTest extends TestCase
{
Log log=LogFactory.getLog(CachedListAopTest.class);
PojoCache cache_;
List languages;
List languages2; // treat the java.util.List implementation like a reference implementation
// for comparison.
public CachedListAopTest(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 testAddAndRemoveIndex() throws Throwable
{
stage();
languages.add(1, "Taiwanese");
assertEquals("Languages size ", 4, languages.size());
assertEquals("Language ", (Object)"Taiwanese", (Object)languages.get(1));
languages.remove(2);
assertEquals("Languages size ", 3, languages.size());
assertEquals("Language ", (Object)"English", (Object)languages.get(2));
languages.add("Mandarin");
assertEquals("Languages size ", 4, languages.size());
languages.remove("Mandarin");
assertEquals("Languages size ", 3, languages.size());
}
protected void stage() throws Throwable
{
languages = new ArrayList();
languages.add("English");
languages.add("French");
languages.add("English");
cache_.putObject("/person/test6", languages);
languages = (List)cache_.getObject("/person/test6");
int size = languages.size();
assertEquals("Size of list ", 3, size);
languages2 = new ArrayList();
languages2.addAll(languages);
assertEquals("New ArrayList().addAll(CachedList)",languages,languages2);
}
public void testAddAllAndClear() throws Throwable
{
stage();
List list = new ArrayList();
list.add("Taiwanese");
list.add("Madarin");
assertTrue("Language is Taiwanese ", list.contains("Taiwanese"));
languages.addAll(list);
assertEquals("Languages size ", 5, languages.size());
languages.removeAll(list);
assertEquals("Languages size ", 3, languages.size());
assertEquals("Index of French ", 1, languages.indexOf("French"));
languages.clear();
assertEquals("Languages size ", 0, languages.size());
assertTrue("Languages empty ", languages.isEmpty());
}
public void testEquals() throws Throwable {
stage();
List list = (List)cache_.getObject("/person/test6");
assertTrue("List should be the same ", list.equals(languages));
list = new ArrayList();
list.add("German");
list.add("test");
list.add("English");
assertFalse("List should not be the same ", languages.equals(list));
assertFalse("List should not be the same ", list.equals(languages));
}
public void testIterator() throws Throwable
{
stage();
Iterator it = languages.iterator();
Iterator it2 = languages2.iterator();
int counter = 0;
while(it.hasNext()) {
counter++;
assertEquals(it.next(), it2.next());
it.remove();
it2.remove();
}
assertEquals("Size should be ", 3, counter);
assertEquals("Skills should be empty ", 0, languages.size());
}
public void testListIterator() throws Throwable
{
stage();
ListIterator li = languages.listIterator();
ListIterator li2 = languages2.listIterator();
assertFalse("LI has no previous element ", li.hasPrevious());
assertFalse("LI2 has no previous element ", li2.hasPrevious());
assertTrue("LI has next element ", li.hasNext());
assertTrue("LI2 has next element ", li2.hasNext());
assertEquals(li.next(), li2.next());
assertEquals("Index is ", 1, li.nextIndex());
assertEquals("Index is ", 1, li2.nextIndex());
assertEquals("Index is ", 0, li.previousIndex());
assertEquals("Index is ", 0, li2.previousIndex());
assertEquals(li.next(), li2.next());
assertEquals(li.next(), li2.next()); // the end
try {
li.next();
fail("Should throw an exception here ");
}
catch (NoSuchElementException ex) {
;
}
try {
li2.next();
fail("Should throw an exception here ");
}
catch (NoSuchElementException ex) {
;
}
assertEquals("Index is ", 3, li.nextIndex());
assertEquals("Index is ", 3, li2.nextIndex());
assertEquals("Index is ", 2, li.previousIndex());
assertEquals("Index is ", 2, li2.previousIndex());
li.previous();
li2.previous();
assertEquals("Index is ", 2, li.nextIndex());
assertEquals("Index is ", 2, li2.nextIndex());
assertEquals("Index is ", 1, li.previousIndex());
assertEquals("Index is ", 1, li2.previousIndex());
li.previous();
li2.previous();
li.previous();
li2.previous();
try {
li.previous();
fail("Should throw an exception here ");
}
catch (NoSuchElementException ex) {
;
}
try {
li2.previous();
fail("Should throw an exception here ");
}
catch (NoSuchElementException ex) {
;
}
try {
assertEquals(li.next(), li2.next());
li.remove();
li2.remove();
}
catch(Exception e) {
fail("ListIterator.remove failed" + e);
}
try {
assertEquals(li.next(), li2.next());
li.remove();
li2.remove();
}
catch(Exception e) {
fail("ListIterator.remove failed" + e );
}
try {
assertEquals(li.next(), li2.next());
assertEquals("ListIterator.remove test problem with nextIndex, cache next index="+ li.nextIndex()+
", jdk next index=" + li2.nextIndex() + "cache list size = " + languages.size() + ", jdk list size = " + languages.size(),
li.nextIndex(), li2.nextIndex());
li2.set("German");
li.set("German");
String s1 = (String)li.previous();
String s2 = (String)li2.previous();
assertEquals(s1, s2);
assertEquals(s2, "German");
}
catch(Exception e) {
fail("ListIterator.remove failed" + e +", cache list size = " + languages.size() + ", jdk list size = " + languages.size());
}
try {
assertEquals(li.next(),li2.next());
li2.add("Vulcan");
li.add("Vulcan");
String s1 = (String)li.previous();
String s2 = (String)li2.previous();
assertEquals(s1, s2);
assertEquals(s2, "Vulcan");
}
catch(Exception e) {
fail("ListIterator.add failed" + e +", cache list size = " + languages.size() + ", jdk list size = " + languages.size());
}
}
public void testAttachAndDetach() throws Exception
{
List list = new ArrayList();
list.add("English");
list.add("French");
list.add("Taiwanese");
cache_.putObject("/test", list); // attach
list = (List)cache_.getObject("/test");
assertEquals("Size ", 3, list.size());
list = (List)cache_.removeObject("/test");
assertEquals("Size ", 3, list.size());
System.out.println(cache_.printDetails());
System.out.println("**** End of cache content **** ");
list.remove(2);
list.add("Hoklo");
assertEquals("Size ", 3, list.size());
assertEquals("Content ", "Hoklo", list.get(2));
// Try to re-attach
cache_.putObject("/test", list);
list.remove(2);
assertEquals("Size ", 2, list.size());
System.out.println(cache_.printDetails());
}
public void testPojoAttachAndDetach() throws Exception
{
Address add1 = new Address();
add1.setCity("San Jose");
add1.setZip(95123);
Address add2 = new Address();
add2.setCity("Sunnyvale");
add2.setZip(94086);
Address add3 = new Address();
add3.setCity("Santa Clara");
add3.setZip(951131);
List list = new ArrayList();
list.add(add1);
list.add(add2);
list.add(add3);
cache_.putObject("/test", list); // attach
list = (List)cache_.getObject("/test");
assertEquals("Size ", 3, list.size());
list = (List)cache_.removeObject("/test");
assertEquals("Size ", 3, list.size());
System.out.println(cache_.printDetails());
System.out.println("**** End of cache content **** ");
list.remove(2);
list.add(add2);
assertEquals("Size ", 3, list.size());
assertEquals("Content ", add2, list.get(2));
// Try to re-attach
cache_.putObject("/test", list);
list.remove(2);
assertEquals("Size ", 2, list.size());
System.out.println(cache_.printDetails());
}
public static Test suite() throws Exception
{
return new TestSuite(CachedListAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/ReplicatedSyncListTest.java
Index: ReplicatedSyncListTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.PojoCache;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
/**
* Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
*
* @author Ben Wang
*/
public class ReplicatedSyncListTest extends TestCase
{
Log log = LogFactory.getLog(ReplicatedSyncListTest.class);
PojoCache cache1;
PojoCache cache2;
public ReplicatedSyncListTest(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;
}
/**
* Test attachment and then detachment and attachment.
*
* @throws Exception
*/
public void testAttachDetach() throws Exception
{
log.info("testAttachDetach() ....");
List list1 = new ArrayList();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
list1.add(addr);
Address addr2 = new Address();
addr2.setCity("Santa Clara");
addr2.setZip(95131);
Address addr3 = new Address();
addr3.setCity("Sunnyvale");
addr3.setZip(94086);
// Pure list
cache1.putObject("/list", list1);
list1 = (List) cache1.getObject("/list");
list1.add(addr2);
cache1.removeObject("/list");
assertEquals("Detached list should still be", 2, list1.size());
list1.add(addr3);
cache1.putObject("/list", list1);
List list2 = (List) cache2.getObject("/list");
assertTrue("List size should not be 0 ", (list2.size() != 0));
assertEquals("Both list values should be equal ", ((Address) list1.get(0)).getZip(),
((Address) list2.get(0)).getZip());
}
/**
* Two different keys share same list.
*
* @throws Exception
*/
public void testRelationshipWithSharedList1() throws Exception
{
log.info("testRelationshipWithList() ....");
List list1 = new ArrayList();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
list1.add(addr);
// Pure list
cache1.putObject("/list", list1);
// We specifically need to use Proxy otherwise it won't work with multiple references
list1 = (List) cache1.getObject("/list");
cache1.putObject("/alias", list1);
List list2 = (List) cache1.getObject("/alias");
Address add1 = (Address) list2.get(0);
assertNotNull("Address should not be null", add1);
assertEquals("Zip ", 95123, add1.getZip());
list1 = (List) cache2.getObject("/list");
list2 = (List) cache2.getObject("/alias");
assertTrue("List size should not be 0 ", (list2.size() != 0));
assertEquals("Both lists should be equal ", list1, list2);
assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
}
/**
* Shared object between two list item.
*
* @throws Exception
*/
public void testRelationshipWithSharedList2() throws Exception
{
log.info("testRelationshipWithList2() ....");
// 2 lists with shared objects
List list1 = new ArrayList();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
list1.add(addr);
List list2 = new ArrayList();
list2.add(addr);
cache1.putObject("/list1", list1);
cache1.putObject("/list2", list2);
Address add2 = (Address) ((List) cache2.getObject("/list2")).get(0);
Address add1 = (Address) ((List) cache2.getObject("/list1")).get(0);
assertEquals("Address should be the same", add1, add2);
assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
}
/**
* Shared object between regular POJO and List item.
*
* @throws Exception
*/
public void testRelationshipWithSharedList3() throws Exception
{
log.info("testRelationshipWithList3() ....");
// 2 lists with shared objects
List list1 = new ArrayList();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
list1.add(addr);
List list2 = new ArrayList();
list2.add(addr);
cache1.putObject("/address", addr);
cache1.putObject("/list1", list1);
Address add1 = (Address) ((List) cache2.getObject("/list1")).get(0);
Address add2 = (Address) cache2.getObject("/address");
assertEquals("Address should be the same", add1, add2);
assertEquals("Both shared object should be equal ", 95123, add1.getZip());
}
public void testNullWithSharedList1() throws Exception
{
log.info("testNullWithSharedList1() ....");
List list1 = new ArrayList();
list1.add("element 0");
list1.add(null); // element 1
list1.add("element 2");
list1.add(null); // element 3
// Pure set
cache1.putObject("/list", list1);
// We specifically need to use Proxy otherwise it won't work with multiple references
list1 = (List) cache1.getObject("/list");
cache1.putObject("/alias", list1);
List list2 = (List) cache1.getObject("/alias");
list1 = (List) cache2.getObject("/list");
list2 = (List) cache2.getObject("/alias");
assertTrue("List size should not be 0 ", (list2.size() != 0));
assertEquals("Both listss should be equal ", list1, list2);
Object a1[] = list1.toArray();
Object a2[] = list2.toArray();
assertTrue("element 1 is null", (a1[1] == null));
assertTrue("element 1 is null", (a2[1] == null));
assertTrue("element 3 is null", (a1[3] == null));
assertTrue("element 3 is null", (a2[3] == null));
assertTrue("contains test for null value", list1.contains(null));
assertTrue("contains test for null value", list2.contains(null));
assertTrue("index of null ", list2.indexOf(null) == 1);
assertTrue("last index of null ", list2.lastIndexOf(null) == 3);
list1.set(0, null); // set first element to null
assertTrue("set first item to null", list2.get(0) == null);
list1.set(0, "element 0");
assertTrue("set first item to 'element 0'", list2.get(0).equals("element 0"));
ListIterator listIter = list1.listIterator();
assertTrue("listiter has next", listIter.hasNext());
assertTrue("listiter 1st element is 'element 0'", listIter.next().equals("element 0"));
assertTrue("listiter has next", listIter.hasNext());
assertTrue("listiter 2nd element is null", listIter.next() == null);
listIter.remove();
assertTrue("2nd element should be 'element 2'", list2.get(1).equals("element 2"));
}
public static Test suite() throws Exception
{
return new TestSuite(ReplicatedSyncListTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/ReplicatedSyncMapTest.java
Index: ReplicatedSyncMapTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.PojoCache;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Test replicated Map
*
* @author Ben Wang
* @author Scott Marlow
*/
public class ReplicatedSyncMapTest extends TestCase
{
Log log = LogFactory.getLog(ReplicatedSyncMapTest.class);
PojoCache cache1;
PojoCache cache2;
public ReplicatedSyncMapTest(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;
}
/**
* Test attachment and then detachment and attachment.
*
* @throws Exception
*/
public void testAttachDetach() throws Exception
{
log.info("testAttachDetach() ....");
Map map1 = new HashMap();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
map1.put("key1", addr);
Address addr2 = new Address();
addr2.setCity("Santa Clara");
addr2.setZip(95131);
Address addr3 = new Address();
addr2.setCity("Sunnyvale");
addr3.setZip(94086);
// Pure list
cache1.putObject("/map", map1);
map1 = (Map) cache1.getObject("/map");
map1.put("key2", addr2);
cache1.removeObject("/map");
assertEquals("Detached map should still be", 2, map1.size());
map1.put("key3", addr3);
cache1.putObject("/map", map1);
Map map2 = (Map) cache2.getObject("/map");
assertEquals("Map size should be ", 3, map2.size());
}
public void testRelationshipWithSharedMap1() throws Exception
{
log.info("testRelationshipWithMap() ....");
Map map1 = new HashMap();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
map1.put("key1", addr);
// Pure set
cache1.putObject("/map", map1);
// We specifically need to use Proxy otherwise it won't work with multiple references
map1 = (Map) cache1.getObject("/map");
cache1.putObject("/alias", map1);
Map map2 = (Map) cache1.getObject("/alias");
Address add1 = (Address) ((Map.Entry) map2.entrySet().iterator().next()).getValue();
assertNotNull("Address should not be null", add1);
assertEquals("Zip ", 95123, add1.getZip());
map1 = (Map) cache2.getObject("/map");
map2 = (Map) cache2.getObject("/alias");
assertTrue("Map size should not be 0 ", (map2.size() != 0));
assertEquals("Both maps should be equal ", map1, map2);
add1 = (Address) ((Map.Entry) map2.entrySet().iterator().next()).getValue();
assertNotNull("Address should not be null", add1);
assertEquals("Zip ", 95123, add1.getZip());
}
public void testNullWithSharedMap1() throws Exception
{
log.info("testNullWithSharedMap1() ....");
Map map1 = new HashMap();
map1.put("null value test", null);
map1.put(null, "null key test");
// Pure set
cache1.putObject("/map", map1);
// We specifically need to use Proxy otherwise it won't work with multiple references
map1 = (Map) cache1.getObject("/map");
cache1.putObject("/alias", map1);
Map map2 = (Map) cache1.getObject("/alias");
map1 = (Map) cache2.getObject("/map");
map2 = (Map) cache2.getObject("/alias");
assertTrue("Map size should not be 0 ", (map2.size() != 0));
assertEquals("Both maps should be equal ", map1, map2);
assertTrue("Get null key returns non-null value", map2.get(null) != null);
assertTrue("Get null key returns correct value", map2.get(null).equals("null key test"));
assertTrue("Get null value returns null value", map2.get("null value test") == null);
assertTrue("containsKey test for null value", map2.containsKey("null value test"));
assertTrue("containsKey test for null key", map2.containsKey(null));
assertTrue("containsValue test for null value", map2.containsValue(null));
assertTrue("values (positive) null test", map2.values().contains(null));
Collection walk = map1.values();
assertTrue(walk.remove(null));
assertFalse("values (negative) null test", map2.values().contains(null));
assertTrue("values (positive) null test", map2.values().contains("null key test"));
assertTrue(walk.remove("null key test"));
assertFalse("values (negative) null test", map2.values().contains("null key test"));
map1.put("null value test", null);
map1.put(null, "null key test");
assertTrue("remove null item test", map1.remove(null).equals("null key test"));
assertFalse("null item removed", map2.containsKey(null));
}
public void testRelationshipWithSharedMap2() throws Exception
{
log.info("testRelationshipWithMap2() ....");
Map map1 = new HashMap();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
map1.put("key1", addr);
Map map2 = new HashMap();
map2.put("key2", addr);
cache2.putObject("/map1", map1);
cache2.putObject("/map2", map2);
map1 = (Map) cache2.getObject("/map1");
map2 = (Map) cache2.getObject("/map2");
assertTrue("Map size should not be 0 ", (map2.size() != 0));
assertEquals("Both maps should be equal ", map1.get("key1"), map2.get("key2"));
Address add1 = (Address) ((Map.Entry) map2.entrySet().iterator().next()).getValue();
assertNotNull("Address should not be null", add1);
assertEquals("Zip ", 95123, add1.getZip());
}
public void testKeySetRemoveWithSharedMap1() throws Exception
{
log.info("testKeySetRemoveWithSharedMap1() ....");
Map map1 = new HashMap();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
map1.put("key1_map1", addr);
map1.put("key2_map1", null);
map1.put(null, "null value test");
Map map2 = new HashMap();
map2.put("key1_map2", addr);
map2.put("key2_map2", "round");
assertTrue("key1 exists", map1.containsKey("key1_map1"));
cache2.putObject("/map1", map1);
cache2.putObject("/map2", map2);
map1 = (Map) cache1.getObject("/map1");
map2 = (Map) cache1.getObject("/map2");
assertTrue("key1 exists", map1.containsKey("key1_map1"));
assertTrue("null key exists", map1.containsKey(null));
assertTrue("key2 exists", map1.containsKey("key2_map1"));
Set set = map1.keySet();
Iterator iter = set.iterator();
while (iter.hasNext())
{
Object o = iter.next();
System.out.println("testKeySetRemoveWithSharedMap1 iter.next returned: " + o);
if (o == null || "key1_map1".equals(o))
iter.remove();
}
assertTrue("key2 exists", map1.containsKey("key2_map1"));
assertFalse("key1 doesn't exist", map1.containsKey("key1_map1"));
assertFalse("null key doesn't exist", map1.containsKey(null));
map1 = (Map) cache2.getObject("/map1");
map2 = (Map) cache2.getObject("/map2");
assertTrue("key2 exists", map1.containsKey("key2_map1"));
assertFalse("key1 doesn't exist", map1.containsKey("key1_map1"));
assertFalse("null key doesn't exist", map1.containsKey(null));
}
public static Test suite() throws Exception
{
return new TestSuite(ReplicatedSyncMapTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/CachedSetAopTest.java
Index: CachedSetAopTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.PojoCache;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
//import org.jboss.test.JBossTestCase;
/**
* Set interface testing.
*/
public class CachedSetAopTest extends TestCase
{
Log log=LogFactory.getLog(CachedSetAopTest.class);
PojoCache cache_;
Set skills;
public CachedSetAopTest(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();
stage();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
}
protected void stage() throws Exception
{
skills = new HashSet();
skills.add("Java");
skills.add("C++");
skills.add("Perl");
cache_.putObject("/person/test7", skills);
skills = (Set)cache_.getObject("/person/test7");
int size = skills.size();
assertEquals("Size is ", 3, size);
}
public void testClear() throws Throwable
{
int size = skills.size();
assertEquals("Size is ", 3, size);
System.out.println(cache_.printDetails());
skills.clear();
size = skills.size();
assertEquals("Size is ", 0, size);
assertTrue("Should be empty", skills.isEmpty());
}
public void testAddAndRemoveIndex() throws Throwable
{
skills.add("Golf");
int size = skills.size();
assertEquals("Size is ", 4, size);
skills.add("Golf");
size = skills.size();
assertEquals("Size is ", 4, size);
assertTrue("Skill shuold contain Golf ", skills.contains("Golf"));
skills.remove("C++");
size = skills.size();
assertEquals("Size is ", 3, size);
assertFalse("Skill does not contain C++ anymore ", skills.contains("C++"));
skills.add("Golf");
size = skills.size();
assertEquals("Size is ", 3, size);
System.out.println(cache_.printDetails());
skills.clear();
size = skills.size();
assertEquals("Size is ", 0, size);
assertTrue("Should be empty", skills.isEmpty());
}
public void testAddAndRemoveAll() throws Throwable
{
List list = new ArrayList();
list.add("Tennis");
list.add("Polo");
list.add("Baseball");
skills.addAll((Collection)list);
int size = skills.size();
assertEquals("Size is ", 6, size);
assertTrue("Skill contains Polo ", skills.contains("Polo"));
skills.removeAll((Collection)list);
size = skills.size();
assertEquals("Size is ", 3, size);
assertFalse("Skill does not contain Polo ", skills.contains("Polo"));
assertTrue("Skill contains C++ ", skills.contains("C++"));
}
public void testRemoveAndAdd()
{
assertTrue(skills.remove("C++"));
assertTrue(skills.add("C++"));
assertEquals("removeAndAdd: size is 3", 3, skills.size());
}
public void testIterator() {
Iterator it = skills.iterator();
int counter = 0;
while(it.hasNext()) {
counter++;
it.next();
it.remove();
}
assertEquals("iterator: Size should be ", 3, counter);
assertEquals("iterator: Skills should be empty ", 0, skills.size());
List list = new ArrayList();
list.add("Tennis");
list.add("Polo");
list.add("Baseball");
list.add("Soccer");
list.add("Hockey");
list.add("Lacrosse");
skills.addAll(list);
it = skills.iterator();
while(it.hasNext()) {
counter++;
if("Polo".equals(it.next())) {
it.remove();
}
}
assertFalse("iterator: item removed via iterator", skills.contains("Polo"));
assertTrue("iterator: item not removed via iterator", skills.contains("Lacrosse"));
// Check for proper relationship between hasNext and next
list = new ArrayList();
list.add("Tennis");
list.add("Polo");
list.add("Baseball");
skills.addAll(list);
it = skills.iterator();
while(it.hasNext()) {
it.next();
}
try
{
it.next();
fail("iterator: Didn't fail on next() when hasNext() == false");
}
catch (NoSuchElementException good) {}
// Check for proper relationship between next and remove
it = skills.iterator();
try
{
it.remove();
}
catch (IllegalStateException good)
{
// behaves correctly per Iterator contract
}
catch (Exception e)
{
StringWriter stackTrace = new StringWriter();
e.printStackTrace(new PrintWriter(stackTrace));
fail("iterator: failed with incorrect exception type when removing " +
"without first calling next() -- " + e.getClass().getName() + ". stackTrace=" + stackTrace);
}
}
public void testEquals() throws Throwable {
Set set = (Set)cache_.getObject("/person/test7");
assertTrue("iterator: List should be the same ", set.equals(skills));
set = new HashSet();
set.add("German");
set.add("test");
set.add("English");
assertFalse("List should not be the same ", set.equals(skills));
assertFalse("List should not be the same ", skills.equals(set));
}
public void testAttachAndDetach() throws Exception
{
Set set = new HashSet();
set.add("English");
set.add("French");
set.add("Taiwanese");
cache_.putObject("/test", set); // attach
set = (Set)cache_.getObject("/test");
assertEquals("Size ", 3, set.size());
set = (Set)cache_.removeObject("/test");
assertEquals("Size ", 3, set.size());
System.out.println(cache_.printDetails());
System.out.println("**** End of cache content **** ");
set.remove("French");
set.add("Hoklo");
assertEquals("Size ", 3, set.size());
assertTrue("Content ", set.contains("Hoklo"));
// Try to re-attach
cache_.putObject("/test", set);
set.remove("Taiwanese");
assertEquals("Size ", 2, set.size());
System.out.println(cache_.printDetails());
}
public void testPojoAttachAndDetach() throws Exception
{
Address add1 = new Address();
add1.setCity("San Jose");
add1.setZip(95123);
Address add2 = new Address();
add2.setCity("Sunnyvale");
add2.setZip(94086);
Address add3 = new Address();
add3.setCity("Santa Clara");
add3.setZip(951131);
Set set = new HashSet();
set.add(add1);
set.add(add2);
set.add(add3);
cache_.putObject("/test", set); // attach
set = (Set)cache_.getObject("/test");
assertEquals("Size ", 3, set.size());
set = (Set)cache_.removeObject("/test");
assertEquals("Size ", 3, set.size());
System.out.println(cache_.printDetails());
System.out.println("**** End of cache content **** ");
set.remove(add2);
set.add(add2);
assertEquals("Size ", 3, set.size());
assertTrue("Content ", set.contains(add2));
// Try to re-attach
cache_.putObject("/test", set);
set.remove(add2);
assertEquals("Size ", 2, set.size());
System.out.println(cache_.printDetails());
}
public void testToArray() throws Exception
{
Object[] arr = skills.toArray();
assertEquals("toArray: array length is correct", 3, arr.length);
testLanguagesFound(arr);
Object[] arr1 = skills.toArray(arr);
assertTrue("toArray: arrays match", Arrays.equals(arr, arr1));
arr = new Object[5];
arr = skills.toArray(arr);
assertEquals("toArray: array length is correct", 5, arr.length);
testLanguagesFound(arr);
assertEquals(null, arr[3]);
assertEquals(null, arr[4]);
arr = new Object[2];
arr = skills.toArray(arr);
assertEquals("toArray: array length is correct", 3, arr.length);
testLanguagesFound(arr);
}
private void testLanguagesFound(Object[] arr)
{
boolean[] found = new boolean[3];
for (int i = 0; i < arr.length; i++)
{
if ("Java".equals(arr[i]))
found[0] = true;
else if ("C++".equals(arr[i]))
found[1] = true;
else if ("Perl".equals(arr[i]))
found[2] = true;
}
assertTrue("toArray: all elements found", found[0] && found[1] && found[2]);
}
public void testRetainAll() throws Exception
{
LinkedList list2 = new LinkedList();
list2.add("Java");
assertTrue("testRetainAll", skills.retainAll(list2) );
// should only have Java left
assertTrue("testRetainAll, skills size should be 1 but is " + skills.size(), skills.size() == 1);
assertTrue("testRetainAll", skills.contains("Java"));
}
public void testContainsAll() throws Exception
{
LinkedList list2 = new LinkedList();
list2.add("Java");
list2.add("C++");
list2.add("Perl");
skills.clear();
assertTrue(skills.addAll(list2));
list2.remove("Java");
assertTrue("testContainsAll", skills.containsAll(list2) );
skills.remove("C++");
assertFalse("testContainsAll", skills.containsAll(list2) );
}
public static Test suite() throws Exception
{
return new TestSuite(CachedSetAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/CachedMapAopTest.java
Index: CachedMapAopTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.PojoCache;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
//import org.jboss.test.JBossTestCase;
/**
* Set interface testing.
* @author Ben Wang
*/
public class CachedMapAopTest extends TestCase
{
Log log=LogFactory.getLog(CachedMapAopTest.class);
PojoCache cache_;
Map hobbies;
public CachedMapAopTest(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();
stage();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
}
protected void stage() throws Exception
{
hobbies = new HashMap();
hobbies.put( "1", "golf");
hobbies.put("2", "tennis");
hobbies.put("3", "polo");
cache_.putObject("/person/test7", hobbies);
hobbies = (Map)cache_.getObject("/person/test7");
assertEquals("Map size", 3, hobbies.size());
if( !(hobbies instanceof ClassProxy || hobbies instanceof Map) ) {
fail("testPut(): hobbies is not instance of ClassProxy nor Map");
}
}
/**
* Test simple put
* @throws Throwable
*/
public void testPut() throws Throwable {
int size = hobbies.size();
assertEquals("Size is ", 3, size);
hobbies.put("6", "baseball");
size = hobbies.size();
assertEquals("Size is ", 4, size);
}
public void testAddAndRemoveIndex() throws Throwable
{
hobbies.put("4", "baseball");
int size = hobbies.size();
assertEquals("Size is ", 4, size);
assertTrue("Skill contain Golf ", hobbies.containsKey("3"));
hobbies.remove("3");
size = hobbies.size();
assertEquals("Size is ", 3, size);
assertFalse("Skill does not contain 3 anymore ", hobbies.containsKey("3"));
hobbies.clear();
size = hobbies.size();
assertEquals("Size is ", 0, size);
assertTrue("Should be empty", hobbies.isEmpty());
}
public void testPutAllEtc() throws Throwable {
Map map = new HashMap();
map.put("4", "pingpong");
map.put("5", "handball");
hobbies.putAll(map);
int size = hobbies.size();
assertEquals("Size is ", 5, size);
assertTrue("Key is ", hobbies.containsKey("4"));
Set keys = hobbies.keySet();
assertEquals("Key size ", 5, keys.size());
Set entries = hobbies.entrySet();
assertEquals("Entry size ", 5, entries.size());
}
public void testLongValue() throws Throwable {
Long val = new Long("8225676592564383");
Long val2 = new Long(8225676592564383L);
assertTrue(0 == val.compareTo(val2)); // sanity check
hobbies.put(val, "prateek");
assertTrue(hobbies.containsKey(val));
assertTrue(hobbies.get(val).equals("prateek"));
}
public void testEntrySet() throws Throwable {
System.out.println("Map "+ hobbies.toString());
for (Iterator i = hobbies.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
System.out.println("Entry key and value "+ entry.getKey()+ " "+ entry.getValue());
}
}
public void testValues() throws Throwable {
System.out.println("Map "+ hobbies.toString());
Set correct = new HashSet();
correct.add("golf");
correct.add("tennis");
correct.add("polo");
Collection values = hobbies.values();
assertEquals("Correct number of elements in value collection",
correct.size(), values.size());
Iterator iter = null;
for (iter = correct.iterator(); iter.hasNext();)
assertTrue(values.contains(iter.next()));
for (iter = values.iterator(); iter.hasNext();) {
Object value = iter.next();
assertTrue( value + " expected", correct.remove(value));
}
assertTrue("No missing elements from iterator", correct.size() == 0);
iter.remove();
assertTrue("2 elements left after remove via iter", values.size() == 2);
assertTrue("Iter removal reflected in map", hobbies.size() == 2);
Object[] data = values.toArray();
assertTrue("2 elements in values array", data.length == 2);
values.remove(data[0]);
assertTrue("1 element left after remove", values.size() == 1);
assertTrue("Removal reflected in map", hobbies.size() == 1);
values.clear();
assertTrue("0 elements left after clear", values.size() == 0);
assertTrue("Clear reflected in map", hobbies.size() == 0);
}
public void testContainsValue() throws Throwable {
System.out.println("Map "+ hobbies.toString());
assertTrue("contains golf", hobbies.containsValue("golf"));
assertTrue("contains tennis", hobbies.containsValue("tennis"));
assertTrue("contains polo", hobbies.containsValue("polo"));
assertFalse("does not contain squash", hobbies.containsValue("squash"));
}
public void testEquals() throws Throwable {
Map map = new HashMap();
map.put("1", "test");
map.put("4", "test");
map.put("2", "tennis");
assertFalse("Map should not be the same ", map.equals(hobbies));
}
public void testAttachAndDetach() throws Exception
{
Map map = new HashMap();
map.put("1", "English");
map.put("2", "French");
map.put("3", "Taiwanese");
cache_.putObject("/test", map); // attach
map = (Map)cache_.getObject("/test");
assertEquals("Size ", 3, map.size());
map = (Map)cache_.removeObject("/test"); // detach
assertEquals("Size ", 3, map.size());
System.out.println(cache_.printDetails());
System.out.println("**** End of cache content **** ");
map.remove("2");
map.put("2", "Hoklo");
assertEquals("Size ", 3, map.size());
assertEquals("Content ", "Hoklo", map.get("2"));
// Try to re-attach
cache_.putObject("/test", map);
map.remove("3");
assertEquals("Size ", 2, map.size());
System.out.println(cache_.printDetails());
}
public void testPojoAttachAndDetach() throws Exception
{
Address add1 = new Address();
add1.setCity("San Jose");
add1.setZip(95123);
Address add2 = new Address();
add2.setCity("Sunnyvale");
add2.setZip(94086);
Address add3 = new Address();
add3.setCity("Santa Clara");
add3.setZip(951131);
Map map = new HashMap();
map.put("1", add1);
map.put("2", add2);
map.put("3", add3);
cache_.putObject("/test", map); // attach
map = (Map)cache_.getObject("/test");
assertEquals("Size ", 3, map.size());
map = (Map)cache_.removeObject("/test");
assertEquals("Size ", 3, map.size());
System.out.println(cache_.printDetails());
System.out.println("**** End of cache content **** ");
map.remove("2");
map.put("2", add2);
assertEquals("Size ", 3, map.size());
assertEquals("Content ", add2, map.get("2"));
// Try to re-attach
cache_.putObject("/test", map);
map.remove("2");
assertEquals("Size ", 2, map.size());
System.out.println(cache_.printDetails());
}
public static Test suite() throws Exception
{
return new TestSuite(CachedMapAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
1.1 date: 2006/10/31 08:01:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/ObjectGraphAopTest.java
Index: ObjectGraphAopTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.PojoCache;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.factories.XmlConfigurationParser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 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;
}
public void testListWithMultipleSharedObjccts() throws Exception
{
log.info("testListWithMultipleSharedObjects() ....");
List list1 = new ArrayList();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
list1.add(addr);
List list2 = new ArrayList();
List list3 = new ArrayList();
list2.add(addr);
list3.add(addr);
cache_.putObject("/list1", list1);
cache_.putObject("/list2", list2);
cache_.putObject("/list3", list3);
list3 = (List)cache_.getObject("/list3");
assertTrue("List size should not be 0 ", (list3.size() != 0));
assertEquals("Address ", addr.getZip(), ((Address)list3.get(0)).getZip());
addr.setCity("Sunnyvale");
assertEquals("Address ", addr.getCity(), ((Address)list3.get(0)).getCity());
cache_.removeObject("/list1");
cache_.removeObject("/list2");
cache_.removeObject("/list3");
}
public void testRelationshipWithSharedList1() throws Exception
{
log.info("testRelationshipWithList() ....");
List list1 = new ArrayList();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
list1.add(addr);
cache_.putObject("/list", list1);
list1 = (List)cache_.getObject("/list");
assertEquals("List size should be ", 1, list1.size());
cache_.putObject("/alias", list1);
list1 = (List)cache_.getObject("/list");
assertEquals("List size should be ", 1, list1.size());
List list2 = (List)cache_.getObject("/alias");
// System.out.println(cache_.printDetails());
assertEquals("List size should be ", 1, list2.size());
assertEquals("Both lists should be equal ", list1, list2);
assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
}
public void testRelationshipWithSharedList2() throws Exception
{
log.info("testRelationshipWithList2() ....");
List list1 = new ArrayList();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
list1.add(addr);
List list2 = new ArrayList();
list2.add(addr);
cache_.putObject("/list1", list1);
cache_.putObject("/list2", list2);
Address add2 = (Address)((List)cache_.getObject("/list2")).get(0);
Address add1 = (Address)((List)cache_.getObject("/list1")).get(0);
assertEquals("Address should be the same", add1, add2);
assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
}
public void testListWithAttachAndDetach() throws Exception
{
log.info("testListWithAttachAndDetach() ....");
List list1 = new ArrayList();
Address addr1 = new Address();
addr1.setCity("San Jose");
addr1.setZip(95123);
Address addr2 = new Address();
addr2.setCity("Sunnyvale");
addr2.setZip(94086);
list1.add(addr2);
cache_.putObject("/list", list1);
list1 = (List)cache_.getObject("/list");
assertEquals("List size should be ", 1, list1.size());
cache_.putObject("/alias", list1);
list1 = (List)cache_.getObject("/list");
assertEquals("List size should be ", 1, list1.size());
List list2 = (List)cache_.getObject("/alias");
// System.out.println(cache_.printDetails());
assertEquals("List size should be ", 1, list2.size());
assertEquals("Both lists should be equal ", list1, list2);
assertEquals("Both list values should be equal ", list1.get(0), list2.get(0));
}
public void testRelationshipWithSharedSet1() throws Exception
{
log.info("testRelationshipWithSet() ....");
Set set1 = new HashSet();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
set1.add(addr);
// Pure set
cache_.putObject("/set", set1);
// We specifically need to use Proxy otherwise it won't work with multiple references
set1 = (Set)cache_.getObject("/set");
cache_.putObject("/alias", set1);
set1 = (Set)cache_.getObject("/set");
Set set2 = (Set)cache_.getObject("/alias");
assertTrue("Set size should not be 0 ", (set2.size() != 0));
assertEquals("Both sets should be equal ", set1, set2);
Address add1 = (Address)set2.iterator().next();
assertNotNull("Address should not be null", add1);
assertEquals("Zip ", 95123, add1.getZip());
}
public void testRelationshipWithSharedSet2() throws Exception
{
log.info("testRelationshipWithSet2() ....");
Set set1 = new HashSet();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
set1.add(addr);
Set set2 = new HashSet();
set2.add(addr);
cache_.putObject("/set1", set1);
cache_.putObject("/set2", set2);
Address add2 = (Address)((Set)cache_.getObject("/set2")).iterator().next();
Address add1 = (Address)((Set)cache_.getObject("/set1")).iterator().next();
assertEquals("Address should be the same", add1, add2);
assertEquals("Both shared object should be equal ", add2.getZip(), add1.getZip());
}
public void testRelationshipWithSharedMap1() throws Exception
{
log.info("testRelationshipWithMap() ....");
Map map1 = new HashMap();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
map1.put("key1", addr);
cache_.putObject("/map", map1);
cache_.putObject("/alias", map1);
map1 = (Map)cache_.getObject("/map");
Map map2 = (Map)cache_.getObject("/alias");
assertTrue("Map size should not be 0 ", (map2.size() != 0));
assertEquals("Both maps should be equal ", map1, map2);
Address add1 = (Address)((Map.Entry)map2.entrySet().iterator().next()).getValue();
assertNotNull("Address should not be null", add1);
assertEquals("Zip ", 95123, add1.getZip());
}
public void testRelationshipWithSharedMap2() throws Exception
{
log.info("testRelationshipWithMap2() ....");
Map map1 = new HashMap();
Address addr = new Address();
addr.setCity("San Jose");
addr.setZip(95123);
map1.put("key1", addr);
Map map2 = new HashMap();
map2.put("key2", addr);
// Pure map
cache_.putObject("/map", map1);
cache_.putObject("/alias", map2);
map1 = (Map)cache_.getObject("/map");
map2 = (Map)cache_.getObject("/alias");
assertTrue("Map size should not be 0 ", (map2.size() != 0));
Address add1 = (Address)((Map.Entry)map2.entrySet().iterator().next()).getValue();
assertNotNull("Address should not be null", add1);
assertEquals("Zip ", 95123, add1.getZip());
}
public void testObjectIdentity() 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:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/CollectionAopTest.java
Index: CollectionAopTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.CacheException;
import org.jboss.cache.aop.PojoCache;
import org.jboss.cache.aop.TreeCacheAopTester;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Iterator;
import java.util.Collection;
import java.util.HashSet;
import java.util.HashMap;
/**
* Generic Collection class support testing.
*/
public class CollectionAopTest extends TestCase
{
Log log=LogFactory.getLog(CollectionAopTest.class);
TreeCacheAopTester tester;
PojoCache cache;
public CollectionAopTest(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);
cache = tester.getCache();
}
protected void tearDown() throws Exception
{
super.tearDown();
tester.stop();
tester = null;
}
/**
* Testing using LinkedList proxy.
* @throws Exception
*/
public void testLinkedList() throws Exception {
LinkedList list = new LinkedList();
LinkedList list1;
list.add("English");
try {
cache.putObject("/aop/list", list);
list = (LinkedList)cache.getObject("/aop/list");
list.add("French");
list1 = (LinkedList)cache.getObject("/aop/list");
assertEquals("Size of list ", 2, list1.size());
} catch (CacheException e) {
fail("pubtObject fails");
throw e;
}
}
/**
* Testing using LinkedMap proxy.
* @throws Exception
*/
public void testLinkedMap() throws Exception {
LinkedHashMap map = new LinkedHashMap();
LinkedHashMap map1;
map.put("1", "English");
try {
cache.putObject("/aop/map", map);
map = (LinkedHashMap)cache.getObject("/aop/map");
map.put("2", "French");
map1 = (LinkedHashMap)cache.getObject("/aop/map");
assertEquals("Size of map ", 2, map1.size());
} catch (CacheException e) {
fail("pubtObject fails");
throw e;
}
}
/**
* Testing using LinkedSet proxy.
* @throws Exception
*/
public void testLinkedSet() throws Exception {
LinkedHashSet set = new LinkedHashSet();
LinkedHashSet set1;
set.add("English");
Map map;
try {
cache.putObject("/aop/set", set);
set = (LinkedHashSet)cache.getObject("/aop/set");
set.add("French");
set1 = (LinkedHashSet)cache.getObject("/aop/set");
assertEquals("Size of set ", 2, set1.size());
} catch (CacheException e) {
fail("pubtObject fails");
throw e;
}
}
public static Test suite() throws Exception
{
return new TestSuite(CollectionAopTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
// tests for each of the methods in Collection interface
public void testSize() throws Exception {
LinkedList list = new LinkedList();
list.add("java");
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
assertEquals("size of collection", 1, list.size());
list.add("c");
list.add("lisp");
assertEquals("size of collection", 3, list.size());
list.remove("lisp");
assertEquals("size of collection", 2, list.size());
list.remove("c");
assertEquals("size of collection", 1, list.size());
list.clear();
assertEquals("size of collection", 0, list.size());
}
catch (CacheException e) {
fail("testSize "+ e.getMessage());
throw e;
}
}
public void testIsEmpty() throws Exception
{
LinkedList list = new LinkedList();
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
assertTrue("collection is empty", list.isEmpty());
list.add("c");
list.add("lisp");
assertFalse("collection is not empty", list.isEmpty());
list.remove("lisp");
assertFalse("collection is not empty", list.isEmpty());
list.remove("c");
assertTrue("collection is empty", list.isEmpty());
}
catch (CacheException e) {
fail("testIsEmpty "+ e.getMessage());
throw e;
}
}
public void testContains() throws Exception
{
LinkedList list = new LinkedList();
list.add("java");
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
assertFalse("collection doesn't contains", list.contains("lisp"));
list.add("c");
list.add("lisp");
assertTrue("collection contains", list.contains("lisp"));
list.remove("lisp");
assertFalse("collection doesn't contain", list.contains("lisp"));
list.remove("c");
list.clear();
assertFalse("collection doesn't contains", list.contains("c"));
}
catch (CacheException e) {
fail("testContains "+ e.getMessage());
throw e;
}
}
public void testIterator() throws Exception
{
LinkedList list = new LinkedList();
LinkedList list2 = new LinkedList();
list2.add("java");
list2.add("c");
list2.add("lisp");
list2.add("c++");
list2.add("forth");
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
list.addAll(list2);
Iterator iter = list.iterator();
while(iter.hasNext()) {
Object o = iter.next();
assertTrue("Iteration test",list2.contains(o));
}
}
catch (CacheException e) {
fail("testIterator "+ e.getMessage());
throw e;
}
}
public void testToArray() throws Exception
{
LinkedList list = new LinkedList();
LinkedList list2 = new LinkedList();
list2.add("java");
list2.add("c");
list2.add("lisp");
list2.add("c++");
list2.add("forth");
list.addAll(list2);
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
Object[] values = list.toArray();
for(int looper = 0; looper < values.length; looper ++) {
assertTrue("toArray test",list2.contains(values[looper]));
}
values = new Object[1];
values = list.toArray(values); // test with too small of an array
for(int looper = 0; looper < values.length; looper ++) {
assertTrue("toArray test",list2.contains(values[looper]));
}
values = new Object[10];
values = list.toArray(values); // test with too large of an array, result should be null padded
for(int looper = 0; looper < values.length; looper ++) {
assertTrue("toArray test",(values[looper] == null || list2.contains(values[looper])));
}
}
catch (CacheException e) {
fail("testToArray "+ e.getMessage());
throw e;
}
}
public void testAdd() throws Exception
{
LinkedList list = new LinkedList();
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
list.add("java");
assertTrue("add test", list.contains("java"));
assertFalse("add test", list.contains("c#"));
}
catch (CacheException e) {
fail("testAdd "+ e.getMessage());
throw e;
}
}
public void testRemove() throws Exception
{
LinkedList list = new LinkedList();
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
list.add("java");
assertTrue(list.remove("java"));
assertFalse("remove test", list.contains("java"));
}
catch (CacheException e) {
fail("testRemove "+ e.getMessage());
throw e;
}
}
public void testAddAll() throws Exception
{
LinkedList list = new LinkedList();
LinkedList list2 = new LinkedList();
list2.add("java");
list2.add("c");
list2.add("lisp");
list2.add("c++");
list2.add("forth");
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
assertTrue(list.addAll(list2));
Object[] values = list.toArray();
for(int looper = 0; looper < values.length; looper ++) {
assertTrue("testAddAll",list2.contains(values[looper]));
}
}
catch (CacheException e) {
fail("testAddAll "+ e.getMessage());
throw e;
}
}
public void testClear() throws Exception
{
LinkedList list = new LinkedList();
LinkedList list2 = new LinkedList();
list2.add("java");
list2.add("c");
list2.add("lisp");
list2.add("c++");
list2.add("forth");
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
assertTrue(list.addAll(list2));
assertTrue("testClear",list.size() > 0);
list.clear();
assertTrue("testClear",list.size() == 0);
}
catch (CacheException e) {
fail("testClear "+ e.getMessage());
throw e;
}
}
public void testRetainAll() throws Exception
{
LinkedList list = new LinkedList();
LinkedList list2 = new LinkedList();
list2.add("java");
list2.add("c");
list2.add("lisp");
list2.add("c++");
list2.add("forth");
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
assertTrue(list.addAll(list2));
list2.remove("c");
list2.remove("lisp");
list2.remove("c++");
list2.remove("forth");
assertTrue("testRetainAll", list.retainAll(list2) );
// should only have java left
assertTrue("testRetainAll, list size should be 1 but is " + list.size(), list.size() == 1);
assertTrue("testRetainAll", list.contains("java"));
}
catch (CacheException e) {
fail("testRetainAll "+ e.getMessage());
throw e;
}
}
public void testRemoveAll() throws Exception
{
LinkedList list = new LinkedList();
LinkedList list2 = new LinkedList();
list2.add("java");
list2.add("c");
list2.add("lisp");
list2.add("c++");
list2.add("forth");
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
assertTrue(list.addAll(list2));
list2.remove("java");
assertTrue("testRemoveAll", list.removeAll(list2) );
// should only have java left
assertTrue("testRemoveAll", list.size() == 1);
assertTrue("testRemoveAll", list.contains("java"));
}
catch (CacheException e) {
fail("testRemoveAll "+ e.getMessage());
throw e;
}
}
public void testContainsAll() throws Exception
{
LinkedList list = new LinkedList();
LinkedList list2 = new LinkedList();
list2.add("java");
list2.add("c");
list2.add("lisp");
list2.add("c++");
list2.add("forth");
try {
cache.putObject("/language/list", list);
list = (LinkedList)cache.getObject("/language/list");
assertTrue(list.addAll(list2));
list2.remove("java");
assertTrue("testContainsAll", list.containsAll(list2) );
list.remove("c");
assertFalse("testContainsAll", list.containsAll(list2) );
}
catch (CacheException e) {
fail("testContainsAll "+ e.getMessage());
throw e;
}
}
/**
* Commented it out now. We should move this to perf directory.
* @throws Exception
*/
public void XtestListAddPerformance() throws Exception
{
Collection cacheList = new LinkedList();
Collection jdkList = new LinkedList();
addPerformance(cacheList, jdkList, "List");
}
public void XtestSetAddPerformance() throws Exception
{
Collection cacheList = new HashSet();
Collection jdkList = new HashSet();
addPerformance(cacheList, jdkList, "Set");
}
public void XtestMapAddPerformance() throws Exception
{
Map cacheList = new HashMap();
Map jdkList = new HashMap();
addPerformance(cacheList, jdkList, "Map");
}
public void XtestListIterPerformance() throws Exception
{
Collection cacheList = new LinkedList();
Collection jdkList = new LinkedList();
iterPerformance(cacheList, jdkList, "List");
}
public void XtestSetIterPerformance() throws Exception
{
Collection cacheList = new HashSet();
Collection jdkList = new HashSet();
iterPerformance(cacheList, jdkList, "Set");
}
public void XtestMapIterPerformance() throws Exception
{
Map cacheList = new HashMap();
Map jdkList = new HashMap();
iterPerformance(cacheList, jdkList, "Map");
}
public void addPerformance(Collection cacheList, Collection jdkList, String label) throws Exception {
try {
cache.putObject("/language/list", cacheList);
cacheList = (Collection)cache.getObject("/language/list");
long start = System.currentTimeMillis();
collectionAdd(1000, jdkList);
long jdkTime = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
collectionAdd(1000, cacheList);
long cacheTime = System.currentTimeMillis() - start;
//assertTrue(label + " 1000 add performance, jdk time=" + jdkTime +", cache time=" + cacheTime, goodEnough(cacheTime,jdkTime));
log.info( label + " 1000 add performance, jdk time=" + jdkTime +", cache time=" + cacheTime);
}
catch (CacheException e) {
fail("testAddAll "+ e.getMessage());
throw e;
}
}
public void addPerformance(Map cacheList, Map jdkList, String label) throws Exception {
try {
cache.putObject("/language/list", cacheList);
cacheList = (Map)cache.getObject("/language/list");
long start = System.currentTimeMillis();
collectionAdd(1000, jdkList);
long jdkTime = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
collectionAdd(1000, cacheList);
long cacheTime = System.currentTimeMillis() - start;
//assertTrue(label + " 1000 add performance, jdk time=" + jdkTime +", cache time=" + cacheTime, goodEnough(cacheTime,jdkTime));
log.info( label + " 1000 add performance, jdk time=" + jdkTime +", cache time=" + cacheTime);
}
catch (CacheException e) {
fail("testAddAll "+ e.getMessage());
throw e;
}
}
public void iterPerformance(Collection cacheList, Collection jdkList, String label) throws Exception {
cache.putObject("/language/list", cacheList);
cacheList = (Collection)cache.getObject("/language/list");
collectionAdd(1000, jdkList);
collectionAdd(1000, cacheList);
long start = System.currentTimeMillis();
Iterator iter = jdkList.iterator();
while(iter.hasNext())
iter.next();
long jdkTime = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
iter = cacheList.iterator();
while(iter.hasNext())
iter.next();
long cacheTime = System.currentTimeMillis() - start;
//assertTrue(label + " 1000 iter performance, jdk time=" + jdkTime +", cache time=" + cacheTime, goodEnough(cacheTime,jdkTime));
log.info( label + " 1000 iter performance, jdk time=" + jdkTime +", cache time=" + cacheTime);
}
public void iterPerformance(Map cacheList, Map jdkList, String label) throws Exception {
cache.putObject("/language/list", cacheList);
cacheList = (Map)cache.getObject("/language/list");
collectionAdd(1000, jdkList);
collectionAdd(1000, cacheList);
long start = System.currentTimeMillis();
Iterator iter = jdkList.keySet().iterator();
while(iter.hasNext())
iter.next();
long jdkTime = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
iter = cacheList.keySet().iterator();
while(iter.hasNext())
iter.next();
long cacheTime = System.currentTimeMillis() - start;
//assertTrue(label + " 1000 iter performance, jdk time=" + jdkTime +", cache time=" + cacheTime, goodEnough(cacheTime,jdkTime));
log.info( label + " 1000 iter performance, jdk time=" + jdkTime +", cache time=" + cacheTime);
System.out.println( label + " 1000 iter performance, jdk time=" + jdkTime +", cache time=" + cacheTime);
}
public void collectionAdd(int count, Collection c) throws Exception {
for( int looper = 0; looper < count; looper ++) {
String value = Integer.toString(looper);
c.add(value);
}
}
public void collectionAdd(int count, Map c) throws Exception {
for( int looper = 0; looper < count; looper ++) {
String value = Integer.toString(looper);
c.put(value,value);
}
}
public boolean goodEnough( double cacheTime, double jdkTime) {
if(jdkTime ==0)
jdkTime =1;
double percent = ((cacheTime - jdkTime) / jdkTime) * 100.0;
log.info("percent =" + percent + " cache=" + cacheTime +" jdk =" + jdkTime);
return percent < 100.0;
}
}
1.1 date: 2006/10/31 08:01:13; author: bwang; state: Exp;JBossCache/old/tests/functional/org/jboss/cache/aop/collection/CachedListAopTxTest.java
Index: CachedListAopTxTest.java
===================================================================
package org.jboss.cache.aop.collection;
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.PojoCache;
import org.jboss.cache.factories.XmlConfigurationParser;
/**
* List interface testing with transaction control.
* @author Scott Marlow
*/
public class CachedListAopTxTest extends TestCase
{
Log log=LogFactory.getLog(CachedListAopTxTest.class);
PojoCache cache_;
public CachedListAopTxTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
String configFile = "META-INF/cache-config.xml";
cache_ = new PojoCache();
cache_.setConfiguration(new XmlConfigurationParser().parseFile(configFile));
cache_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
}
public void testDummy()
{
// No op now.
}
public static Test suite() throws Exception
{
return new TestSuite(CachedListAopTxTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
}
More information about the jboss-cvs-commits
mailing list