001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: import java.sql.Connection;
012: import java.sql.DriverManager;
013: import java.sql.SQLException;
014: import java.sql.Statement;
015: import java.util.Properties;
016:
017: /**
018: * Test whether ProxyStatement works
019: *
020: * @version $Revision: 1.8 $, $Date: 2006/01/18 14:40:06 $
021: * @author bill
022: * @author $Author: billhorsman $ (current maintainer)
023: * @since Proxool 0.8
024: */
025: public class FatalSqlExceptionTest extends AbstractProxoolTest {
026:
027: private static final Log LOG = LogFactory
028: .getLog(FatalSqlExceptionTest.class);
029:
030: public FatalSqlExceptionTest(String alias) {
031: super (alias);
032: }
033:
034: public void testFatalSqlException() throws Exception {
035:
036: String testName = "fatalSqlException";
037: String alias = testName;
038:
039: String url = TestHelper.buildProxoolUrl(alias,
040: TestConstants.HYPERSONIC_DRIVER,
041: TestConstants.HYPERSONIC_TEST_URL);
042: Properties info = new Properties();
043: info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY,
044: TestConstants.FATAL_SQL_EXCEPTION);
045: info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, String
046: .valueOf(Boolean.TRUE));
047: info.setProperty(ProxoolConstants.TRACE_PROPERTY, String
048: .valueOf(Boolean.TRUE));
049: info.setProperty(ProxoolConstants.USER_PROPERTY,
050: TestConstants.HYPERSONIC_USER);
051: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
052: TestConstants.HYPERSONIC_PASSWORD);
053: ProxoolFacade.registerConnectionPool(url, info);
054:
055: Connection c1 = null;
056: long id1 = 0;
057: try {
058: c1 = DriverManager.getConnection(url);
059: id1 = ProxoolFacade.getId(c1);
060: } finally {
061: if (c1 != null) {
062: c1.close();
063: }
064: }
065:
066: Connection c2 = null;
067: long id2 = 0;
068: try {
069: c2 = DriverManager.getConnection(url);
070: id2 = ProxoolFacade.getId(c2);
071: assertTrue("Expected same connection back", id1 == id2);
072: Statement s = c2.createStatement();
073: // Doing it twice will guarantee a failure. Even if it exists
074: s.execute(TestConstants.FATAL_SQL_STATEMENT);
075: s.execute(TestConstants.FATAL_SQL_STATEMENT);
076: } catch (SQLException e) {
077: assertTrue("Didn't expect a "
078: + FatalSQLException.class.getName(),
079: !(e instanceof FatalSQLException));
080: // Expected exception (foo doesn't exist)
081: //LOG.debug("Expected exception (safe to ignore)", e);
082: } finally {
083: if (c2 != null) {
084: c2.close();
085: }
086: }
087:
088: Connection c3 = null;
089: long id3 = 0;
090: try {
091: c3 = DriverManager.getConnection(url);
092: id3 = ProxoolFacade.getId(c3);
093: assertTrue("Expected a different connection", id1 != id3);
094: } finally {
095: if (c3 != null) {
096: c3.close();
097: }
098: }
099:
100: }
101:
102: public void testWrappedFatalSqlException() throws Exception {
103:
104: String testName = "wrappedFatalSqlException";
105: String alias = testName;
106:
107: String url = TestHelper.buildProxoolUrl(alias,
108: TestConstants.HYPERSONIC_DRIVER,
109: TestConstants.HYPERSONIC_TEST_URL);
110: Properties info = new Properties();
111: info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY,
112: TestConstants.FATAL_SQL_EXCEPTION);
113: info.setProperty(ProxoolConstants.USER_PROPERTY,
114: TestConstants.HYPERSONIC_USER);
115: info
116: .setProperty(
117: ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY,
118: FatalSQLException.class.getName());
119: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
120: TestConstants.HYPERSONIC_PASSWORD);
121: ProxoolFacade.registerConnectionPool(url, info);
122:
123: Connection c = null;
124: try {
125: c = DriverManager.getConnection(url);
126: Statement s = c.createStatement();
127: s.execute(TestConstants.FATAL_SQL_STATEMENT);
128: } catch (SQLException e) {
129: assertTrue("Expected a "
130: + FatalSQLException.class.getName() + " but got a "
131: + e.getClass().getName() + " instead",
132: e instanceof FatalSQLException);
133: // Expected exception (foo doesn't exist)
134: //LOG.debug("Expected exception (safe to ignore)", e);
135: }
136:
137: try {
138: if (c != null) {
139: c.close();
140: }
141: } catch (SQLException e) {
142: LOG.debug("Couldn't close connection", e);
143: }
144:
145: Thread.sleep(1000);
146:
147: // Proxool should automatically throw away that connection that caused a fatal sql exception
148: assertEquals("availableConnectionCount", 0L, ProxoolFacade
149: .getSnapshot(alias, false)
150: .getAvailableConnectionCount());
151:
152: }
153:
154: public void testWrappedFatalRuntimeException() throws Exception {
155:
156: String testName = "wrappedFatalRuntimeException";
157: String alias = testName;
158:
159: String url = TestHelper.buildProxoolUrl(alias,
160: TestConstants.HYPERSONIC_DRIVER,
161: TestConstants.HYPERSONIC_TEST_URL);
162: Properties info = new Properties();
163: info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY,
164: TestConstants.FATAL_SQL_EXCEPTION);
165: info.setProperty(ProxoolConstants.USER_PROPERTY,
166: TestConstants.HYPERSONIC_USER);
167: info
168: .setProperty(
169: ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY,
170: FatalRuntimeException.class.getName());
171: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
172: TestConstants.HYPERSONIC_PASSWORD);
173: ProxoolFacade.registerConnectionPool(url, info);
174:
175: Connection c = null;
176: try {
177: c = DriverManager.getConnection(url);
178: Statement s = c.createStatement();
179: s.execute(TestConstants.FATAL_SQL_STATEMENT);
180: } catch (RuntimeException e) {
181: assertTrue("Expected a "
182: + FatalRuntimeException.class.getName()
183: + " but got a " + e.getClass().getName()
184: + " instead", e instanceof FatalRuntimeException);
185: // Expected exception (foo doesn't exist)
186: LOG.debug("Expected exception (safe to ignore)", e);
187: }
188:
189: try {
190: if (c != null) {
191: c.close();
192: }
193: } catch (SQLException e) {
194: LOG.debug("Couldn't close connection", e);
195: }
196:
197: Thread.sleep(1000);
198:
199: // Proxool should automatically throw away that connection that caused a fatal sql exception
200: assertEquals("availableConnectionCount", 0L, ProxoolFacade
201: .getSnapshot(alias, false)
202: .getAvailableConnectionCount());
203:
204: }
205:
206: public void testFatalSqlExceptionWrapperNotFound() throws Exception {
207:
208: String testName = "fatalSqlExceptionWrapperNotFound";
209: String alias = testName;
210:
211: String url = TestHelper.buildProxoolUrl(alias,
212: TestConstants.HYPERSONIC_DRIVER,
213: TestConstants.HYPERSONIC_TEST_URL);
214: Properties info = new Properties();
215: info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY,
216: TestConstants.FATAL_SQL_EXCEPTION);
217: info.setProperty(ProxoolConstants.USER_PROPERTY,
218: TestConstants.HYPERSONIC_USER);
219: info
220: .setProperty(
221: ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY,
222: "org.does.not.Exist");
223: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
224: TestConstants.HYPERSONIC_PASSWORD);
225: try {
226: ProxoolFacade.registerConnectionPool(url, info);
227: fail("Registration was expected to have failed");
228: } catch (ProxoolException e) {
229: LOG.debug("Expected exception", e);
230: // That's OK. We're expecting one of these
231: }
232:
233: }
234:
235: public void testFatalSqlExceptionWrapperInvalid() throws Exception {
236:
237: String testName = "fatalSqlExceptionWrapperInvalid";
238: String alias = testName;
239:
240: String url = TestHelper.buildProxoolUrl(alias,
241: TestConstants.HYPERSONIC_DRIVER,
242: TestConstants.HYPERSONIC_TEST_URL);
243: Properties info = new Properties();
244: info.setProperty(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY,
245: TestConstants.FATAL_SQL_EXCEPTION);
246: info.setProperty(ProxoolConstants.USER_PROPERTY,
247: TestConstants.HYPERSONIC_USER);
248: // ProxoolException isn't a RuntimeException or an SQLException
249: info
250: .setProperty(
251: ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY,
252: ProxoolException.class.getName());
253: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
254: TestConstants.HYPERSONIC_PASSWORD);
255: try {
256: ProxoolFacade.registerConnectionPool(url, info);
257: fail("Registration was expected to have failed");
258: } catch (ProxoolException e) {
259: LOG.debug("Expected exception", e);
260: // That's OK. We're expecting one of these
261: }
262:
263: }
264: }
265:
266: /*
267: Revision history:
268: $Log: FatalSqlExceptionTest.java,v $
269: Revision 1.8 2006/01/18 14:40:06 billhorsman
270: Unbundled Jakarta's Commons Logging.
271:
272: Revision 1.7 2004/05/26 17:19:09 brenuart
273: Allow JUnit tests to be executed against another database.
274: By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
275: This behavior can be overriden by setting the 'testConfig' environment property to another location.
276:
277: Revision 1.6 2004/03/23 21:16:05 billhorsman
278: make use of new getId() to compare connections
279:
280: Revision 1.5 2003/11/04 23:58:48 billhorsman
281: Made more robust (against existing database state)
282:
283: Revision 1.4 2003/09/29 17:50:45 billhorsman
284: Tests for new wrapper.
285:
286: Revision 1.3 2003/09/05 16:59:20 billhorsman
287: Tests for wrapped exceptions.
288:
289: Revision 1.2 2003/08/27 18:58:11 billhorsman
290: Fixed up test
291:
292: Revision 1.1 2003/07/23 06:54:48 billhorsman
293: draft JNDI changes (shouldn't effect normal operation)
294:
295: Revision 1.5 2003/03/04 10:24:40 billhorsman
296: removed try blocks around each test
297:
298: Revision 1.4 2003/03/03 17:09:05 billhorsman
299: all tests now extend AbstractProxoolTest
300:
301: Revision 1.3 2003/03/03 11:12:05 billhorsman
302: fixed licence
303:
304: Revision 1.2 2003/03/01 15:27:24 billhorsman
305: checkstyle
306:
307: Revision 1.1 2003/02/27 18:01:48 billhorsman
308: completely rethought the test structure. it's now
309: more obvious. no new tests yet though.
310:
311: */
|