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

Galder Zamarreno galder.zamarreno at jboss.com
Sun Jan 21 10:46:42 EST 2007


  User: gzamarreno
  Date: 07/01/21 10:46:42

  Modified:    tests/functional/org/jboss/cache/loader     
                        CacheLoaderTestsBase.java JDBCCacheLoaderTest.java
  Added:       tests/functional/org/jboss/cache/loader     
                        JDBCCacheLoaderConfigTest.java
                        C3p0JDBCCacheLoaderTest.java
                        C3p0ConnectionFactoryTest.java
  Log:
  [JBCACHE-802] c3p0 JDBC connection pooling functionality and corresponding unit tests added. JDBCCacheLoader has been refactored to provide better reporting by not swallowing exceptions and making ConnectionFactory and implementations separate java class files.
  
  Revision  Changes    Path
  1.45      +20 -10    JBossCache/tests/functional/org/jboss/cache/loader/CacheLoaderTestsBase.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheLoaderTestsBase.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/loader/CacheLoaderTestsBase.java,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -b -r1.44 -r1.45
  --- CacheLoaderTestsBase.java	17 Jan 2007 14:13:05 -0000	1.44
  +++ CacheLoaderTestsBase.java	21 Jan 2007 15:46:42 -0000	1.45
  @@ -16,6 +16,8 @@
   import org.jboss.util.stream.MarshalledValueOutputStream;
   
   import javax.transaction.Transaction;
  +import javax.transaction.TransactionManager;
  +import javax.transaction.Status;
   import java.io.ByteArrayInputStream;
   import java.io.ByteArrayOutputStream;
   import java.io.File;
  @@ -30,7 +32,7 @@
    * Commons tests for all CacheLoaders
    *
    * @author Bela Ban
  - * @version $Id: CacheLoaderTestsBase.java,v 1.44 2007/01/17 14:13:05 msurtani Exp $
  + * @version $Id: CacheLoaderTestsBase.java,v 1.45 2007/01/21 15:46:42 gzamarreno Exp $
    */
   abstract public class CacheLoaderTestsBase extends AbstractCacheLoaderTestBase
   {
  @@ -977,7 +979,7 @@
         cache.evict(Fqn.fromString("/a"));
         addDelay();
   
  -      DummyTransactionManager mgr = DummyTransactionManager.getInstance();
  +      TransactionManager mgr = getTransactionManager();
   
         mgr.begin();
         tx = mgr.getTransaction();
  @@ -1000,7 +1002,7 @@
   
      public void testTxPutCommit() throws Exception
      {
  -      DummyTransactionManager mgr = DummyTransactionManager.getInstance();
  +      TransactionManager mgr = getTransactionManager();
         mgr.begin();
         tx = mgr.getTransaction();
   
  @@ -1018,7 +1020,7 @@
   
      public void testTxPutRollback() throws Exception
      {
  -      DummyTransactionManager mgr = DummyTransactionManager.getInstance();
  +      TransactionManager mgr = getTransactionManager();
   
         cache.remove("/one");
         addDelay();
  @@ -1059,7 +1061,7 @@
              throws Exception
      {
   
  -      DummyTransactionManager mgr = DummyTransactionManager.getInstance();
  +      TransactionManager mgr = getTransactionManager();
         mgr.begin();
         tx = mgr.getTransaction();
         doTestBasicOperations();
  @@ -1446,7 +1448,7 @@
              throws Exception
      {
   
  -      DummyTransactionManager mgr = DummyTransactionManager.getInstance();
  +      TransactionManager mgr = getTransactionManager();
         mgr.begin();
         tx = mgr.getTransaction();
         doTestModifications();
  @@ -1518,10 +1520,14 @@
              throws Exception
      {
   
  -      Object txnKey = new Object();
  +      // Object txnKey = new Object();
  +      TransactionManager mgr = getTransactionManager();
  +      mgr.begin();
  +      tx = mgr.getTransaction();
  +
         List mods = createUpdates();
  -      loader.prepare(txnKey, mods, false);
  -      loader.commit(txnKey);
  +      loader.prepare(tx, mods, false);
  +      loader.commit(tx);
         addDelay();
         checkModifications(mods);
      }
  @@ -2025,4 +2031,8 @@
   
      }
   
  +   protected TransactionManager getTransactionManager()
  +   {
  +      return DummyTransactionManager.getInstance();
  +   }
   }
  
  
  
  1.10      +8 -24     JBossCache/tests/functional/org/jboss/cache/loader/JDBCCacheLoaderTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: JDBCCacheLoaderTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/loader/JDBCCacheLoaderTest.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -b -r1.9 -r1.10
  --- JDBCCacheLoaderTest.java	17 Jan 2007 18:57:41 -0000	1.9
  +++ JDBCCacheLoaderTest.java	21 Jan 2007 15:46:42 -0000	1.10
  @@ -24,34 +24,24 @@
    *
    * @author <a href="hmesha at novell.com">Hany Mesha</a>
    * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
  - * @version <tt>$Revision: 1.9 $</tt>
  + * @version <tt>$Revision: 1.10 $</tt>
    */
   public class JDBCCacheLoaderTest extends CacheLoaderTestsBase
   {
  -   private static final boolean MANAGED = true;
  +   public JDBCCacheLoaderTest(String name)
  +   {
  +      super(name);
  +   }
   
      protected void configureCache() throws Exception
      {
         Properties prop = getProperties();
   
  -      String props;
  -      if (MANAGED)
  -      {
  -         props = "cache.jdbc.datasource=" + prop.getProperty("cache.jdbc.datasource") + "\n" +
  -               "cache.jdbc.node.type=" + prop.getProperty("cache.jdbc.node.type") + "\n" +
  -               "cache.jdbc.url=" + prop.getProperty("cache.jdbc.url") + "\n" +
  -               "cache.jdbc.user=" + prop.getProperty("cache.jdbc.user") + "\n" +
  -               "cache.jdbc.password=" + prop.getProperty("cache.jdbc.password");
  -         //cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
  -      }
  -      else
  -      {
  -         props = "cache.jdbc.driver =" + prop.getProperty("cache.jdbc.driver") + "\n" +
  +      String props = "cache.jdbc.driver =" + prop.getProperty("cache.jdbc.driver") + "\n" +
               "cache.jdbc.url=" + prop.getProperty("cache.jdbc.url") + "\n" +
               "cache.jdbc.user=" + prop.getProperty("cache.jdbc.user") + "\n" +
               "cache.jdbc.password=" + prop.getProperty("cache.jdbc.password") + "\n" +
               "cache.jdbc.node.type=" + prop.getProperty("cache.jdbc.node.type");
  -      }
   
         cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("",
               "org.jboss.cache.loader.JDBCCacheLoader", props, false, true, false));
  @@ -84,12 +74,11 @@
            assertEquals(updatedValue, (String) loader.get(FQN).get(key));
         } catch (Exception e)
         {
  -         e.printStackTrace();         
  -         fail("See Stack Trace");
  +         fail(e.toString());
         }
      }
   
  -   private Properties getProperties() throws Exception
  +   protected Properties getProperties() throws Exception
      {
         Properties properties = new Properties();
         try
  @@ -107,9 +96,4 @@
      {
         return new TestSuite(JDBCCacheLoaderTest.class);
      }
  -
  -   public static void main(String[] args)
  -   {
  -      junit.textui.TestRunner.run(suite());
  -   }
   }
  
  
  
  1.1      date: 2007/01/21 15:46:42;  author: gzamarreno;  state: Exp;JBossCache/tests/functional/org/jboss/cache/loader/JDBCCacheLoaderConfigTest.java
  
  Index: JDBCCacheLoaderConfigTest.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   * 
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.loader;
  
  import junit.framework.TestCase;
  
  /**
   * Unit test for JDBCCacheLoaderConfig
   *
   * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
   */
  public class JDBCCacheLoaderConfigTest extends TestCase
  {
     private JDBCCacheLoaderConfig cacheLoaderConfig;
  
     protected void setUp() throws Exception
     {
        cacheLoaderConfig = new JDBCCacheLoaderConfig();
     }
  
     public void testSetGetConnectionFactory()
     {
        cacheLoaderConfig.setConnectionFactoryClass("com.acme.Paradise");
        assertEquals("com.acme.Paradise", cacheLoaderConfig.getConnectionFactoryClass());
     }
  
     public void testEqualsHashCode()
     {
        cacheLoaderConfig.setConnectionFactoryClass("com.acme.Paradise");
        JDBCCacheLoaderConfig other = new JDBCCacheLoaderConfig();
        other.setConnectionFactoryClass("com.acme.Paradise");
        assertTrue(cacheLoaderConfig.equals(other));
        assertEquals(cacheLoaderConfig.hashCode(), other.hashCode());
  
        other.setConnectionFactoryClass("com.ibm.flaming.Gala");
        assertFalse(cacheLoaderConfig.equals(other));
     }
  }
  
  
  
  1.1      date: 2007/01/21 15:46:42;  author: gzamarreno;  state: Exp;JBossCache/tests/functional/org/jboss/cache/loader/C3p0JDBCCacheLoaderTest.java
  
  Index: C3p0JDBCCacheLoaderTest.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   * 
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.loader;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  import java.util.Properties;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * Unit test that runs the the tests defined JDBCCacheLoaderTest using a standalone
   * connection pool factory based on c3p0 library. 
   *
   * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
   */
  public class C3p0JDBCCacheLoaderTest extends JDBCCacheLoaderTest
  {
     private static final Log log = LogFactory.getLog(C3p0JDBCCacheLoaderTest.class);
     private static final String CF_CLASS = "org.jboss.cache.loader.C3p0ConnectionFactory";
  
     public C3p0JDBCCacheLoaderTest(String name)
     {
        super(name);
     }
  
     protected void configureCache() throws Exception
     {
        Properties prop = getProperties();
  
        String props = "cache.jdbc.driver =" + prop.getProperty("cache.jdbc.driver") + "\n" +
              "cache.jdbc.url=" + prop.getProperty("cache.jdbc.url") + "\n" +
              "cache.jdbc.user=" + prop.getProperty("cache.jdbc.user") + "\n" +
              "cache.jdbc.password=" + prop.getProperty("cache.jdbc.password") + "\n" +
              "cache.jdbc.node.type=" + prop.getProperty("cache.jdbc.node.type") + "\n" +
              "cache.jdbc.connection.factory=" + CF_CLASS;
  
        cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("",
              "org.jboss.cache.loader.JDBCCacheLoader", props, false, true, false));
     }
  
     public static Test suite()
     {
        return new TestSuite(C3p0JDBCCacheLoaderTest.class);
     }
  }
  
  
  
  1.1      date: 2007/01/21 15:46:42;  author: gzamarreno;  state: Exp;JBossCache/tests/functional/org/jboss/cache/loader/C3p0ConnectionFactoryTest.java
  
  Index: C3p0ConnectionFactoryTest.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   * 
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.loader;
  
  import com.mchange.v2.c3p0.PooledDataSource;
  import junit.framework.TestCase;
  
  import java.sql.Connection;
  import java.util.Properties;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * Unit test for C3p0ConnectionFactory
   *
   * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
   */
  public class C3p0ConnectionFactoryTest extends TestCase
  {
     private static final Log log = LogFactory.getLog(C3p0ConnectionFactoryTest.class);
  
     private C3p0ConnectionFactory cf;
     private JDBCCacheLoaderConfig config;
  
     public C3p0ConnectionFactoryTest(String string)
     {
        super(string);
     }
  
     protected void setUp() throws Exception
     {
        Properties prop = load("cache-jdbc.properties");
  
        config = new JDBCCacheLoaderConfig();
        config.setProperties(prop);
     }
  
     protected void tearDown() throws Exception
     {
        cf.stop();
     }
  
     public void testSetConfig() throws Exception
     {
        config.getProperties().setProperty("c3p0.checkoutTimeout", "10000");
  
        /* We set the maxPoolSize in two different ways. First, via a System property and secondly, emulating XML
        configuration, as it maxPoolSize had been set in the cache loader properties. The system property should
        be the one used. Further explanation in C3p0ConnectionFactory */
        
        System.setProperty("c3p0.maxPoolSize", "5");
        config.getProperties().setProperty("c3p0.maxPoolSize", "3");
  
        cf = new C3p0ConnectionFactory();
        cf.setConfig(config);
        cf.start();
  
        Connection c1 = cf.getConnection();
        Connection c2 = cf.getConnection();
        Connection c3 = cf.getConnection();
        Connection c4 = cf.getConnection();
        Connection c5 = cf.getConnection();
        Connection c6 = null;
  
        try
        {
           c6 = cf.getConnection();
           fail("Should have produced an SQLException indicating that it timed out checking out a Connection");
        }
        catch (IllegalStateException ise)
        {
        }
        finally
        {
           cf.close(c1);
           cf.close(c2);
           cf.close(c3);
           cf.close(c4);
           cf.close(c5);
           cf.close(c6);
        }
     }
  
     public void testGetConnection() throws Exception
     {
        cf = new C3p0ConnectionFactory();
        cf.setConfig(config);
        cf.start();
        PooledDataSource internalDs = (PooledDataSource) cf.getDataSource();
  
        Connection c1 = cf.getConnection();
        Connection c2 = cf.getConnection();
        assertEquals("There should be two connections checked out", 2, internalDs.getNumBusyConnectionsDefaultUser());
  
        cf.close(c1);
        Thread.sleep(100);
        assertEquals("There should be one connection checked out", 1, internalDs.getNumBusyConnectionsDefaultUser());
  
        cf.close(c2);
        Thread.sleep(100);
        assertEquals("There should be no connections checked out", 0, internalDs.getNumBusyConnectionsDefaultUser());
     }
  
     private Properties load(String resource) throws Exception
     {
        Properties prop = new Properties();
        try
        {
           prop.load(this.getClass().getClassLoader().getResourceAsStream(resource));
           return prop;
        } catch (Exception e)
        {
           log("Error loading jdbc properties ");
           throw (e);
        }
     }
  
     private void log(Object o)
     {
        System.out.println(o);
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list