KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > SURBaseTest


1 /*
2  *
3  * Derby - Class SURBaseTest
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 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
21 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
22 import org.apache.derbyTesting.junit.JDBC;
23
24 import junit.framework.*;
25 import java.sql.*;
26
27 /**
28  * Base class for testing Scrollable Updatable ResultSets.
29  * The setUp() provides a Connection to the database.
30  *
31  * Tests of this class needs to be decorated by a DBSetup
32  * and SURDataModelSetup.
33  *
34  * @author Andreas Korneliussen
35  */

36 abstract public class SURBaseTest extends BaseJDBCTestCase {
37     
38     /** Creates a new instance of SURBaseTest */
39     public SURBaseTest(String JavaDoc name) {
40         super(name);
41         recordCount = SURDataModelSetup.recordCount;
42     }
43
44     /** Creates a new instance of SURBaseTest*/
45     public SURBaseTest(String JavaDoc name, int records) {
46         super(name);
47         recordCount = records;
48     }
49     
50     /**
51      * Override a connection's default state to ensure it
52      * is always in autocommit false and repeatable
53      * read as a starting point.
54      */

55     protected void initializeConnection(Connection conn) throws SQLException {
56         conn.setAutoCommit(false);
57         conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
58     }
59
60     /**
61      * Set up the connection to the database.
62      */

63     public void setUp() throws Exception JavaDoc {
64         println("SetUp");
65         // temp save the connection in this class as con
66
// as well as the default connection in the parent
67
con = getConnection();
68     }
69     
70     /**
71      * Verify the data of a tuple in the ResultSet, based on the data
72      * that were inserted.
73      */

74     protected void verifyTuple(ResultSet rs) throws SQLException {
75         int id = rs.getInt(1);
76         int a = rs.getInt(2);
77         int b = rs.getInt(3);
78         int sum = a + id + 17;
79         println("Reading tuple:(" + id + "," + a + "," + b + ",'" +
80                 rs.getString(4) + "')");
81         assertEquals("Expecting b==id+a+17, got: id=" + id +
82                      ",a=" + a + ",b=" + b + ",c=" +rs.getString(4), b, sum);
83     }
84     
85     /**
86      * Update the current tuple in the ResultSet using updateXXX() and
87      * updateRow()
88      */

89     protected void updateTuple(ResultSet rs) throws SQLException {
90         assertFalse("Cannot use updateRow() in autocommit mode",
91                    rs.getStatement().getConnection().getAutoCommit());
92         int id = rs.getInt(1);
93         int a = rs.getInt(2);
94         int b = rs.getInt(3);
95         int newA = a*2 +id + 37;
96         int newB = newA + id + 17;
97         println("Updating record (" + id + "," + newA + "," + newB + ")");
98         rs.updateInt(2, newA);
99         rs.updateInt(3, newB);
100         rs.updateRow();
101     }
102     
103     /**
104      * Update the current tuple in the ResultSet using positioned update
105      */

106     protected void updateTuplePositioned(ResultSet rs) throws SQLException {
107         int id = rs.getInt(1);
108         int a = rs.getInt(2);
109         int b = rs.getInt(3);
110         int newA = a*2 +id + 37;
111         int newB = newA + id + 17;
112         PreparedStatement ps =
113             prepareStatement("update T1 set a=?,b=? where current of " +
114                              rs.getCursorName());
115         ps.setInt(1, newA);
116         ps.setInt(2, newB);
117         assertEquals("Expected one tuple to be updated", 1, ps.executeUpdate());
118     }
119     
120     /**
121      * Scroll forward to the end of the ResultSet, and verify tuples while
122      * scrolling.
123      */

124     protected void scrollForward(ResultSet rs) throws SQLException
125     {
126         boolean ignoreCount = rs.getType()==ResultSet.TYPE_FORWARD_ONLY
127             || !rs.isBeforeFirst();
128         int nRecords = 0;
129         while (rs.next()) {
130             nRecords++;
131             verifyTuple(rs);
132         }
133         if (!ignoreCount) {
134             assertEquals("Expected " + recordCount + " records", nRecords,
135                          recordCount);
136         }
137     }
138     
139     /**
140      * Scroll backward to the beginning of the ResultSet, and verify tuples
141      * while scrolling.
142      */

143     protected void scrollBackward(ResultSet rs) throws SQLException
144     {
145         boolean ignoreCount = rs.getType()==ResultSet.TYPE_FORWARD_ONLY
146             || !rs.isAfterLast();
147         
148         int nRecords = 0;
149         while (rs.previous()) {
150             nRecords++;
151             verifyTuple(rs);
152         }
153         if (!ignoreCount) {
154             assertEquals("Expected " + recordCount + " records", nRecords,
155                          recordCount);
156         }
157     }
158     
159     /**
160      * Scroll forward and update the tuples using updateXXX() and updateRow()
161      */

162     protected void scrollForwardAndUpdate(ResultSet rs) throws SQLException
163     {
164         int nRecords = 0;
165         boolean ignoreCount = rs.getType()==ResultSet.TYPE_FORWARD_ONLY
166             || !rs.isBeforeFirst();
167         
168         while (rs.next()) {
169             nRecords++;
170             verifyTuple(rs);
171             updateTuple(rs);
172         }
173         if (!ignoreCount) {
174             assertEquals("Expected " + recordCount + " records", nRecords,
175                          recordCount);
176         }
177         assertNotNull("rs.getCursorName()", rs.getCursorName());
178     }
179     
180     /**
181      * Scroll forward and do positioned updates.
182      */

183     protected void scrollForwardAndUpdatePositioned(ResultSet rs)
184         throws SQLException
185     {
186         int nRecords = 0;
187         boolean ignoreCount = rs.getType()==ResultSet.TYPE_FORWARD_ONLY
188             || !rs.isBeforeFirst();
189         while (rs.next()) {
190             nRecords++;
191             verifyTuple(rs);
192             updateTuplePositioned(rs);
193         }
194         if (!ignoreCount) {
195             assertEquals("Expected " + recordCount + " records", nRecords,
196                          recordCount);
197         }
198         assertNotNull("rs.getCursorName()", rs.getCursorName());
199     }
200     
201     /**
202      * Scroll backward and update the records using updateXXX() and updateRow()
203      */

204     protected void scrollBackwardAndUpdate(ResultSet rs) throws SQLException
205     {
206         int nRecords = 0;
207         boolean ignoreCount = rs.getType()==ResultSet.TYPE_FORWARD_ONLY
208             || !rs.isAfterLast();
209         while (rs.previous()) {
210             nRecords++;
211             verifyTuple(rs);
212             updateTuple(rs);
213         }
214         if (!ignoreCount) {
215             assertEquals("Expected " + recordCount + " records", nRecords,
216                          recordCount);
217         }
218         assertNotNull("rs.getCursorName()", rs.getCursorName());
219     }
220     
221     /**
222      * Scroll backward and update the records using positioned updates.
223      */

224     protected void scrollBackwardAndUpdatePositioned(ResultSet rs)
225         throws SQLException
226     {
227         int nRecords = 0;
228         boolean ignoreCount = rs.getType()==ResultSet.TYPE_FORWARD_ONLY
229             || !rs.isAfterLast();
230         while (rs.previous()) {
231             nRecords++;
232             verifyTuple(rs);
233             updateTuplePositioned(rs);
234         }
235         if (!ignoreCount) {
236             assertEquals("Expected " + recordCount + " records", nRecords,
237                          recordCount);
238         }
239         assertNotNull("rs.getCursorName()", rs.getCursorName());
240     }
241     
242     /**
243      * Assert that update of ResultSet fails with a SQLException
244      * due to read-only ResultSet.
245      */

246     protected void assertFailOnUpdate(ResultSet rs)
247         throws SQLException
248     {
249         boolean failedCorrect = false;
250         try {
251             updateTuple(rs);
252         } catch (SQLException e) {
253             failedCorrect = true;
254             assertEquals("Unexpected SQL state",
255                          RESULTSET_NOT_UPDATABLE_SQL_STATE,
256                          e.getSQLState());
257             
258         }
259         assertTrue("Expected cursor to fail on update, since it is read only",
260                    failedCorrect);
261     }
262     
263     /**
264      * Assert that a warning was received
265      */

266     protected void assertWarning(SQLWarning warn, String JavaDoc sqlState)
267         throws SQLException
268     {
269         if (warn!=null || usingEmbedded()) {
270             assertEquals("Unexpected SQL state",
271                          sqlState,
272                          warn.getSQLState());
273         } else {
274             println("Expected warning with SQLState = '" + sqlState +
275                     "', however warning not propagated to client driver");
276         }
277     }
278     
279     protected Connection con = null; // Connection established in setUp()
280
final int recordCount;
281     
282        
283     /**
284      * Error codes and SQL state
285      */

286     final static String JavaDoc FOR_UPDATE_NOT_PERMITTED_SQL_STATE = "42Y90";
287     final static String JavaDoc CURSOR_NOT_UPDATABLE_SQL_STATE = "42X23";
288     final static String JavaDoc RESULTSET_NOT_UPDATABLE_SQL_STATE = "XJ083";
289     final static String JavaDoc LOCK_TIMEOUT_SQL_STATE = "40XL1";
290     final static String JavaDoc LOCK_TIMEOUT_EXPRESSION_SQL_STATE = "38000";
291     final static String JavaDoc INVALID_CURSOR_STATE_NO_CURRENT_ROW = "24000";
292     final static String JavaDoc CURSOR_OPERATION_CONFLICT = "01001";
293     final static String JavaDoc QUERY_NOT_QUALIFIED_FOR_UPDATABLE_RESULTSET = "01J06";
294     final static String JavaDoc CURSOR_NOT_POSITIONED_ON_INSERT_ROW = "XJ086";
295 }
296
Popular Tags