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

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


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

  Added:       tests/functional/org/jboss/cache/pojo/region    
                        LocalConcurrentTest.java NewLocalTest.java
                        LocalTest.java ReplicatedTest.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:06;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/region/LocalConcurrentTest.java
  
  Index: LocalConcurrentTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.region;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.jboss.cache.pojo.*;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.cache.lock.UpgradeException;
  import org.jboss.cache.Fqn;
  
  import javax.transaction.UserTransaction;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import java.util.Properties;
  import java.util.ArrayList;
  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_;
     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();
        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() throws Exception
     {
        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, "t1");
        RunThread t2 = new RunThread(2, "t2");
        RunThread t3 = new RunThread(3, "t3");
        RunThread t4 = new RunThread(4, "t4");
  
        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, String threadName)
        {
           super(threadName);
           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
           {
              cache_.getCache().getRegion(Fqn.fromString(Thread.currentThread().getName()), true);
              _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.
              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 = (String) 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
        String str = Thread.currentThread().getName();
        oldList.add(str);
        newList.add(str);
        strList.add(str);
  
        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 = (String) oldList.get(i);
                 tmp += Integer.toString(j);
                 if (depth != 1)
                 {
                    tmp += "/";
                 }
  
                 newList.add(tmp);
              }
           }
           strList.addAll(newList);
           oldList = newList;
           depth--;
        }
  
        // let's prune out root node
        for (int i = 0; i < strList.size(); i++)
        {
           if (strList.get(i).equals("/"))
           {
              strList.remove(i);
              break;
           }
        }
        log("Nodes generated: " + strList.size());
        return strList;
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(org.jboss.cache.pojo.region.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:06;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/region/NewLocalTest.java
  
  Index: NewLocalTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.region;
  
  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.InternalConstant;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.test.Person;
  
  import java.util.Map;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class NewLocalTest extends TestCase
  {
     Log log_ = LogFactory.getLog(org.jboss.cache.pojo.region.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();
        cache_.getCache().getRegion(Fqn.fromString("SESSION"), true);
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     /**
      * Not applied anymore.
      *
      * @throws Exception
      */
     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().get("person"));
     }
  
     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());
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(org.jboss.cache.pojo.region.NewLocalTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(org.jboss.cache.pojo.region.NewLocalTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:06;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/region/LocalTest.java
  
  Index: LocalTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.region;
  
  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.CacheImpl;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.Node;
  import org.jboss.cache.pojo.InternalConstant;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.pojo.test.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(org.jboss.cache.pojo.region.LocalTest.class);
     PojoCache cache_;
     static final String REGION = "person";
  
     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();
        cache_.getCache().getRegion(Fqn.fromString(REGION), true);
        cache_.getCache().getRegion(Fqn.fromString("RANDOM"), true);
     }
  
     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());
  
        assertTrue("Region node should exist ",
                cache_.getCache().getRoot().hasChild(new Fqn(REGION)));
        Fqn fqn = new Fqn(Fqn.fromString(REGION), InternalConstant.JBOSS_INTERNAL);
        assertTrue("Internal region node should exist ",
                cache_.getCache().getRoot().hasChild(fqn));
     }
  
     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");
  
        String str = ((CacheImpl) cache_.getCache()).printDetails();
        System.out.println("**** Details ***/n" + str);
  
        Fqn fqn = new Fqn(Fqn.fromString(REGION), InternalConstant.JBOSS_INTERNAL);
        Node n = cache_.getCache().getRoot().getChild(fqn);
        assertTrue("Internal region node should not exist ",
                n.getChildren().size() == 0);
     }
  
     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 ", (Object) "Lipitor", (Object) 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");
  
        Fqn fqn = new Fqn(Fqn.fromString(REGION), InternalConstant.JBOSS_INTERNAL);
        Node n = cache_.getCache().getRoot().getChild(fqn);
        assertTrue("Internal region node should not exist ",
                n.getChildren().size() == 0);
  
        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 = (String) 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);
        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 = (String) 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(org.jboss.cache.pojo.region.LocalTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(org.jboss.cache.pojo.region.LocalTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:06;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/region/ReplicatedTest.java
  
  Index: ReplicatedTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.region;
  
  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.*;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Student;
  import org.jboss.cache.Fqn;
  
  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(org.jboss.cache.pojo.region.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();
        cache.getCache().getRegion(Fqn.fromString("SESSION"), true);
        cache1.getCache().getRegion(Fqn.fromString("SESSION"), true);
     }
  
     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(org.jboss.cache.pojo.region.ReplicatedTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(org.jboss.cache.pojo.region.ReplicatedTest.suite());
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list