KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > WrapperTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.sql.DriverManager JavaDoc;
12 import java.sql.Connection JavaDoc;
13 import java.sql.Statement JavaDoc;
14 import java.sql.SQLException JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 /**
18  * Tests whether we are wrapping up connections correctly. There disposable
19  * wrappers stop the user doing nasty things to the connection after it has
20  * been closed.
21  * @version $Revision: 1.4 $, $Date: 2006/01/18 14:40:06 $
22  * @author bill
23  * @author $Author: billhorsman $ (current maintainer)
24  * @since Proxool 0.9
25  */

26 public class WrapperTest extends AbstractProxoolTest {
27
28     private static final Log LOG = LogFactory.getLog(WrapperTest.class);
29
30     /**
31      * @see AbstractProxoolTest
32      */

33     public WrapperTest(String JavaDoc alias) {
34         super(alias);
35     }
36
37     /**
38      * Check that closing a connection twice can't close the same connection for the
39      * next user
40      */

41     public void testDoubleConnection() throws Exception JavaDoc {
42
43         String JavaDoc testName = "alias";
44         String JavaDoc alias = testName;
45
46         // Register pool
47
String JavaDoc url = TestHelper.buildProxoolUrl(alias,
48                 TestConstants.HYPERSONIC_DRIVER,
49                 TestConstants.HYPERSONIC_TEST_URL);
50         Properties JavaDoc info = new Properties JavaDoc();
51         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
52         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
53         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1");
54
55         Connection JavaDoc c1 = DriverManager.getConnection(url, info);
56         Connection JavaDoc dc1 = ProxoolFacade.getDelegateConnection(c1);
57         c1.close();
58         assertEquals("servedCount", 1, ProxoolFacade.getSnapshot(alias).getServedCount());
59         LOG.debug("c1 = " + c1.toString() + ", dc1 = " + dc1);
60
61         Connection JavaDoc c2 = DriverManager.getConnection(url, info);
62         Connection JavaDoc dc2 = ProxoolFacade.getDelegateConnection(c2);
63         LOG.debug("c2 = " + c2 + ", dc2 = " + dc2);
64
65         assertEquals("Expected the delegate connection to be the same", dc1, dc2);
66         // Closing the first connection should not fail, but it shouldn't do anything either.
67
c1.close();
68         // Check that the second connection hasn't been effected
69
assertTrue("Connection was closed unexpectedly", !c2.isClosed());
70
71         // Trying to use the first connection should fail
72
Statement JavaDoc s = null;
73         try {
74             s = c1.createStatement();
75             s.execute(TestConstants.HYPERSONIC_TEST_SQL);
76             fail("Expected to get an exception because the test failed");
77         } catch (SQLException JavaDoc e) {
78             LOG.debug("Expected exception.", e);
79         }
80
81         // The second connection should work okay though
82
try {
83             s = c2.createStatement();
84             s.execute(TestConstants.HYPERSONIC_TEST_SQL);
85         } catch (SQLException JavaDoc e) {
86             fail("Connection failed, but it should have worked");
87         }
88
89         c2.close();
90         assertTrue("Connection was not closed", c2.isClosed());
91
92     }
93
94     /**
95      * Check that nothing bad happens if you close a connection twice
96      * @throws SQLException if anything goes wrong
97      * @throws ProxoolException if anything goes wrong
98      */

99     public void testDoubleClose() throws SQLException JavaDoc, ProxoolException {
100
101         String JavaDoc testName = "alias";
102         String JavaDoc alias = testName;
103
104         // Register pool
105
String JavaDoc url = TestHelper.buildProxoolUrl(alias,
106                 TestConstants.HYPERSONIC_DRIVER,
107                 TestConstants.HYPERSONIC_TEST_URL);
108         Properties JavaDoc info = new Properties JavaDoc();
109         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
110         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
111         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1");
112
113         Connection JavaDoc c1 = DriverManager.getConnection(url, info);
114         c1.close();
115         c1.close();
116
117     }
118
119     /**
120      * Check that calling createStatement after a connection has been closed
121      * throws an exception
122      * @throws SQLException if anything goes wrong
123      * @throws ProxoolException if anything goes wrong
124      */

125     public void testCreateStatementAfterClose() throws SQLException JavaDoc, ProxoolException {
126
127         String JavaDoc testName = "alias";
128         String JavaDoc alias = testName;
129
130         // Register pool
131
String JavaDoc url = TestHelper.buildProxoolUrl(alias,
132                 TestConstants.HYPERSONIC_DRIVER,
133                 TestConstants.HYPERSONIC_TEST_URL);
134         Properties JavaDoc info = new Properties JavaDoc();
135         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
136         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
137         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1");
138
139         Connection JavaDoc c1 = DriverManager.getConnection(url, info);
140         c1.close();
141         try {
142             c1.createStatement();
143             fail("Expected createStatement() to fail after connection was closed");
144         } catch (SQLException JavaDoc e) {
145             // Ignore (we expected it)
146
}
147     }
148
149     /**
150      * Check that isClosed() returns true after we have closed a connection
151      * @throws SQLException if anything goes wrong
152      * @throws ProxoolException if anything goes wrong
153      */

154     public void testIsClosedAfterClose() throws SQLException JavaDoc, ProxoolException {
155
156         String JavaDoc testName = "alias";
157         String JavaDoc alias = testName;
158
159         // Register pool
160
String JavaDoc url = TestHelper.buildProxoolUrl(alias,
161                 TestConstants.HYPERSONIC_DRIVER,
162                 TestConstants.HYPERSONIC_TEST_URL);
163         Properties JavaDoc info = new Properties JavaDoc();
164         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
165         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
166         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1");
167
168         Connection JavaDoc c1 = DriverManager.getConnection(url, info);
169         c1.close();
170         assertTrue("isClosed()", c1.isClosed());
171     }
172
173     /**
174      * Check that isClosed() returns true after we have closed a connection
175      * @throws SQLException if anything goes wrong
176      * @throws ProxoolException if anything goes wrong
177      */

178     public void testHashCode() throws SQLException JavaDoc, ProxoolException {
179
180         String JavaDoc testName = "alias";
181         String JavaDoc alias = testName;
182
183         // Register pool
184
String JavaDoc url = TestHelper.buildProxoolUrl(alias,
185                 TestConstants.HYPERSONIC_DRIVER,
186                 TestConstants.HYPERSONIC_TEST_URL);
187         Properties JavaDoc info = new Properties JavaDoc();
188         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
189         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
190         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1");
191
192         Connection JavaDoc c1 = DriverManager.getConnection(url, info);
193         LOG.debug(c1 + " = " + c1.hashCode());
194         c1.close();
195         LOG.debug(c1 + " = " + c1.hashCode());
196
197         Connection JavaDoc c2 = DriverManager.getConnection(url, info);
198         LOG.debug(c2 + " = " + c2.hashCode());
199         c2.close();
200         LOG.debug(c2 + " = " + c2.hashCode());
201
202     }
203
204     /**
205      * Check tha equals() works right
206      * @throws SQLException if anything goes wrong
207      * @throws ProxoolException if anything goes wrong
208      */

209     public void testEquals() throws SQLException JavaDoc, ProxoolException {
210
211         String JavaDoc testName = "alias";
212         String JavaDoc alias = testName;
213
214         // Register pool
215
String JavaDoc url = TestHelper.buildProxoolUrl(alias,
216                 TestConstants.HYPERSONIC_DRIVER,
217                 TestConstants.HYPERSONIC_TEST_URL);
218         Properties JavaDoc info = new Properties JavaDoc();
219         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
220         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
221         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "1");
222
223         Connection JavaDoc c1 = DriverManager.getConnection(url, info);
224         assertTrue("c1 == c1", c1.equals(c1));
225         c1.close();
226         assertTrue("c1 == c1", c1.equals(c1));
227
228         Connection JavaDoc c2 = DriverManager.getConnection(url, info);
229         assertTrue("c1 == c2", c1.equals(c2));
230         c2.close();
231         assertTrue("c1 == c2", c1.equals(c2));
232
233     }
234
235     /**
236      * Check whether {@link ProxoolFacade#getId(java.sql.Connection)} returns
237      * sensible values
238      * @throws ProxoolException if anything goes wrong
239      * @throws SQLException if anything goes wrong
240      */

