1 25 package testsuite.simple; 26 27 import com.mysql.jdbc.NotUpdatable; 28 29 import testsuite.BaseTestCase; 30 31 import java.sql.DatabaseMetaData ; 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 import java.sql.Statement ; 35 36 43 public class UpdatabilityTest extends BaseTestCase { 44 50 public UpdatabilityTest(String name) { 51 super(name); 52 } 53 54 59 public static void main(String [] args) { 60 junit.textui.TestRunner.run(UpdatabilityTest.class); 61 } 62 63 69 public void setUp() throws Exception { 70 super.setUp(); 71 createTestTable(); 72 } 73 74 81 public void testAliasedTables() throws Exception { 82 DatabaseMetaData dbmd = this.conn.getMetaData(); 83 84 if ((dbmd.getDatabaseMajorVersion() >= 4) 85 && (dbmd.getDatabaseMinorVersion() >= 1)) { 86 Statement scrollableStmt = null; 87 88 try { 89 scrollableStmt = this.conn.createStatement( 90 ResultSet.TYPE_SCROLL_INSENSITIVE, 91 ResultSet.CONCUR_UPDATABLE); 92 this.rs = scrollableStmt 93 .executeQuery("SELECT pos1 AS p1, pos2 AS P2, char_field AS cf FROM UPDATABLE AS UPD LIMIT 1"); 94 this.rs.next(); 95 this.rs.close(); 96 this.rs = null; 97 98 scrollableStmt.close(); 99 scrollableStmt = null; 100 } finally { 101 if (this.rs != null) { 102 try { 103 this.rs.close(); 104 } catch (SQLException sqlEx) { 105 ; } 107 108 this.rs = null; 109 } 110 111 if (scrollableStmt != null) { 112 try { 113 scrollableStmt.close(); 114 } catch (SQLException sqlEx) { 115 ; } 117 118 scrollableStmt = null; 119 } 120 } 121 } 122 } 123 124 131 public void testBogusTable() throws SQLException { 132 this.stmt.executeUpdate("DROP TABLE IF EXISTS BOGUS_UPDATABLE"); 133 this.stmt.executeUpdate("CREATE TABLE BOGUS_UPDATABLE (field1 int)"); 134 135 Statement scrollableStmt = null; 136 137 try { 138 scrollableStmt = this.conn.createStatement( 139 ResultSet.TYPE_SCROLL_INSENSITIVE, 140 ResultSet.CONCUR_UPDATABLE); 141 this.rs = scrollableStmt 142 .executeQuery("SELECT * FROM BOGUS_UPDATABLE"); 143 144 try { 145 this.rs.moveToInsertRow(); 146 fail("ResultSet.moveToInsertRow() should not succeed on non-updatable table"); 147 } catch (NotUpdatable noUpdate) { 148 } 150 } finally { 151 if (scrollableStmt != null) { 152 try { 153 scrollableStmt.close(); 154 } catch (SQLException sqlEx) { 155 ; 156 } 157 } 158 159 this.stmt.executeUpdate("DROP TABLE IF EXISTS BOGUS_UPDATABLE"); 160 } 161 } 162 163 170 public void testMultiKeyTable() throws SQLException { 171 this.stmt.executeUpdate("DROP TABLE IF EXISTS MULTI_UPDATABLE"); 172 this.stmt 173 .executeUpdate("CREATE TABLE MULTI_UPDATABLE (field1 int NOT NULL, field2 int NOT NULL, PRIMARY KEY (field1, field2))"); 174 175 Statement scrollableStmt = null; 176 177 try { 178 scrollableStmt = this.conn.createStatement( 179 ResultSet.TYPE_SCROLL_INSENSITIVE, 180 ResultSet.CONCUR_UPDATABLE); 181 this.rs = scrollableStmt 182 .executeQuery("SELECT field1 FROM MULTI_UPDATABLE"); 183 184 try { 185 this.rs.moveToInsertRow(); 186 fail("ResultSet.moveToInsertRow() should not succeed on query that does not select all primary keys"); 187 } catch (NotUpdatable noUpdate) { 188 } 190 } finally { 191 if (scrollableStmt != null) { 192 try { 193 scrollableStmt.close(); 194 } catch (SQLException sqlEx) { 195 } 197 } 198 199 this.stmt.executeUpdate("DROP TABLE IF EXISTS MULTI_UPDATABLE"); 200 } 201 } 202 203 209 public void testUpdatability() throws SQLException { 210 Statement scrollableStmt = null; 211 212 try { 213 scrollableStmt = this.conn.createStatement( 214 ResultSet.TYPE_SCROLL_INSENSITIVE, 215 ResultSet.CONCUR_UPDATABLE); 216 this.rs = scrollableStmt 217 .executeQuery("SELECT * FROM UPDATABLE ORDER BY pos1"); 218 219 this.rs.getMetaData().getColumnCount(); 220 221 while (this.rs.next()) { 222 int rowPos = this.rs.getInt(1); 223 this.rs.updateString(3, "New Data" + (100 - rowPos)); 224 this.rs.updateRow(); 225 } 226 227 this.rs.moveToInsertRow(); 231 this.rs.updateInt(1, 400); 232 this.rs.updateInt(2, 400); 233 this.rs.updateString(3, "New Data" + (100 - 400)); 234 this.rs.insertRow(); 235 236 int rememberedPosition = this.rs.getRow(); 238 this.rs.moveToInsertRow(); 239 this.rs.moveToCurrentRow(); 240 assertTrue("ResultSet.moveToCurrentRow() failed", 241 this.rs.getRow() == rememberedPosition); 242 this.rs.close(); 243 this.rs = scrollableStmt 244 .executeQuery("SELECT * FROM UPDATABLE ORDER BY pos1"); 245 246 boolean dataGood = true; 247 248 while (this.rs.next()) { 249 int rowPos = this.rs.getInt(1); 250 251 if (!this.rs.getString(3).equals("New Data" + (100 - rowPos))) { 252 dataGood = false; 253 } 254 } 255 256 assertTrue("Updates failed", dataGood); 257 258 int newPrimaryKeyId = 99999; 261 this.rs.absolute(1); 262 this.rs.updateInt(1, newPrimaryKeyId); 263 this.rs.updateRow(); 264 265 int savedPrimaryKeyId = this.rs.getInt(1); 266 assertTrue("Updated primary key does not match", 267 (newPrimaryKeyId == savedPrimaryKeyId)); 268 269 this.rs.absolute(1); 271 272 int primaryKey = this.rs.getInt(1); 273 int originalValue = this.rs.getInt(2); 274 this.rs.updateInt(2, -3); 275 this.rs.cancelRowUpdates(); 276 277 int newValue = this.rs.getInt(2); 278 assertTrue("ResultSet.cancelRowUpdates() failed", 279 newValue == originalValue); 280 281 this.rs.absolute(1); 284 primaryKey = this.rs.getInt(1); 285 this.stmt 286 .executeUpdate("UPDATE UPDATABLE SET char_field='foo' WHERE pos1=" 287 + primaryKey); 288 this.rs.refreshRow(); 289 assertTrue("ResultSet.refreshRow failed", this.rs.getString( 290 "char_field").equals("foo")); 291 292 this.rs.last(); 294 295 int oldLastRow = this.rs.getRow(); 296 this.rs.deleteRow(); 297 this.rs.last(); 298 assertTrue("ResultSet.deleteRow() failed", 299 this.rs.getRow() == (oldLastRow - 1)); 300 this.rs.close(); 301 302 332 } finally { 333 if (scrollableStmt != null) { 334 try { 335 scrollableStmt.close(); 336 } catch (SQLException sqlEx) { 337 ; 338 } 339 } 340 } 341 } 342 343 private void createTestTable() throws SQLException { 344 try { 348 this.stmt.executeUpdate("DROP TABLE UPDATABLE"); 349 } catch (SQLException SQLE) { 350 ; 351 } 352 353 this.stmt 354 .executeUpdate("CREATE TABLE UPDATABLE (pos1 int not null, pos2 int not null, char_field VARCHAR(32), PRIMARY KEY (pos1, pos2))"); 355 356 for (int i = 0; i < 100; i++) { 357 this.stmt.executeUpdate("INSERT INTO UPDATABLE VALUES (" + i + ", " 358 + i + ",'StringData" + i + "')"); 359 } 360 } 361 } 362 | Popular Tags |