[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/pojo ...

Ben Wang bwang at jboss.com
Sat Jan 13 10:55:02 EST 2007


  User: bwang   
  Date: 07/01/13 10:55:02

  Added:       tests/functional/org/jboss/cache/pojo                       
                        RecursiveRefTest.java ReplicatedByteTest.java
                        ReplicatedCircularGraphTest.java LocalTxTest.java
                        NewLocalTest.java TxUndoTest.java
                        ReplicatedPutWithBulkRemoveTest.java
                        ReplicatedTest.java ArrayTest.java
                        ObjectGraphTest.java CircularGraphTest.java
                        CachedTypeTest.java NewReplicatedTest.java
                        ReplicatedTxTest.java LocalTest.java
                        TestingUtil.java EnumTest.java
                        ReplicatedObjectGraphTest.java
                        LocalConcurrentTest.java NonAspectizedTest.java
                        NewReplicatedTxTest.java
                        ReplicatedSerializableTest.java
                        ReplicatedNonSerializableTest.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:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/RecursiveRefTest.java
  
  Index: RecursiveRefTest.java
  ===================================================================
  package org.jboss.cache.pojo;
  
  import junit.framework.TestCase;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.test.IdObject;
  import org.jboss.cache.pojo.test.ValueObject;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.util.ArrayList;
  import java.util.HashSet;
  
  public class RecursiveRefTest extends TestCase
  {
     private static final String CONFIG_FILENAME = "META-INF/local-service.xml";
     private PojoCache cache;
     Log log = LogFactory.getLog(RecursiveRefTest.class);
  
     private Map cachedMap;
  
     public RecursiveRefTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        boolean toStart = false;
        cache = PojoCacheFactory.createCache(CONFIG_FILENAME, toStart);
        cache.start();
        cache.attach("/aop/test", new HashMap());
        cachedMap = (Map) cache.find("/aop/test");
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
     }
  
     public void XtestDummy()
     {
  
     }
  
     /**
      * This test feature is not currently supported now since we can't support key is non-primitive object
      * that overrides the hashCode().
      */
     public void testRecuriveMapKey()
     {
        try
        {
           IdObject id = new IdObject("1");
           ValueObject value = new ValueObject(id, 3.0f);
           cachedMap.put(id, value);
        } // try
        catch (Exception x)
        {
           x.printStackTrace();
           fail("testFailed");
        } // catch
     } // initCache
  
     public void testRecursiveList() throws Exception
     {
        ArrayList list = new ArrayList();
        list.add("1");
        cache.attach("list", list);
        list = (ArrayList)cache.find("list");
        list.add(list);
        System.out.println(list.toString());
     }
  
     public void testRecursiveSet() throws Exception
     {
        HashSet set = new HashSet();
        set.add("1");
        cache.attach("set", set);
        set = (HashSet)cache.find("set");
        set.add(set);
        System.out.println(set.toString());
     }
  
     public void testRecursiveMap() throws Exception
     {
        HashMap map = new HashMap();
        map.put("1", "1");
        cache.attach("map", map);
        map = (HashMap)cache.find("map");
        map.put("2", map);
        System.out.println(map.toString());
     }
  
  } // class TestRunner
  
  
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/ReplicatedByteTest.java
  
  Index: ReplicatedByteTest.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;
  
  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.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.test.Resource;
  
  /**
   * Replicate POJO with byte array field.
   *
   * @author Ben Wang
   */
  public class ReplicatedByteTest extends TestCase
  {
     Log log = LogFactory.getLog(ReplicatedByteTest.class);
     PojoCache cache, cache1;
  
  
     public ReplicatedByteTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        boolean toStart = false;
        cache = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache.start();
        cache1 = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache1.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache.stop();
        cache1.stop();
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
        Resource res = new Resource();
        res.setName("mapping");
        res.setConnection("wire");
        String s = "This is a test";
        byte by = 1;
        byte[] b = new byte[1];
        b[0] = by;
        res.setByte(b);
  
        cache.attach("resource", res);
  
        Resource res1 = (Resource)cache1.find("resource");
        assertEquals("Name ", res.getName(), res1.getName());
  
        assertEquals("byte ", res.getByte()[0], res1.getByte()[0]);
  
        // field modification
        by = 2;
        b[0] = by;
        res1.setByte(b);
        assertEquals("Name ", res.getName(), res1.getName());
  
        assertEquals("byte ", res.getByte()[0], res1.getByte()[0]);
  
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ReplicatedByteTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/ReplicatedCircularGraphTest.java
  
  Index: ReplicatedCircularGraphTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  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.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Link;
  import org.jboss.cache.pojo.test.NodeManager;
  import org.jboss.cache.Fqn;
  
  import java.util.List;
  import java.util.ArrayList;
  
  /**
   * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
   *
   * @author Ben Wang
   */
  
  public class ReplicatedCircularGraphTest extends TestCase
  {
     Log log = LogFactory.getLog(ReplicatedCircularGraphTest.class);
     PojoCache cache1;
     PojoCache cache2;
  
     public ReplicatedCircularGraphTest(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.getCache().removeNode(Fqn.fromString("/"));
        cache1.stop();
        cache2.stop();
     }
  
     private PojoCache createCache(String name) throws Exception
     {
        boolean toStart = false;
        PojoCache tree = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        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;
     }
  
     public void testCircularReference1() throws Exception
     {
  //        try {Thread.sleep(10000); } catch (Exception e) {};
        log.info("testCircularReference1() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        parent.setLink(child);
        child.setLink(parent);
        cache1.attach("/link/parent", parent);
        TestingUtil.sleepThread(100);
        assertEquals("parent", ((Link) cache1.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache1.find("/link/parent")).getLink().getName());
        assertEquals("parent", ((Link) cache2.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache2.find("/link/parent")).getLink().getName());
        ((Link) cache2.find("/link/parent")).setLink(null);
        assertNull("Child should be null", ((Link) cache2.find("/link/parent")).getLink());
        Link link = (Link) cache1.detach("/link/parent");
        assertNotNull("Link should not be null ", link);
        System.out.println("Link: " + link);
     }
  
     public void testCircularReference2() throws Exception
     {
  //        try {Thread.sleep(10000); } catch (Exception e) {};
        log.info("testCircularReference2() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        cache1.attach("/link/parent", parent);
        parent.setLink(child);
        child.setLink(parent);
        assertEquals("parent", ((Link) cache1.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache1.find("/link/parent")).getLink().getName());
        assertEquals("parent", ((Link) cache2.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache2.find("/link/parent")).getLink().getName());
        ((Link) cache2.find("/link/parent")).setLink(null);
        assertNull("Child should be null", ((Link) cache2.find("/link/parent")).getLink());
        Link link = (Link) cache1.detach("/link/parent");
        assertNotNull("Link should not be null ", link);
     }
  
     public void testCircularReference3() throws Exception
     {
  //        try {Thread.sleep(10000); } catch (Exception e) {};
        log.info("testCircularReference3() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        cache1.attach("/link/parent", parent);
        cache1.attach("/link/child", child);
        TestingUtil.sleepThread(100);
        parent.setLink(child);
        child.setLink(parent);
  
        Link p1 = (Link) cache1.find("/link/parent");
        Link c1 = (Link) cache1.find("/link/child");
        assertEquals("parent", p1.getName());
        assertEquals("child", p1.getLink().getName());
        assertEquals("child", c1.getName());
        assertEquals("parent", c1.getLink().getName());
  
        Link p2 = (Link) cache1.find("/link/parent");
        Link c2 = (Link) cache1.find("/link/child");
  
        assertEquals("parent", p2.getName());
        assertEquals("child", p2.getLink().getName());
        assertEquals("child", c2.getName());
        assertEquals("parent", c2.getLink().getName());
  
        p2.setLink(null);
        assertNull("Child should be null", p2.getLink());
        Link link = (Link) cache1.detach("/link/parent");
        assertNotNull("Link should not be null ", link);
     }
  
     /**
      * Setting the circular relationship and also as a shared object.
      *
      * @throws Exception
      */
     public void testCircularReference4() throws Exception
     {
  //        try {Thread.sleep(10000); } catch (Exception e) {};
        log.info("testCircularReference3() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        parent.setLink(child);
        child.setLink(parent);
  
        List<Link> list = new ArrayList<Link>();
        list.add(parent);
  
        cache1.attach("/list", list);
        cache1.attach("/alias", list);
  
        TestingUtil.sleepThread(100);
        List list1 = (List) cache2.find("/list");
        List list2 = (List) cache2.find("/alias");
  
        assertEquals("parent", ((Link) list1.get(0)).getName());
        assertEquals("child", ((Link) list2.get(0)).getLink().getName());
     }
  
     public void testCircularAndSharedReferences() throws Exception
     {
        log.info("testCircularAndSharedReferences() ...");
        NodeManager pm_ = new NodeManager();
  
        pm_.setRootNode("root");
        pm_.addNode("root", "kanto");
        pm_.addNode("root.kanto", "tokyo");
        pm_.addNode("root.kanto", "kanagawa");
  
        cache1.attach("/propagation", pm_);
        assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN());
        pm_.addNode("root.kanto.tokyo", "hadanshita");
        assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
  
        NodeManager pm2_ = (NodeManager) cache2.find("/propagation");
        assertEquals("kanagawa", pm2_.findNode("root.kanto.kanagawa").getNodeRDN());
        assertEquals("hadanshita", pm2_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
  
  /*
        System.out.println("\n\n");
        System.out.println("---------------------------------------------");
        System.out.println("Initial pm state");
        System.out.println("---------------------------------------------");
        pm_.printNodes();
  
        System.out.println("\n\n");
        System.out.println("---------------------------------------------");
        System.out.println("Initial cache content");
        System.out.println(cache_.printDetails());
        System.out.println("---------------------------------------------");
  */
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ReplicatedCircularGraphTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(ReplicatedCircularGraphTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/LocalTxTest.java
  
  Index: LocalTxTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  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.transaction.DummyTransactionManager;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Address;
  
  import javax.naming.Context;
  import javax.naming.NamingException;
  import javax.naming.InitialContext;
  import javax.transaction.UserTransaction;
  import javax.transaction.SystemException;
  import javax.transaction.NotSupportedException;
  import javax.transaction.Transaction;
  import javax.transaction.RollbackException;
  import java.util.Properties;
  import java.util.List;
  import java.util.ArrayList;
  
  /**
   */
  
  public class LocalTxTest extends TestCase
  {
     Log log = LogFactory.getLog(LocalTxTest.class);
     PojoCache cache;
     final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
     DummyTransactionManager tx_mgr;
     Throwable t1_ex, t2_ex;
     long start = 0;
  
  
     public LocalTxTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache = PojoCacheFactory.createCache(configFile, toStart);
        cache.start();
  
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
  
        tx_mgr = DummyTransactionManager.getInstance();
        t1_ex = t2_ex = null;
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache.stop();
  
        DummyTransactionManager.destroy();
     }
  
  //   public void testDummy() {}
  
     UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
     {
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.jboss.cache.transaction.DummyContextFactory");
        return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
     }
  
     private Person createPerson(String id, String name, int age)
     {
        Person p = new Person();
        p.setName(name);
        p.setAge(age);
        cache.attach(id, p);
        return p;
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
        UserTransaction tx = getTransaction();
        tx.begin();
        Person p = createPerson("/person/test1", "Harald Gliebe", 32);
        tx.commit();
        tx.begin();
        p.setName("Benoit");
        tx.commit();
        Person p1 = (Person) cache.find("/person/test1");
        assertEquals("Benoit", p.getName());
        assertEquals("Benoit", p1.getName());
        tx.begin();
        p1.setAge(61);
        tx.commit();
        assertEquals(61, p.getAge());
        assertEquals(61, p1.getAge());
     }
  
     public void testModification() throws Exception
     {
        UserTransaction tx = getTransaction();
        tx.begin();
        Person p = createPerson("/person/test2", "Harald", 32);
        p.setName("Harald Gliebe");
        tx.commit();
        Person p1 = (Person) cache.find("/person/test2");
        tx.begin();
        p1.setName("Benoit");
        tx.commit();
        assertEquals(p.getName(), "Benoit");
        assertEquals(p1.getName(), "Benoit");
        tx.begin();
        p1.setName("Harald");
        tx.rollback();
        assertEquals(p.getName(), "Benoit");
        assertEquals(p1.getName(), "Benoit");
     }
  
     public void testConcurrentSimplePuts() throws Exception
     {
        Thread t1 = new Thread("t1")
        {
           Transaction tx;
  
           public void run()
           {
              try
              {
                 Person p = (Person) cache.find("/person/test6");
                 Address addr = new Address();
                 addr.setCity("San Jose");
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 // Note that this will create a write lock on p (on the JBossInternal node)
                 p.setAddress(addr);
                 TestingUtil.sleepThread(17000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
              }
           }
        };
  
        Thread t2 = new Thread("t2")
        {
           Transaction tx;
  
           public void run()
           {
              UserTransaction tx = null;
              try
              {
                 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
                 Person p = (Person) cache.find("/person/test6");
                 Address addr = new Address();
                 addr.setCity("Santa Clara");
                 tx = getTransaction();
                 tx.begin();
                 p.setAddress(addr);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (org.jboss.cache.pojo.PojoCacheException tex)
              {
                 // This will be a timeout exception. OK.
              }
              catch (Exception ex)
              {
  //               t2_ex = ex;
                 if(tx != null)
                 {
                    try
                    {
                       tx.rollback();
                    } catch (SystemException e)
                    {
                       e.printStackTrace();
                       t2_ex = e;
                    }
                 }
                 t2_ex = ex;
                 ex.printStackTrace();
              }
           }
        };
  
        Person p = createPerson("/person/test6", "p6", 50);
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t2_ex != null)
           fail("Thread1 failed: " + t2_ex);
        if (t1_ex != null)
           fail("Thread2 failed: " + t1_ex);
  
        assertEquals("City ", "San Jose", p.getAddress().getCity());
     }
  
     public void testConcurrentPuts() throws Exception
     {
        Thread t1 = new Thread("t1")
        {
           Transaction tx;
  
           public void run()
           {
              try
              {
                 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 lang.add("German");
                 TestingUtil.sleepThread(17000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
              }
           }
        };
  
        Thread t2 = new Thread("t2")
        {
           Transaction tx;
  
           public void run()
           {
              UserTransaction tx = null;
              try
              {
                 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
                 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                 tx = getTransaction();
                 tx.begin();
                 lang.add("English");
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (org.jboss.cache.lock.TimeoutException tex)
              {
                 //
              }
              catch (Exception ex)
              {
                 if(tx != null)
                 {
                    try
                    {
                       tx.rollback();
                    } catch (SystemException e)
                    {
                       e.printStackTrace();
                       t2_ex = e;
                    }
                 }
                 t2_ex = ex;
                 ex.printStackTrace();
              }
           }
        };
  
        Person p = createPerson("/person/test6", "p6", 50);
        List<String> lang = new ArrayList<String>();
        lang.add("German");
        p.setLanguages(lang);
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t2_ex != null)
           fail("Thread1 failed: " + t2_ex);
        if (t1_ex != null)
           fail("Thread2 failed: " + t1_ex);
  
        int size = ((Person) cache.find("/person/test6")).getLanguages().size();
        assertEquals("number of languages ",
                2, size);
     }
  
     public void testConcurrentPutsNoWait() throws Exception
     {
        Thread t1 = new Thread("t1")
        {
           UserTransaction tx;
  
           public void run()
           {
              try
              {
                 for(int i=0; i< 100; i++)
                 {
                    String id = "/p1/test7";
                    Person p = createPerson(id, "p6", 50);
                    cache.detach(id);
                    p = createPerson(id, "p6", 51);
                    List<String> lang = new ArrayList<String>();
                    tx = getTransaction();
                    tx.begin();
                    lang.add("German");
                    lang.add("English");
                    try {
                      p.setLanguages(lang);
                    } catch (PojoCacheException ex)
                    {
                       ex.printStackTrace();
                    }
                    tx.commit();
                    TestingUtil.sleepThread(20); // give Thread1 time to createPerson
                 }
              }
              catch (RollbackException rollback)
              {
                 rollback.printStackTrace();
              }
              catch (PojoCacheException pe)
              {
                 pe.printStackTrace();
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
              }
           }
        };
  
        Thread t2 = new Thread("t2")
        {
           UserTransaction tx;
  
           public void run()
           {
              try
              {
                 for(int i=0; i< 100; i++)
                 {
                    String id = "/p1/test8";
                    Person p = createPerson(id, "p6", 50);
                    cache.detach(id);
                    p = createPerson(id, "p6", 51);
                    List<String> lang = new ArrayList<String>();
                    tx = getTransaction();
                    tx.begin();
                    lang.add("German");
                    lang.add("English");
                       try {
                         p.setLanguages(lang);
                       } catch (PojoCacheException ex)
                       {
                          ex.printStackTrace();
                       }
                    tx.commit();
                    TestingUtil.sleepThread(20); // give Thread1 time to createPerson
                    }
              }
              catch (RollbackException rollback)
              {
                 rollback.printStackTrace();
              }
              catch (PojoCacheException pe)
              {
                 pe.printStackTrace();
              }
              catch (Exception ex)
              {
                 t2_ex = ex;
                 ex.printStackTrace();
              }
           }
        };
  
        Person p1 = createPerson("/p1/test6", "p6", 50);
        Person p2 = createPerson("/p2/test6", "p6", 50);
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t2_ex != null)
           fail("Thread1 failed: " + t2_ex);
        if (t1_ex != null)
           fail("Thread2 failed: " + t1_ex);
  
  //      int size = ((Person) cache.find("/p1/test7")).getLanguages().size();
  //      assertEquals("number of languages ",
  //              2, size);
     }
  
     void log(String s)
     {
        long now;
        if (start == 0)
           start = System.currentTimeMillis();
        now = System.currentTimeMillis();
  
        System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
     }
  
  
     public static Test suite() throws Exception
     {
        return new TestSuite(LocalTxTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(LocalTxTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/NewLocalTest.java
  
  Index: NewLocalTest.java
  ===================================================================
  package org.jboss.cache.pojo;
  
  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.Fqn;
  import org.jboss.cache.pojo.test.Person;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Map;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class NewLocalTest extends TestCase
  {
     Log log_ = LogFactory.getLog(NewLocalTest.class);
     PojoCache cache_;
  
     public NewLocalTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     public void XtestBadFqn() throws Exception
     {
        log_.info("testBadFqn() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
        Person result = (Person) cache_.detach("/a");
        assertEquals(" ", test, result);
        result.setAge(20);
  
        try
        {
           cache_.attach(InternalConstant.JBOSS_INTERNAL_STRING, test);
           fail("putObject under JBoss_Internal should fail");
        }
        catch (IllegalArgumentException iex)
        {
           // ok
        }
  
        try
        {
           cache_.detach(InternalConstant.JBOSS_INTERNAL_STRING);
           fail("putObject under JBoss_Internal should fail");
        }
        catch (IllegalArgumentException iex)
        {
           // ok
        }
     }
  
     public void testPutRemove() throws Exception
     {
        log_.info("testPutRemove() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
        Person result = (Person) cache_.find("/a");
        assertEquals(" ", test, result);
        result.setAge(20);
        cache_.detach("/a");
        assertNull("Object should be null ", cache_.find("/a"));
        assertEquals("Age should be updated as ", 20, test.getAge());
     }
  
     public void testPutRemoveNodeExistence() throws Exception
     {
        log_.info("testPutRemove() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("person", test);
        Person result = (Person) cache_.find("person");
        assertEquals(" ", test, result);
        result.setAge(20);
        cache_.detach("person");
        assertNull("Object should be null ", cache_.find("person"));
        assertEquals("Age should be updated as ", 20, test.getAge());
  
        assertNull("DataNode should not exisit ", cache_.getCache().getRoot().getChild(Fqn.fromString("person")));
     }
  
     public void testRemoveProxyList() throws Exception
     {
        log_.info("testRemoveProxyList() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        ArrayList list = new ArrayList();
        list.add("English");
        list.add("Taiwanese");
        test.setLanguages(list);
        cache_.attach("/a", test);
        Person result = (Person) cache_.find("/a");
        assertEquals(" ", test, result);
  
        assertTrue("Instance of proxyclass ", result.getLanguages() instanceof ClassProxy);
  
        Person r1 = (Person) cache_.detach("/a");
  
        assertEquals("Same instance ", result, r1);
        assertFalse("Instance of proxyclass ", result.getLanguages() instanceof ClassProxy);
        assertEquals("Same Collection instance", list, result.getLanguages());
     }
  
     public void testRemoveProxySet() throws Exception
     {
        log_.info("testRemoveProxySet() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashSet set = new HashSet();
        set.add("Golf");
        set.add("Cooking");
        test.setSkills(set);
        cache_.attach("/a", test);
        Person result = (Person) cache_.find("/a");
        assertEquals(" ", test, result);
  
        assertTrue("Instance of proxyclass ", result.getSkills() instanceof ClassProxy);
  
        Person r1 = (Person) cache_.detach("/a");
  
        assertEquals("Same instance ", result, r1);
        assertFalse("Instance of proxyclass ", result.getSkills() instanceof ClassProxy);
        assertEquals("Same Collection instance", set, result.getSkills());
     }
  
     public void testRemoveProxyMap() throws Exception
     {
        log_.info("testRemoveProxyMap() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
  
        HashMap map = new HashMap();
        map.put("1", "Golf");
        map.put("2", "Surfing");
        test.setHobbies(map);
  
        cache_.attach("/a", test);
        Person result = (Person) cache_.find("/a");
        assertEquals(" ", test, result);
  
        assertTrue("Instance of proxyclass ", result.getHobbies() instanceof ClassProxy);
  
        Person r1 = (Person) cache_.detach("/a");
  
        assertEquals("Same instance ", result, r1);
        assertFalse("Instance of proxyclass ", result.getHobbies() instanceof ClassProxy);
        assertEquals("Same Collection instance", map, result.getHobbies());
     }
  
     public void testFindObjects() throws Exception
     {
        log_.info("testFindObjects() ....");
        Map map = cache_.findAll("/");
        assertEquals("Objects size should be ", 0, map.size());
        Person ben = new Person();
        ben.setName("Ben");
        ben.setAge(10);
        cache_.attach("/a/b/c", ben);
        cache_.attach("/e", ben); // multiple keys, same pojo
        Person joe = new Person();
        joe.setName("Joe");
        joe.setAge(10);
        cache_.attach("/f/joe", joe);
        map = cache_.findAll("/");
        assertEquals("Objects size should be ", 3, map.size());
  
        map = cache_.findAll("/a");
        assertEquals("Objects size should be ", 1, map.size());
        cache_.detach("/e");
        map = cache_.findAll("/");
        assertEquals("Objects size should be ", 2, map.size());
  
        map = cache_.findAll(null); // should everything.
        assertEquals("Objects size should be ", 2, map.size());
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(NewLocalTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/TxUndoTest.java
  
  Index: TxUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  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.test.Person;
  import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.aop.Advised;
  import org.jboss.aop.advice.Interceptor;
  
  import javax.transaction.TransactionManager;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class TxUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(TxUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
  
     public TxUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
  
        tx_mgr.begin();
        cache_.attach("/a", test);
        tx_mgr.getTransaction().rollback();
        assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
     }
  
     private boolean hasCacheInterceptor(Object pojo)
     {
        Interceptor[] interceptors = null;
        try {
           interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
        } catch (Exception ex)
        {
           return false;
        }
  
        for(int i=0; i < interceptors.length; i++)
        {
           if(interceptors[i] instanceof CacheFieldInterceptor)
              return true;
        }
        return false;
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
  
        tx_mgr.begin();
        cache_.detach("/a");
        tx_mgr.getTransaction().rollback();
  
        assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
     }
  
     public void testSimpleTxWithRollback3() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        tx_mgr.begin();
        cache_.attach("/a", test);
        cache_.detach("/a");
        tx_mgr.getTransaction().rollback();
  
        assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(TxUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(TxUndoTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/ReplicatedPutWithBulkRemoveTest.java
  
  Index: ReplicatedPutWithBulkRemoveTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  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.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.pojo.test.Person;
  
  import javax.naming.Context;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Properties;
  
  /**
   * Test for attach with existing pojo for bulkd remove for speed.
   *
   * @author Ben Wang
   */
  
  public class ReplicatedPutWithBulkRemoveTest extends TestCase
  {
     Log log_ = LogFactory.getLog(ReplicatedPutWithBulkRemoveTest.class);
     PojoCache cache_;
     PojoCache cache1_;
  
     public ReplicatedPutWithBulkRemoveTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache1_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache_.start();
        cache1_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
        cache1_.stop();
     }
  
     public void testPutPutLocal() throws Exception
     {
        log_.info("testPutPut() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        Address addr = new Address();
        addr.setZip(95123);
        addr.setCity("Sunnyvale");
        test.setAddress(addr);
        cache_.attach("/a", test);
        Person result = (Person) cache_.find("/a");
        assertEquals(" ", test, result);
  
        Person joe = new Person();
        joe.setName("joe");
        joe.setAge(20);
        cache_.attach("/a", joe);
        Person joe1 = (Person) cache_.find("/a");
        assertEquals("Age should be ", 20, joe1.getAge());
  
        assertEquals("Age should be ", 10, result.getAge());
        assertEquals("Zip should be ", 95123, result.getAddress().getZip());
  
        // Try to re-use the pojo
        cache_.attach("/a", test);
        Person result1 = (Person) cache_.find("/a");
        assertEquals("Zip should be ", 95123, result1.getAddress().getZip());
  
     }
  
     public void testPutPut() throws Exception
     {
        log_.info("testPutPut() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        Address addr = new Address();
        addr.setZip(95123);
        addr.setCity("Sunnyvale");
        test.setAddress(addr);
        cache_.attach("/a", test);
        Person result = (Person) cache_.find("/a");
        assertEquals(" ", test, result);
  
        Person result1 = (Person) cache1_.find("/a");
        assertEquals("Age should be ", 10, result1.getAge());
        assertEquals("Zip should be ", 95123, result1.getAddress().getZip());
  
        Person joe = new Person();
        joe.setName("joe");
        joe.setAge(20);
        cache_.attach("/a", joe);
        Person joe1 = (Person) cache_.find("/a");
        assertEquals("Age should be ", 20, joe1.getAge());
  
        assertEquals("Age should be ", 10, result.getAge());
        assertEquals("Zip should be ", 95123, result.getAddress().getZip());
     }
  
     Address getAddress(String city)
     {
        Address addr = new Address();
        addr.setCity(city);
        addr.setZip(95123);
        addr.setStreet("Sunnyvale");
        return addr;
     }
  
     public void testPutCollection() throws Exception
     {
        log_.info("testPutCollection() ....");
        Person p1 = new Person();
        p1.setName("Ben");
        p1.setAge(10);
        p1.setAddress(getAddress("Sunnyvale"));
  
        List<Address> lang = new ArrayList<Address>();
        lang.add(getAddress("Taipei"));
        lang.add(getAddress("Tainan"));
        p1.setLanguages(lang);
  
        cache_.attach("/a", p1);
        Person result = (Person) cache_.find("/a");
        assertEquals(" ", p1, result);
  
        Person result1 = (Person) cache1_.find("/a");
        assertEquals("Age should be ", 10, result1.getAge());
        assertEquals("Zip should be ", 95123, result1.getAddress().getZip());
  
        Person p2 = new Person();
        p2.setName("joe");
        p2.setAge(20);
        cache_.attach("/a", p2);
        Person joe1 = (Person) cache_.find("/a");
        assertEquals("Age should be ", 20, joe1.getAge());
  
        // put p1 again
        cache_.attach("/a", p1);
  
        // remove p1
        Person p3 = (Person) cache_.detach("/a");
        assertEquals("Zip should be ", 95123, p3.getAddress().getZip());
  
        assertEquals("Age should be ", 10, result.getAge());
        assertEquals("Zip should be ", 95123, result.getAddress().getZip());
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ReplicatedPutWithBulkRemoveTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(ReplicatedPutWithBulkRemoveTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/ReplicatedTest.java
  
  Index: ReplicatedTest.java
  ===================================================================
  package org.jboss.cache.pojo;
  
  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.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Student;
  
  import java.util.List;
  
  /**
   * Replicated test that use a tester wrapper. Future new test should use NewReplicatedAopTest
   *
   * @author Ben Wang
   */
  public class ReplicatedTest extends TestCase
  {
     Log log = LogFactory.getLog(ReplicatedTest.class);
     PojoCache cache, cache1;
  
  
     public ReplicatedTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        boolean toStart = false;
        cache = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache.start();
        cache1 = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache1.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache.stop();
        cache1.stop();
     }
  
  //   public void testDummy() {}
  
     private Person createPerson(String id, String name, int age)
     {
        Person p = new Person();
        p.setName(name);
        p.setAge(age);
        cache.attach(id, p);
        return p;
     }
  
     private Student createStudent(String id, String name, int age, String grade)
     {
        Student p = new Student();
        p.setName(name);
        p.setAge(age);
        p.setYear(grade);
        cache.attach(id, p);
        return p;
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
        Person ben = createPerson("/person/test1", "Ben Wang", 40);
        assertEquals("Ben Wang", ben.getName());
        assertEquals("Ben Wang", ((Person) cache1.find("/person/test1")).getName());
        cache.detach("/person/test1");
     }
  
  
     public void testDynamicRefSwapping() throws Exception
     {
        Person person = createPerson("/person/test3", "Joe", 32);
        try
        {
           person.setAge(30);
           List med = person.getMedication();
           assertNull("Medication should be null ", med);
           person.setAge(61);
           med = person.getMedication();
           assertEquals("Medication ", (Object) "Lipitor", (Object) med.get(0));
           assertEquals("Medication on cache1 ", "Lipitor",
                   person.getMedication().get(0));
  
           person.setAge(71);
           assertEquals("Medication ", "Vioxx", med.get(1));
           assertEquals("Medication on cache1 ", "Vioxx",
                   ((Person) cache1.find("/person/test3")).getMedication().get(1));
           cache.detach("/person/test3");
  
        } catch (Exception e)
        {
           // should be thrown
        }
     }
  
     public void testTransient() throws Exception
     {
        log.info("testTransient() ....");
        Person ben = createPerson("/person/test1", "Ben Wang", 40);
        ben.setCurrentStatus("Idle");
        assertEquals("Cache 1 ", "Idle", ben.getCurrentStatus());
        assertEquals("Cache 2 ", "Active",
                ((Person) cache1.find("/person/test1")).getCurrentStatus());
        cache.detach("/person/test1");
     }
  
     public void testModification() throws Exception
     {
        Person ben = createPerson("/person/test2", "Ben Wang", 40);
        ben.setName("Harald Gliebe");
        assertEquals(ben.getName(), "Harald Gliebe");
        assertEquals(((Person) cache1.find("/person/test2")).getName(), "Harald Gliebe");
        cache.detach("/person/test2");
     }
  
     public void testInheritance() throws Exception
     {
        Student joe = createStudent("/person/joe", "Joe", 32, "Senior");
        joe.setName("Joe Black");
        assertEquals(joe.getName(), "Joe Black");
        Student joe1 = (Student) cache1.find("/person/joe");
        assertEquals(joe1.getName(), "Joe Black");
        joe1.setYear("Junior");
        assertEquals(joe.getYear(), "Junior");
        assertEquals(joe1.getYear(), "Junior");
        cache.detach("/person/joe");
        cache.detach("/person/joe");
     }
  
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ReplicatedTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/ArrayTest.java
  
  Index: ArrayTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  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.test.Person;
  import org.jboss.cache.pojo.test.ArrayObject;
  
  /**
   * Basic PojoCache test case.
   *
   * @author Ben Wang
   */
  
  public class ArrayTest extends TestCase
  {
     Log log = LogFactory.getLog(ArrayTest.class);
     PojoCache cache_;
  
     public ArrayTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
        ArrayObject ao = new ArrayObject();
        Person joe = new Person();
        joe.setName("Joe");
        joe.setAge(19);
  
        Person ben = new Person();
        ben.setName("Ben");
        ben.setAge(19);
  
        ao.setPerson(0, joe);
  
        cache_.attach("ao", ao);
  
        // TODO This should trigger a write on team but instead it does a read only. Why?
        ao.setPerson(1, ben);
  
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ArrayTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(ArrayTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/ObjectGraphTest.java
  
  Index: ObjectGraphTest.java
  ===================================================================
  package org.jboss.cache.pojo;
  
  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.pojo.test.Address;
  import org.jboss.cache.pojo.test.Person;
  
  /**
   * Test object graph handling in PojoCache, e.g., circular reference, multiple reference, link, etc.
   *
   * @author Ben Wang
   */
  
  public class ObjectGraphTest extends TestCase
  {
     Log log = LogFactory.getLog(ObjectGraphTest.class);
     PojoCache cache_;
  
     public ObjectGraphTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     //   public void testDummy() {}
  
     protected Person createPerson(String name, int age)
     {
        Person p = new Person();
        p.setName(name);
        p.setAge(age);
        return p;
     }
  
     /**
      * Test shared sub-object. In diagram, it is a forest
      * where the sub-object is connected by two tree parents.
      */
     public void testMultipleReference() throws Exception
     {
        log.info("testMultipleReference() ...");
        cache_.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 = ((Person) cache_.find("/person/joe")).getAddress();
        Address add2 = ((Person) cache_.find("/person/ben")).getAddress();
        assertEquals(add1.getCity(), add2.getCity());
        addr.setCity("Santa Clara");
        assertEquals(add1.getCity(), add2.getCity());
     }
  
     public void testReAttach() 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 = ((Person) cache_.find("/person/joe")).getAddress();
        Address add2 = ((Person) cache_.find("/person/ben")).getAddress();
        assertEquals(add1.getCity(), add2.getCity());
        addr.setCity("Santa Clara");
        assertEquals(add1.getCity(), add2.getCity());
  
        cache_.detach("/person/joe");
  
        cache_.attach("/person/joe", joe);
     }
  
     /**
      * Reattach in reverse order
      *
      * @throws Exception
      */
     public void testReAttach1() 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 = ((Person) cache_.find("/person/joe")).getAddress();
        Address add2 = ((Person) cache_.find("/person/ben")).getAddress();
        assertEquals(add1.getCity(), add2.getCity());
        addr.setCity("Santa Clara");
        assertEquals(add1.getCity(), add2.getCity());
  
        cache_.detach("/person/ben");
        cache_.detach("/person/joe");
  
        cache_.attach("/person/ben", ben);
        cache_.attach("/person/joe", joe);
     }
  
     public void testRefCount() throws Exception
     {
        log.info("testRefCount() ...");
        Person joe = createPerson("Joe Black", 31);
        Person ben = createPerson("Ben Hogan", 51);
        cache_.attach("/person/joe", joe);
        cache_.attach("/person/ben", ben);
  
        Address addr = new Address();
        addr.setStreet("123 Albert Ave.");
        addr.setCity("Sunnyvale");
        addr.setZip(94087);
  
        joe.setAddress(addr);
        ben.setAddress(addr);
        // They share the sub-object: address
        Address add1 = ((Person) cache_.find("/person/joe")).getAddress();
        Address add2 = ((Person) cache_.find("/person/ben")).getAddress();
        assertEquals("Joe's address should still be valid ", "Sunnyvale", add1.getCity());
        assertEquals("Ben's address should still be valid ", "Sunnyvale", add2.getCity());
        assertEquals(add1.getCity(), add2.getCity());
        addr.setCity("Santa Clara");
        assertEquals(add1.getCity(), add2.getCity());
  
        cache_.detach("/person/joe");
        ben = (Person) cache_.find("/person/ben");
        addr = ben.getAddress();
        addr.setCity("Santa Clara");
        assertEquals("Ben's address should be changed ", "Santa Clara", addr.getCity());
  
        cache_.detach("/person/ben");
     }
  
     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 = ((Person) cache_.find("/person/joe")).getAddress();
        Address add2 = ((Person) cache_.find("/person/ben")).getAddress();
        assertEquals(add1.getCity(), add2.getCity());
        addr.setCity("Santa Clara");
        assertEquals(add1.getCity(), add2.getCity());
  
        // Remove pojo joe will relocate the address field to ben's
        cache_.detach("/person/joe");
        add2 = ((Person) cache_.find("/person/ben")).getAddress();
        assertEquals("City ", "Santa Clara", add2.getCity());
     }
  
     public void testRemoveObject2() throws Exception
     {
        log.info("testRemoveObject2() ...");
        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");
        cache_.attach("/person/john", createPerson("John Daly", 41));
        Person john = (Person) cache_.find("/person/john");
  
        Address addr = new Address();
        addr.setStreet("123 Albert Ave.");
        addr.setCity("Sunnyvale");
        addr.setZip(94087);
  
        Address addr1 = new Address();
        addr1.setStreet("123 Albert Ave.");
        addr1.setCity("San Jose");
        addr1.setZip(94087);
  
        // They share the sub-object: address
        log.info("testMultipleReference(): set Joe address");
        joe.setAddress(addr);
        log.info("testMultipleReference(): set Ben address");
        ben.setAddress(addr);
        log.info("testMultipleReference(): set John address");
        john.setAddress(addr);
  
        Address add1 = ((Person) cache_.find("/person/joe")).getAddress();
        Address add2 = ((Person) cache_.find("/person/ben")).getAddress();
        assertEquals(add1.getCity(), add2.getCity());
        addr.setCity("Santa Clara");
        assertEquals(add1.getCity(), add2.getCity());
  
        // Remove pojo joe will relocate the address field to ben's
        joe.setAddress(addr1);
        add2 = ((Person) cache_.find("/person/joe")).getAddress();
        assertEquals("City ", "San Jose", add2.getCity());
        add2 = ((Person) cache_.find("/person/ben")).getAddress();
        assertEquals("City ", "Santa Clara", add2.getCity());
        add2 = ((Person) cache_.find("/person/john")).getAddress();
        assertEquals("City ", "Santa Clara", add2.getCity());
     }
  
     public void XtestObjectIdentity() throws Exception
     {
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ObjectGraphTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/CircularGraphTest.java
  
  Index: CircularGraphTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  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.Fqn;
  import org.jboss.cache.pojo.test.Link;
  import org.jboss.cache.pojo.test.NodeManager;
  import org.jboss.cache.pojo.test.Person;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Test object graph handling in PojoCache, e.g., circular reference, multiple reference, link, etc.
   *
   * @author Ben Wang
   */
  
  public class CircularGraphTest extends TestCase
  {
     Log log = LogFactory.getLog(CircularGraphTest.class);
     PojoCache cache_;
  
     public CircularGraphTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        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;
     }
  
     /**
      * Pure parent child relationsip
      *
      * @throws Exception
      */
     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);
        assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
     }
  
     /**
      * Pure parent child relationsip. Attach both parent and child.
      *
      * @throws Exception
      */
     public void testCircularReference2() throws Exception
     {
        log.info("testCircularReference2() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        parent.setLink(child);
        child.setLink(parent);
        cache_.attach("/link/parent", parent);
        cache_.attach("/link/child", child);
        assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
     }
  
     /**
      * Pure parent child relationsip with detach
      *
      * @throws Exception
      */
     public void testCircularReference3() throws Exception
     {
        log.info("testCircularReference3() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        parent.setLink(child);
        child.setLink(parent);
        cache_.attach("/link/parent", parent);
        cache_.attach("/link/child", child);
        assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
        Link link = (Link) cache_.detach("/link/parent");
        cache_.detach("/link/child");
        assertEquals("child", link.getLink().getName());
        assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/parent"))));
        assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/child"))));
     }
  
     /**
      * Pure parent child relationsip with detach reversed
      *
      * @throws Exception
      */
     public void testCircularReference3a() throws Exception
     {
        log.info("testCircularReference3() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        parent.setLink(child);
        child.setLink(parent);
        cache_.attach("/link/parent", parent);
        cache_.attach("/link/child", child);
        assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
        cache_.detach("/link/child");
        Link link = (Link) cache_.detach("/link/parent");
        assertEquals("child", link.getLink().getName());
        assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/parent"))));
        assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/link/child"))));
     }
  
     /**
      * cache managed first before put in the relationsip.
      *
      * @throws Exception
      */
     public void testCircularReference4() throws Exception
     {
        log.info("testCircularReference4() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        cache_.attach("/link/parent", parent);
        cache_.attach("/link/child", child);
        parent.setLink(child);
        child.setLink(parent);
        assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
        Link link = (Link) cache_.detach("/link/parent");
        assertEquals("child", link.getLink().getName());
        assertNull("Cache should be null ", cache_.getCache().getRoot().getChild(Fqn.fromString("/parent")));
     }
  
     /**
      * Put first before settting the relationship
      *
      * @throws Exception
      */
     public void testCircularReference5() throws Exception
     {
        log.info("testCircularReference5() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        cache_.attach("/link/parent", parent);
        cache_.attach("/link/child", child);
        parent.setLink(child);
        child.setLink(parent);
        assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
        assertEquals("child", ((Link) cache_.find("/link/child")).getName());
        assertEquals("parent", ((Link) cache_.find("/link/child")).getLink().getName());
        Link link = (Link) cache_.detach("/link/parent");
        assertEquals("child", link.getLink().getName());
        assertNull("Cache should be null ", cache_.getCache().getRoot().getChild(Fqn.fromString("/parent")));
     }
  
     /**
      * Put first before settting the relationship
      *
      * @throws Exception
      */
     public void testCircularReference6() throws Exception
     {
        log.info("testCircularReference6() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        cache_.attach("/link/parent", parent);
        cache_.attach("/link/child", child);
        parent.setLink(child);
        child.setLink(parent);
        assertEquals("parent", ((Link) cache_.find("/link/parent")).getName());
        assertEquals("child", ((Link) cache_.find("/link/parent")).getLink().getName());
        assertEquals("child", ((Link) cache_.find("/link/child")).getName());
        assertEquals("parent", ((Link) cache_.find("/link/child")).getLink().getName());
        Link link = (Link) cache_.detach("/link/parent");
        assertEquals("child", link.getLink().getName());
        assertNull("Cache should be null ", (cache_.getCache().getRoot().getChild(Fqn.fromString("/parent"))));
  
        // re-attach
        cache_.attach("/link/parent", parent);
     }
  
     /**
      * Setting the circular relationship and also as a shared object.
      *
      * @throws Exception
      */
     public void testCircularReference7() throws Exception
     {
        log.info("testCircularReference7() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        parent.setLink(child);
        child.setLink(parent);
  
        List<Link> list = new ArrayList<Link>();
        list.add(parent);
  
        cache_.attach("/list", list);
        cache_.attach("/alias", list);
  
        List<Link> list1 = (List<Link>) cache_.find("/list");
        List<Link> list2 = (List<Link>) cache_.find("/alias");
  
        assertEquals("parent", list1.get(0).getName());
        assertEquals("child", list2.get(0).getLink().getName());
     }
  
     /**
      * @throws Exception
      */
     public void testCircularReference8() throws Exception
     {
        log.info("testCircularReference8() ...");
        Link parent = new Link("parent");
        Link child = new Link("child");
        parent.setLink(child);
        child.setLink(parent);
  
        cache_.attach("parent", parent);
        parent.setLink(child);// again
        child.setLink(parent);
     }
  
     public void testCircularAndSharedReferences() throws Exception
     {
        log.info("testCircularAndSharedReferences() ...");
        NodeManager pm_ = new NodeManager();
  
        pm_.setRootNode("root");
        pm_.addNode("root", "kanto");
        pm_.addNode("root.kanto", "tokyo");
        pm_.addNode("root.kanto", "kanagawa");
  
        assertEquals("kanagawa", pm_.findNode("root.kanto.kanagawa").getNodeRDN());
        cache_.attach("/propagation", pm_);
        pm_.addNode("root.kanto.tokyo", "hadanshita");
        assertEquals("hadanshita", pm_.findNode("root.kanto.tokyo.hadanshita").getNodeRDN());
  
        List list = pm_.findNode("root").getChildren();
        assertEquals("Root should have children of ", 1, list.size());
        pm_.printNodes();
     }
  
     public void XtestObjectIdentity() throws Exception
     {
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(CircularGraphTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(CircularGraphTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/CachedTypeTest.java
  
  Index: CachedTypeTest.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;
  
  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.test.Person;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.pojo.test.Student;
  import org.jboss.aop.proxy.ClassProxy;
  
  import java.util.List;
  import java.util.Map;
  import java.util.HashMap;
  import java.util.ArrayList;
  import java.util.Set;
  import java.util.HashSet;
  
  /**
   * Basic CachedType test case.
   *
   * @author Ben Wang
   */
  
  public class CachedTypeTest extends TestCase
  {
     Log log = LogFactory.getLog(CachedTypeTest.class);
  
     public CachedTypeTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
     }
  
     public void testInteger() throws Exception
     {
        CachedType t = new CachedType(int.class);
        assertTrue("Int ", t.isImmediate());
  
        t = new CachedType(Integer.class);
        assertTrue("Int ", t.isImmediate());
     }
  
     public void testShort() throws Exception
     {
        CachedType t = new CachedType(short.class);
        assertTrue("Short ", t.isImmediate());
  
        t = new CachedType(Short.class);
        assertTrue("Short ", t.isImmediate());
     }
  
     public void testString() throws Exception
     {
        CachedType t = new CachedType(String.class);
        assertTrue("String ", t.isImmediate());
     }
  
     public void testDouble() throws Exception
     {
        CachedType t = new CachedType(double.class);
        assertTrue("Double ", t.isImmediate());
  
        t = new CachedType(Double.class);
        assertTrue("Double ", t.isImmediate());
     }
  
     public void testChar() throws Exception
     {
        CachedType t = new CachedType(Character.class);
        assertTrue("Character ", t.isImmediate());
  
        t = new CachedType(Character.class);
        assertTrue("Character ", t.isImmediate());
     }
  
     public void testBoolean() throws Exception
     {
        CachedType t = new CachedType(boolean.class);
        assertTrue("Boolean ", t.isImmediate());
  
        t = new CachedType(Boolean.class);
        assertTrue("Boolean ", t.isImmediate());
     }
  
     public void testLong() throws Exception
     {
        CachedType t = new CachedType(long.class);
        assertTrue("Long ", t.isImmediate());
  
        t = new CachedType(Long.class);
        assertTrue("Long ", t.isImmediate());
     }
  
     public void testByte() throws Exception
     {
        CachedType t = new CachedType(byte.class);
        assertTrue("Byte ", t.isImmediate());
  
        t = new CachedType(Short.class);
        assertTrue("Byte ", t.isImmediate());
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(CachedTypeTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/NewReplicatedTest.java
  
  Index: NewReplicatedTest.java
  ===================================================================
  package org.jboss.cache.pojo;
  
  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.Fqn;
  import org.jboss.cache.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.SpecialSerializedAddress;
  
  import javax.naming.Context;
  import java.util.Properties;
  
  
  /**
   * Replication PojoCache test. This is the real beef as local mode for PojoCache really does
   * not do anything.
   *
   * @author Ben Wang
   */
  
  public class NewReplicatedTest extends TestCase
  {
     Log log_ = LogFactory.getLog(NewReplicatedTest.class);
     PojoCache cache_;
     PojoCache cache1_;
  
     public NewReplicatedTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache1_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache_.start();
        cache1_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
        cache1_.stop();
     }
  
     /*
     public void testObjectSizePut() throws Exception
     {
        org.jboss.cache.data.Student student = (org.jboss.cache.data.Student)constructObject();
        cache_.putObject("/joe", student);
     }
  
     static Object constructObject()
     {
        org.jboss.cache.data.Student joe = new org.jboss.cache.data.Student();
        joe.setName("Joe");
  
        Address add = new Address();
        add.setZip(94086);
        add.setCity("Sunnyvale)");
        add.setStreet("Albertson");
        joe.setAddress(add);
  
        String str;
        for(int i=0; i < 100; i++)
        {
           Course course = new Course();
           str = RandomString.randomstring(10,20);
           course.setInstructor(str);
           str = RandomString.randomstring(10,20);
           course.setTitle(str);
           str = RandomString.randomstring(10,20);
           course.setRoom(str);
           joe.addCourse(course);
        }
  
        return joe;
     }
     */
  
     public void testRemoteRemove() throws Exception
     {
        log_.info("testRemoteRemove() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
        Person result = (Person) cache_.find("/a");
        assertEquals(" ", test, result);
  
        Person remote = (Person) cache1_.find("/a");
        assertEquals("Age should be ", 10, remote.getAge());
  
        // Remote remove
        cache1_.detach("/a");
        assertNull("Object should be null ", cache_.find("/a"));
  
        assertNull("Object should be null ", cache1_.find("/a"));
        // It is 0 since it will be un-initialized.
        assertEquals("Age should be ", 0, remote.getAge());
     }
  
     public void testRemoteRemove2() throws Exception
     {
        log_.info("testRemoteRemove() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
        Person result = (Person) cache_.find("/a");
        assertEquals(" ", test, result);
  
        Person remote = (Person) cache1_.find("/a");
        assertEquals("Age should be ", 10, remote.getAge());
  
        // Remote remove
        cache_.detach("/a");
        assertNull("Object should be null ", cache_.find("/a"));
  
        assertNull("Object should be null ", cache1_.find("/a"));
        // this will trigger the PojoCacheAlreadyDetachedException
        try
        {
           remote.getAge();
           fail("Should throw out exception here.");
        }
        catch (PojoCacheAlreadyDetachedException pe)
        {
        }
     }
  
     /**
      * Test for pojo detachment and then serialization.
      *
      * @throws Exception
      */
     public void testRemoteDetach() throws Exception
     {
        log_.info("testRemoteDetach() ....");
        SpecialSerializedAddress addr = new SpecialSerializedAddress();
        addr.setZip(95123);
        addr.addResidents("Ben");
        addr.addResidents("Joe");
        // Test serialization first
        Fqn fqn = new Fqn("/plain");
        cache_.getCache().put(fqn, "test", addr);
        cache_.getCache().remove(fqn, "test");
  
        cache_.attach("/a", addr);
        SpecialSerializedAddress result = (SpecialSerializedAddress) cache_.find("/a");
        assertEquals(" ", addr, result);
  
        // Remote remove
        cache_.detach("/a");
        assertNull("Object should be null ", cache_.find("/a"));
  
        // Test serialization after detach
        cache_.getCache().put(fqn, "test", addr);
  
        SpecialSerializedAddress remote = (SpecialSerializedAddress)
                cache1_.getCache().get(fqn, "test");
        assertEquals("Name should be ", 95123, remote.getZip());
     }
  
     public void testPutArray1() throws Exception
     {
        log_.info("testPutArray1() ....");
        long[] arr = new long[]{1, 2};
        cache_.attach("array", arr);
  
        long[] a2 = (long[]) cache1_.find("array");
        assertEquals("arr 0", 1, a2[0]);
     }
  
     public void testPutArray2() throws Exception
     {
        log_.info("testPutArray2() ....");
        Person p1 = new Person();
        p1.setName("Ben");
        p1.setAge(10);
  
        Person p2 = new Person();
        p2.setName("Joe");
        p2.setAge(20);
        Person[] arr = new Person[]{p1, p2};
  
        cache_.attach("array", arr);
  
        Person[] a2 = (Person[]) cache1_.find("array");
        assertEquals("arr 0", "Ben", a2[0].getName());
     }
  
  
     /**
      * JBCACHE-200.
      *
      * @throws Exception
      */
     public void testStateTransfer() throws Exception
     {
        log_.info("testStateTransfer() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
        Person result = (Person) cache_.find("/a");
        assertEquals(" ", test, result);
  
        // restart cache1_
        cache1_.stop();
        cache1_.getCache().removeNode(Fqn.fromString("/a"));
        cache1_.start();
        // Start from scratch for initial state transfer
        Person remote = (Person) cache1_.find("/a");
        assertEquals("Age should be ", 10, remote.getAge());
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(NewReplicatedTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/ReplicatedTxTest.java
  
  Index: ReplicatedTxTest.java
  ===================================================================
  package org.jboss.cache.pojo;
  
  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.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Student;
  import org.jboss.cache.transaction.DummyTransactionManager;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.transaction.NotSupportedException;
  import javax.transaction.RollbackException;
  import javax.transaction.SystemException;
  import javax.transaction.Transaction;
  import javax.transaction.UserTransaction;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Properties;
  
  /**
   */
  
  public class ReplicatedTxTest extends TestCase
  {
     Log log = LogFactory.getLog(ReplicatedTxTest.class);
     PojoCache cache, cache1;
     final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
     DummyTransactionManager tx_mgr;
     Throwable t1_ex, t2_ex;
     long start = 0;
  
  
     public ReplicatedTxTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        boolean toStart = false;
        cache = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache.start();
        cache1 = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache1.start();
  
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
  
        tx_mgr = DummyTransactionManager.getInstance();
        t1_ex = t2_ex = null;
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache.stop();
        cache1.stop();
  
        DummyTransactionManager.destroy();
     }
  
  //   public void testDummy() {}
  
     UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
     {
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.jboss.cache.transaction.DummyContextFactory");
        return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
     }
  
     private Person createPerson(String id, String name, int age)
     {
        Person p = new Person();
        p.setName(name);
        p.setAge(age);
        cache.attach(id, p);
        return p;
     }
  
     private Student createStudent(String id, String name, int age, String grade)
     {
        Student p = new Student();
        p.setName(name);
        p.setAge(age);
        p.setYear(grade);
        cache.attach(id, p);
        return p;
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
        UserTransaction tx = getTransaction();
        tx.begin();
        Person p = createPerson("/person/test1", "Harald Gliebe", 32);
        tx.commit();
        tx.begin();
        p.setName("Benoit");
        tx.commit();
        Person p1 = (Person) cache1.find("/person/test1");
        assertEquals("Benoit", p.getName());
        assertEquals("Benoit", p1.getName());
        tx.begin();
        p1.setAge(61);
        tx.commit();
        assertEquals(61, p.getAge());
        assertEquals(61, p1.getAge());
     }
  
     public void testModification() throws Exception
     {
        UserTransaction tx = getTransaction();
        tx.begin();
        Person p = createPerson("/person/test2", "Harald", 32);
        p.setName("Harald Gliebe");
        tx.commit();
        Person p1 = (Person) cache1.find("/person/test2");
        tx.begin();
        p1.setName("Benoit");
        tx.commit();
        assertEquals(p.getName(), "Benoit");
        assertEquals(p1.getName(), "Benoit");
        tx.begin();
        p1.setName("Harald");
        tx.rollback();
        assertEquals(p.getName(), "Benoit");
        assertEquals(p1.getName(), "Benoit");
     }
  
     public void testConcurrentPuts() throws Exception
     {
        Thread t1 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              try
              {
                 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 lang.add("German");
                 TestingUtil.sleepThread(17000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
              }
           }
        };
  
        Thread t2 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              UserTransaction tx = null;
              try
              {
                 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
                 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                 tx = getTransaction();
                 tx.begin();
                 lang.add("English");
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 try
                 {
                    tx.rollback();
                 } catch (SystemException e)
                 {
                    e.printStackTrace();
                    t2_ex = e;
                 }
              }
           }
        };
  
        Person p = createPerson("/person/test6", "p6", 50);
        List<String> lang = new ArrayList<String>();
        lang.add("German");
        p.setLanguages(lang);
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t2_ex != null)
           fail("Thread1 failed: " + t2_ex);
        if (t1_ex != null)
           fail("Thread2 failed: " + t1_ex);
  
        int size = ((Person) cache.find("/person/test6")).getLanguages().size();
        assertEquals("number of languages should be 2, but is " + size + " (" +
                ((Person) cache.find("/person/test6")).getLanguages().size() + ")",
                2, size);
        size = ((Person) cache1.find("/person/test6")).getLanguages().size();
        assertEquals("number of languages should be 2, but is " + size + " (" +
                ((Person) cache.find("/person/test6")).getLanguages().size() + ")",
                2, size);
     }
  
     void log(String s)
     {
        long now;
        if (start == 0)
           start = System.currentTimeMillis();
        now = System.currentTimeMillis();
  
        System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
     }
  
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ReplicatedTxTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/LocalTest.java
  
  Index: LocalTest.java
  ===================================================================
  package org.jboss.cache.pojo;
  
  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.pojo.test.Address;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Student;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  /**
   * Basic PojoCache test case.
   *
   * @author Ben Wang
   */
  
  public class LocalTest extends TestCase
  {
     Log log = LogFactory.getLog(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-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     //   public void testDummy() {}
  
     private Person createPerson(String id, 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);
        cache_.attach(id, p);
        return p;
     }
  
     private Student createStudent(String id, String name, int age, String grade)
     {
        Student p = new Student();
        p.setName(name);
        p.setAge(age);
        p.setYear(grade);
        Address add = new Address();
        add.setZip(95123);
        add.setCity("San Jose");
        p.setAddress(add);
        cache_.attach(id, p);
        return p;
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
        Person p = createPerson("/person/test1", "Joe Black", 32);
        assertEquals((Object) "Joe Black", p.getName());
     }
  
     public void testSimpleType() throws Exception
     {
        log.info("testSimpleType() ....");
        String test = "This is a test";
        cache_.attach("test", test);
  
        assertEquals("String ", test, cache_.find("test"));
     }
  
     public void testModification() throws Exception
     {
        Person joe = createPerson("/person/test2", "Joe", 32);
        joe.setName("Joe Black");
        assertEquals(joe.getName(), "Joe Black");
        cache_.detach("/person/test2");
     }
  
     public void testRemove() throws Exception
     {
        Person joe = createPerson("/person/test3", "Joe", 32);
        cache_.detach("/person/test3");
        //      assertNull("Underlying cache content " + cache_.getCache().printDetails(),
        //              cache_.getCache().get("/person/test3"));
     }
  
     public void testDynamicRefSwapping() throws Exception
     {
        Person person = createPerson("/person/test3", "Joe", 32);
        try
        {
           person.setAge(30);
           List med = person.getMedication();
           assertNull("Medication should be null ", med);
           person.setAge(60);
           med = person.getMedication();
           assertEquals("Medication ", "Lipitor", med.get(0));
        }
        catch (Exception e)
        {
           // should be thrown
        }
     }
  
     public void testMap() throws Exception
     {
        log.info("testMap() ....");
        Person ben = createPerson("/person/test1", "Ben Wang", 40);
        assertEquals((Object) "Ben Wang", ben.getName());
        Map<String, String> hobbies = ben.getHobbies();
        if (hobbies == null)
        {
           hobbies = new HashMap<String, String>();
           ben.setHobbies(hobbies);
           // NB: it is neccessary to get hobbies again to get advised version
           hobbies = ben.getHobbies();
        }
        hobbies.put("1", "English");
        hobbies.put("2", "French");
        if (!(hobbies instanceof ClassProxy))
        {
           fail("Hobbies is not an instance of ClassProxy");
        }
  
        hobbies = ben.getHobbies();
        assertEquals("Hobbies size", 2, hobbies.size());
        log.debug("Hobbies is " + hobbies.toString());
     }
  
     public void testMapDetachAttach() throws Exception
     {
        log.info("testMapDetachATtach() ....");
        Person ben = createPerson("/person/test1", "Ben Wang", 40);
        assertEquals((Object) "Ben Wang", ben.getName());
        Map<String, String> hobbies = ben.getHobbies();
        if (hobbies == null)
        {
           hobbies = new HashMap<String, String>();
           ben.setHobbies(hobbies);
           // NB: it is neccessary to get hobbies again to get advised version
           hobbies = ben.getHobbies();
        }
        hobbies.put("1", "English");
        hobbies.put("2", "French");
        if (!(hobbies instanceof ClassProxy))
        {
           fail("Hobbies is not an instance of ClassProxy");
        }
  
        hobbies = ben.getHobbies();
        assertEquals("Hobbies size", 2, hobbies.size());
        log.debug("Hobbies is " + hobbies.toString());
  
        cache_.detach("/person/test1");
        hobbies = ben.getHobbies();
        if ((hobbies instanceof ClassProxy))
        {
           fail("Hobbies should not be an instance of ClassProxy");
        }
  
        cache_.attach("/person/1", ben);
  
     }
  
     public void testMap2() throws Throwable
     {
        Person joe = createPerson("/person/test5", "Joe Black", 32);
        Map<String, String> hobby = new HashMap<String, String>();
        hobby.put("music", "guitar");
        joe.setHobbies(hobby);
        Object val = joe.getHobbies().get("music");
        assertEquals("guitar", val);
        hobby = joe.getHobbies();
        hobby.put("novel", "English");
        assertEquals("Size of map ", 2, joe.getHobbies().size());
     }
  
     public void testList() throws Throwable
     {
        Person joe = createPerson("/person/test6", "Joe", 50);
        List<String> language = new ArrayList<String>();
        language.add("German");
        language.add("English");
        language.add("French");
        joe.setLanguages(language);
  
        assertEquals("Size of language ", 3, joe.getLanguages().size());
        language = joe.getLanguages();
        language.add("Mandarin");
        language.add("Taiwanese");
        language.add("Haka");
        assertEquals("Size of language ", 6, joe.getLanguages().size());
  
        String English = language.get(1);
        assertEquals((Object) "English", English);
        cache_.detach("/person/test6");
     }
  
     public void testListDetachAndAttach() throws Throwable
     {
        String id = "/person/test6";
        Person joe = new Person();
        List<String> language = new ArrayList<String>();
        language.add("German");
        language.add("English");
        language.add("French");
        joe.setLanguages(language);
  
        cache_.attach(id, joe);
        cache_.detach(id);
        //      TreeCacheProxyImpl impl = (TreeCacheProxyImpl)cache_.getCache();
        //      System.out.println("*** Here I ");
        //      System.out.println(impl.printDetails());
  
        joe.getAge();
        cache_.attach(id, joe);
     }
  
     public void testListDetachAndAttach2() throws Throwable
     {
        String id = "/person/test6";
        Person joe = createPerson(id, "Joe", 50);
        List<String> language = new ArrayList<String>();
        language.add("German");
        language.add("English");
        language.add("French");
        joe.setLanguages(language);
  
        assertEquals("Size of language ", 3, joe.getLanguages().size());
        language = joe.getLanguages();
        language.add("Mandarin");
        language.add("Taiwanese");
        language.add("Haka");
        assertEquals("Size of language ", 6, joe.getLanguages().size());
  
        String English = language.get(1);
        assertEquals((Object) "English", English);
  
        if (!(language instanceof ClassProxy))
        {
           fail("Language is not an instance of ClassProxy");
        }
  
        cache_.detach(id);
        joe.getAge();
        language = joe.getLanguages();
        if ((language instanceof ClassProxy))
        {
           fail("Language is an instance of ClassProxy");
        }
  
        cache_.attach(id, joe);
     }
  
     public void testSet() throws Throwable
     {
        Person joe = createPerson("/person/test7", "Joe", 27);
        Set<String> skill = new HashSet<String>();
        skill.add("Java");
        skill.add("Java");
        skill.add("Java");
        joe.setSkills(skill);
        skill = joe.getSkills();
        assertEquals("Size of skill ", 1, skill.size());
  
        skill.remove("Java");
        assertTrue(skill.isEmpty());
        skill.add("Java");
        skill.add("J2EE");
        skill.add("JBoss");
        assertEquals(new Integer(3), new Integer(skill.size()));
     }
  
     public void testSetDetachAttach() throws Throwable
     {
        String id = "/person/test7";
        Person joe = createPerson(id, "Joe", 27);
        Set<String> skill = new HashSet<String>();
        skill.add("Java");
        skill.add("Java");
        skill.add("Java");
        joe.setSkills(skill);
        skill = joe.getSkills();
        assertEquals("Size of skill ", 1, skill.size());
  
        skill.remove("Java");
        assertTrue(skill.isEmpty());
        skill.add("Java");
        skill.add("J2EE");
        skill.add("JBoss");
        assertEquals(new Integer(3), new Integer(skill.size()));
  
        if (!(skill instanceof ClassProxy))
        {
           fail("Skill is not an instance of ClassProxy");
        }
  
        cache_.detach(id);
        joe.getAge();
        skill = joe.getSkills();
        if ((skill instanceof ClassProxy))
        {
           fail("Skill is an instance of ClassProxy");
        }
  
        cache_.attach(id, joe);
     }
  
     public void testInheritance() throws Exception
     {
        Student joe = createStudent("/person/joe", "Joe", 32, "Senior");
        joe.setName("Joe Black");
        assertEquals(joe.getName(), "Joe Black");
        joe.setYear("Junior");
        assertEquals(joe.getYear(), "Junior");
        cache_.detach("/person/joe");
     }
  
  
     public static Test suite() throws Exception
     {
        return new TestSuite(LocalTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/TestingUtil.java
  
  Index: TestingUtil.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  import org.jboss.cache.CacheImpl;
  
  import java.util.Vector;
  
  /**
   * Utilities for unit testing JBossCache.
   *
   * @author <a href="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
   * @version $Revision: 1.1 $
   */
  public class TestingUtil
  {
  
     /**
      * @param caches  caches which must all have consistent views
      * @param timeout max number of ms to loop
      * @throws RuntimeException if <code>timeout</code> ms have elapse without
      *                          all caches having the same number of members.
      */
     public static void blockUntilViewsReceived(PojoCache[] caches, long timeout)
     {
        long failTime = System.currentTimeMillis() + timeout;
  
        while (System.currentTimeMillis() < failTime)
        {
           org.jboss.cache.pojo.TestingUtil.sleepThread(100);
           if (org.jboss.cache.pojo.TestingUtil.areCacheViewsComplete(caches))
              return;
        }
  
        throw new RuntimeException("timed out before caches had complete views");
     }
  
     /**
      */
     public static void blockUntilViewReceived(PojoCache cache, int groupSize, long timeout)
     {
        long failTime = System.currentTimeMillis() + timeout;
  
        CacheImpl tcache = (CacheImpl) cache.getCache();
        while (System.currentTimeMillis() < failTime)
        {
           org.jboss.cache.pojo.TestingUtil.sleepThread(100);
           if (org.jboss.cache.pojo.TestingUtil.isCacheViewComplete(tcache, groupSize))
              return;
        }
  
        throw new RuntimeException("timed out before caches had complete views");
     }
  
     /**
      * Checks each cache to see if the number of elements in the array
      * returned by {@link org.jboss.cache.CacheImpl#getMembers()} matches the size of
      * the <code>caches</code> parameter.
      *
      * @param caches caches that should form a View
      * @return <code>true</code> if all caches have
      *         <code>caches.length</code> members; false otherwise
      * @throws IllegalStateException if any of the caches have MORE view
      *                               members than caches.length
      */
     public static boolean areCacheViewsComplete(PojoCache[] caches)
     {
        int memberCount = caches.length;
  
        for (int i = 0; i < memberCount; i++)
        {
           CacheImpl cache = (CacheImpl) caches[i].getCache();
           return org.jboss.cache.pojo.TestingUtil.isCacheViewComplete(cache, memberCount);
        }
  
        return true;
     }
  
     /**
      * FIXME Comment this
      *
      * @param cache
      * @param memberCount
      */
     public static boolean isCacheViewComplete(CacheImpl cache, int memberCount)
     {
        Vector members = cache.getMembers();
        if (members == null || memberCount > members.size())
        {
           return false;
        }
        else if (memberCount < members.size())
        {
           // This is an exceptional condition
           StringBuffer sb = new StringBuffer("Cache at address ");
           sb.append(cache.getLocalAddress());
           sb.append(" had ");
           sb.append(members.size());
           sb.append(" members; expecting ");
           sb.append(memberCount);
           sb.append(". Members were (");
           for (int j = 0; j < members.size(); j++)
           {
              if (j > 0)
                 sb.append(", ");
              sb.append(members.elementAt(j));
           }
           sb.append(')');
  
           throw new IllegalStateException(sb.toString());
        }
  
        return true;
     }
  
  
     /**
      * Puts the current thread to sleep for the desired number of ms, suppressing
      * any exceptions.
      *
      * @param sleeptime number of ms to sleep
      */
     public static void sleepThread(long sleeptime)
     {
        try
        {
           Thread.sleep(sleeptime);
        }
        catch (InterruptedException ie)
        {
        }
     }
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/EnumTest.java
  
  Index: EnumTest.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;
  
  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.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.test.ArrayObject;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.EnumPlanet;
  
  /**
   * Basic PojoCache test case for Enum.
   *
   * @author Ben Wang
   */
  
  public class EnumTest extends TestCase
  {
     Log log = LogFactory.getLog(EnumTest.class);
     PojoCache cache_, cache1_;
  
     public EnumTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        boolean toStart = true;
        cache_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache1_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
        cache1_.stop();
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
  
        double earthWeight = 165;
        double mass = earthWeight / EnumPlanet.EARTH.surfaceGravity();
  
        cache_.attach("enum", EnumPlanet.EARTH);
  
        EnumPlanet ep = (EnumPlanet)cache1_.find("enum");
  
        EnumPlanet.EARTH.setMass(7.0e+24);
  
        assertEquals("Earth mass ", EnumPlanet.EARTH.mass(), ep.mass());
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(EnumTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/ReplicatedObjectGraphTest.java
  
  Index: ReplicatedObjectGraphTest.java
  ===================================================================
  package org.jboss.cache.pojo;
  
  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.Fqn;
  import org.jboss.cache.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.pojo.test.Person;
  
  /**
   * Test object graph handling in aop, e.g., circular reference, multiple reference, link, etc.
   *
   * @author Ben Wang
   */
  
  public class ReplicatedObjectGraphTest extends TestCase
  {
     Log log = LogFactory.getLog(ReplicatedObjectGraphTest.class);
     PojoCache cache1;
     PojoCache cache2;
  
     public ReplicatedObjectGraphTest(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.getCache().removeNode(Fqn.fromString("/"));
        cache1.stop();
        cache2.stop();
     }
  
     private PojoCache createCache(String name) throws Exception
     {
        boolean toStart = false;
        PojoCache tree = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        tree.start();
        return tree;
     }
  
     //   public void testDummy() {}
  
     protected Person createPerson(String name, int age)
     {
        Person p = new Person();
        p.setName(name);
        p.setAge(age);
        return p;
     }
  
     private void stage0() throws Exception
     {
        cache1.attach("/person/joe", createPerson("Joe Black", 31));
        Person joe = (Person) cache1.find("/person/joe");
        cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
        Person ben = (Person) cache1.find("/person/ben");
  
        Address addr = new Address();
        addr.setStreet("123 Albert Ave.");
        addr.setCity("Sunnyvale");
        addr.setZip(94087);
        cache1.attach("/address", addr);
  
        // They share the sub-object: address
        joe.setAddress(addr);
        ben.setAddress(addr);
        assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity());
        assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
     }
  
     private void stage1() throws Exception
     {
        cache1.attach("/person/joe", createPerson("Joe Black", 31));
        Person joe = (Person) cache1.find("/person/joe");
        cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
        Person ben = (Person) cache1.find("/person/ben");
  
        Address addr = new Address();
        addr.setStreet("123 Albert Ave.");
        addr.setCity("Sunnyvale");
        addr.setZip(94087);
  
        // They share the sub-object: address
        joe.setAddress(addr);
        ben.setAddress(addr);
        assertEquals("Joe's address should still be valid ", "Sunnyvale", joe.getAddress().getCity());
        assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
     }
  
     private void stage2(PojoCache cache) throws Exception
     {
        //
        cache.detach("/person/joe");
        Person ben = (Person) cache.find("/person/ben");
        assertEquals("Ben's address should still be valid ", "Sunnyvale", ben.getAddress().getCity());
        Address addr = ben.getAddress();
        addr.setCity("Santa Clara");
        assertEquals("Ben's address should be changed ", "Santa Clara", ben.getAddress().getCity());
     }
  
     /**
      * Test whether repeated update on the ref count will change the replicated aop instances
      *
      * @throws Exception
      */
     public void testCheckReplInstance() throws Exception
     {
        log.info("testCheckReplInstance() ...");
        stage0();
        TestingUtil.sleepThread(100);
        Person joe = (Person) cache1.find("/person/joe");
        Person ben = (Person) cache1.find("/person/ben");
        assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(),
                ben.getAddress().getCity());
  
        Address joe1 = (Address) cache2.find("/address");
        assertEquals("Ben's address should not be changed ", joe.getAddress().getCity(), joe1.getCity());
        ben = (Person) cache2.find("/person/ben");
        cache2.detach("/person/ben");
        Address joe2 = (Address) cache2.find("/address");
        assertEquals("Joe's reference should be the same.", joe1, joe2);
     }
  
     public void testRefCountCheckRepl() throws Exception
     {
        log.info("testRefCountCheckRepl() ...");
        stage1();
        TestingUtil.sleepThread(100);
        Person joe = (Person) cache1.find("/person/joe");
        Person ben = (Person) cache1.find("/person/ben");
        assertEquals("Ben and Joe's address should be the same ", joe.getAddress().getCity(),
                ben.getAddress().getCity());
        TestingUtil.sleepThread(100);
        stage2(cache2);
        assertEquals("Ben's address should be changed on cache1 as well ", "Santa Clara", ben.getAddress().getCity());
        cache2.detach("/person/ben");
     }
  
  
     public void testdetach1() throws Exception
     {
        log.info("testdetach1() ...");
        cache1.attach("/person/joe", createPerson("Joe Black", 31));
        Person joe = (Person) cache1.find("/person/joe");
        cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
        Person ben = (Person) cache1.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 = ((Person) cache2.find("/person/joe")).getAddress();
        Address add2 = ((Person) cache2.find("/person/ben")).getAddress();
        assertEquals(add1.getCity(), add2.getCity());
        addr.setCity("Santa Clara");
        assertEquals(add1.getCity(), add2.getCity());
  
        // Remove pojo joe will relocate the address field to ben's
        cache2.detach("/person/joe");
        add2 = ((Person) cache2.find("/person/ben")).getAddress();
  
        assertEquals("City ", "Santa Clara", add2.getCity());
     }
  
     public void testdetach2() throws Exception
     {
        log.info("testdetach2() ...");
        cache1.attach("/person/joe", createPerson("Joe Black", 31));
        Person joe = (Person) cache1.find("/person/joe");
        cache1.attach("/person/ben", createPerson("Ben Hogan", 51));
        Person ben = (Person) cache1.find("/person/ben");
        cache1.attach("/person/john", createPerson("John Daly", 41));
        Person john = (Person) cache1.find("/person/john");
  
        Address addr = new Address();
        addr.setStreet("123 Albert Ave.");
        addr.setCity("Sunnyvale");
        addr.setZip(94087);
  
        Address addr1 = new Address();
        addr1.setStreet("123 Albert Ave.");
        addr1.setCity("San Jose");
        addr1.setZip(94087);
  
        // They share the sub-object: address
        log.info("testMultipleReference(): set Joe address");
        joe.setAddress(addr);
        log.info("testMultipleReference(): set Ben address");
        ben.setAddress(addr);
        john.setAddress(addr);
  
        Address add1 = ((Person) cache2.find("/person/joe")).getAddress();
        Address add2 = ((Person) cache2.find("/person/ben")).getAddress();
        assertEquals(add1.getCity(), add2.getCity());
        addr.setCity("Santa Clara");
        assertEquals(add1.getCity(), add2.getCity());
  
        // Remove pojo joe will relocate the address field to ben's
        joe.setAddress(addr1);
        add2 = ((Person) cache2.find("/person/joe")).getAddress();
        assertEquals("City ", "San Jose", add2.getCity());
        add2 = ((Person) cache2.find("/person/ben")).getAddress();
        assertEquals("City ", "Santa Clara", add2.getCity());
        add2 = ((Person) cache2.find("/person/john")).getAddress();
        assertEquals("City ", "Santa Clara", add2.getCity());
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ReplicatedObjectGraphTest.class);
     }
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/LocalConcurrentTest.java
  
  Index: LocalConcurrentTest.java
  ===================================================================
  /*
   *
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.lock.UpgradeException;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.transaction.DummyTransactionManager;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.transaction.UserTransaction;
  import java.util.ArrayList;
  import java.util.Properties;
  import java.util.Random;
  
  
  /**
   * Local concurrent test for PojoCache. Test attach and detach under load
   * and concurrency.
   *
   * @version $Revision: 1.1 $
   * @author<a href="mailto:bwang at jboss.org">Ben Wang</a> December 2004
   */
  public class LocalConcurrentTest extends TestCase
  {
     static PojoCache cache_;
     Configuration.CacheMode cachingMode_ = Configuration.CacheMode.LOCAL;
     Properties p_;
     String oldFactory_ = null;
     final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
     static ArrayList nodeList_;
     static final int depth_ = 2;
     static final int children_ = 2;
     static final int MAX_LOOP = 100;
     static final int SLEEP_TIME = 50;
     static Exception thread_ex = null;
     UserTransaction tx_ = null;
  
     public LocalConcurrentTest(String name)
     {
        super(name);
     }
  
     public void setUp() throws Exception
     {
        super.setUp();
        oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
        DummyTransactionManager.getInstance();
        if (p_ == null)
        {
           p_ = new Properties();
           p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
        }
  
        tx_ = (UserTransaction) new InitialContext(p_).lookup("UserTransaction");
  
        initCaches(Configuration.CacheMode.LOCAL);
        nodeList_ = nodeGen(depth_, children_);
  
        log("LocalConcurrentTestCase: cacheMode=TRANSIENT, one cache");
     }
  
     public void tearDown() throws Exception
     {
        super.tearDown();
        thread_ex = null;
        DummyTransactionManager.destroy();
        destroyCaches();
  
        if (oldFactory_ != null)
        {
           System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
           oldFactory_ = null;
        }
  
     }
  
     void initCaches(Configuration.CacheMode caching_mode) throws Exception
     {
        cachingMode_ = caching_mode;
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache("META-INF/local-service.xml", toStart);
        cache_.start();
     }
  
     void destroyCaches() throws Exception
     {
        cache_.stop();
        cache_ = null;
     }
  
     public void testAll_RWLock() throws Exception
     {
        try
        {
           all();
        }
        catch (UpgradeException ue)
        {
           log("Upgrade exception. Can ingore for repeatable read. " + ue);
        }
        catch (Exception ex)
        {
           log("Exception: " + ex);
           throw ex;
        }
     }
  
     private void all() throws Exception
     {
        RunThread t1 = new RunThread(1);
        RunThread t2 = new RunThread(2);
        RunThread t3 = new RunThread(3);
        RunThread t4 = new RunThread(4);
  
        t1.start();
        TestingUtil.sleepThread(100);
        t2.start();
        TestingUtil.sleepThread(100);
        t3.start();
        TestingUtil.sleepThread(100);
        t4.start();
  
        t1.join(60000);// wait for 20 secs
        t2.join(60000);// wait for 20 secs
        t3.join(60000);// wait for 20 secs
        t4.join(60000);// wait for 20 secs
  
        if (thread_ex != null)
        {
           throw thread_ex;
        }
     }
  
     class RunThread extends Thread
     {
        final int seed_;
        Random random_;
        Person person_;
  
        public RunThread(int seed)
        {
           seed_ = seed;
           random_ = new Random(seed);
        }
  
        private void createPerson()
        {
           person_ = new Person();
           person_.setName("Ben");
           person_.setAge(18);
           ArrayList<String> lang = new ArrayList<String>();
           lang.add("English");
           lang.add("French");
           lang.add("Mandarin");
           person_.setLanguages(lang);
           Address addr = new Address();
           addr.setZip(95123);
           addr.setStreet("Almeria");
           addr.setCity("San Jose");
           person_.setAddress(addr);
        }
  
        public void run()
        {
           try
           {
              _run();
           }
           catch (Exception e)
           {
              thread_ex = e;
           }
        }
  
        /**
         */
        public void _run() throws Exception
        {
           for (int loop = 0; loop < MAX_LOOP; loop++)
           {
              createPerson();// create a new person instance every loop.
              TestingUtil.sleepThread(random_.nextInt(50));
              op1();
           }
        }
  
        // Operation 1
        private void op1()
        {
           int i = random_.nextInt(nodeList_.size() - 1);
           if (i == 0) return;// it is meaningless to test root
           String node = nodeList_.get(i) + "/aop";
           cache_.attach(node, person_);
           TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME));// sleep for max 200 millis
           TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME));// sleep for max 200 millis
           cache_.detach(node);
        }
     }
  
     /**
      * Generate the tree nodes quasi-exponentially. I.e., depth is the level
      * of the hierarchy and children is the number of children under each node.
      * This strucutre is used to add, get, and remove for each node.
      */
     private ArrayList nodeGen(int depth, int children)
     {
        ArrayList<String> strList = new ArrayList<String>();
        ArrayList<String> oldList = new ArrayList<String>();
        ArrayList<String> newList = new ArrayList<String>();
  
        // Skip root node
        oldList.add("/");
        newList.add("/");
        strList.add("/");
  
        while (depth > 0)
        {
           // Trying to produce node name at this depth.
           newList = new ArrayList<String>();
           for (int i = 0; i < oldList.size(); i++)
           {
              for (int j = 0; j < children; j++)
              {
                 String tmp = oldList.get(i);
                 tmp += Integer.toString(j);
                 if (depth != 1)
                 {
                    tmp += "/";
                 }
  
                 newList.add(tmp);
              }
           }
           strList.addAll(newList);
           oldList = newList;
           depth--;
        }
  
        // let's prune out root node
        for (int i = 0; i < strList.size(); i++)
        {
           if (strList.get(i).equals("/"))
           {
              strList.remove(i);
              break;
           }
        }
        log("Nodes generated: " + strList.size());
        return strList;
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(LocalConcurrentTest.class);
     }
  
     private static void log(String str)
     {
        System.out.println("Thread: " + Thread.currentThread() + ": " + str);
        //        System.out.println(str);
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/NonAspectizedTest.java
  
  Index: NonAspectizedTest.java
  ===================================================================
  package org.jboss.cache.pojo;
  
  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.pojo.test.SerializedAddress;
  
  
  /**
   * Test for Serializable POJOs
   *
   * @author Ben Wang
   */
  
  public class NonAspectizedTest extends TestCase
  {
     Log log_ = LogFactory.getLog(NonAspectizedTest.class);
     PojoCache cache_;
  
     public NonAspectizedTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     public void testPutPrimitive() throws Exception
     {
        log_.info("testPutPrimitive() ....");
        String test = "test";
        cache_.attach("/a", test);
        String result = (String) cache_.find("/a");
        assertEquals("test string ", "test", result);
        cache_.detach("/a");
        assertNull("Object should be null ", cache_.find("/a"));
     }
  
     public void testPutSerializable() throws Exception
     {
        log_.info("testPutSerializable() ....");
        SerializedAddress test = new SerializedAddress();
        test.setCity("Sunnyvale");
        test.setZip(94086);
        cache_.attach("/a", test);
        SerializedAddress result = (SerializedAddress) cache_.find("/a");
        assertEquals("test SerializedAddress ", test, result);
        cache_.detach("/a");
        assertNull("Object should be null ", cache_.find("/a"));
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(NonAspectizedTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
  }
  
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/NewReplicatedTxTest.java
  
  Index: NewReplicatedTxTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  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.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.transaction.DummyTransactionManager;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.transaction.NotSupportedException;
  import javax.transaction.RollbackException;
  import javax.transaction.SystemException;
  import javax.transaction.Transaction;
  import javax.transaction.UserTransaction;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Properties;
  
  /**
   * PojoCache replicated test cases with tx.
   *
   * @author Ben Wang
   */
  
  public class NewReplicatedTxTest extends TestCase
  {
     Log log = LogFactory.getLog(NewReplicatedTxTest.class);
     PojoCache cache_;
     PojoCache cache1_;
     final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
     DummyTransactionManager tx_mgr;
     Throwable t1_ex, t2_ex;
     long start = 0;
  
  
     public NewReplicatedTxTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        super.setUp();
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache1_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache_.start();
        cache1_.start();
  
        tx_mgr = DummyTransactionManager.getInstance();
        t1_ex = t2_ex = null;
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
        cache1_.stop();
        DummyTransactionManager.destroy();
     }
  
     UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
     {
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.jboss.cache.transaction.DummyContextFactory");
        return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
     }
  
     public void testSimpleTxWithRollback() throws Exception
     {
        log.info("testSimpleTxWithRollback() ....");
        UserTransaction tx = getTransaction();
  
        Person joe = new Person();
        joe.setName("Joe");
        Address add = new Address();
        add.setZip(104);
        add.setCity("Taipei");
        joe.setAddress(add);
  
        tx.begin();
        cache_.attach("/person/joe", joe);
        tx.commit();
        Person p = (Person) cache1_.find("/person/joe");
        assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
  
        // test rollback
        Person ben = new Person();
        ben.setName("Ben");
        add = new Address();
        add.setZip(104);
        add.setCity("Taipei");
        joe.setAddress(add);
        tx.begin();
        cache_.attach("/person/ben", ben);
        tx.rollback();
        assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
     }
  
     public void testCollectionTxWithRollback() throws Exception
     {
        log.info("testCollectionTxWithRollback() ....");
        UserTransaction tx = getTransaction();
  
        Person joe = new Person();
        joe.setName("Joe");
        Address add = new Address();
        add.setZip(104);
        add.setCity("Taipei");
        joe.setAddress(add);
        ArrayList<String> lang = new ArrayList<String>();
        lang.add("English");
        lang.add("Taiwanese");
        lang.add("Mandirin");
        joe.setLanguages(lang);
  
        tx.begin();
        cache_.attach("/person/joe", joe);
        tx.commit();
        Person p = (Person) cache1_.find("/person/joe");
        assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
  
        // test rollback
        Person ben = new Person();
        ben.setName("Ben");
        add = new Address();
        add.setZip(104);
        add.setCity("Taipei");
        ben.setAddress(add);
        tx.begin();
        cache_.attach("/person/ben", ben);
        tx.rollback();
        assertEquals("Zip should be the same ", joe.getAddress().getZip(), p.getAddress().getZip());
        assertEquals("Langue 1 should be: ", "English", joe.getLanguages().get(0));
     }
  
  
     public void testConcurrentPutsWithRollback() throws Exception
     {
        Thread t1 = new Thread()
        {
           public void run()
           {
              Person p = new Person();
              p.setName("Ben");
              Address add = new Address();
              add.setCity("Taipei");
              p.setAddress(add);
              List<String> lang = new ArrayList<String>();
              lang.add("English");
              lang.add("Taiwanese");
              lang.add("Japanese");
              p.setLanguages(lang);
              try
              {
                 cache_.attach("/test/ben", p);
              }
              catch (Exception ex)
              {
                 try
                 {
                    throw new RuntimeException("PojoCache is not rolling back properly for Collection yet.");
                    //                  cache_.attach("/test/ben", p);
                 }
                 catch (Exception ex1)
                 {
                    t1_ex = ex1;
                 }
              }
           }
        };
  
        Thread t2 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              try
              {
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 Person p = new Person();
                 p.setName("Ben");
                 Address add = new Address();
                 add.setCity("Taipei");
                 p.setAddress(add);
                 cache1_.attach("/test/ben", p);
                 TestingUtil.sleepThread(1000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
              }
              catch (Exception ex)
              {
                 t2_ex = ex;
              }
           }
        };
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t1_ex != null)
        {
           fail("Thread1 failed: " + t1_ex);
        }
        if (t2_ex != null)
        {
           fail("Thread2 failed: " + t2_ex);
        }
     }
  
     public void testConcurrentTxPutsWithRollback() throws Exception
     {
        Thread t1 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              UserTransaction tx = null;
              Person p = new Person();
              p.setName("Ben");
              Address add = new Address();
              add.setCity("Taipei");
              p.setAddress(add);
              List<String> lang = new ArrayList<String>();
              lang.add("English");
              lang.add("Taiwanese");
              lang.add("Japanese");
              p.setLanguages(lang);
              try
              {
                 tx = getTransaction();
                 tx.begin();
                 cache_.attach("/test/ben", p);
                 TestingUtil.sleepThread(500);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 try
                 {
                    tx = getTransaction();
                    tx.begin();
                    //                  throw new RuntimeException("PojoCache is not rolling back properly for Collection yet.");
                    cache_.attach("/test/ben", p);
                    tx.commit();
                 }
                 catch (Exception ex)
                 {
                    t1_ex = ex;
                    ex.printStackTrace();
                 }
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
                 ex.printStackTrace();
              }
           }
        };
  
        Thread t2 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              try
              {
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 Person p = new Person();
                 p.setName("Ben");
                 Address add = new Address();
                 add.setCity("Taipei");
                 p.setAddress(add);
                 cache1_.attach("/test/ben", p);
                 TestingUtil.sleepThread(1000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
              }
              catch (Exception ex)
              {
                 t2_ex = ex;
              }
           }
        };
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t1_ex != null)
        {
           fail("Thread1 failed: " + t1_ex);
        }
        if (t2_ex != null)
        {
           fail("Thread2 failed: " + t2_ex);
        }
     }
  
     public void testConcurrentTxPutsWithRollbackField() throws Exception
     {
        Person p = new Person();
        p.setName("Ben");
        Address add = new Address();
        add.setCity("Taipei");
        p.setAddress(add);
        List<String> lang = new ArrayList<String>();
        lang.add("1");
        p.setLanguages(lang);
        cache_.attach("/test/ben", p);
  
        Thread t1 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              UserTransaction tx = null;
              List<String> lang = null;
              try
              {
                 Person ben = (Person) cache_.find("/test/ben");
                 lang = ben.getLanguages();
  
                 tx = getTransaction();
                 tx.begin();
                 lang.add("2");
                 lang.add("3");
                 lang.remove(0);
                 TestingUtil.sleepThread(1000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 try
                 {
                    tx = getTransaction();
                    tx.begin();
                    lang.add("2");
                    lang.add("3");
                    lang.remove(0);
                    tx.commit();
                 }
                 catch (Exception ex)
                 {
                    t1_ex = ex;
                 }
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
              }
           }
        };
  
        Thread t2 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              List<String> lang = null;
              UserTransaction tx = null;
              try
              {
                 Person ben = (Person) cache1_.find("/test/ben");
                 lang = ben.getLanguages();
  
                 tx = getTransaction();
                 tx.begin();
                 lang.add("2");
                 TestingUtil.sleepThread(1000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 TestingUtil.sleepThread(1000);
                 try
                 {
                    tx = getTransaction();
                    tx.begin();
                    lang.add("2");
                    tx.commit();
                 }
                 catch (Exception e)
                 {
                    e.printStackTrace();
                 }
              }
              catch (Exception ex)
              {
                 t2_ex = ex;
              }
           }
        };
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t1_ex != null)
        {
           fail("Thread1 failed: " + t1_ex);
        }
        if (t2_ex != null)
        {
           fail("Thread2 failed: " + t2_ex);
        }
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(NewReplicatedTxTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(NewReplicatedTxTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/ReplicatedSerializableTest.java
  
  Index: ReplicatedSerializableTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  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.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.test.CacheObject;
  import org.jboss.cache.pojo.test.Student;
  
  import javax.naming.Context;
  import java.util.Properties;
  
  /**
   * New NewReplicatedAopTest that doesn't use TreeCacheAopTester.
   *
   * @author Ben Wang
   */
  
  public class ReplicatedSerializableTest extends TestCase
  {
     Log log_ = LogFactory.getLog(ReplicatedSerializableTest.class);
     PojoCache cache_;
     PojoCache cache1_;
  
     public ReplicatedSerializableTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache1_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache_.start();
        cache1_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
        cache1_.stop();
     }
  
     public void testSeriazableSubObject() throws Exception
     {
        log_.info("testSerializableSubObject() ....");
  
        Student ben = new Student();
        ben.setName("Ben");
        ben.setYear("9th grade");
        CacheObject co1 = new CacheObject("1");
        ben.setCO1(co1);
        CacheObject co2 = new CacheObject("2");
        ben.setCO2(co2);
  
        cache_.attach("/test", ben);
  
        Student be = (Student) cache1_.find("/test");
        assertNotNull("CacheObject should not be null", be.getCO1());
        assertNotNull("CacheObject should not be null", be.getCO2());
        assertEquals("1", be.getCO1().getId());
        assertEquals("2", be.getCO2().getId());
     }
  
     /**
      * We don;t currently support Serializable with relationship now.
      *
      * @throws Exception
      */
     public void XtestSeriazableSubObjectRelation() throws Exception
     {
        log_.info("testSerializableSubObjectRelation() ....");
  
        Student ben = new Student();
        ben.setName("Ben");
        ben.setYear("9th grade");
        CacheObject co1 = new CacheObject("1");
        ben.setCO1(co1);
  
        Student elynne = new Student();
        elynne.setName("Elynne");
        elynne.setYear("9th grade");
        // Same co object
        elynne.setCO1(co1);
  
        cache_.attach("/ben", ben);
        cache_.attach("/elynne", elynne);
  
        Student be = (Student) cache1_.find("/ben");
        Student el = (Student) cache1_.find("/elynne");
        assertNotNull("CacheObject should not be null", be.getCO1());
        assertNotNull("CacheObject should not be null", el.getCO1());
        assertEquals("Both co object should be the same", be.getCO1().getId(), el.getCO1().getId());
        assertTrue("Both co should be the same reference", be.getCO1() == el.getCO1());
     }
  
     public void testPlainSeriazable() throws Exception
     {
        log_.info("testPlainSerializable() ....");
        // First the flag is set to false
        CacheObject co = new CacheObject("1");
        cache_.attach("/test", co);
        CacheObject co1 = (CacheObject) cache1_.find("/test");
        assertNotNull("co on remote cache should not be null", co1);
        assertEquals("co should be the same", co.getId(), co1.getId());
  
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ReplicatedSerializableTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(ReplicatedSerializableTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:01;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/ReplicatedNonSerializableTest.java
  
  Index: ReplicatedNonSerializableTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo;
  
  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.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  
  import javax.naming.Context;
  import java.util.Properties;
  
  /**
   * New NewReplicatedAopTest that doesn't use TreeCacheAopTester.
   *
   * @author Ben Wang
   */
  
  public class ReplicatedNonSerializableTest extends TestCase
  {
     Log log_ = LogFactory.getLog(ReplicatedNonSerializableTest.class);
     PojoCache cache_;
     PojoCache cache1_;
  
     public ReplicatedNonSerializableTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache1_ = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache_.start();
        cache1_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
        cache1_.stop();
     }
  
     public void testDummy()
     {
     }
  
     /* JBCACHE-770
     public void testNonSeriazable1() throws Exception
     {
        log_.info("testNonSerializable1() ....");
        // First the flag is set to false
        NonSerializableObject nso = new NonSerializableObject();
        nso.setId("2");
        try {
           cache_.attach("/test", nso);
           fail("should fail becuase vo is not Serializable");
        }
        catch (RuntimeException e) {
        }
  
        // Then we set the flag
        cache_.setMarshallNonSerializable(true);
        cache1_.setMarshallNonSerializable(true);
        cache_.attach("/test", nso);
        NonSerializableObject nso1 = (NonSerializableObject)cache1_.find("/test");
        assertNotNull("nso on remote cache should not be null", nso1);
        assertEquals("VO should be the same", nso, nso1);
  
     }
  
     public void testNonSeriazable2() throws Exception
     {
        log_.info("testNonSerializable2() ....");
        // First the flag is set to false
        NonSerializableObject nso = new NonSerializableObject();
        nso.setId("2");
  
        // Then we set the flag
        cache_.setMarshallNonSerializable(true);
        cache1_.setMarshallNonSerializable(true);
        cache_.attach("/test", nso);
        NonSerializableObject nso1 = (NonSerializableObject)cache1_.find("/test");
        assertNotNull("nso on remote cache should not be null", nso1);
        assertEquals("VO should be the same", nso, nso1);
  
        nso1 = new NonSerializableObject();
        nso1.setId("4");
        cache1_.attach("/test", nso1);
        nso = (NonSerializableObject)cache_.find("/test");
        assertNotNull("nso on remote cache should not be null", nso);
        assertEquals("VO should be the same", nso, nso1);
  
     }
  
  */
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ReplicatedNonSerializableTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(ReplicatedNonSerializableTest.suite());
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list