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

Ben Wang bwang at jboss.com
Wed Jul 26 22:19:31 EDT 2006


  User: bwang   
  Date: 06/07/26 22:19:31

  Added:       tests-50/functional/org/jboss/cache/pojo  LocalTxTest.java
  Log:
  created
  
  Revision  Changes    Path
  1.1      date: 2006/07/27 02:19:31;  author: bwang;  state: Exp;JBossCache/tests-50/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.Student;
  
  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";
        cache = PojoCacheFactory.createInstance(configFile);
        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 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()
           {
              try
              {
                 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
                 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 lang.add("English");
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 t2_ex = ex;
              }
           }
        };
  
        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);
     }
  
     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());
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list