KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
12 import java.sql.DriverManager JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 /**
16  * Test whether the {@link ConnectionResetter} works.
17  *
18  * @version $Revision: 1.16 $, $Date: 2006/01/18 14:40:06 $
19  * @author Bill Horsman (bill@logicalcobwebs.co.uk)
20  * @author $Author: billhorsman $ (current maintainer)
21  * @since Proxool 0.5
22  */

23 public class ConnectionResetterTest extends AbstractProxoolTest {
24
25     private static final Log LOG = LogFactory.getLog(ConnectionResetterTest.class);
26
27     /**
28      * @see junit.framework.TestCase#TestCase
29      */

30     public ConnectionResetterTest(String JavaDoc s) {
31         super(s);
32     }
33
34     /**
35      * Test whether autoCommit is correctly reset when a connection is
36      * returned to the pool.
37      */

38     public void testAutoCommit() throws Exception JavaDoc {
39
40         String JavaDoc testName = "autoCommit";
41         String JavaDoc alias = testName;
42
43         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
44                 TestConstants.HYPERSONIC_DRIVER,
45                 TestConstants.HYPERSONIC_TEST_URL);
46         Properties JavaDoc info = new Properties JavaDoc();
47         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
48         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
49         ProxoolFacade.registerConnectionPool(url, info);
50
51         Connection JavaDoc c1 = DriverManager.getConnection(url);
52
53         Connection JavaDoc c2 = DriverManager.getConnection(url);
54
55
56         c1.setAutoCommit(false);
57         c1.close();
58
59         c1 = DriverManager.getConnection(url);
60         assertTrue("c1.getAutoCommit", c1.getAutoCommit());
61
62         c2.close();
63         c1.close();
64         assertEquals("connectionCount", 2, ProxoolFacade.getSnapshot(alias).getConnectionCount());
65
66     }
67
68     /**
69      * Test connectionCount when we deliberately introduce an exception during connection reset.
70      */

71     public void testFailedReset() throws Exception JavaDoc {
72
73         try {
74             // This is a bit of a hack to force an exception during reset
75
ConnectionResetter.setTriggerResetException(true);
76
77             String JavaDoc testName = "failedReset";
78             String JavaDoc alias = testName;
79
80             String JavaDoc url = TestHelper.buildProxoolUrl(alias,
81                     TestConstants.HYPERSONIC_DRIVER,
82                     TestConstants.HYPERSONIC_TEST_URL);
83             Properties JavaDoc info = new Properties JavaDoc();
84             info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
85             info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
86             ProxoolFacade.registerConnectionPool(url, info);
87
88             Connection JavaDoc c1 = DriverManager.getConnection(url);
89             c1.setAutoCommit(false);
90             c1.close();
91
92             assertEquals("connectionCount", 0, ProxoolFacade.getSnapshot(alias).getConnectionCount());
93         } finally {
94             // Back to normal
95
ConnectionResetter.setTriggerResetException(false);
96         }
97
98     }
99
100     /**
101      * Test whether autoCommit is correctly reset when a connection is
102      * returned to the pool.
103      */

104     public void testReadOnly() throws Exception JavaDoc {
105
106         String JavaDoc testName = "readOnly";
107         String JavaDoc alias = testName;
108
109         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
110                 TestConstants.HYPERSONIC_DRIVER,
111                 TestConstants.HYPERSONIC_TEST_URL);
112         Properties JavaDoc info = new Properties JavaDoc();
113         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
114         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
115         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "2");
116         ProxoolFacade.registerConnectionPool(url, info);
117
118         Connection JavaDoc c1 = DriverManager.getConnection(url);
119         ;
120         Connection JavaDoc c2 = DriverManager.getConnection(url);
121         ;
122
123         boolean originalReadOnly = c1.isReadOnly();
124         c1.setReadOnly(true);
125         c1.close();
126
127         c1 = DriverManager.getConnection(url);
128         ;
129         assertTrue("readOnly", c1.isReadOnly() == originalReadOnly);
130
131         c2.close();
132         c1.close();
133
134     }
135
136 }
137
138 /*
139  Revision history:
140  $Log: ConnectionResetterTest.java,v $
141  Revision 1.16 2006/01/18 14:40:06 billhorsman
142  Unbundled Jakarta's Commons Logging.
143
144  Revision 1.15 2005/10/07 08:12:58 billhorsman
145  Test deliberate exception durinng reset
146
147  Revision 1.14 2003/03/04 10:58:43 billhorsman
148  checkstyle
149
150  Revision 1.13 2003/03/04 10:24:40 billhorsman
151  removed try blocks around each test
152
153  Revision 1.12 2003/03/03 17:08:56 billhorsman
154  all tests now extend AbstractProxoolTest
155
156  Revision 1.11 2003/03/03 11:12:04 billhorsman
157  fixed licence
158
159  Revision 1.10 2003/02/27 18:01:47 billhorsman
160  completely rethought the test structure. it's now
161  more obvious. no new tests yet though.
162
163  Revision 1.9 2003/02/19 15:14:22 billhorsman
164  fixed copyright (copy and paste error,
165  not copyright change)
166
167  Revision 1.8 2003/02/06 17:41:02 billhorsman
168  now uses imported logging
169
170  Revision 1.7 2002/12/16 17:05:38 billhorsman
171  new test structure
172
173  Revision 1.6 2002/12/03 10:53:08 billhorsman
174  checkstyle
175
176  Revision 1.5 2002/11/13 20:53:30 billhorsman
177  new tests for autoCommit and readOnly
178
179  Revision 1.4 2002/11/12 20:24:12 billhorsman
180  checkstyle
181
182  Revision 1.3 2002/11/12 20:18:26 billhorsman
183  Made connection resetter a bit more friendly. Now, if it encounters any problems during
184  reset then that connection is thrown away. This is going to cause you problems if you
185  always close connections in an unstable state (e.g. with transactions open. But then
186  again, it's better to know about that as soon as possible, right?
187
188  Revision 1.2 2002/11/09 16:01:21 billhorsman
189  fixed CommandFilterIF implementation
190
191  Revision 1.1 2002/11/06 21:08:02 billhorsman
192  new ConnectionResetter test
193
194 */

195
Popular Tags