| 1 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 36 abstract public class SURBaseTest extends BaseJDBCTestCase { 37 38 39 public SURBaseTest(String name) { 40 super(name); 41 recordCount = SURDataModelSetup.recordCount; 42 } 43 44 45 public SURBaseTest(String name, int records) { 46 super(name); 47 recordCount = records; 48 } 49 50 55 protected void initializeConnection(Connection conn) throws SQLException { 56 conn.setAutoCommit(false); 57 conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 58 } 59 60 63 public void setUp() throws Exception { 64 println("SetUp"); 65 con = getConnection(); 68 } 69 70 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 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 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 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 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 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 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 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 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 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 266 protected void assertWarning(SQLWarning warn, String 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; final int recordCount; 281 282 283 286 final static String FOR_UPDATE_NOT_PERMITTED_SQL_STATE = "42Y90"; 287 final static String CURSOR_NOT_UPDATABLE_SQL_STATE = "42X23"; 288 final static String RESULTSET_NOT_UPDATABLE_SQL_STATE = "XJ083"; 289 final static String LOCK_TIMEOUT_SQL_STATE = "40XL1"; 290 final static String LOCK_TIMEOUT_EXPRESSION_SQL_STATE = "38000"; 291 final static String INVALID_CURSOR_STATE_NO_CURRENT_ROW = "24000"; 292 final static String CURSOR_OPERATION_CONFLICT = "01001"; 293 final static String QUERY_NOT_QUALIFIED_FOR_UPDATABLE_RESULTSET = "01J06"; 294 final static String CURSOR_NOT_POSITIONED_ON_INSERT_ROW = "XJ086"; 295 } 296 | Popular Tags |