KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > connection > AbstractPoolConnectionManagerTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Mathieu Peltier.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.connection;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.util.EmptyStackException JavaDoc;
31 import java.util.Stack JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException;
35 import org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager;
36 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
37 import org.objectweb.cjdbc.scenario.tools.mock.MockDriver;
38 import org.objectweb.cjdbc.scenario.tools.util.PrivilegedAccessor;
39
40 import com.mockobjects.sql.MockConnection2;
41
42 /**
43  * <code>AbstractPoolConnectionManager</code> test class.
44  *
45  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
46  * @see org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager
47  */

48 public class AbstractPoolConnectionManagerTest extends NoTemplate
49 {
50   /** Fake driver. */
51   private MockDriver mockDriver;
52
53   /** Pool manager to test. */
54   private AbstractPoolConnectionManager pool;
55
56   /** Idle connections contained in the pool. */
57   private Stack JavaDoc freeConnections;
58
59   /** Active connections contained in the pool. */
60   private Vector JavaDoc activeConnections;
61
62   /**
63    * Creates and initializes a <code>VariablePoolConnectionManager</code>.
64    *
65    * @param maxConnectionNumber maximum number of connection allowed by the
66    * driver.
67    * @param requestedPoolSize requested pool size.
68    */

69   private void initializePool(int maxConnectionNumber, int requestedPoolSize)
70   {
71     // Create a new driver that will return only maxConnectionNumber
72
// connections
73
mockDriver = new MyMockDriver(maxConnectionNumber);
74
75     // Register the driver
76
try
77     {
78       DriverManager.registerDriver(mockDriver);
79     }
80     catch (SQLException JavaDoc e)
81     {
82       fail("Failed to register driver: " + e);
83     }
84
85     // Create a new pool
86
try
87     {
88       pool = new MockPoolConnectionManager("", "", "", "", requestedPoolSize);
89     }
90     catch (Exception JavaDoc e)
91     {
92       fail("Failed to create pool connection manager: " + e);
93     }
94
95     // Initialize the pool
96
try
97     {
98       pool.initializeConnections();
99     }
100     catch (SQLException JavaDoc e)
101     {
102       fail("Failed to initialize pool connection manager: " + e);
103     }
104
105     // Get a reference to the idle and active connections
106
try
107     {
108       freeConnections = (Stack JavaDoc) PrivilegedAccessor.getValue(pool,
109           "freeConnections");
110       activeConnections = (Vector JavaDoc) PrivilegedAccessor.getValue(pool,
111           "activeConnections");
112     }
113     catch (Exception JavaDoc e)
114     {
115       fail("Unexpected exception thrown: " + e);
116     }
117
118     // Deregister the driver
119
try
120     {
121       DriverManager.deregisterDriver(mockDriver);
122     }
123     catch (SQLException JavaDoc e)
124     {
125       fail("Failed to deregister driver: " + e);
126     }
127   }
128
129   /**
130    * @see org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager#initializeConnections()
131    */

132   public void testInitializeConnections()
133   {
134     try
135     {
136       // Request connections from a driver that never gets one connection
137
initializePool(0, 10);
138       assertEquals(0, freeConnections.size());
139       assertEquals(0, activeConnections.size());
140       assertEquals(0, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
141           .intValue());
142       assertTrue(pool.isInitialized());
143
144       // Normal case
145
initializePool(20, 10);
146       assertEquals(10, freeConnections.size());
147       assertEquals(0, activeConnections.size());
148       assertEquals(10,
149           ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize")).intValue());
150       assertTrue(pool.isInitialized());
151
152       // Limit case
153
initializePool(20, 20);
154       assertEquals(20, freeConnections.size());
155       assertEquals(0, activeConnections.size());
156       assertEquals(20,
157           ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize")).intValue());
158       assertTrue(pool.isInitialized());
159
160       // Request too much connections
161
initializePool(20, 30);
162       assertEquals(20, freeConnections.size());
163       assertEquals(0, activeConnections.size());
164       assertEquals(20,
165           ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize")).intValue());
166       assertTrue(pool.isInitialized());
167     }
168     catch (Exception JavaDoc e)
169     {
170       fail("Unexpected exception thrown: " + e);
171     }
172
173     // Try to initialize a pool twice
174
try
175     {
176       pool.initializeConnections();
177       fail("Exception not thrown when trying to initialized twice a pool");
178     }
179     catch (SQLException JavaDoc expected)
180     {
181     }
182   }
183
184   /**
185    * @see org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager#finalizeConnections()
186    */

187   public void testFinalizeConnections()
188   {
189     // Create a pool of 10 connections and initialize it
190
initializePool(10, 10);
191
192     // Get 6 connections to make them active
193
for (int i = 0; i < 6; i++)
194     {
195       try
196       {
197         pool.getConnection();
198       }
199       catch (UnreachableBackendException e1)
200       {
201         fail("Backend unreachable during test.");
202       }
203     }
204
205     // Finalize the pool
206
try
207     {
208       pool.finalizeConnections();
209     }
210     catch (SQLException JavaDoc e)
211     {
212       fail("Failed to finalize pool connection manager: " + e);
213     }
214
215     // Check that connections are closed
216
mockDriver.verify();
217   }
218
219   /**
220    * Mock pool connection manager (minimum implementation of the
221    * <code>AbstractPoolConnectionManager</code> class).
222    *
223    * @see org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager
224    */

225   public class MockPoolConnectionManager extends AbstractPoolConnectionManager
226   {
227     /**
228      * @see org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager#AbstractPoolConnectionManager
229      */

230     public MockPoolConnectionManager(String JavaDoc backendUrl, String JavaDoc backendName,
231         String JavaDoc rLogin, String JavaDoc rPassword, int poolSize)
232     {
233       super(backendUrl, backendName, rLogin, rPassword, null, null, poolSize);
234     }
235
236     /**
237      * @see org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager#getConnection()
238      */

239     public Connection JavaDoc getConnection()
240     {
241       try
242       {
243         Connection JavaDoc c = (Connection JavaDoc) this.freeConnections.removeLast();
244         this.activeConnections.add(c);
245         return c;
246       }
247       catch (EmptyStackException JavaDoc e)
248       {
249         return null;
250       }
251     }
252
253     /**
254      * @see org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager#releaseConnection(Connection)
255      */

256     public void releaseConnection(Connection JavaDoc connection)
257     {
258       this.freeConnections.addLast(connection);
259     }
260
261     /**
262      * @see org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager#deleteConnection(Connection)
263      */

264     public void deleteConnection(Connection JavaDoc connection)
265     {
266       this.activeConnections.remove(connection);
267     }
268
269     /**
270      * @see org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager#getXmlImpl()
271      */

272     public String JavaDoc getXmlImpl()
273     {
274       return null;
275     }
276
277     /**
278      * @see java.lang.Object#clone()
279      */

280     protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
281     {
282       return null;
283     }
284   }
285
286   /**
287    * Mock driver. The method <code>connect()</code> will return only a limited
288    * number of connections, specified by <code>maxConnectionNumber</code>. If
289    * more connections are requested, an exception is thrown.
290    */

291   public class MyMockDriver extends MockDriver
292   {
293     /** Expected connections. */
294     private MockConnection2[] mockConnections;
295
296     /**
297      * Creates a new <code>MyMockDriver</code> instance.
298      *
299      * @param maxConnectionNumber maximum number of connection allowed by the
300      * driver.
301      */

302     public MyMockDriver(int maxConnectionNumber)
303     {
304       mockConnections = new MockConnection2[maxConnectionNumber];
305       for (int i = 0; i < maxConnectionNumber; i++)
306       {
307         mockConnections[i] = new MockConnection2();
308         mockConnections[i].setExpectedCloseCalls(1);
309         mockConnections[i].setupIsClosed(true);
310         setupConnect(mockConnections[i]);
311       }
312       setupExceptionConnect(new SQLException JavaDoc(
313           "Max number of connection reached (" + maxConnectionNumber + ")"));
314     }
315
316     /**
317      * Verifies that all connections are closed.
318      *
319      * @see org.objectweb.cjdbc.scenario.tools.mock.MockDriver#verify()
320      */

321     public void verify()
322     {
323       super.verify();
324       for (int i = 0; i < mockConnections.length; i++)
325       {
326         mockConnections[i].verify();
327       }
328     }
329   }
330 }
Popular Tags