KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
14 import java.sql.Statement JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 /**
18  * Test whether ProxyStatement works
19  *
20  * @version $Revision: 1.8 $, $Date: 2006/01/18 14:40:06 $
21  * @author bill
22  * @author $Author: billhorsman $ (current maintainer)
23  * @since Proxool 0.8
24  */

25 public class FatalSqlExceptionTest extends AbstractProxoolTest {
26
27     private static final Log LOG = LogFactory.getLog(FatalSqlExceptionTest.class);
28
29     public FatalSqlExceptionTest(String JavaDoc alias) {
30         super(alias);
31     }
32
33
34     public void testFatalSqlException() throws Exception JavaDoc {
35
36         String JavaDoc testName = "fatalSqlException";
37         String JavaDoc alias = testName;
38
39         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
40                 TestConstants.HYPERSONIC_DRIVER,
41                 TestConstants.HYPERSONIC_TEST_URL);
42         Properties JavaDoc info = new Properties JavaDoc();
43         info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, TestConstants.FATAL_SQL_EXCEPTION);
44         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, String.valueOf(Boolean.TRUE));
45         info.setProperty(ProxoolConstants.TRACE_PROPERTY, String.valueOf(Boolean.TRUE));
46         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
47         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
48         ProxoolFacade.registerConnectionPool(url, info);
49
50         Connection JavaDoc c1 = null;
51         long id1 = 0;
52         try {
53             c1 = DriverManager.getConnection(url);
54             id1 = ProxoolFacade.getId(c1);
55         } finally {
56             if (c1 != null) {
57                 c1.close();
58             }
59         }
60
61         Connection JavaDoc c2 = null;
62         long id2 = 0;
63         try {
64             c2 = DriverManager.getConnection(url);
65             id2 = ProxoolFacade.getId(c2);
66             assertTrue("Expected same connection back", id1 == id2);
67             Statement JavaDoc s = c2.createStatement();
68             // Doing it twice will guarantee a failure. Even if it exists
69
s.execute(TestConstants.FATAL_SQL_STATEMENT);
70             s.execute(TestConstants.FATAL_SQL_STATEMENT);
71         } catch (SQLException JavaDoc e) {
72             assertTrue("Didn't expect a " + FatalSQLException.class.getName(), !(e instanceof FatalSQLException));
73             // Expected exception (foo doesn't exist)
74
//LOG.debug("Expected exception (safe to ignore)", e);
75
} finally {
76             if (c2 != null) {
77                 c2.close();
78             }
79         }
80
81         Connection JavaDoc c3 = null;
82         long id3 = 0;
83         try {
84             c3 = DriverManager.getConnection(url);
85             id3 = ProxoolFacade.getId(c3);
86             assertTrue("Expected a different connection", id1 != id3);
87         } finally {
88             if (c3 != null) {
89                 c3.close();
90             }
91         }
92
93     }
94
95     
96     public void testWrappedFatalSqlException() throws Exception JavaDoc {
97
98         String JavaDoc testName = "wrappedFatalSqlException";
99         String JavaDoc alias = testName;
100
101         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
102                 TestConstants.HYPERSONIC_DRIVER,
103                 TestConstants.HYPERSONIC_TEST_URL);
104         Properties JavaDoc info = new Properties JavaDoc();
105         info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, TestConstants.FATAL_SQL_EXCEPTION);
106         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
107         info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY, FatalSQLException.class.getName());
108         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
109         ProxoolFacade.registerConnectionPool(url, info);
110
111         Connection JavaDoc c = null;
112         try {
113             c = DriverManager.getConnection(url);
114             Statement JavaDoc s = c.createStatement();
115             s.execute(TestConstants.FATAL_SQL_STATEMENT);
116         } catch (SQLException JavaDoc e) {
117             assertTrue("Expected a " + FatalSQLException.class.getName() + " but got a " + e.getClass().getName() + " instead", e instanceof FatalSQLException);
118             // Expected exception (foo doesn't exist)
119
//LOG.debug("Expected exception (safe to ignore)", e);
120
}
121
122         try {
123             if (c != null) {
124                 c.close();
125             }
126         } catch (SQLException JavaDoc e) {
127             LOG.debug("Couldn't close connection", e);
128         }
129
130         Thread.sleep(1000);
131
132         // Proxool should automatically throw away that connection that caused a fatal sql exception
133
assertEquals("availableConnectionCount", 0L, ProxoolFacade.getSnapshot(alias, false).getAvailableConnectionCount());
134
135     }
136
137     public void testWrappedFatalRuntimeException() throws Exception JavaDoc {
138
139         String JavaDoc testName = "wrappedFatalRuntimeException";
140         String JavaDoc alias = testName;
141
142         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
143                 TestConstants.HYPERSONIC_DRIVER,
144                 TestConstants.HYPERSONIC_TEST_URL);
145         Properties JavaDoc info = new Properties JavaDoc();
146         info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, TestConstants.FATAL_SQL_EXCEPTION);
147         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
148         info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY, FatalRuntimeException.class.getName());
149         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
150         ProxoolFacade.registerConnectionPool(url, info);
151
152         Connection JavaDoc c = null;
153         try {
154             c = DriverManager.getConnection(url);
155             Statement JavaDoc s = c.createStatement();
156             s.execute(TestConstants.FATAL_SQL_STATEMENT);
157         } catch (RuntimeException JavaDoc e) {
158             assertTrue("Expected a " + FatalRuntimeException.class.getName() + " but got a " + e.getClass().getName() + " instead", e instanceof FatalRuntimeException);
159             // Expected exception (foo doesn't exist)
160
LOG.debug("Expected exception (safe to ignore)", e);
161         }
162
163         try {
164             if (c != null) {
165                 c.close();
166             }
167         } catch (SQLException JavaDoc e) {
168             LOG.debug("Couldn't close connection", e);
169         }
170
171         Thread.sleep(1000);
172
173         // Proxool should automatically throw away that connection that caused a fatal sql exception
174
assertEquals("availableConnectionCount", 0L, ProxoolFacade.getSnapshot(alias, false).getAvailableConnectionCount());
175
176     }
177
178     public void testFatalSqlExceptionWrapperNotFound() throws Exception JavaDoc {
179
180         String JavaDoc testName = "fatalSqlExceptionWrapperNotFound";
181         String JavaDoc alias = testName;
182
183         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
184                 TestConstants.HYPERSONIC_DRIVER,
185                 TestConstants.HYPERSONIC_TEST_URL);
186         Properties JavaDoc info = new Properties JavaDoc();
187         info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, TestConstants.FATAL_SQL_EXCEPTION);
188         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
189         info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY, "org.does.not.Exist");
190         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
191         try {
192             ProxoolFacade.registerConnectionPool(url, info);
193             fail("Registration was expected to have failed");
194         } catch (ProxoolException e) {
195             LOG.debug("Expected exception", e);
196             // That's OK. We're expecting one of these
197
}
198
199     }
200
201     public void testFatalSqlExceptionWrapperInvalid() throws Exception JavaDoc {
202
203         String JavaDoc testName = "fatalSqlExceptionWrapperInvalid";
204         String JavaDoc alias = testName;
205
206         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
207                 TestConstants.HYPERSONIC_DRIVER,
208                 TestConstants.HYPERSONIC_TEST_URL);
209         Properties JavaDoc info = new Properties JavaDoc();
210         info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, TestConstants.FATAL_SQL_EXCEPTION);
211         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
212         // ProxoolException isn't a RuntimeException or an SQLException
213
info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY, ProxoolException.class.getName());
214         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
215         try {
216             ProxoolFacade.registerConnectionPool(url, info);
217             fail("Registration was expected to have failed");
218         } catch (ProxoolException e) {
219             LOG.debug("Expected exception", e);
220             // That's OK. We're expecting one of these
221
}
222
223     }
224 }
225
226
227 /*
228  Revision history:
229  $Log: FatalSqlExceptionTest.java,v $
230  Revision 1.8 2006/01/18 14:40:06 billhorsman
231  Unbundled Jakarta's Commons Logging.
232
233  Revision 1.7 2004/05/26 17:19:09 brenuart
234  Allow JUnit tests to be executed against another database.
235  By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
236  This behavior can be overriden by setting the 'testConfig' environment property to another location.
237
238  Revision 1.6 2004/03/23 21:16:05 billhorsman
239  make use of new getId() to compare connections
240
241  Revision 1.5 2003/11/04 23:58:48 billhorsman
242  Made more robust (against existing database state)
243
244  Revision 1.4 2003/09/29 17:50:45 billhorsman
245  Tests for new wrapper.
246
247  Revision 1.3 2003/09/05 16:59:20 billhorsman
248  Tests for wrapped exceptions.
249
250  Revision 1.2 2003/08/27 18:58:11 billhorsman
251  Fixed up test
252
253  Revision 1.1 2003/07/23 06:54:48 billhorsman
254  draft JNDI changes (shouldn't effect normal operation)
255
256  Revision 1.5 2003/03/04 10:24:40 billhorsman
257  removed try blocks around each test
258
259  Revision 1.4 2003/03/03 17:09:05 billhorsman
260  all tests now extend AbstractProxoolTest
261
262  Revision 1.3 2003/03/03 11:12:05 billhorsman
263  fixed licence
264
265  Revision 1.2 2003/03/01 15:27:24 billhorsman
266  checkstyle
267
268  Revision 1.1 2003/02/27 18:01:48 billhorsman
269  completely rethought the test structure. it's now
270  more obvious. no new tests yet though.
271
272  */
Popular Tags