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

Brian Stansberry brian.stansberry at jboss.com
Wed Jun 20 16:46:55 EDT 2007


  User: bstansberry
  Date: 07/06/20 16:46:55

  Added:       tests/functional/org/jboss/cache/pojo 
                        BuddyReplicationTest.java
  Log:
  Add test for JBCACHE-1111.
  
  Revision  Changes    Path
  1.1      date: 2007/06/20 20:46:55;  author: bstansberry;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/BuddyReplicationTest.java
  
  Index: BuddyReplicationTest.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 java.util.List;
  import java.util.Set;
  
  import junit.framework.TestCase;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.Node;
  import org.jboss.cache.buddyreplication.NextMemberBuddyLocatorConfig;
  import org.jboss.cache.config.BuddyReplicationConfig;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
  import org.jboss.cache.pojo.impl.PojoReference;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Student;
  
  /**
   * A BuddyReplicatedTest.
   * 
   * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
   * @version $Revision: 1.1 $
   */
  public class BuddyReplicationTest extends TestCase
  {
     Log log = LogFactory.getLog(ReplicatedTest.class);
     PojoCache cache, cache1;
  
     public BuddyReplicationTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        boolean toStart = false;
        Configuration cfg1 = UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC);
        addBuddyReplication(cfg1);
        cache = PojoCacheFactory.createCache(cfg1, toStart);
        cache.start();
        Configuration cfg2 = UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC);
        addBuddyReplication(cfg2);
        cache1 = PojoCacheFactory.createCache(cfg2, toStart);
        cache1.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache.stop();
        cache1.stop();
     }
  
     private void addBuddyReplication(Configuration cfg)
     {
        BuddyReplicationConfig brc = new BuddyReplicationConfig();
        brc.setAutoDataGravitation(false);
        brc.setBuddyPoolName("test");
        brc.setEnabled(true);
        NextMemberBuddyLocatorConfig blc = new NextMemberBuddyLocatorConfig();
        brc.setBuddyLocatorConfig(blc);
        
        cfg.setBuddyReplicationConfig(brc);
     }
  
     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;
     }
  
     private Person getReplicatedPerson(String id) throws PojoCacheException
     {
        // FIXME -- this is a hacky way to force gravitation via the Cache API.
        // Adding the Option should be done before the PojoCache.find() call.
        // I do it this way just to prove the node gets gravitated and that
        // the problem is dereferencing to _JBossInternal_ is will fail
        cache1.getCache().getInvocationContext().getOptionOverrides().setForceDataGravitation(true);
        Node node = cache1.getCache().getRoot().getChild(Fqn.fromString(id));
        assertNotNull("Node found at " + id, node);
        Set keys = node.getKeys();
        assertTrue("Node at " + id + " has keys", keys.size() > 0);
        // FIXME also hacky -- prove that the node has PojoCache data
        assertTrue("Node at " + id + " has key " + PojoReference.KEY, keys.contains(PojoReference.KEY));
        
        
        Person repl = (Person) cache1.find(id);
        assertNotNull("Person found at " + id, repl);
        return repl;
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
        Person ben = createPerson("/person/test1", "Ben Wang", 40);
        assertEquals("Ben Wang", ben.getName());
        Person repl = getReplicatedPerson("/person/test1");
        assertEquals("Ben Wang", repl.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));
           Person repl = getReplicatedPerson("/person/test3");
           assertEquals("Medication on cache1 ", "Vioxx",
                        repl.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());
        Person repl = getReplicatedPerson("/person/test1");
        assertEquals("Cache 2 ", "Active",
                     repl.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");
        Person repl = getReplicatedPerson("/person/test2");
        assertEquals("Harald Gliebe", repl.getName());
        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");
        cache1.getCache().getInvocationContext().getOptionOverrides().setForceDataGravitation(true);
        Student joe1 = (Student) getReplicatedPerson("/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");
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list