1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
/*
*
* Derby - Class SURQueryMixTest
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.jdbcapi;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import junit.extensions.TestSetup;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Tests for Scrollable Updatable ResultSet (SUR). This TestCase tests
* scrolling (navigation), updates (using updateXXX() and updateRow() or
* positioned updates), deletion of records (using deleteRow() or positioned
* deletes) of ResultSets.
*/
public class SURQueryMixTest extends SURBaseTest
{
/**
* Constructor
* @param model name of data model for this TestCase
* @param query to use for producing the resultset
* @param cursorName name of cursor
* @param positioned flag to determine if the Test should use positioned
* updates/deletes instead of updateRow() and deleteRow()
*/
public SURQueryMixTest(final String model, final String query,
final String cursorName, final boolean positioned)
{
super("SURQueryMixTest{Model=" + model + ",Query=" +query + ",Cursor="
+ cursorName + ",Positioned=" + positioned + "}");
this.query = query;
this.cursorName = cursorName;
this.positioned = positioned;
this.checkRowUpdated = false;
this.checkRowDeleted = false;
}
/**
* Test SUR properties of the query
*/
public void runTest()
throws SQLException
{
println(query);
DatabaseMetaData dbMeta = getConnection().getMetaData();
if (dbMeta.ownDeletesAreVisible(ResultSet.TYPE_SCROLL_INSENSITIVE)) {
checkRowDeleted = true;
}
Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(cursorName);
ResultSet rs = s.executeQuery(query);
checkRowUpdated = dbMeta.ownUpdatesAreVisible(rs.getType());
checkRowDeleted = dbMeta.ownDeletesAreVisible(rs.getType());
// Create map with rows
Map<Integer, String> rows = createRowMap(rs);
// Set of rows which are updated (contains Integer with position in RS)
final Set<Integer> updatedRows = new HashSet<Integer>();
// Set of rows which are deleted (contains Integer with position in RS)
final Set<Integer> deletedRows = new HashSet<Integer>();
// Test navigation
testNavigation(rs, rows, updatedRows, deletedRows);
// Only test updatability if the ResultSet is updatable:
// (Note: this enables the test do run successfully even if
// scrollable updatable resultsets are not implemented.
// If SUR is not implemented, a well behaved JDBC driver will
// downgrade the concurrency mode to READ_ONLY).
// SUR may be implemented incrementally, i.e first in embedded mode
// then in the network driver.)
if (rs.getConcurrency()==ResultSet.CONCUR_UPDATABLE) {
// update a random sample of 2 records
updateRandomSampleOfNRecords(rs, rows, updatedRows, 2);
testNavigation(rs, rows, updatedRows, deletedRows);
// update a random sample of 5 records
updateRandomSampleOfNRecords(rs, rows, updatedRows, 5);
testNavigation(rs, rows, updatedRows, deletedRows);
// update a random sample of 10 records
updateRandomSampleOfNRecords(rs, rows, updatedRows, 10);
testNavigation(rs, rows, updatedRows, deletedRows);
// delete a random sample of 2 records
deleteRandomSampleOfNRecords(rs, rows, deletedRows, 2);
testNavigation(rs, rows, updatedRows, deletedRows);
// delete a random sample of 5 records
deleteRandomSampleOfNRecords(rs, rows, deletedRows, 5);
testNavigation(rs, rows, updatedRows, deletedRows);
// delete a random sample of 10 records
deleteRandomSampleOfNRecords(rs, rows, deletedRows, 10);
testNavigation(rs, rows, updatedRows, deletedRows);
} else {
assertTrue("ResultSet concurrency downgraded to CONCUR_READ_ONLY",
false);
}
rs.close();
s.close();
}
/**
* Creates a Map of the values in the ResultSet.
* The key object in the map, is the postion in the
* ResultSet (Integer 1..n), while the value is a
* concatenation of the strings for all columns in the row.
*/
private Map<Integer, String> createRowMap(final ResultSet rs)
throws SQLException
{
final Map<Integer, String> rows = new HashMap<Integer, String>();
rs.beforeFirst();
assertTrue("Unexpected return from isBeforeFirst()",
rs.isBeforeFirst());
int i = 0;
int sum = 0;
int expectedSum = 0;
boolean checkSum = true;
while (rs.next()) {
expectedSum += i;
i++;
String row = getRowString(rs);
println(row);
rows.put(i, row);
sum += rs.getInt(1);
if (rs.getInt(1) < 0) {
checkSum = false;
}
}
if (i<SURDataModelSetup.recordCount) {
checkSum = false;
}
assertTrue("Unexpected return from isAfterLast()", rs.isAfterLast());
if (checkSum) {
assertEquals("Sum for column 1 is not correct", expectedSum, sum);
}
return rows;
}
/**
* Create a random sample of rows
* @param rows Map to create sample from
* @param k number of rows in the sample
* @return a list containing k elements of rows
**/
private List createRandomSample(final Map<Integer, String> rows, int k) {
Random r = new Random();
ArrayList<Integer> sampledKeys = new ArrayList<Integer>();
int n = 0;
for (Integer key : rows.keySet()) {
n++;
if (n<=k) {
sampledKeys.add(key);
} else {
// sampledKeys now has a size of k
double d = r.nextDouble();
// p = probability of going into the sample
double p = (double) k / (double) n;
if (d<p) {
// Replace a random value from the sample with the new value
int keyToReplace = r.nextInt(k);
sampledKeys.set(keyToReplace, key);
}
}
}
return sampledKeys;
}
/**
* Delete a random sample of n records in the resultset
* @param rs result set to be updated
* @param rows map of rows, will also be updated
* @param deletedRows set of rows being deleted (position in RS)
* @param k number of records to be deleted
*/
private void deleteRandomSampleOfNRecords(final ResultSet rs,
final Map<Integer, String> rows,
final Set<Integer> deletedRows,
final int k)
throws SQLException
{
List sampledKeys = createRandomSample(rows, k);
println("Sampled keys:" + sampledKeys);
ResultSetMetaData meta = rs.getMetaData();
for (Iterator i = sampledKeys.iterator(); i.hasNext();) {
Integer key = (Integer) i.next();
rs.absolute(key.intValue());
if (rs.rowDeleted()) continue; // skip deleting row if already deleted
if (positioned) {
createStatement().executeUpdate
("DELETE FROM T1 WHERE CURRENT OF \"" + cursorName +
"\"");
} else {
rs.deleteRow();
}
rs.relative(0);
println("Deleted row " + key);
// Update the rows table
rows.put(key, getRowString(rs));
// Update the updatedRows set
deletedRows.add(key);
}
}
/**
* Update a random sample of n records in the resultset
* @param rs result set to be updated
* @param rows map of rows, will also be updated
* @param updatedRows set of being updated (position in RS)
* @param k number of records to be updated
*/
private void updateRandomSampleOfNRecords(final ResultSet rs,
final Map<Integer, String> rows,
final Set<Integer> updatedRows,
final int k)
throws SQLException
{
List sampledKeys = createRandomSample(rows, k);
println("Sampled keys:" + sampledKeys);
ResultSetMetaData meta = rs.getMetaData();
for (Iterator i = sampledKeys.iterator(); i.hasNext();) {
Integer key = (Integer) i.next();
rs.absolute(key.intValue());
if (positioned) {
updatePositioned(rs, meta);
rs.relative(0); // If this call is not here, the old values are
// returned in rs.getXXX calls
} else {
updateRow(rs, meta);
}
// Update the rows table
rows.put(key, getRowString(rs));
// Update the updatedRows set
updatedRows.add(key);
}
}
/**
* Updates the current row in the ResultSet using updateRow()
* @param rs ResultSet to be updated
* @param meta meta for the ResultSet
**/
private void updateRow(final ResultSet rs, final ResultSetMetaData meta)
throws SQLException
{
for (int column = 1; column<=meta.getColumnCount(); column++) {
if (meta.getColumnType(column)==Types.INTEGER) {
// Set to negative value
rs.updateInt(column, -rs.getInt(column));
} else {
rs.updateString(column, "UPDATED_" + rs.getString(column));
}
}
rs.updateRow();
}
/**
* Updates the current row in the ResultSet using updateRow()
* @param rs ResultSet to be updated
* @param meta meta for the ResultSet
**/
private void updatePositioned(final ResultSet rs,
final ResultSetMetaData meta)
throws SQLException
{
StringBuffer sb = new StringBuffer();
sb.append("UPDATE T1 SET ");
for (int column = 1; column<=meta.getColumnCount(); column++) {
sb.append(meta.getColumnName(column));
sb.append("=?");
if (column<meta.getColumnCount()) {
sb.append(",");
}
}
sb.append(" WHERE CURRENT OF \"");
sb.append(cursorName);
sb.append("\"");
println(sb.toString());
PreparedStatement ps = prepareStatement(sb.toString());
for (int column = 1; column<=meta.getColumnCount(); column++) {
if (meta.getColumnType(column)==Types.INTEGER) {
// Set to negative value
ps.setInt(column, -rs.getInt(column));
} else {
ps.setString(column, "UPDATED_" + rs.getString(column));
}
}
assertEquals("Expected one row to be updated", 1, ps.executeUpdate());
}
/**
* Tests navigation in ResultSet.
* @param rs ResultSet to test navigation of.
* Needs to be scrollable
* @param rows a sample of the rows which are in the ResultSet. Maps
* position to a concatenation of the string values
* @param updatedRows a integer set of which rows that have been
* updated. Used to test rowUpdated()
* @param deletedRows a integer set of which rows that have been
* deleted. Used to test rowDeleted()
*/
private void testNavigation(final ResultSet rs, final Map rows,
final Set updatedRows, final Set deletedRows)
throws SQLException
{
rs.afterLast();
{
int i = rows.size();
while (rs.previous()) {
String rowString = getRowString(rs);
assertEquals("Navigating with rs.previous(). The row is " +
"different compared to the value when navigating " +
"forward.", rows.get(i), rowString);
if (checkRowUpdated && updatedRows.contains(i)) {
assertTrue("Expected rs.rowUpdated() to return true on " +
"updated row " + rowString, rs.rowUpdated());
}
if (checkRowDeleted && deletedRows.contains(i)) {
assertTrue("Expected rs.rowDeleted() to return true on " +
"deleted row " + rowString, rs.rowDeleted());
}
i--;
}
}
// Test absolute
for (int i = 1; i <= rows.size(); i++) {
assertTrue("Unexpected return from absolute()", rs.absolute(i));
String rowString = getRowString(rs);
assertEquals("Navigating with rs.absolute(). The row is " +
"different compared to the value" +
" when navigating forward.",
rows.get(i),
rowString);
if (checkRowUpdated && updatedRows.contains(i)) {
assertTrue("Expected rs.rowUpdated() to return true on " +
"updated row " + rowString, rs.rowUpdated());
}
if (checkRowDeleted && deletedRows.contains(i)) {
assertTrue("Expected rs.rowDeleted() to return true on " +
"deleted row " + rowString, rs.rowDeleted());
}
}
assertFalse("Unexpected return from absolute()", rs.absolute(0));
assertTrue("Unexpected return from isBeforeFirst()",
rs.isBeforeFirst());
assertFalse("Unexpected return from absolute()",
rs.absolute(rows.size() + 1));
assertTrue("Unexpected return from isAfterLast()", rs.isAfterLast());
assertTrue("Unexpected return from absolute()", rs.absolute(-1));
assertTrue("Unexpected return from isLast()", rs.isLast());
assertTrue("Unexpected return from absolute()", rs.absolute(1));
assertTrue("Unexpected return from isFirst()", rs.isFirst());
// Test relative
{
rs.beforeFirst();
assertTrue("Unexptected return from isBeforeFirst()",
rs.isBeforeFirst());
int relativePos = rows.size();
assertTrue("Unexpected return from relative()",
rs.relative(relativePos));
// Should now be on the last row
assertTrue("Unexptected return from isLast()", rs.isLast());
assertEquals("Navigating with rs.relative(+). " +
"A tuple was different compared to the value" +
" when navigating forward.",
rows.get(relativePos),
getRowString(rs));
assertTrue("Unexpected return from relative()",
rs.relative((-relativePos + 1)));
// Should now be on the first row
assertTrue("Unexptected return from isFirst()", rs.isFirst());
assertEquals("Navigating with rs.relative(-). " +
"A tuple was different compared to the value" +
" when navigating forward.",
rows.get(1),
getRowString(rs));
}
// Test navigation in the end of the ResultSet
rs.afterLast();
assertTrue("Unexpected return from isAfterLast()", rs.isAfterLast());
assertTrue("Unexpected return from previous()", rs.previous());
assertTrue("Unexpected return from isLast()", rs.isLast());
assertFalse("Unexpected return from next()", rs.next());
assertTrue("Unexpected return from isAfterLast()", rs.isAfterLast());
rs.last();
assertTrue("Unexpected return from isLast()", rs.isLast());
assertFalse("Unexpected return from next()", rs.next());
assertTrue("Unexpected return from isAfterLast()", rs.isAfterLast());
// Test navigation in the beginning of the ResultSet
rs.beforeFirst();
assertTrue("Unexpected return from isBeforeFirst()",
rs.isBeforeFirst());
assertTrue("Unexpected return from next()", rs.next());
assertTrue("Unexpected return from isFirst", rs.isFirst());
assertFalse("Unexpected return from previous()", rs.previous());
assertTrue("Unexpected return from isBeforeFirst()",
rs.isBeforeFirst());
rs.first();
assertTrue("Unexpected return from isFirst", rs.isFirst());
assertFalse("Unexpected return from previous()", rs.previous());
assertTrue("Unexpected return from isBeforeFirst()",
rs.isBeforeFirst());
}
/**
* Get a concatenation of the values of the
* current Row in the ResultSet
*/
private String getRowString(final ResultSet rs)
throws SQLException
{
int numberOfColumns = rs.getMetaData().getColumnCount();
StringBuffer sb = new StringBuffer();
if (rs.rowDeleted()) return "";
for (int i = 1; i <= numberOfColumns; i++) {
sb.append(rs.getString(i));
if (i < numberOfColumns) {
sb.append(',');
}
}
return sb.toString();
}
private final String query;
private final String cursorName;
private final boolean positioned;
private boolean checkRowUpdated;
private boolean checkRowDeleted;
private final static String[] selectConditions = new String[] {
"WHERE c like 'T%'",
" ",
"WHERE b > 5",
"WHERE id >= a",
"WHERE id > 1 and id < 900",
"WHERE id = 1",
"WHERE id in (1,3,4,600,900,955,966,977,978)",
"WHERE a in (1,3,4,600,9200,955,966,977,978)",
"WHERE a>2 and a<9000"
};
private final static String[] projectConditions = new String[] {
"id,c,a,b",
"id,c",
"a,b",
"*",
"id,a,b,c",
"id,a",
"a,b,c",
"a,c"
};
private static BaseTestSuite createTestCases(final String modelName) {
BaseTestSuite suite = new BaseTestSuite();
for (int doPos = 0; doPos<2; doPos++) {
boolean positioned = doPos>0; // true if to use positioned updates
for (int i = 0; i < selectConditions.length; i++) {
for (int j = 0; j < projectConditions.length; j++) {
final String cursorName = "cursor_" + i + "_" + j;
final String stmtString = "SELECT " + projectConditions[j] +
" FROM T1 " + selectConditions[i];
suite.addTest(new SURQueryMixTest(modelName, stmtString, cursorName,
positioned));
}
}
}
return suite;
}
/**
* Run in client and embedded.
*/
public static Test suite()
{
BaseTestSuite mainSuite = new BaseTestSuite("SURQueryMixTest suite");
mainSuite.addTest(baseSuite("SURQueryMixTest:embedded"));
mainSuite.addTest(
TestConfiguration.clientServerDecorator(
baseSuite("SURQueryMixTest:client")));
return mainSuite;
}
/**
* The suite contains all testcases in this class running on different data models
*/
private static Test baseSuite(String name) {
BaseTestSuite mainSuite = new BaseTestSuite(name);
// Iterate over all data models and decorate the tests:
for (Iterator i = SURDataModelSetup.SURDataModel.values().iterator();
i.hasNext();) {
SURDataModelSetup.SURDataModel model =
(SURDataModelSetup.SURDataModel) i.next();
BaseTestSuite suite = createTestCases(model.toString());
TestSetup decorator = new SURDataModelSetup(suite, model);
mainSuite.addTest(decorator);
}
return mainSuite;
}
}
|