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 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.TimeHandlingTest
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.lang;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Calendar;
import java.util.Random;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
public class TimeHandlingTest extends BaseJDBCTestCase {
/**
* All the functions or expressions that result in
* a TIME value with the same value as CURRENT_TIME.
*/
private static final String[] CURRENT_TIME_FUNCTIONS =
{"CURRENT TIME", "CURRENT_TIME"};
/**
* All the functions or expressions that result in
* a TIMESTAMP value with the same value as CURRENT_TIMESTAMP.
*/
private static final String[] CURRENT_TIMESTAMP_FUNCTIONS =
{"CURRENT TIMESTAMP", "CURRENT_TIMESTAMP"};
/**
* Time to sleep that will result in different TIME values.
*/
private static final long SLEEP_TIME = 2000;
/**
* Calendar for testing returned values.
*/
private Calendar cal;
/**
* Runs the tests in the default embedded configuration and then
* the client server configuration.
*/
public static Test suite()
{
BaseTestSuite suite = new BaseTestSuite(TimeHandlingTest.class);
suite.addTest(TestConfiguration.clientServerSuite(TimeHandlingTest.class));
return new CleanDatabaseTestSetup(suite) {
protected void decorateSQL(Statement s) throws SQLException {
s.execute("CREATE FUNCTION SLEEP() RETURNS INTEGER" +
" LANGUAGE JAVA PARAMETER STYLE JAVA" +
" NO SQL " +
" EXTERNAL NAME '"+
TimeHandlingTest.class.getName().concat(".sleep'"));
s.execute("CREATE TABLE TIME_ALL (ID INT," +
" C_T TIME," +
" C_D DATE," +
" C_TS TIMESTAMP)");
for (int f = 0; f < CURRENT_TIME_FUNCTIONS.length; f++)
{
s.execute("ALTER TABLE TIME_ALL ADD COLUMN" +
" D_T" + f + " TIME WITH DEFAULT " +
CURRENT_TIME_FUNCTIONS[f]);
}
for (int f = 0; f < CURRENT_TIMESTAMP_FUNCTIONS.length; f++)
{
s.execute("ALTER TABLE TIME_ALL ADD COLUMN" +
" D_TS" + f + " TIMESTAMP WITH DEFAULT " +
CURRENT_TIMESTAMP_FUNCTIONS[f]);
}
}
};
}
/**
* Method for SQL SLEEP function. Sleeps for the time
* that will result in a change in
* System.currentTimeMillis and a Derby TIME value.
* @throws InterruptedException
*/
public static int sleep() throws InterruptedException {
Thread.sleep(SLEEP_TIME);
return 0;
}
public TimeHandlingTest(String name) {
super(name);
}
/**
* Simple set up, just get a Calendar
* and ensure the table T_ALL is empty.
* @throws SQLException
*/
protected void setUp() throws SQLException
{
cal = Calendar.getInstance();
Statement s = createStatement();
s.executeUpdate("DELETE FROM TIME_ALL");
s.close();
}
/**
* Test inserting and selecting of TIME values.
* A set of random TIME values are inserted along with an
* identifier that encodes the time value. The values are then
* fetched and compared to a value calculated from the identifier.
* The returned values are fetched using checkTimeValue thus inheriting
* all the checks within that method.
* <BR>
*
* @throws SQLException
*/
public void testInsertTime() throws SQLException
{
getConnection().setAutoCommit(false);
// Insert a set of time values,
Random r = new Random();
// Insert 500 TIME values using a PreparedStatement,
// but randomly selecting the way the value is inserted
// between:
// java.sql.Time object
// String representation hh:mm:ss from Time.toString()
// String representation hh.mm.ss
// prime number used to select the way the
// selected value is inserted.
final int itk = 71;
PreparedStatement ps = prepareStatement(
"INSERT INTO TIME_ALL(ID, C_T) VALUES (?, ?)");
for (int i = 0; i < 500; i++) {
// Just some big range from zero upwards
int id = r.nextInt(1000000);
ps.setInt(1, id);
Time ct = getCodedTime(id);
switch ((id % itk) % 3)
{
case 0: // Insert using Time object
ps.setTime(2, ct);
break;
case 1: // Insert using String provided by Time.toString() (hh:mm:ss)
ps.setString(2, ct.toString());
break;
case 2: // Insert using String format (hh.mm.ss)
ps.setString(2, ct.toString().replace(':', '.'));
break;
default:
fail("not reached");
}
ps.executeUpdate();
}
ps.close();
commit();
Statement s = createStatement();
ResultSet rs = s.executeQuery("SELECT ID, C_T FROM TIME_ALL");
int rowCount = 0;
while (rs.next())
{
int id = rs.getInt(1);
Time t = checkTimeValue(rs, 2);
assertTimeEqual(getCodedTime(id), t);
rowCount++;
}
rs.close();
s.close();
commit();
assertEquals(rowCount, 500);
}
/**
* Return a time simply encoded from an integer identifier
* and a set of fixed encoding keys, each a prime number.
* This allows a random value to be inserted into a table
* as a TIME and an INTEGER and thus checked for consistency
* on a SELECT.
* @param id
*/
private Time getCodedTime(int id)
{
final int hk = 17;
final int mk = 41;
final int sk = 67;
int hour = (id % hk) % 24;
int min = (id % mk) % 60;
int sec = (id % sk) % 60;
return getTime19700101(hour, min ,sec);
}
/**
* Tests for CURRENT TIME and CURRENT_TIME.
* A set of tests that ensure the CURRENT TIME maintains
* a single value for the life time of a statement and
* that (subject to the resolution) the returned value
* is correctly between the start time of the statement
* execution and the first fetch or completion.
* <BR>
*
* @throws SQLException
* @throws InterruptedException
*/
public void testCurrentTime() throws SQLException, InterruptedException
{
currentFunctionTests(Types.TIME, CURRENT_TIME_FUNCTIONS);
}
/**
* Tests for CURRENT TIMESTAMP functions.
* A set of tests that ensure the CURRENT TIMESTAMP maintains
* a single value for the life time of a statement and
* that (subject to the resolution) the returned value
* is correctly between the start time of the statement
* execution and the first fetch or completion.
* @throws SQLException
* @throws InterruptedException
*/
public void testCurrentTimestamp() throws SQLException, InterruptedException
{
currentFunctionTests(Types.TIMESTAMP, CURRENT_TIMESTAMP_FUNCTIONS);
}
/**
* Test all the current timedate functions passed in that
* return the specified type. Generic function that checks
* the functions' are all identical in various situations
* and that the have the correct value, and change across
* executions.
*
* @param jdbcType JDBC type, Types.TIME, DATE or TIMESTAMP.
* @param functions List of functions or expressions that map to the
* current time date value and return the specified type.
* @throws SQLException
* @throws InterruptedException
*/
private void currentFunctionTests(int jdbcType, String[] functions)
throws SQLException, InterruptedException
{
Statement s = createStatement();
// Single value returned by each function.
for (int f = 0; f < functions.length; f++) {
checkCurrentQuery(jdbcType, s, "VALUES " + functions[f],
new int[] {1}, 1);
}
// Create text for a single row in a VALUES clause,
// each function represented once.
StringBuffer rb = new StringBuffer("(");
for (int f = 0; f < functions.length; f++) {
if (f != 0)
rb.append(", ");
rb.append(functions[f]);
}
rb.append(")");
String row = rb.toString();
int[] columns = new int[functions.length];
for (int f = 0; f < columns.length; f++)
columns[f] = f + 1;
// All the functions as a single row, all return the same value
String sql = "VALUES " + row;
checkCurrentQuery(jdbcType, s, sql, columns, functions.length);
// Check they produce the same value across multiple rows
sql = "VALUES " + row + "," + row + "," + row;
checkCurrentQuery(jdbcType, s, sql, columns, 3 * functions.length);
// Check they produce the same value across multiple rows
// with a forced sleep within row creaton
String sleepRow = row.substring(0, row.length() - 1)
+ ", SLEEP())";
sql = "VALUES " + sleepRow + "," + sleepRow + "," + sleepRow;
checkCurrentQuery(jdbcType, s, sql, columns, 3 * functions.length);
// Check behaviour in non-queries.
String ccol = null;
String dcol = null;
switch (jdbcType)
{
case Types.TIME:
dcol = "D_T";
ccol = "C_T";
break;
case Types.TIMESTAMP:
dcol = "D_TS";
ccol = "C_TS";
break;
case Types.DATE:
dcol = "D_D";
ccol = "C_D";
break;
default:
fail("Unexpected JDBC Type " + jdbcType);
}
// All the functions as multiple rows, one function per row.
StringBuffer rm = new StringBuffer();
for (int f = 0; f < functions.length; f++) {
if (f != 0)
rm.append(", ");
rm.append(functions[f]);
}
String mrow = rm.toString();
// Select list with all the columns of this type
StringBuffer sb = new StringBuffer();
sb.append(ccol); // Column without the defaul
for (int f = 0; f < functions.length; f++) {
sb.append(", ");
sb.append(dcol);
sb.append(f);
}
String typeColumnList = sb.toString();
String selectAllType = "SELECT " + typeColumnList + " FROM TIME_ALL";
int[] tableColumns = new int[columns.length + 1];
for (int i = 0; i < tableColumns.length; i++)
tableColumns[i] = i+1;
// Insert multiple rows, one per function
// Check all the inserted value and the default
// columns have the same value.
String insert = "INSERT INTO TIME_ALL(" + ccol + ") VALUES " + mrow;
s.executeUpdate("DELETE FROM TIME_ALL");
long start = System.currentTimeMillis();
s.executeUpdate(insert);
long end = System.currentTimeMillis();
ResultSet rs = s.executeQuery(selectAllType);
rs.next();
checkCurrentMultiple(jdbcType, start, end, rs, tableColumns,
functions.length * (functions.length + 1));
rs.close();
// Insert of multiple rows from a query with a delay
// All the functions as multiple rows, one function per row
// with a SLEEP as the first column.
sb = new StringBuffer();
for (int f = 0; f < functions.length; f++) {
if (f != 0)
sb.append(", ");
sb.append("(SLEEP(), ");
sb.append(functions[f]);
sb.append(")");
}
String mSleepRow = sb.toString();
insert = "INSERT INTO TIME_ALL(ID, " + ccol + ") " +
" SELECT * FROM TABLE (VALUES " +
mSleepRow +
") AS T";
s.executeUpdate("DELETE FROM TIME_ALL");
start = System.currentTimeMillis();
s.executeUpdate(insert);
end = System.currentTimeMillis();
rs = s.executeQuery(selectAllType);
rs.next();
checkCurrentMultiple(jdbcType, start, end, rs, tableColumns,
functions.length * (functions.length + 1));
rs.close();
// Ensure a PreparedStatement (psI) resets its current time correctly
// and does not get stuck with a single value for all executions.
PreparedStatement psQ = prepareStatement(
selectAllType + " WHERE ID = ?");
Object last = null;
for (int f = 0; f < functions.length; f++) {
PreparedStatement psI = prepareStatement("INSERT INTO TIME_ALL(ID, " +
ccol + ")" +
" VALUES (?, " + functions[f] +")");
s.executeUpdate("DELETE FROM TIME_ALL");
for (int i = 1; i <=3; i++) {
psI.setInt(1, i);
psQ.setInt(1, i);
start = System.currentTimeMillis();
psI.executeUpdate();
end = System.currentTimeMillis();
rs = psQ.executeQuery();
rs.next();
Object next = checkCurrentMultiple(jdbcType, start, end, rs,
tableColumns, functions.length + 1);
rs.close();
if (last != null) {
// This check is redundant because the last and next have
// been checked they are within limit of the start and end.
// But why not check it.
assertFalse("CURRENT value not changed over executions",
last.equals(next));
}
last = next;
// Ensure the next execution is meant to get a different value
Thread.sleep(SLEEP_TIME);
}
psI.close();
}
psQ.close();
s.close();
}
/**
* Execute a query that uses CURRENT expressions directly.
* The time returned for these values should be between the
* start of execution and after the return from the first rs.next().
* @param sqlType
* @param s
* @param sql
* @param columns
* @param expectedCount
* @throws SQLException
*/
private void checkCurrentQuery(int sqlType,
Statement s, String sql, int[] columns, int expectedCount)
throws SQLException
{
long start = System.currentTimeMillis();
ResultSet rs = s.executeQuery(sql);
rs.next();
long end = System.currentTimeMillis();
checkCurrentMultiple(sqlType, start, end, rs,
columns, expectedCount);
rs.close();
}
/**
* Check the validity of all CURRENT time values returned and
* that they are identical.
* @param jdbcType Types.TIME or TIMESTAMP
* @param start Start of window for valid value.
* @param end End of window for valid value.
* @param rs Result set positioned on row.
* @param columns Columns holding current values.
* @param expectedCount Total number of values exected to see
* (row count times column count)
* @throws SQLException
*/
private Object checkCurrentMultiple(int jdbcType, long start, long end, ResultSet rs,
int[] columns, int expectedCount) throws SQLException
{
switch (jdbcType)
{
case Types.TIME:
return checkCurrentTimeMultiple(start, end, rs, columns, expectedCount);
case Types.TIMESTAMP:
return checkCurrentTimestampMultiple(start, end, rs, columns, expectedCount);
default:
fail("Unexpected type " + jdbcType);
return null;
}
}
/**
* Check a set of rows and columns with values set to CURRENT TIME
* in a single statement are the same.
* @param start Start time for the statement that set the values.
* @param end End time for the statement that set the values.
* @param rs ResultSet positioned on the first row.
* @param columns Set of columns holding the TIME values
* @param expectedCount Number of values we are execpted to check.
* @throws SQLException
*/
private Time checkCurrentTimeMultiple(long start, long end, ResultSet rs,
int[] columns, int expectedCount) throws SQLException
{
// Result set is positioned on starting row
// Since all values must be the same since they are based upon
// CURRENT TIME from a single statement, pick one as the base
// and compare the rest to it.
Time base = checkCurrentTimeValue(start, end, rs, columns[0]);
assertNotNull(base);
int count = 1;
// check the remaining columns on this row.
for (int i = 1; i < columns.length; i++)
{
Time t = checkCurrentTimeValue(start, end, rs, columns[i]);
assertEquals("CURENT TIME changed during execution", base, t);
count++;
}
// now check all columns on any remaining rows
while (rs.next()) {
for (int i = 0; i < columns.length; i++)
{
Time t = checkCurrentTimeValue(start, end, rs, columns[i]);
assertEquals("CURENT TIME changed during execution", base, t);
count++;
}
}
assertEquals(expectedCount, count);
return base;
}
/**
* Check a set of rows and columns with values set to CURRENT TIMESTAMP
* in a single statement are the same.
* @param start Start time for the statement that set the values.
* @param end End time for the statement that set the values.
* @param rs ResultSet positioned on the first row.
* @param columns Set of columns holding the TIME values
* @param expectedCount Number of values we are execpted to check.
* @throws SQLException
*/
private Timestamp checkCurrentTimestampMultiple(long start, long end, ResultSet rs,
int[] columns, int expectedCount) throws SQLException
{
// Result set is positioned on starting row
// Since all values must be the same since they are based upon
// CURRENT TIME from a single statement, pick one as the base
// and compare the rest to it.
Timestamp base = checkCurrentTimestampValue(start, end, rs, columns[0]);
assertNotNull(base);
int count = 1;
// check the remaining columns on this row.
for (int i = 1; i < columns.length; i++)
{
Timestamp ts = checkCurrentTimestampValue(start, end, rs, columns[i]);
assertEquals("CURENT TIMESTAMP changed during execution", base, ts);
count++;
}
// now check all columns on any remaining rows
while (rs.next()) {
for (int i = 0; i < columns.length; i++)
{
Timestamp ts = checkCurrentTimestampValue(start, end, rs, columns[i]);
assertEquals("CURENT TIMESTAMP changed during execution", base, ts);
count++;
}
}
assertEquals(expectedCount, count);
return base;
}
/**
* Check the consistency of a ResultSet column that returns
* a TIME value. Can be used for any column of type TIME.
*
* @param rs ResultSet holding the column, positioned on a row
* @param column Column with the TIME value.
* @return Returns the Time object obtained from the column.
* @throws SQLException
*/
private Time checkTimeValue(ResultSet rs, int column) throws SQLException
{
assertEquals(java.sql.Types.TIME, rs.getMetaData().getColumnType(column));
try {
rs.getDate(column);
fail("ResultSet.getDate() succeeded on TIME column");
} catch (SQLException e) {
assertSQLState("22005", e);
}
Time tv = rs.getTime(column);
assertEquals(tv == null, rs.wasNull());
Object ov = rs.getObject(column);
assertEquals(ov == null, rs.wasNull());
if (tv == null) {
assertNull(ov);
return null;
}
assertTrue(ov instanceof java.sql.Time);
assertEquals(tv, ov);
// Check the date portion is set to 1970/01/01
assertTime1970(tv);
cal.clear();
cal.setTime(tv);
// Check the milli-seconds portion is 0
// Derby does not support that precision in TIME
assertEquals(0, cal.get(Calendar.MILLISECOND));
long now = System.currentTimeMillis();
Timestamp tsv = rs.getTimestamp(column);
long now2 = System.currentTimeMillis();
assertNotNull(tsv);
assertFalse(rs.wasNull());
// Check if the timestamp is as expected. Skip the check if the test
// runs around midnight, since we don't know which date to expect in
// that case.
if (isDateEqual(now, new Timestamp(now2))) {
// When reading a TIME value with getTimestamp(), the date
// component of the timestamp will be based on the current date.
// See DERBY-889.
cal.clear();
cal.setTimeInMillis(now);
int thisYear = cal.get(Calendar.YEAR);
int thisMonth = cal.get(Calendar.MONTH);
int thisDate = cal.get(Calendar.DATE);
// Construct a new timestamp based on the time component of the
// TIME value and the date component of the current time.
cal.clear();
cal.setTime(tv);
cal.set(thisYear, thisMonth, thisDate);
// Verify that the timestamp returned from getTimestamp() is as
// expected.
Timestamp expectedValue = new Timestamp(cal.getTimeInMillis());
assertTimeEqual(expectedValue, tsv);
}
// DERBY-1811, DERBY-889 being fixed could add tests
// Check the returned date portion is the current date
// using the value from 'now' and 'now2'. Double check
// just in case this test runs at midnight.
if (!(isDateEqual(now, tsv) || isDateEqual(now2, tsv)))
{
fail("TIME to java.sql.Timestamp does not contain current date " + tsv);
}
String sv = rs.getString(column);
assertNotNull(sv);
assertFalse(rs.wasNull());
// Assert the string converted back into a Time matches the Time returned.
assertEquals("ResultSet String converted to java.sql.Time mismatch",
tv, getTime19700101(sv, cal));
return tv;
}
/**
* Check the consistency of a ResultSet column that returns
* a TIMESTAMP value. Can be used for any column of type TIMESTAMP.
*
* @param rs ResultSet holding the column, positioned on a row
* @param column Column with the TIMESTAMP value.
* @return Returns the Time object obtained from the column.
* @throws SQLException
*/
private Timestamp checkTimestampValue(ResultSet rs, int column) throws SQLException
{
assertEquals(java.sql.Types.TIMESTAMP,
rs.getMetaData().getColumnType(column));
Timestamp tsv = rs.getTimestamp(column);
assertEquals(tsv == null, rs.wasNull());
Object ov = rs.getObject(column);
assertEquals(ov == null, rs.wasNull());
if (tsv == null) {
assertNull(ov);
return null;
}
assertTrue(ov instanceof java.sql.Timestamp);
assertEquals(tsv, ov);
Time tv = rs.getTime(column);
assertNotNull(tv);
assertFalse(rs.wasNull());
// Check the date portion is set to 1970/01/01
assertTime1970(tv);
// Check the TIME portion is set to the same as tv
assertTimeEqual(tv, tsv);
String sv = rs.getString(column);
assertNotNull(sv);
assertFalse(rs.wasNull());
// Assert the string converted back into a Time matches the Time returned.
assertEquals("ResultSet String converted to java.sql.Timestamp mismatch",
tsv, Timestamp.valueOf(sv));
return tsv;
}
/**
* Check the consistency of a ResultSet column that returns
* CURRENT TIME or a value set from CURRENT TIME.
*
* @param start Time the statement settng the value was executed
* @param end Time after first rs.next() or update statement was executed
* @param rs ResultSet holding the column, positioned on a row
* @param column Column with the timestamp.
* @return Returns the Time object obtained from the column.
* @throws SQLException
*/
private Time checkCurrentTimeValue(long start, long end,
ResultSet rs, int column) throws SQLException
{
Time tv = checkTimeValue(rs, column);
// The time returned should be between the value
// of start and end (inclusive of both)
Time st = getTime19700101(start, cal);
Time et = getTime19700101(end, cal);
if (st.after(et)) {
// Gone back in time!
// Well test was running around midnight and the
// time for the start time is equal to or before 23:59:59
// and end time is equal to or after 00:00:00
assertTrue("CURRENT TIME outside of range when test crossing midnight",
(tv.equals(st) || tv.after(st))
|| (tv.equals(et) || tv.before(et)));
}
else
{
// End time is after or equal to start time, expected case.
// The returned time must not be before the
// start time or after the end time.
assertFalse("CURRENT TIME before start of statement", tv.before(st));
assertFalse("CURRENT TIME after end of statement", tv.after(et));
}
return tv;
}
/**
* Check the consistency of a ResultSet column that returns
* CURRENT TIMESTAMP or a value set from CURRENT TIMESTAMP.
*
* @param start Time the statement settng the value was executed
* @param end Time after first rs.next() or update statement was executed
* @param rs ResultSet holding the column, positioned on a row
* @param column Column with the timestamp.
* @return Returns the Timestamp object obtained from the column.
* @throws SQLException
*/
private Timestamp checkCurrentTimestampValue(long start, long end,
ResultSet rs, int column) throws SQLException
{
Timestamp tsv = checkTimestampValue(rs, column);
// The time returned should be between the value
// of start and end (inclusive of both)
Timestamp st = new Timestamp(start);
Timestamp et = new Timestamp(end);
if (st.after(et)) {
// Gone back in time!
// Well test was running around midnight and the
// time for the start time is equal to or before 23:59:59
// and end time is equal to or after 00:00:00
assertTrue("CURRENT TIME outside of range when test crossing midnight",
(tsv.equals(st) || tsv.after(st))
|| (tsv.equals(et) || tsv.before(et)));
}
else
{
// End time is after or equal to start time, expected case.
// The returned time must not be before the
// start time or after the end time.
assertFalse("CURRENT TIME before start of statement", tsv.before(st));
assertFalse("CURRENT TIME after end of statement", tsv.after(et));
}
return tsv;
}
/**
* Create a Time object that has its date components
* set to 1970/01/01 and its time to match the time
* represented by h, m and s. This matches Derby by
* setting the milli-second component to zero.
* <BR>
* Note that the Time(long) constructor for java.sql.Time
* does *not* set the date component to 1970/01/01.
* This is a requirement for JDBC java.sql.Time values though
*/
private Time getTime19700101(int hour, int min, int sec)
{
cal.clear();
cal.set(1970, Calendar.JANUARY, 1, hour, min, sec);
cal.set(Calendar.MILLISECOND, 0);
Time to = new Time(cal.getTimeInMillis());
assertTime1970(to);
return to;
}
/**
* Create a Time object that has its date components
* set to 1970/01/01 and its time to match the time
* represented by t and cal. This matches Derby by
* setting the milli-second component to zero.
* <BR>
* Note that the Time(long) constructor for java.sql.Time
* does *not* set the date component to 1970/01/01.
* This is a requirement for JDBC java.sql.Time values though
*/
private Time getTime19700101(long t, Calendar cal)
{
cal.clear();
cal.setTimeInMillis(t);
cal.set(1970, Calendar.JANUARY, 1);
cal.set(Calendar.MILLISECOND, 0);
Time to = new Time(cal.getTimeInMillis());
assertTime1970(to);
return to;
}
/**
* Create a Time object that has its date components
* set to 1970/01/01 and its time to match the time
* represented by t and cal. This matches Derby by
* setting the milli-second component to zero.
* <BR>
* Note that the Time(long) constructor for java.sql.Time
* does *not* set the date component to 1970/01/01.
* This is a requirement for JDBC java.sql.Time values though
*/
private Time getTime19700101(String s, Calendar cal)
{
return getTime19700101(Time.valueOf(s).getTime(), cal);
}
/**
* Javadoc for java.sql.Time states the components of
* date for a java.sql.Time value must be set to January 1, 1970.
* Note that the java.sql.Time class does not enforce this,
* it is up to the driver.
* @param t
*/
private void assertTime1970(Time t) {
/* Cannot do this because all these methods
* throw IllegalArgumentException by definition,
* see java.sql.Time javadoc.
assertEquals(1970, t.getYear());
assertEquals(0, t.getMonth());
assertEquals(1, t.getDate());
*/
cal.clear();
cal.setTime(t);
assertEquals(1970, cal.get(Calendar.YEAR));
assertEquals(Calendar.JANUARY, cal.get(Calendar.MONTH));
assertEquals(1, cal.get(Calendar.DATE));
}
/**
* Assert the SQL time portion of two SQL JDBC type
* types are equal.
* @param tv1 the first time to compare
* @param tv2 the second time to compare
*/
private void assertTimeEqual(java.util.Date tv1, java.util.Date tv2)
{
cal.clear();
cal.setTime(tv1);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int min = cal.get(Calendar.MINUTE);
int sec = cal.get(Calendar.SECOND);
int ms = cal.get(Calendar.MILLISECOND);
// Check the time portion is set to the same as tv
cal.clear();
cal.setTime(tv2);
assertEquals(hour, cal.get(Calendar.HOUR_OF_DAY));
assertEquals(min, cal.get(Calendar.MINUTE));
assertEquals(sec, cal.get(Calendar.SECOND));
assertEquals(ms, cal.get(Calendar.MILLISECOND));
}
/**
* Check if the date portion of a Timestamp value
* is equal to the date portion of a time value
* represented in milli-seconds since 1970.
*/
private boolean isDateEqual(long d, Timestamp tsv)
{
cal.clear();
cal.setTimeInMillis(d);
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
cal.clear();
cal.setTime(tsv);
return day == cal.get(Calendar.DAY_OF_MONTH)
&& month == cal.get(Calendar.MONTH)
&& year == cal.get(Calendar.YEAR);
}
}
|