Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 127   Methods: 7
NCLOC: 93   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
C3p0ConnectionFactoryTest.java - 91.3% 85.7% 90.6%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.loader;
 8   
 9    import com.mchange.v2.c3p0.PooledDataSource;
 10    import junit.framework.TestCase;
 11    import org.apache.commons.logging.Log;
 12    import org.apache.commons.logging.LogFactory;
 13   
 14    import java.sql.Connection;
 15    import java.util.Properties;
 16   
 17    /**
 18    * Unit test for C3p0ConnectionFactory
 19    *
 20    * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
 21    */
 22    public class C3p0ConnectionFactoryTest extends TestCase
 23    {
 24    private static final Log log = LogFactory.getLog(C3p0ConnectionFactoryTest.class);
 25   
 26    private C3p0ConnectionFactory cf;
 27    private AdjListJDBCCacheLoaderConfig config;
 28   
 29  2 public C3p0ConnectionFactoryTest(String string)
 30    {
 31  2 super(string);
 32    }
 33   
 34  2 protected void setUp() throws Exception
 35    {
 36  2 Properties prop = load("cache-jdbc.properties");
 37   
 38  2 config = new AdjListJDBCCacheLoaderConfig();
 39  2 config.setProperties(prop);
 40    }
 41   
 42  2 protected void tearDown() throws Exception
 43    {
 44  2 cf.stop();
 45    }
 46   
 47  1 public void testSetConfig() throws Exception
 48    {
 49  1 config.getProperties().setProperty("c3p0.checkoutTimeout", "10000");
 50   
 51    /* We set the maxPoolSize in two different ways. First, via a System property and secondly, emulating XML
 52    configuration, as it maxPoolSize had been set in the cache loader properties. The system property should
 53    be the one used. Further explanation in C3p0ConnectionFactory */
 54   
 55  1 System.setProperty("c3p0.maxPoolSize", "5");
 56  1 config.getProperties().setProperty("c3p0.maxPoolSize", "3");
 57   
 58  1 cf = new C3p0ConnectionFactory();
 59  1 cf.setConfig(config);
 60  1 cf.start();
 61   
 62  1 Connection c1 = cf.getConnection();
 63  1 Connection c2 = cf.getConnection();
 64  1 Connection c3 = cf.getConnection();
 65  1 Connection c4 = cf.getConnection();
 66  1 Connection c5 = cf.getConnection();
 67  1 Connection c6 = null;
 68   
 69  1 try
 70    {
 71  1 c6 = cf.getConnection();
 72  0 fail("Should have produced an SQLException indicating that it timed out checking out a Connection");
 73    }
 74    catch (IllegalStateException ise)
 75    {
 76    }
 77    finally
 78    {
 79  1 cf.close(c1);
 80  1 cf.close(c2);
 81  1 cf.close(c3);
 82  1 cf.close(c4);
 83  1 cf.close(c5);
 84  1 cf.close(c6);
 85    }
 86    }
 87   
 88  1 public void testGetConnection() throws Exception
 89    {
 90  1 cf = new C3p0ConnectionFactory();
 91  1 cf.setConfig(config);
 92  1 cf.start();
 93  1 PooledDataSource internalDs = (PooledDataSource) cf.getDataSource();
 94   
 95  1 Connection c1 = cf.getConnection();
 96  1 Connection c2 = cf.getConnection();
 97  1 assertEquals("There should be two connections checked out", 2, internalDs.getNumBusyConnectionsDefaultUser());
 98   
 99  1 cf.close(c1);
 100  1 Thread.sleep(100);
 101  1 assertEquals("There should be one connection checked out", 1, internalDs.getNumBusyConnectionsDefaultUser());
 102   
 103  1 cf.close(c2);
 104  1 Thread.sleep(100);
 105  1 assertEquals("There should be no connections checked out", 0, internalDs.getNumBusyConnectionsDefaultUser());
 106    }
 107   
 108  2 private Properties load(String resource) throws Exception
 109    {
 110  2 Properties prop = new Properties();
 111  2 try
 112    {
 113  2 prop.load(this.getClass().getClassLoader().getResourceAsStream(resource));
 114  2 return prop;
 115    }
 116    catch (Exception e)
 117    {
 118  0 log("Error loading jdbc properties ");
 119  0 throw (e);
 120    }
 121    }
 122   
 123  0 private void log(Object o)
 124    {
 125  0 System.out.println(o);
 126    }
 127    }