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