1 package org.apache.ojb.broker; 2 3 import junit.framework.TestCase; 4 import org.apache.commons.lang.SerializationUtils; 5 import org.apache.ojb.broker.accesslayer.ConnectionFactory; 6 import org.apache.ojb.broker.accesslayer.ConnectionFactoryDBCPImpl; 7 import org.apache.ojb.broker.accesslayer.ConnectionFactoryFactory; 8 import org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl; 9 import org.apache.ojb.broker.accesslayer.LookupException; 10 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; 11 import org.apache.ojb.broker.metadata.MetadataManager; 12 13 import java.sql.Connection ; 14 15 21 public class ConnectionFactoryTest extends TestCase 22 { 23 PersistenceBroker broker; 24 25 public ConnectionFactoryTest() 26 { 27 } 28 29 public ConnectionFactoryTest(String s) 30 { 31 super(s); 32 } 33 34 public static void main(String [] args) 35 { 36 String [] arr = {ConnectionFactoryTest.class.getName()}; 37 junit.textui.TestRunner.main(arr); 38 } 39 40 44 public void testConnectionFactoryPooledImpl() throws Exception 45 { 46 checkFactory(ConnectionFactoryPooledImpl.class); 47 } 48 49 53 public void testConnectionFactoryDBCPImpl() throws Exception 54 { 55 checkFactory(ConnectionFactoryDBCPImpl.class); 56 } 57 58 private void checkFactory(Class factory) throws Exception 59 { 60 Class oldFac = null; 61 ConnectionFactoryFactory fac = null; 62 try 63 { 64 fac = ConnectionFactoryFactory.getInstance(); 65 oldFac = fac.getClassToServe(); 66 fac.setClassToServe(factory); 67 ConnectionFactory conFac = (ConnectionFactory) fac.createNewInstance(); 68 69 MetadataManager mm = MetadataManager.getInstance(); 70 JdbcConnectionDescriptor jcd = (JdbcConnectionDescriptor) SerializationUtils.clone( 71 broker.serviceConnectionManager().getConnectionDescriptor()); 72 jcd.setJcdAlias(factory.getName() + "_test_checkFactory_a"); 73 jcd.setUseAutoCommit(2); 74 jcd.addAttribute("initializationCheck", "true"); 76 77 mm.connectionRepository().addDescriptor(jcd); 78 Connection con = conFac.lookupConnection(jcd); 79 Connection con2 = conFac.lookupConnection(jcd); 80 Connection con3 = conFac.lookupConnection(jcd); 81 assertFalse("Expect autocommit state false", con.getAutoCommit()); 82 con.close(); 83 con2.close(); 84 con3.close(); 85 86 87 conFac = (ConnectionFactory) fac.createNewInstance(); 88 89 jcd = (JdbcConnectionDescriptor) SerializationUtils.clone( 90 broker.serviceConnectionManager().getConnectionDescriptor()); 91 jcd.setJcdAlias(factory.getName() + "_test_checkFactory_b"); 92 jcd.setUseAutoCommit(1); 93 94 mm.connectionRepository().addDescriptor(jcd); 95 con = conFac.lookupConnection(jcd); 96 assertTrue("Expect autocommit state true", con.getAutoCommit()); 97 } 98 finally 99 { 100 if (oldFac != null) fac.setClassToServe(oldFac); 101 } 102 } 103 104 public void testExhaustedPoolConFacPooledImpl() throws Exception 105 { 106 checkFactoryPoolExhausted(ConnectionFactoryPooledImpl.class); 107 } 108 109 public void testExhaustedPoolConFacDBCPImpl() throws Exception 110 { 111 checkFactoryPoolExhausted(ConnectionFactoryDBCPImpl.class); 112 } 113 114 private void checkFactoryPoolExhausted(Class factory) throws Exception 115 { 116 Class oldFac = null; 117 ConnectionFactoryFactory fac = null; 118 try 119 { 120 fac = ConnectionFactoryFactory.getInstance(); 121 oldFac = fac.getClassToServe(); 122 fac.setClassToServe(factory); 123 ConnectionFactory conFac = (ConnectionFactory) fac.createNewInstance(); 124 125 MetadataManager mm = MetadataManager.getInstance(); 126 JdbcConnectionDescriptor jcd = (JdbcConnectionDescriptor) SerializationUtils.clone( 127 broker.serviceConnectionManager().getConnectionDescriptor()); 128 jcd.setJcdAlias(factory.getName() + "_test_checkFactoryPoolExhausted_1"); 129 jcd.setUseAutoCommit(1); 130 jcd.getConnectionPoolDescriptor().setMaxActive(2); 131 jcd.getConnectionPoolDescriptor().setConnectionFactory(factory); 132 mm.connectionRepository().addDescriptor(jcd); 133 134 Connection con = null; 135 Connection con2 = null; 136 Connection con3 = null; 137 try 138 { 139 con = conFac.lookupConnection(jcd); 140 con2 = conFac.lookupConnection(jcd); 141 try 142 { 143 con3 = conFac.lookupConnection(jcd); 144 fail("We expect an exception indicating that the pool is exhausted"); 145 } 146 catch (LookupException e) 147 { 148 assertTrue(true); 150 } 151 } 152 finally 153 { 154 try 155 { 156 con.close(); 157 con2.close(); 158 } 159 catch (Exception e) 160 { 161 } 162 } 163 } 164 finally 165 { 166 if (oldFac != null) fac.setClassToServe(oldFac); 167 } 168 } 169 170 174 public void setUp() throws PBFactoryException 175 { 176 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 177 } 178 179 183 public void tearDown() 184 { 185 try 186 { 187 broker.close(); 188 } 189 catch (PersistenceBrokerException e) 190 { 191 } 192 } 193 } 194 | Popular Tags |