KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbc4 > StatementEventsTest


1 /*
2  
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.StatementEventsTest
4  
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11  
12       http://www.apache.org/licenses/LICENSE-2.0
13  
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19  
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.jdbc4;
23
24 import java.sql.*;
25 import javax.sql.*;
26 import junit.framework.*;
27
28 import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory;
29 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
30
31 import java.util.Enumeration JavaDoc;
32
33 /*
34     This class is used to test the JDBC4 statement event
35     support
36 */

37 public class StatementEventsTest extends BaseJDBCTestCase {
38
39     /**
40      * Type of data source to use. If <code>true</code>, use
41      * <code>XADataSource</code>; otherwise, use
42      * <code>ConnectionPoolDataSource</code>.
43      */

44     private boolean xa;
45     /**
46      * Type of statement to use. If <code>true</code>, use
47      * <code>CallableStatement</code>; otherwise, use
48      * <code>PreparedStatement</code>.
49      */

50     private boolean callable;
51
52     /** The statement that caused the last statementClosed event. */
53     private Statement closedStatement;
54     /** Number of times statementClosed events have been raised. */
55     private int closedCount;
56     /** The statement that caused the last statementError event. */
57     private Statement errorStatement;
58     /** Number of times statementError events have been raised. */
59     private int errorCount;
60
61     /**
62      * The pooled connection to use in the test (could also be an XA
63      * connection).
64      */

65     private PooledConnection pooledConnection;
66     /** The connection object to use in the test. */
67     private Connection connection;
68
69     /**
70      * Create a test with the given name.
71      *
72      * @param name name of the test.
73      */

74     public StatementEventsTest(String JavaDoc name) {
75         super(name);
76     }
77
78     /**
79      * Set whether the test should use <code>XADataSource</code> or
80      * <code>ConnectionPoolDataSource</code>.
81      *
82      * @param xa if <code>true</code>, use XA
83      */

84     private void setXA(boolean xa) {
85         this.xa = xa;
86     }
87
88     /**
89      * Set whether the test should use <code>CallableStatement</code> or
90      * <code>PreparedStatement</code>.
91      *
92      * @param callable if <code>true</code>, use callable statement; otherwise,
93      * use prepared statement
94      */

95     private void setCallable(boolean callable) {
96         this.callable = callable;
97     }
98
99     /**
100      * Return the name of the test.
101      *
102      * @return name of the test
103      */

104     public String JavaDoc getName() {
105         return super.getName() + (xa ? "_xa" : "_pooled") +
106             (callable ? "_callable" : "_prepared");
107     }
108
109     // TEST SETUP
110

111     /**
112      * Set up the connection to the database and register a statement event
113      * listener.
114      *
115      * @exception SQLException if a database error occurs
116      */

117     public void setUp() throws SQLException {
118         if (xa) {
119             XADataSource ds = TestDataSourceFactory.getXADataSource();
120             pooledConnection = ds.getXAConnection();
121         } else {
122             ConnectionPoolDataSource ds =
123                 TestDataSourceFactory.getConnectionPoolDataSource();
124             pooledConnection = ds.getPooledConnection();
125         }
126         StatementEventListener listener = new StatementEventListener() {
127                 public void statementClosed(StatementEvent event) {
128                     closedStatement = event.getStatement();
129                     closedCount++;
130                 }
131                 public void statementErrorOccurred(StatementEvent event) {
132                     errorStatement = event.getStatement();
133                     errorCount++;
134                 }
135             };
136         pooledConnection.addStatementEventListener(listener);
137         connection = pooledConnection.getConnection();
138     }
139
140     /**
141      * Free resources used in the test.
142      *
143      * @exception SQLException if a database error occurs
144      */

145     public void tearDown() throws SQLException {
146         connection.close();
147         pooledConnection.close();
148     }
149
150     /**
151      * Return suite with all tests of the class for all combinations of
152      * pooled/xa connection and prepared/callable statement.
153      *
154      * @return a test suite
155      */

156     public static Test suite() {
157         TestSuite suites = new TestSuite();
158         boolean[] truefalse = new boolean[] { true, false };
159         for (boolean xa : truefalse) {
160             for (boolean callable : truefalse) {
161                 suites.addTest(new Suite(xa, callable));
162             }
163         }
164         return suites;
165     }
166
167     /**
168      * Test suite class which contains all test cases in
169      * <code>StatementEventsTest</code> for a given configuration.
170      */

171     private static class Suite extends TestSuite {
172         private Suite(boolean xa, boolean callable) {
173             super(StatementEventsTest.class);
174             for (Enumeration JavaDoc e = tests(); e.hasMoreElements(); ) {
175                 StatementEventsTest test =
176                     (StatementEventsTest) e.nextElement();
177                 test.setXA(xa);
178                 test.setCallable(callable);
179             }
180         }
181     }
182
183     // UTILITIES
184

185     /**
186      * Prepare a statement.
187      *
188      * @param sql SQL text
189      * @return a <code>PreparedStatement</code> or
190      * <code>CallableStatement</code> object
191      * @exception SQLException if a database error occurs
192      */

193     private PreparedStatement prepare(String JavaDoc sql) throws SQLException {
194         if (callable) {
195             return connection.prepareCall(sql);
196         }
197         return connection.prepareStatement(sql);
198     }
199
200     // TEST CASES
201

202     /**
203      * Test that a close event is raised when a statement is closed.
204      *
205      * @exception SQLException if a database error occurs
206      */

207     public void testCloseEvent() throws SQLException {
208         PreparedStatement ps = prepare("VALUES (1)");
209         ps.close();
210         assertSame("Close event raised on wrong statement.",
211                    ps, closedStatement);
212         assertEquals("Incorrect close count.", 1, closedCount);
213     }
214
215     /**
216      * Test whether a close event is raised when a connection is
217      * closed. (Client should raise a close event since the connection calls
218      * <code>close()</code> on its statements. Embedded should not raise a
219      * close event since the connection does not call <code>close()</code> on
220      * its statements.)
221      *
222      * @exception SQLException if a database error occurs
223      */

224     public void testCloseEventOnClosedConnection() throws SQLException {
225         PreparedStatement ps = prepare("VALUES (1)");
226         connection.close();
227         if (usingDerbyNetClient()) {
228             assertSame("Close event raised on wrong statement.",
229                        ps, closedStatement);
230             assertEquals("Incorrect close count.", 1, closedCount);
231         } else if (usingEmbedded()) {
232             assertNull("Didn't expect close event.", closedStatement);
233             assertEquals("Incorrect close count.", 0, closedCount);
234         } else {
235             fail("Unknown framework.");
236         }
237     }
238
239     /**
240      * Test that an error event is raised when <code>execute()</code> fails
241      * because the connection is closed.
242      *
243      * @exception SQLException if a database error occurs
244      */

245     public void testErrorEventOnClosedConnection() throws SQLException {
246         PreparedStatement ps = prepare("VALUES (1)");
247         connection.close();
248         try {
249             ps.execute();
250             fail("No exception thrown.");
251         } catch (SQLException e) {
252             assertSQLState("Unexpected SQL state.", "08003", e);
253             assertSame("Error event raised on wrong statement.",
254                        ps, errorStatement);
255             assertEquals("Incorrect error count.", 1, errorCount);
256         }
257     }
258 }
259
Popular Tags