001: package org.apache.ojb.broker;
002:
003: import junit.framework.TestCase;
004: import org.apache.commons.lang.SerializationUtils;
005: import org.apache.ojb.broker.accesslayer.ConnectionFactory;
006: import org.apache.ojb.broker.accesslayer.ConnectionFactoryDBCPImpl;
007: import org.apache.ojb.broker.accesslayer.ConnectionFactoryFactory;
008: import org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl;
009: import org.apache.ojb.broker.accesslayer.LookupException;
010: import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
011: import org.apache.ojb.broker.metadata.MetadataManager;
012:
013: import java.sql.Connection;
014:
015: /**
016: * ConnectionFactory implementation related tests.
017: *
018: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
019: * @version $Id: ConnectionFactoryTest.java,v 1.4.4.1 2005/04/27 00:24:33 arminw Exp $
020: */
021: public class ConnectionFactoryTest extends TestCase {
022: PersistenceBroker broker;
023:
024: public ConnectionFactoryTest() {
025: }
026:
027: public ConnectionFactoryTest(String s) {
028: super (s);
029: }
030:
031: public static void main(String[] args) {
032: String[] arr = { ConnectionFactoryTest.class.getName() };
033: junit.textui.TestRunner.main(arr);
034: }
035:
036: /**
037: * Simple test to check base functionality of the
038: * ConnectionFactory implementation
039: */
040: public void testConnectionFactoryPooledImpl() throws Exception {
041: checkFactory(ConnectionFactoryPooledImpl.class);
042: }
043:
044: /**
045: * Simple test to check base functionality of the
046: * ConnectionFactory implementation
047: */
048: public void testConnectionFactoryDBCPImpl() throws Exception {
049: checkFactory(ConnectionFactoryDBCPImpl.class);
050: }
051:
052: private void checkFactory(Class factory) throws Exception {
053: Class oldFac = null;
054: ConnectionFactoryFactory fac = null;
055: try {
056: fac = ConnectionFactoryFactory.getInstance();
057: oldFac = fac.getClassToServe();
058: fac.setClassToServe(factory);
059: ConnectionFactory conFac = (ConnectionFactory) fac
060: .createNewInstance();
061:
062: MetadataManager mm = MetadataManager.getInstance();
063: JdbcConnectionDescriptor jcd = (JdbcConnectionDescriptor) SerializationUtils
064: .clone(broker.serviceConnectionManager()
065: .getConnectionDescriptor());
066: jcd.setJcdAlias(factory.getName() + "_test_checkFactory_a");
067: jcd.setUseAutoCommit(2);
068: // use this attribute to allow OJB changing initial state of connections
069: jcd.addAttribute("initializationCheck", "true");
070:
071: mm.connectionRepository().addDescriptor(jcd);
072: Connection con = conFac.lookupConnection(jcd);
073: Connection con2 = conFac.lookupConnection(jcd);
074: Connection con3 = conFac.lookupConnection(jcd);
075: assertFalse("Expect autocommit state false", con
076: .getAutoCommit());
077: con.close();
078: con2.close();
079: con3.close();
080:
081: conFac = (ConnectionFactory) fac.createNewInstance();
082:
083: jcd = (JdbcConnectionDescriptor) SerializationUtils
084: .clone(broker.serviceConnectionManager()
085: .getConnectionDescriptor());
086: jcd.setJcdAlias(factory.getName() + "_test_checkFactory_b");
087: jcd.setUseAutoCommit(1);
088:
089: mm.connectionRepository().addDescriptor(jcd);
090: con = conFac.lookupConnection(jcd);
091: assertTrue("Expect autocommit state true", con
092: .getAutoCommit());
093: } finally {
094: if (oldFac != null)
095: fac.setClassToServe(oldFac);
096: }
097: }
098:
099: public void testExhaustedPoolConFacPooledImpl() throws Exception {
100: checkFactoryPoolExhausted(ConnectionFactoryPooledImpl.class);
101: }
102:
103: public void testExhaustedPoolConFacDBCPImpl() throws Exception {
104: checkFactoryPoolExhausted(ConnectionFactoryDBCPImpl.class);
105: }
106:
107: private void checkFactoryPoolExhausted(Class factory)
108: throws Exception {
109: Class oldFac = null;
110: ConnectionFactoryFactory fac = null;
111: try {
112: fac = ConnectionFactoryFactory.getInstance();
113: oldFac = fac.getClassToServe();
114: fac.setClassToServe(factory);
115: ConnectionFactory conFac = (ConnectionFactory) fac
116: .createNewInstance();
117:
118: MetadataManager mm = MetadataManager.getInstance();
119: JdbcConnectionDescriptor jcd = (JdbcConnectionDescriptor) SerializationUtils
120: .clone(broker.serviceConnectionManager()
121: .getConnectionDescriptor());
122: jcd.setJcdAlias(factory.getName()
123: + "_test_checkFactoryPoolExhausted_1");
124: jcd.setUseAutoCommit(1);
125: jcd.getConnectionPoolDescriptor().setMaxActive(2);
126: jcd.getConnectionPoolDescriptor().setConnectionFactory(
127: factory);
128: mm.connectionRepository().addDescriptor(jcd);
129:
130: Connection con = null;
131: Connection con2 = null;
132: Connection con3 = null;
133: try {
134: con = conFac.lookupConnection(jcd);
135: con2 = conFac.lookupConnection(jcd);
136: try {
137: con3 = conFac.lookupConnection(jcd);
138: fail("We expect an exception indicating that the pool is exhausted");
139: } catch (LookupException e) {
140: // we expected that
141: assertTrue(true);
142: }
143: } finally {
144: try {
145: con.close();
146: con2.close();
147: } catch (Exception e) {
148: }
149: }
150: } finally {
151: if (oldFac != null)
152: fac.setClassToServe(oldFac);
153: }
154: }
155:
156: /**
157: * Insert the method's description here.
158: * Creation date: (06.12.2000 21:58:53)
159: */
160: public void setUp() throws PBFactoryException {
161: broker = PersistenceBrokerFactory.defaultPersistenceBroker();
162: }
163:
164: /**
165: * Insert the method's description here.
166: * Creation date: (06.12.2000 21:59:14)
167: */
168: public void tearDown() {
169: try {
170: broker.close();
171: } catch (PersistenceBrokerException e) {
172: }
173: }
174: }
|