KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > ConnectionFactoryTest


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 JavaDoc;
14
15 /**
16  * ConnectionFactory implementation related tests.
17  *
18  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
19  * @version $Id: ConnectionFactoryTest.java,v 1.4.4.1 2005/04/27 00:24:33 arminw Exp $
20  */

21 public class ConnectionFactoryTest extends TestCase
22 {
23     PersistenceBroker broker;
24
25     public ConnectionFactoryTest()
26     {
27     }
28
29     public ConnectionFactoryTest(String JavaDoc s)
30     {
31         super(s);
32     }
33
34     public static void main(String JavaDoc[] args)
35     {
36         String JavaDoc[] arr = {ConnectionFactoryTest.class.getName()};
37         junit.textui.TestRunner.main(arr);
38     }
39
40     /**
41      * Simple test to check base functionality of the
42      * ConnectionFactory implementation
43      */

44     public void testConnectionFactoryPooledImpl() throws Exception JavaDoc
45     {
46         checkFactory(ConnectionFactoryPooledImpl.class);
47     }
48
49     /**
50      * Simple test to check base functionality of the
51      * ConnectionFactory implementation
52      */

53     public void testConnectionFactoryDBCPImpl() throws Exception JavaDoc
54     {
55         checkFactory(ConnectionFactoryDBCPImpl.class);
56     }
57
58     private void checkFactory(Class JavaDoc factory) throws Exception JavaDoc
59     {
60         Class JavaDoc 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             // use this attribute to allow OJB changing initial state of connections
75
jcd.addAttribute("initializationCheck", "true");
76
77             mm.connectionRepository().addDescriptor(jcd);
78             Connection JavaDoc con = conFac.lookupConnection(jcd);
79             Connection JavaDoc con2 = conFac.lookupConnection(jcd);
80             Connection JavaDoc 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 JavaDoc
105     {
106         checkFactoryPoolExhausted(ConnectionFactoryPooledImpl.class);
107     }
108
109     public void testExhaustedPoolConFacDBCPImpl() throws Exception JavaDoc
110     {
111         checkFactoryPoolExhausted(ConnectionFactoryDBCPImpl.class);
112     }
113
114     private void checkFactoryPoolExhausted(Class JavaDoc factory) throws Exception JavaDoc
115     {
116         Class JavaDoc 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 JavaDoc con = null;
135             Connection JavaDoc con2 = null;
136             Connection JavaDoc 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                     // we expected that
149
assertTrue(true);
150                 }
151             }
152             finally
153             {
154                 try
155                 {
156                     con.close();
157                     con2.close();
158                 }
159                 catch (Exception JavaDoc e)
160                 {
161                 }
162             }
163         }
164         finally
165         {
166             if (oldFac != null) fac.setClassToServe(oldFac);
167         }
168     }
169
170     /**
171      * Insert the method's description here.
172      * Creation date: (06.12.2000 21:58:53)
173      */

174     public void setUp() throws PBFactoryException
175     {
176         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
177     }
178
179     /**
180      * Insert the method's description here.
181      * Creation date: (06.12.2000 21:59:14)
182      */

183     public void tearDown()
184     {
185         try
186         {
187             broker.close();
188         }
189         catch (PersistenceBrokerException e)
190         {
191         }
192     }
193 }
194
Popular Tags