241     public void testId() throws ProxoolException, SQLException JavaDoc {
242         String JavaDoc testName = "alias";
243         String JavaDoc alias = testName;
244
245         // Register pool
246
String JavaDoc url = TestHelper.buildProxoolUrl(alias,
247                 TestConstants.HYPERSONIC_DRIVER,
248                 TestConstants.HYPERSONIC_TEST_URL);
249         Properties JavaDoc info = new Properties JavaDoc();
250         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
251         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
252         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT, "2");
253
254         Connection JavaDoc c1 = DriverManager.getConnection(url, info);
255         assertEquals("c1.getId()", 1, ProxoolFacade.getId(c1));
256
257         Connection JavaDoc c2 = DriverManager.getConnection(url, info);
258         assertEquals("c2.getId()", 2, ProxoolFacade.getId(c2));
259         c1.close();
260         assertEquals("c1.getId()", 1, ProxoolFacade.getId(c1));
261         c2.close();
262         assertEquals("c2.getId()", 2, ProxoolFacade.getId(c2));
263
264     }
265
266     /**
267      * Check whether {@link ProxoolFacade#getAlias(java.sql.Connection)} returns
268      * sensible values
269      * @throws ProxoolException if anything goes wrong
270      * @throws SQLException if anything goes wrong
271      */

272     public void testAlias() throws ProxoolException, SQLException JavaDoc {
273         String JavaDoc testName = "alias";
274         String JavaDoc alias = testName;
275
276         // Register pool
277
String JavaDoc url = TestHelper.buildProxoolUrl(alias,
278                 TestConstants.HYPERSONIC_DRIVER,
279                 TestConstants.HYPERSONIC_TEST_URL);
280         Properties JavaDoc info = new Properties JavaDoc();
281         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
282         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
283
284         Connection JavaDoc c1 = DriverManager.getConnection(url, info);
285         assertEquals("c1.getAlias()", alias, ProxoolFacade.getAlias(c1));
286         c1.close();
287         assertEquals("c1.getAlias()", alias, ProxoolFacade.getAlias(c1));
288
289     }
290 }
291
292
Popular Tags