[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/pojo/passivation ...
Ben Wang
bwang at jboss.com
Sat Jan 13 10:55:13 EST 2007
User: bwang
Date: 07/01/13 10:55:13
Added: tests/functional/org/jboss/cache/pojo/passivation
LocalTest.java ReplicatedTest.java
RandomString.java
Log:
JBCACHE-922 Merged src-50 and tests-50 into src and tests, respectively.
Revision Changes Path
1.1 date: 2007/01/13 15:55:13; author: bwang; state: Exp;JBossCache/tests/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.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.AbstractCacheListener;
import org.jboss.cache.Fqn;
import org.jboss.cache.pojo.PojoCache;
import org.jboss.cache.pojo.PojoCacheFactory;
import org.jboss.cache.pojo.test.Address;
import org.jboss.cache.pojo.test.Link;
import org.jboss.cache.pojo.test.Person;
/**
* 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_;
MyCacheListener listener_;
public LocalTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
String configFile = "META-INF/pojocache-passivation-service.xml";
boolean toStart = false;
cache_ = PojoCacheFactory.createCache(configFile, toStart);
cache_.getCache().getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.BatchModeTransactionManagerLookup");
listener_ = new MyCacheListener();
cache_.getCache().addCacheListener(listener_);
cache_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.getCache().removeNode(Fqn.fromString("/"));
cache_.getCache().removeCacheListener(listener_);
cache_.stop();
Thread.sleep(1000);
}
// 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 void sanityCheck()
{
if (listener_.getActivationCount() == 0 || listener_.getPassivationCount() == 0)
{
fail("Sanity checking for passivation failed. Counters: activation - " + listener_.getActivationCount()
+ " passivation - " + listener_.getPassivationCount());
}
listener_.reset();
}
public void testSimple() throws Exception
{
log.info("testSimple() ....");
String id = "person";
Person joe = createPerson("Joe Black", 20);
cache_.attach(id, joe);
Thread.sleep(9100); // 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().getRoot().hasChild(new Fqn(id)));
assertEquals("age ", 20, joe.getAge());
joe.setAge(30);
assertEquals("age ", 30, joe.getAge());
sanityCheck();
}
public void testSetAddress() throws Exception
{
log.info("testAddress() ....");
String id = "person";
Person joe = createPerson("Joe Black", 20);
cache_.attach(id, joe);
Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
assertFalse("Node should be evicted ",
cache_.getCache().getRoot().hasChild(new Fqn(id)));
Address addr = new Address();
addr.setCity("Taipei");
addr.setZip(106);
joe.setAddress(addr);
sanityCheck();
}
public void testFindAgain() throws Exception
{
log.info("testFindAgain() ....");
String id = "person";
Person joe = createPerson("Joe Black", 20);
cache_.attach(id, joe);
Thread.sleep(9100); // 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().getRoot().hasChild(new Fqn(id)));
// assertFalse("Node should be evicted ",
// cache_.getCache().exists(id));
assertFalse("Node should be evicted ",
cache_.getCache().getRoot().hasChild(new Fqn(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);
sanityCheck();
}
public void testMultipleReference() throws Exception
{
log.info("testMultipleReference() ...");
String id1 = "/person/ben";
cache_.attach(id1, createPerson("Ben Hogan", 51));
Person joe = (Person) cache_.find(id1);
String id = "/person/joe";
cache_.attach(id, createPerson("Joe Black", 31));
Person ben = (Person) cache_.find(id);
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(id)).getAddress();
Address add2 = (Address) ((Person) cache_.find(id)).getAddress();
assertEquals(add1.getCity(), add2.getCity());
addr.setCity("Santa Clara");
assertEquals(add1.getCity(), add2.getCity());
Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
// assertFalse("Node should be evicted ",
// cache_.getCache().exists("/person/joe"));
assertFalse("Node should be evicted ",
cache_.getCache().getRoot().hasChild(new Fqn(id)));
assertEquals("City is ", "Santa Clara", add2.getCity());
Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
assertEquals("City is ", "Santa Clara", joe.getAddress().getCity());
cache_.detach(id);
cache_.detach(id1);
}
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(9100);
// 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(9100);
assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
}
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());
}
public class MyCacheListener extends AbstractCacheListener
{
int activation = 0;
int passivation = 0;
public int getActivationCount()
{
return activation;
}
public int getPassivationCount()
{
return passivation;
}
public void reset()
{
activation = 0;
passivation = 0;
}
public void nodeActivated(Fqn fqn, boolean pre)
{
if (!pre)
{
System.out.println("nodeActivated: " + fqn);
activation++;
}
}
public void nodePassivated(Fqn fqn, boolean pre)
{
if (pre)
{
System.out.println("nodePassivated: " + fqn);
passivation++;
}
}
}
}
1.1 date: 2007/01/13 15:55:13; author: bwang; state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/passivation/ReplicatedTest.java
Index: ReplicatedTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.cache.pojo.passivation;
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.AbstractCacheListener;
import org.jboss.cache.Fqn;
import org.jboss.cache.pojo.PojoCache;
import org.jboss.cache.pojo.PojoCacheFactory;
import org.jboss.cache.pojo.test.Address;
import org.jboss.cache.pojo.test.Link;
import org.jboss.cache.pojo.test.Person;
/**
* Replicated passivation test.
*
* @author Ben Wang
*/
public class ReplicatedTest extends TestCase
{
Log log = LogFactory.getLog(ReplicatedTest.class);
PojoCache cache_, cache1_;
MyCacheListener listener_;
public ReplicatedTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
super.setUp();
log.info("setUp() ....");
String configFile = "META-INF/pojocache-passivation-service.xml";
boolean toStart = false;
cache_ = PojoCacheFactory.createCache(configFile, toStart);
cache_.getCache().getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.BatchModeTransactionManagerLookup");
cache_.start();
cache1_ = PojoCacheFactory.createCache(configFile, toStart);
cache1_.getCache().getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.BatchModeTransactionManagerLookup");
listener_ = new MyCacheListener();
cache1_.getCache().addCacheListener(listener_);
cache1_.start();
}
protected void tearDown() throws Exception
{
super.tearDown();
cache_.getCache().removeNode(Fqn.fromString("/"));
cache_.stop();
cache1_.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 void sanityCheck() throws InterruptedException
{
Thread.sleep(100);
if (listener_.getActivationCount() == 0 || listener_.getPassivationCount() == 0)
{
fail("Sanity checking for passivation failed. Counters: activation - " + listener_.getActivationCount()
+ " passivation - " + listener_.getPassivationCount());
}
listener_.reset();
}
public void testFindAfterPassivation() throws Exception
{
log.info("testFindAfterPassivation() ....");
String id = "person";
Person joe = createPerson("Joe Black", 20);
cache_.attach(id, joe);
Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
assertFalse("Node should be evicted ",
cache_.getCache().getRoot().hasChild(new Fqn(id)));
Person p = (Person) cache1_.find(id);
assertNotNull("Person on remote node ", p);
sanityCheck();
}
public void testRemoteSetAfterPassivation() throws Exception
{
log.info("testFindAfterPassivation() ....");
String id = "person";
Person joe = createPerson("Joe Black", 20);
cache_.attach(id, joe);
Person p = (Person) cache1_.find(id);
assertNotNull("Person on remote node ", p);
Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
assertFalse("Node should be evicted ",
cache_.getCache().getRoot().hasChild(new Fqn(id)));
Address addr = new Address();
addr.setCity("Taipei");
addr.setZip(106);
p.setAddress(addr);
sanityCheck();
}
public void testMultipleReference() throws Exception
{
log.info("testMultipleReference() ...");
String id1 = "/person/ben";
cache_.attach(id1, createPerson("Ben Hogan", 51));
Person joe = (Person) cache_.find(id1);
String id = "/person/joe";
cache_.attach(id, createPerson("Joe Black", 31));
Person ben = (Person) cache_.find(id);
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) cache1_.find(id)).getAddress();
Address add2 = (Address) ((Person) cache1_.find(id)).getAddress();
assertEquals(add1.getCity(), add2.getCity());
addr.setCity("Santa Clara");
assertEquals(add1.getCity(), add2.getCity());
Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
assertFalse("Node should be evicted ",
cache_.getCache().getRoot().hasChild(new Fqn(id)));
assertEquals("City is ", "Santa Clara", add2.getCity());
Thread.sleep(9100); // default is 3 seconds so joe should have been passivated.
assertEquals("City is ", "Santa Clara", joe.getAddress().getCity());
cache_.detach(id);
cache_.detach(id1);
}
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) cache1_.find("/person/joe")).getAddress();
Address add2 = (Address) ((Person) cache1_.find("/person/ben")).getAddress();
assertEquals(add1.getCity(), add2.getCity());
addr.setCity("Santa Clara");
assertEquals(add1.getCity(), add2.getCity());
Thread.sleep(9100);
// Remove pojo joe will relocate the address field to ben's
cache_.detach("/person/joe");
add2 = (Address) ((Person) cache1_.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(9100);
assertEquals("parent", ((Link) cache1_.find("/link/parent")).getName());
assertEquals("child", ((Link) cache1_.find("/link/parent")).getLink().getName());
}
public static Test suite() throws Exception
{
return new TestSuite(ReplicatedTest.class);
}
public static void main(String[] args) throws Exception
{
junit.textui.TestRunner.run(suite());
}
public class MyCacheListener extends AbstractCacheListener
{
int activation = 0;
int passivation = 0;
public int getActivationCount()
{
return activation;
}
public int getPassivationCount()
{
return passivation;
}
public void reset()
{
activation = 0;
passivation = 0;
}
public void nodeActivated(Fqn fqn, boolean pre)
{
if (!pre)
{
System.out.println("nodeActivated: " + fqn);
activation++;
}
}
public void nodePassivated(Fqn fqn, boolean pre)
{
if (pre)
{
System.out.println("nodePassivated: " + fqn);
passivation++;
}
}
}
}
1.1 date: 2007/01/13 15:55:13; author: bwang; state: Exp;JBossCache/tests/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);
}
}
More information about the jboss-cvs-commits
mailing list