[jboss-cvs] JBossCache/tests-50/functional/org/jboss/cache/pojo/passivation ...
Ben Wang
bwang at jboss.com
Wed Jul 19 22:44:20 EDT 2006
User: bwang
Date: 06/07/19 22:44:20
Added: tests-50/functional/org/jboss/cache/pojo/passivation
LocalTest.java RandomString.java
ReplicatedTest.java
Log:
Passivation specific tests.
Revision Changes Path
1.1 date: 2006/07/20 02:44:20; author: bwang; state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/passivation/LocalTest.java
Index: LocalTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.pojo.passivation;
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.pojo.*;
import org.jboss.cache.pojo.test.Person;
import org.jboss.cache.pojo.test.Address;
import org.jboss.cache.pojo.test.Link;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.util.id.UID;
import java.util.List;
import java.util.ArrayList;
/**
* Basic PojoCache test case.
*
* @author Ben Wang
*/
public class LocalTest extends TestCase
{
Log log = LogFactory.getLog(org.jboss.cache.pojo.passivation.LocalTest.class);
PojoCache cache_;
public LocalTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
String configFile = "META-INF/local-passivation-service.xml";
cache_ = PojoCacheFactory.createInstance(configFile);
PropertyConfigurator config = new PropertyConfigurator();
config.configure(cache_, configFile); // read in generic replSync xml
cache_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.stop();
}
// public void testDummy() {}
private Person createPerson(String name, int age)
{
Person p = new Person();
p.setName(name);
p.setAge(age);
Address add = new Address();
add.setZip(95123);
add.setCity("San Jose");
p.setAddress(add);
return p;
}
private Person createHugePerson(String name, int age)
{
Person p = new Person();
p.setName(name);
p.setAge(age);
Address add = new Address();
add.setZip(95123);
add.setCity("San Jose");
p.setAddress(add);
List list = new ArrayList();
for(int i=0; i < 10; i++)
{
String tmp = RandomString.randomstring(200000, 220000);
list.add(tmp);
}
p.setLanguages(list);
return p;
}
public void testSimple() throws Exception
{
log.info("testSimple() ....");
String id = "person";
Person joe = createPerson("Joe Black", 20);
cache_.attach(id, joe);
Thread.sleep(11000); // default is 3 seconds so joe should have been passivated.
assertFalse("Node should be evicted ",
cache_.getCache().exists(id));
assertEquals("age ", 20, joe.getAge());
joe.setAge(30);
assertEquals("age ", 30, joe.getAge());
}
public void testFindAgain() throws Exception
{
log.info("testFindAgain() ....");
String id = "person";
Person joe = createPerson("Joe Black", 20);
cache_.attach(id, joe);
Thread.sleep(11000); // default is 3 seconds so joe should have been passivated.
assertFalse("Node should be evicted ",
cache_.getCache().exists(id));
assertFalse("Node should be evicted ",
cache_.getCache().exists(id));
Person p = (Person)cache_.find(id);
assertEquals("age ", 20, joe.getAge());
joe.setAge(30);
assertEquals("age ", 30, joe.getAge());
assertEquals("age ", p.getAge(), joe.getAge());
assertFalse("Instance not equal (this is known side effect) ", joe == p);
}
public void testMultipleReference() throws Exception
{
log.info("testMultipleReference() ...");
cache_.attach("/person/joe", createPerson("Joe Black", 31));
Person joe = (Person) cache_.find("/person/joe");
cache_.attach("/person/ben", createPerson("Ben Hogan", 51));
Person ben = (Person) cache_.find("/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_.find("/person/joe")).getAddress();
Address add2 = (Address) ((Person) cache_.find("/person/ben")).getAddress();
assertEquals(add1.getCity(), add2.getCity());
addr.setCity("Santa Clara");
assertEquals(add1.getCity(), add2.getCity());
Thread.sleep(11000); // default is 3 seconds so joe should have been passivated.
assertFalse("Node should be evicted ",
cache_.getCache().exists("/person/joe"));
assertEquals("City is ", "Santa Clara", add2.getCity());
Thread.sleep(11000); // default is 3 seconds so joe should have been passivated.
assertEquals("City is ", "Santa Clara", joe.getAddress().getCity());
}
public void testRemoveObject1() throws Exception
{
log.info("testRemoveObject1() ...");
cache_.attach("/person/joe", createPerson("Joe Black", 31));
Person joe = (Person) cache_.find("/person/joe");
cache_.attach("/person/ben", createPerson("Ben Hogan", 51));
Person ben = (Person) cache_.find("/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_.find("/person/joe")).getAddress();
Address add2 = (Address) ((Person) cache_.find("/person/ben")).getAddress();
assertEquals(add1.getCity(), add2.getCity());
addr.setCity("Santa Clara");
assertEquals(add1.getCity(), add2.getCity());
Thread.sleep(11000);
// Remove pojo joe will relocate the address field to ben's
cache_.detach("/person/joe");
add2 = (Address) ((Person) cache_.find("/person/ben")).getAddress();
assertEquals("City ", "Santa Clara", add2.getCity());
}
public void testCircularReference1() throws Exception
{
log.info("testCircularReference1() ...");
Link parent = new Link("parent");
Link child = new Link("child");
parent.setLink(child);
child.setLink(parent);
cache_.attach("/link/parent", parent);
Thread.sleep(11000);
assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
}
public void XtestGC() throws Exception
{
log.info("testGC() ....");
int i = 0;
while(i < 500)
{
UID uid = new UID();
String id = "/person/test/" +uid.toString();
Person p = createHugePerson("Joe Black", 32);
cache_.attach(id, p);
// cache_.detach(id);
i++;
}
// Thread.sleep(10000000);
}
public static Test suite() throws Exception
{
return new TestSuite(org.jboss.cache.pojo.passivation.LocalTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(org.jboss.cache.pojo.passivation.LocalTest.suite());
}
}
1.1 date: 2006/07/20 02:44:20; author: bwang; state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/passivation/RandomString.java
Index: RandomString.java
===================================================================
package org.jboss.cache.pojo.passivation;
import java.util.*;
public class RandomString {
private static Random rn = new Random(12);
public static int rand(int lo, int hi)
{
int n = hi - lo + 1;
int i = rn.nextInt() % n;
if (i < 0)
i = -i;
return lo + i;
}
public static String randomstring(int lo, int hi)
{
int n = rand(lo, hi);
byte b[] = new byte[n];
for (int i = 0; i < n; i++)
b[i] = (byte)rand('a', 'z');
return new String(b, 0);
}
public static String randomstring()
{
return randomstring(5, 25);
}
}
1.1 date: 2006/07/20 02:44:20; author: bwang; state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/passivation/ReplicatedTest.java
Index: ReplicatedTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.pojo.passivation;
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.pojo.*;
import org.jboss.cache.pojo.test.Person;
import org.jboss.cache.pojo.test.Student;
import java.util.List;
/**
*
* @author Ben Wang
*/
public class ReplicatedTest extends TestCase
{
Log log = LogFactory.getLog(org.jboss.cache.pojo.passivation.ReplicatedTest.class);
PojoCache cache, cache1;
public ReplicatedTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
String configFile = "META-INF/replSync-passivation-service.xml";
cache = PojoCacheFactory.createInstance(configFile);
cache.start();
cache1 = PojoCacheFactory.createInstance(configFile);
cache1.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache.stop();
cache1.stop();
}
// public void testDummy() {}
private Person createPerson(String name, int age)
{
Person p = new Person();
p.setName(name);
p.setAge(age);
return p;
}
public void testSimple() throws Exception
{
log.info("testSimple() ....");
String id = "/person/test1";
Person ben = createPerson("Ben Wang", 40);
cache.attach(id, ben);
cache1.find(id);
Thread.sleep(11000);
assertFalse("Node should be evicted ",
cache.getCache().exists(id));
assertFalse("Node should be evicted ",
cache1.getCache().exists(id));
assertEquals("Ben Wang", ben.getName());
assertEquals("Ben Wang", ((Person) cache1.find(id)).getName());
cache.detach(id);
}
public static Test suite() throws Exception
{
return new TestSuite(org.jboss.cache.pojo.passivation.ReplicatedTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(org.jboss.cache.pojo.passivation.ReplicatedTest.suite());
}
}
More information about the jboss-cvs-commits
mailing list