KickJava   Java API By Example, From Geeks To Geeks.

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


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.Stack JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException;
34 import org.objectweb.cjdbc.controller.connection.FailFastPoolConnectionManager;
35 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
36 import org.objectweb.cjdbc.scenario.tools.mock.MockDriver;
37 import org.objectweb.cjdbc.scenario.tools.util.PrivilegedAccessor;
38
39 import com.mockobjects.sql.MockConnection2;
40
41 /**
42  * <code>FailFastPoolConnectionManager</code> test class.
43  *
44  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
45  * @see org.objectweb.cjdbc.controller.connection.FailFastPoolConnectionManager
46  */

47 public class FailFastPoolConnectionManagerTest extends NoTemplate
48 {
49   /** Pool size to use for the test. */
50   private static final int POOL_SIZE = 30;
51
52   /** Fake driver. */
53   private MockDriver mockDriver;
54
55   /** Fail fast pool connection manager pool to test. */
56   private FailFastPoolConnectionManager pool;
57
58   /**
59    * @see junit.framework.TestCase#setUp()
60    */

61   protected void setUp()
62   {
63     // Create and register the mock driver
64
try
65     {
66       mockDriver = new MockDriver();
67       mockDriver.setExpectedConnectCalls(POOL_SIZE);
68       for (int i = 0; i < POOL_SIZE; i++)
69       {
70         mockDriver.setupConnect(new MockConnection2());
71       }
72
73       DriverManager.registerDriver(mockDriver);
74     }
75     catch (SQLException JavaDoc e)
76     {
77       fail("Failed to register driver: " + e);
78     }
79
80     // Create a fail fast pool connection manager of POOL_SIZE connections
81
try
82     {
83       pool = new FailFastPoolConnectionManager("", "", "", "", null, null,
84           POOL_SIZE);
85     }
86     catch (Exception JavaDoc e)
87     {
88       fail("Failed to create pool connection manager: " + e);
89     }
90
91     // Initialize the pool
92
try
93     {
94       pool.initializeConnections();
95     }
96     catch (SQLException JavaDoc e)
97     {
98       fail("Failed to initialize pool connection manager: " + e);
99     }
100     mockDriver.verify();
101   }
102
103   /**
104    * @see junit.framework.TestCase#tearDown()
105    */

106   protected void tearDown()
107   {
108     // Deregister the driver
109
try
110     {
111       DriverManager.deregisterDriver(mockDriver);
112     }
113     catch (SQLException JavaDoc e)
114     {
115       fail("Failed to deregister driver: " + e);
116     }
117   }
118
119   /**
120    * @see org.objectweb.cjdbc.controller.connection.FailFastPoolConnectionManager#getConnection()
121    */

122   public void testGetConnection() throws Exception JavaDoc
123   {
124     // Get POOL_SIZE connections from the pool
125
Connection JavaDoc c = null;
126
127     int free = ((Stack JavaDoc) PrivilegedAccessor.getValue(pool, "freeConnections"))
128         .size();
129     int active = ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
130         "activeConnections")).size();
131     assertEquals(30, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
132         .intValue());
133
134     int i = 0;
135     while (i < POOL_SIZE)
136     {
137       try
138       {
139         c = pool.getConnection();
140       }
141       catch (UnreachableBackendException e)
142       {
143         fail("Backend unreachable during test.");
144       }
145       if (c == null)
146         fail("Failed to get a connection from the pool");
147       else
148       {
149         i++;
150         assertEquals(--free, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
151             "freeConnections")).size());
152         assertEquals(++active, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
153             "activeConnections")).size());
154
155       }
156     }
157
158     // Try to get one more connection from the pool
159
try
160     {
161       c = pool.getConnection();
162     }
163     catch (UnreachableBackendException e)
164     {
165       fail("Backend unreachable during test.");
166     }
167     if (c != null)
168       fail("Got more connection than available from the pool");
169   }
170
171   /**
172    * @see org.objectweb.cjdbc.controller.connection.FailFastPoolConnectionManager#releaseConnection(Connection)
173    */

174   public void testReleaseConnection() throws Exception JavaDoc
175   {
176     // Get a connections from the pool
177
Connection JavaDoc c = null;
178     try
179     {
180       c = pool.getConnection();
181     }
182     catch (UnreachableBackendException e)
183     {
184       fail("Backend unreachable during test.");
185     }
186     if (c == null)
187       fail("Failed to get connection from the pool");
188     assertEquals(POOL_SIZE - 1, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
189         "freeConnections")).size());
190     assertEquals(1, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
191         "activeConnections")).size());
192
193     // Release the connection
194
pool.releaseConnection(c);
195     assertEquals(POOL_SIZE, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
196         "freeConnections")).size());
197     assertEquals(0, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
198         "activeConnections")).size());
199
200   }
201 }
Popular Tags