KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  *
3  * Derby - Class StatementTest
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,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20
21 package org.apache.derbyTesting.functionTests.tests.jdbc4;
22
23 import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
24 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
25
26 import junit.framework.*;
27
28 import java.sql.*;
29
30 /**
31  * Tests for new methods added for Statement in JDBC4.
32  */

33 public class StatementTest
34     extends BaseJDBCTestCase {
35
36    /** Default statement used by the tests. */
37     private Statement stmt = null;
38     
39     /**
40      * Create a new test with the given name.
41      *
42      * @param name name of the test.
43      */

44     public StatementTest(String JavaDoc name) {
45         super(name);
46     }
47
48     /**
49      * Create default connection and statement.
50      *
51      * @throws SQLException if setAutoCommit, createStatement or
52      * BaseJDBCTestCase.getConnection fails.
53      */

54     protected void setUp()
55         throws SQLException {
56         getConnection().setAutoCommit(false);
57         // Create a default statement.
58
stmt = createStatement();
59         assertFalse("First statement must be open initially",
60                 stmt.isClosed());
61     }
62
63     /**
64      * Close default connection and statement if necessary.
65      *
66      * @throws SQLException if a database access exception occurs.
67      */

68     protected void tearDown()
69         throws Exception JavaDoc {
70         // Close default statement
71
if (stmt != null) {
72             stmt.close();
73         }
74
75         super.tearDown();
76     }
77
78     /**
79      * Check that <code>isClosed</code> returns <code>true</code> after
80      * the statement has been explicitly closed.
81      */

82     public void testIsClosedBasic()
83         throws SQLException {
84         ResultSet rs = stmt.executeQuery("select count(*) from stmtTable");
85         assertFalse("Statement should still be open", stmt.isClosed());
86         rs.close();
87         assertFalse("Statement should be open after ResultSet has been " +
88                 "closed", stmt.isClosed());
89         stmt.close();
90         assertTrue("Statement should be closed, close() has been called",
91                 stmt.isClosed());
92     }
93     
94     /**
95      * Test that creating two statements on the same connection does not
96      * cause side effects on the statements.
97      */

98     public void testIsClosedWithTwoStatementsOnSameConnection()
99         throws SQLException {
100         // Create a second statement on the default connection.
101
Statement stmt2 = createStatement();
102         assertFalse("Second statement must be open initially",
103                 stmt2.isClosed());
104         assertFalse("First statement should not be closed when " +
105                 "creating a second statement", stmt.isClosed());
106         ResultSet rs = stmt2.executeQuery("select count(*) from stmtTable");
107         assertFalse("Second statement should be open after call to " +
108                 "execute()", stmt2.isClosed());
109         assertFalse("First statement should be open after call to " +
110                 "second statment's execute()", stmt.isClosed());
111         stmt2.close();
112         assertTrue("Second statement should be closed, close() has " +
113                 "been called!", stmt2.isClosed());
114         assertFalse("First statement should be open after call to " +
115                 "second statment's close()", stmt.isClosed());
116     }
117
118     /**
119      * Test that the two statements created on the connection are closed
120      * when the connection itself is closed.
121      */

122     public void testIsClosedWhenClosingConnection()
123         throws SQLException {
124         // Create an extra statement for good measure.
125
Statement stmt2 = createStatement();
126         assertFalse("Second statement must be open initially",
127                 stmt2.isClosed());
128         // Exeute something on it, as opposed to the default statement.
129
stmt2.execute("select count(*) from stmtTable");
130         assertFalse("Second statement should be open after call to " +
131                 "execute()", stmt2.isClosed());
132         // Close the connection. We must commit/rollback first, or else a
133
// "Invalid transaction state" exception is raised.
134
rollback();
135         Connection con = getConnection();
136         con.close();
137         assertTrue("Connection should be closed after close()",
138                 con.isClosed());
139         assertTrue("First statement should be closed, as parent " +
140                 "connection has been closed", stmt.isClosed());
141         assertTrue("Second statement should be closed, as parent " +
142                 "connection has been closed", stmt2.isClosed());
143     }
144     
145     /**
146      * Check the state of the statement when the connection is first attempted
147      * closed when in an invalid transaction state, then closed after a
148      * commit. According to the JDBC 4 API documentation: </i>"It is strongly
149      * recommended that an application explictly commits or rolls back an
150      * active transaction prior to calling the close method. If the close
151      * method is called and there is an active transaction,
152      * the results are implementation-defined."</i>
153      * Derby throws an exception and keeps the connection open.
154      */

155     public void testIsClosedWhenClosingConnectionInInvalidState()
156         throws SQLException {
157         stmt.executeQuery("select count(*) from stmtTable");
158         // Connection should now be in an invalid transaction state.
159
Connection con = stmt.getConnection();
160         try {
161             con.close();
162             fail("Invalid transaction state exception was not thrown");
163         } catch (SQLException sqle) {
164             // The SQL State is incorrect in the embedded client, see
165
// JIRA id DERBY-1168
166
String JavaDoc expectedState;
167             if ( this.usingDerbyNetClient() )
168                 expectedState = SQLStateConstants.INVALID_TRANSACTION_STATE_ACTIVE_SQL_TRANSACTION;
169             else
170                 expectedState = SQLStateConstants.INVALID_TRANSACTION_STATE_NO_SUBCLASS;
171             
172             if ( ! expectedState.equals(sqle.getSQLState()) )
173             {
174                 System.err.println("ERROR: Unexpected SQL State encountered; "
175                     + "got " + sqle.getSQLState() + ", expected "
176                     + expectedState +
177                     ". Unexpected exception message is " + sqle.getMessage());
178                 
179                 throw sqle;
180             }
181         }
182         assertFalse("Statement should still be open, because " +
183                 "Connection.close() failed", stmt.isClosed());
184         assertFalse("Connection should still be open", con.isClosed());
185         // Do a commit here, since we do a rollback in another test.
186
con.commit();
187         con.close();
188         assertTrue("Connection should be closed after close()",
189                 con.isClosed());
190         assertTrue("Statement should be closed, because " +
191                 "the connection has been closed", stmt.isClosed());
192         stmt.close();
193         assertTrue("Statement should still be closed", stmt.isClosed());
194     }
195         
196     /**
197      * Execute a query on a statement after the parent connection has been
198      * closed.
199      */

200     public void testStatementExecuteAfterConnectionClose()
201         throws SQLException {
202         Connection con = stmt.getConnection();
203         con.close();
204         assertTrue("Connection should be closed after close()",
205                 con.isClosed());
206         try {
207             stmt.executeQuery("select count(*) from stmtTable");
208         } catch (SQLException sqle) {
209             assertEquals("Unexpected SQL state for performing " +
210                     "operations on a closed statement.",
211                     SQLStateConstants.CONNECTION_EXCEPTION_CONNECTION_DOES_NOT_EXIST,
212                     sqle.getSQLState());
213         }
214         assertTrue("Statement should be closed, because " +
215                 "the connection has been closed", stmt.isClosed());
216     }
217
218     public void testIsWrapperForStatement() throws SQLException {
219         assertTrue(stmt.isWrapperFor(Statement.class));
220     }
221
222     public void testIsNotWrapperForPreparedStatement() throws SQLException {
223         assertFalse(stmt.isWrapperFor(PreparedStatement.class));
224     }
225
226     public void testIsNotWrapperForCallableStatement() throws SQLException {
227         assertFalse(stmt.isWrapperFor(CallableStatement.class));
228     }
229
230     public void testIsNotWrapperForResultSet() throws SQLException {
231         assertFalse(stmt.isWrapperFor(ResultSet.class));
232     }
233
234     public void testUnwrapStatement() throws SQLException {
235         Statement stmt2 = stmt.unwrap(Statement.class);
236         assertSame("Unwrap returned wrong object.", stmt, stmt2);
237     }
238
239     public void testUnwrapPreparedStatement() {
240         try {
241             PreparedStatement ps = stmt.unwrap(PreparedStatement.class);
242             fail("Unwrap didn't fail.");
243         } catch (SQLException e) {
244             assertSQLState("XJ128", e);
245         }
246     }
247
248     public void testUnwrapCallableStatement() {
249         try {
250             CallableStatement cs = stmt.unwrap(CallableStatement.class);
251             fail("Unwrap didn't fail.");
252         } catch (SQLException e) {
253             assertSQLState("XJ128", e);
254         }
255     }
256
257     public void testUnwrapResultSet() throws SQLException {
258         try {
259             ResultSet rs = stmt.unwrap(ResultSet.class);
260             fail("Unwrap didn't fail.");
261         } catch (SQLException e) {
262             assertSQLState("XJ128", e);
263         }
264     }
265
266     /**
267      * Tests isPoolable, setPoolable, and the default poolability.
268      */

269     public void testPoolable() throws SQLException {
270         assertFalse("Statement cannot be poolable by default",
271                     stmt.isPoolable());
272         stmt.setPoolable(true);
273         assertTrue("Statement must be poolable", stmt.isPoolable());
274
275         stmt.setPoolable(false);
276         assertFalse("Statement cannot be poolable", stmt.isPoolable());
277     }
278
279     /**
280      * Create test suite for StatementTest.
281      */

282     public static Test suite() {
283         TestSuite suite = new TestSuite("StatementTest suite");
284         // Decorate test suite with a TestSetup class.
285
suite.addTest(new StatementTestSetup(
286                         new TestSuite(StatementTest.class)));
287
288         return suite;
289     }
290     
291 } // End class StatementTest
292
Popular Tags