1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.io.IOException ; 25 26 import java.sql.CallableStatement ; 27 import java.sql.Connection ; 28 import java.sql.DriverManager ; 29 import java.sql.Statement ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.ResultSetMetaData ; 33 import java.sql.SQLException ; 34 import java.sql.SQLWarning ; 35 import java.sql.Types ; 36 import java.sql.Date ; 37 import java.sql.Time ; 38 import java.sql.Timestamp ; 39 40 import org.apache.derby.tools.ij; 41 import org.apache.derby.tools.JDBCDisplayUtil; 42 import org.apache.derbyTesting.functionTests.util.TestUtil; 43 44 49 50 public class scrollCursors2 { 51 52 private static boolean isDerbyNetClient = false; 53 54 public static void main(String [] args) { 55 boolean passed = true; 56 Connection conn = null; 57 Statement s_i_r = null; 58 59 60 try { 61 System.out.println("Test scrollCurors2 starting"); 62 63 isDerbyNetClient = TestUtil.isDerbyNetClientFramework(); 64 ij.getPropertyArg(args); 67 conn = ij.startJBMS(); 68 cleanUp(conn); 69 conn.setAutoCommit(false); 70 71 72 passed = passed && setUpTest(conn); 73 74 passed = passed && forwardOnlyNegative(conn); 76 77 passed = passed && forwardOnlyPositive(conn); 79 80 passed = passed && scrollSensitiveTest(conn); 82 83 passed = passed && scrollInsensitivePositive(conn); 85 86 passed = passed && scrollInsensitiveNegative(conn); 88 89 passed = passed && testCallableStatements(conn); 91 92 passed = passed && getMetaDataTests(conn); 94 95 passed = passed && scrollVerifyMaxRowWithFetchSize(conn, 10, 10); 97 passed = passed && scrollVerifyMaxRowWithFetchSize(conn, 10, 5); 98 passed = passed && scrollVerifyMaxRowWithFetchSize(conn, 10, 0); 99 passed = passed && scrollVerifyMaxRowWithFetchSize(conn, 0, 0); 100 passed = passed && scrollVerifyMaxRowWithFetchSize(conn, 0, 5); 101 passed = passed && scrollVerifyMaxRowWithFetchSize(conn, 0, 10); 102 passed = passed && scrollVerifyMaxRowWithFetchSize(conn, 0, 15); 103 104 } 105 catch (SQLException se) 106 { 107 passed = false; 108 dumpSQLExceptions(se); 109 } 110 catch (Throwable e) 111 { 112 System.out.println("FAIL -- unexpected exception caught in main():\n"); 113 System.out.println(e.getMessage()); 114 e.printStackTrace(); 115 passed = false; 116 } 117 finally 118 { 119 120 121 passed = passed && cleanUp(conn, s_i_r); 122 } 123 124 if (passed) 125 System.out.println("PASS"); 126 127 System.out.println("Test scrollCursors2 finished"); 128 } 129 130 static private void dumpSQLExceptions (SQLException se) { 131 System.out.println("FAIL -- unexpected exception"); 132 while (se != null) { 133 System.out.print("SQLSTATE("+se.getSQLState()+"):"); 134 se.printStackTrace(); 135 se = se.getNextException(); 136 } 137 } 138 139 150 151 static boolean setUpTest(Connection conn) 152 throws SQLException 153 { 154 boolean passed = true; 155 int rows; 156 PreparedStatement ps; 157 Statement s_i_r; 158 159 s_i_r = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 160 ResultSet.CONCUR_READ_ONLY); 161 162 163 s_i_r.execute("create table t (i int, c50 char(50))"); 164 165 166 s_i_r.execute("insert into t (i) values (2), (3), (4), (5), (6)"); 167 s_i_r.execute("update t set c50 = RTRIM(CAST (i AS CHAR(50)))"); 168 s_i_r.close(); 169 170 return passed; 171 } 172 173 184 185 static boolean forwardOnlyNegative( Connection conn) 186 throws SQLException 187 { 188 boolean passed = true; 189 PreparedStatement ps_f_r = null; 190 ResultSet rs; 191 SQLWarning warning; 192 Statement s_f_r = null; 193 194 s_f_r = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 195 ResultSet.CONCUR_READ_ONLY); 196 warning = conn.getWarnings(); 198 while (warning != null) 199 { 200 System.out.println("warning = " + warning); 201 warning = warning.getNextWarning(); 202 } 203 conn.clearWarnings(); 204 205 try 207 { 208 s_f_r.setMaxRows(-1); 209 System.out.println("setMaxRows(-1) expected to fail"); 211 passed = false; 212 } 213 catch (SQLException sqle) 214 { 215 216 if (!isDerbyNetClient) { 217 passed = passed && checkException(sqle, "XJ063"); 218 } else { 219 System.out.println(sqle.getMessage()); 220 } 221 } 222 if (s_f_r.getMaxRows() != 0) 224 { 225 System.out.println("getMaxRows() expected to return 0"); 226 passed = false; 227 } 228 229 rs = s_f_r.executeQuery("select * from t"); 232 if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) 233 { 234 System.out.println("cursor type = " + rs.getType() + 235 ", not " + ResultSet.TYPE_FORWARD_ONLY); 236 } 237 if (rs.getConcurrency() != ResultSet.CONCUR_READ_ONLY) 238 { 239 System.out.println("concurrency = " + rs.getConcurrency() + 240 ", not " + ResultSet.CONCUR_READ_ONLY); 241 } 242 243 try 245 { 246 rs.first(); 247 System.out.println("first() expected to fail"); 249 passed = false; 250 } 251 catch (SQLException sqle) 252 { 253 254 if (!isDerbyNetClient) { 255 passed = passed && checkException(sqle, "XJ061"); 256 } else { 257 System.out.println(sqle.getMessage()); 258 } 259 260 } 261 try 262 { 263 rs.beforeFirst(); 264 System.out.println("beforeFirst() expected to fail"); 266 passed = false; 267 } 268 catch (SQLException sqle) 269 { 270 271 if (!isDerbyNetClient) { 272 passed = passed && checkException(sqle, "XJ061"); 273 } else { 274 System.out.println(sqle.getMessage()); 275 } 276 277 } 278 try 279 { 280 rs.isBeforeFirst(); 281 System.out.println("isBeforeFirst() expected to fail"); 283 passed = false; 284 } 285 catch (SQLException sqle) 286 { 287 288 if (!isDerbyNetClient) { 289 passed = passed && checkException(sqle, "XJ061"); 290 } else { 291 System.out.println(sqle.getMessage()); 292 } 293 294 } 295 try 296 { 297 rs.isAfterLast(); 298 System.out.println("isAfterLast() expected to fail"); 300 passed = false; 301 } 302 catch (SQLException sqle) 303 { 304 305 if (!isDerbyNetClient) { 306 passed = passed && checkException(sqle, "XJ061"); 307 } else { 308 System.out.println(sqle.getMessage()); 309 } 310 311 } 312 try 313 { 314 rs.isFirst(); 315 System.out.println("isFirst() expected to fail"); 317 passed = false; 318 } 319 catch (SQLException sqle) 320 { 321 322 if (!isDerbyNetClient) { 323 passed = passed && checkException(sqle, "XJ061"); 324 } else { 325 System.out.println(sqle.getMessage()); 326 } 327 } 328 try 329 { 330 rs.isLast(); 331 System.out.println("isLast() expected to fail"); 333 passed = false; 334 } 335 catch (SQLException sqle) 336 { 337 338 if (!isDerbyNetClient) { 339 passed = passed && checkException(sqle, "XJ061"); 340 } else { 341 System.out.println(sqle.getMessage()); 342 } 343 } 344 try 345 { 346 rs.absolute(1); 347 System.out.println("absolute() expected to fail"); 349 passed = false; 350 } 351 catch (SQLException sqle) 352 { 353 354 if (!isDerbyNetClient) { 355 passed = passed && checkException(sqle, "XJ061"); 356 } else { 357 System.out.println(sqle.getMessage()); 358 } 359 } 360 try 361 { 362 rs.relative(1); 363 System.out.println("relative() expected to fail"); 365 passed = false; 366 } 367 catch (SQLException sqle) 368 { 369 370 if (!isDerbyNetClient) { 371 passed = passed && checkException(sqle, "XJ061"); 372 } else { 373 System.out.println(sqle.getMessage()); 374 } 375 } 376 377 try 379 { 380 rs.setFetchDirection(ResultSet.FETCH_FORWARD); 381 System.out.println("setFetchDirection() expected to fail"); 383 passed = false; 384 } 385 catch (SQLException sqle) 386 { 387 388 if (!isDerbyNetClient) { 389 passed = passed && checkException(sqle, "XJ061"); 390 } else { 391 System.out.println(sqle.getMessage()); 392 } 393 } 394 395 398 if ((rs.getFetchSize() != 1 && !isDerbyNetClient) || (rs.getFetchSize() != 0 && isDerbyNetClient)) 399 { 400 if (!isDerbyNetClient) { 401 System.out.println("getFetchSize() expected to return 1"); 402 } else { 403 System.out.println("getFetchSize() expected to return 0"); 404 } 405 passed = false; 406 } 407 rs.setFetchSize(5); 408 if (rs.getFetchSize() != 5) 409 { 410 System.out.println("getFetchSize() expected to return 5"); 411 passed = false; 412 } 413 414 if (rs.getFetchDirection() != ResultSet.FETCH_FORWARD) 415 { 416 System.out.println( 417 "getFetchDirection() expected to return FETCH_FORWARD, not " + 418 rs.getFetchDirection()); 419 passed = false; 420 } 421 422 rs.close(); 423 s_f_r.close(); 424 425 ps_f_r = conn.prepareStatement( 426 "select * from t", 427 ResultSet.TYPE_FORWARD_ONLY, 428 ResultSet.CONCUR_READ_ONLY); 429 warning = conn.getWarnings(); 431 while (warning != null) 432 { 433 System.out.println("warning = " + warning); 434 warning = warning.getNextWarning(); 435 } 436 conn.clearWarnings(); 437 438 rs = ps_f_r.executeQuery(); 441 if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) 442 { 443 System.out.println("cursor type = " + rs.getType() + 444 ", not " + ResultSet.TYPE_FORWARD_ONLY); 445 } 446 if (rs.getConcurrency() != ResultSet.CONCUR_READ_ONLY) 447 { 448 System.out.println("concurrency = " + rs.getConcurrency() + 449 ", not " + ResultSet.CONCUR_READ_ONLY); 450 } 451 452 try 454 { 455 rs.first(); 456 System.out.println("first() expected to fail"); 458 passed = false; 459 } 460 catch (SQLException sqle) 461 { 462 463 if (!isDerbyNetClient) { 464 passed = passed && checkException(sqle, "XJ061"); 465 } else { 466 System.out.println(sqle.getMessage()); 467 } 468 469 } 470 rs.close(); 471 ps_f_r.close(); 472 473 return passed; 474 } 475 476 487 488 static boolean forwardOnlyPositive( Connection conn) 489 throws SQLException 490 { 491 boolean passed = true; 492 ResultSet rs; 493 SQLWarning warning; 494 Statement s_f_r = null; 495 496 s_f_r = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 497 ResultSet.CONCUR_READ_ONLY); 498 warning = conn.getWarnings(); 500 while (warning != null) 501 { 502 System.out.println("warning = " + warning); 503 warning = warning.getNextWarning(); 504 } 505 conn.clearWarnings(); 506 507 s_f_r.setMaxRows(5); 509 if (s_f_r.getMaxRows() != 5) 510 { 511 System.out.println("getMaxRows() expected to return 5"); 512 passed = false; 513 } 514 rs = s_f_r.executeQuery("values 1, 2, 3, 4, 5, 6"); 515 if (rs == null) 516 { 517 System.out.println("rs expected to be non-null."); 518 passed = false; 519 } 520 for (int index = 1; index < 6; index++) 522 { 523 if (! rs.next()) 524 { 525 System.out.println("rs.next() failed, index = " + index); 526 passed = false; 527 break; 528 } 529 } 530 if (rs.next()) 532 { 533 System.out.println("rs.next() failed, should not have seen 6th row."); 534 passed = false; 535 } 536 rs.close(); 537 s_f_r.close(); 538 return passed; 539 } 540 541 554 555 static boolean scrollSensitiveTest( Connection conn) 556 throws SQLException 557 { 558 ResultSet rs; 559 SQLWarning warning; 560 Statement s_s_r = null; Statement s_s_u = null; 563 564 s_s_r = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 565 ResultSet.CONCUR_READ_ONLY); 566 567 warning = conn.getWarnings(); 569 while (warning != null) 570 { 571 System.out.println("warning = " + warning); 572 warning = warning.getNextWarning(); 573 } 574 conn.clearWarnings(); 575 576 rs = s_s_r.executeQuery("select * from t"); 579 if (rs.getType() != ResultSet.TYPE_SCROLL_INSENSITIVE) 580 { 581 System.out.println("cursor type = " + rs.getType() + 582 ", not " + ResultSet.TYPE_SCROLL_INSENSITIVE); 583 } 584 if (rs.getConcurrency() != ResultSet.CONCUR_READ_ONLY) 585 { 586 System.out.println("concurrency = " + rs.getConcurrency() + 587 ", not " + ResultSet.CONCUR_READ_ONLY); 588 } 589 rs.close(); 590 591 s_s_r.close(); 593 594 s_s_u = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 595 ResultSet.CONCUR_UPDATABLE); 596 warning = conn.getWarnings(); 599 while (warning != null) 600 { 601 System.out.println("warning = " + warning); 602 warning = warning.getNextWarning(); 603 } 604 conn.clearWarnings(); 605 606 rs = s_s_u.executeQuery("select * from t"); 609 if (rs.getType() != ResultSet.TYPE_SCROLL_INSENSITIVE) 610 { 611 System.out.println("cursor type = " + rs.getType() + 612 ", not " + ResultSet.TYPE_SCROLL_INSENSITIVE); 613 } 614 if (rs.getConcurrency() != ResultSet.CONCUR_UPDATABLE) 615 { 616 System.out.println("concurrency = " + rs.getConcurrency() + 617 ", not " + ResultSet.CONCUR_UPDATABLE); 618 } 619 rs.close(); 620 621 622 return true; 623 } 624 625 634 static boolean scrollInsensitivePositive( Connection conn) 635 throws SQLException 636 { 637 boolean passed = true; 638 PreparedStatement ps_i_r = null; 639 ResultSet rs; 640 SQLWarning warning; 641 Statement s_i_r = null; 643 644 s_i_r = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 645 ResultSet.CONCUR_READ_ONLY); 646 647 warning = conn.getWarnings(); 650 while (warning != null) 651 { 652 System.out.println("unexpected warning = " + warning); 653 warning = warning.getNextWarning(); 654 passed = false; 655 } 656 conn.clearWarnings(); 657 658 rs = s_i_r.executeQuery("select * from t"); 660 if (rs.getType() != ResultSet.TYPE_SCROLL_INSENSITIVE) 662 { 663 System.out.println( 664 "rs.getType() expected to return TYPE_SCROLL_INSENSITIVE, not " + 665 rs.getType()); 666 passed = false; 667 } 668 if (rs.getConcurrency() != ResultSet.CONCUR_READ_ONLY) 669 { 670 System.out.println( 671 "rs.getConcurrency() expected to return CONCUR_READ_ONLY, not " + 672 rs.getConcurrency()); 673 passed = false; 674 } 675 676 if (! rs.isBeforeFirst()) 678 { 679 System.out.println("expected to be before the 1st row"); 680 passed = false; 681 } 682 if (rs.absolute(0)) 683 { 684 System.out.println("absolute(0) expected to return false"); 685 passed = false; 686 } 687 if (! rs.isBeforeFirst()) 688 { 689 System.out.println("still expected to be before the 1st row"); 690 passed = false; 691 } 692 if (! rs.first()) 694 { 695 System.out.println("expected first() to succeed"); 696 passed = false; 697 } 698 if (rs.getInt(1) != 2) 699 { 700 System.out.println( 701 "rs.getInt(1) expected to return 2, not " + rs.getInt(1)); 702 passed = false; 703 } 704 if (! rs.isFirst()) 705 { 706 System.out.println("expected to be on the 1st row"); 707 passed = false; 708 } 709 rs.beforeFirst(); 711 if (! rs.isBeforeFirst()) 712 { 713 System.out.println("expected to be before the 1st row"); 714 passed = false; 715 } 716 if (! rs.last()) 718 { 719 System.out.println("expected last() to succeed"); 720 passed = false; 721 } 722 if (! rs.isLast()) 723 { 724 System.out.println("expected to be on the last row"); 725 passed = false; 726 } 727 if (rs.isAfterLast()) 728 { 729 System.out.println("not expected to be after the last row"); 730 passed = false; 731 } 732 if (rs.getInt(1) != 6) 733 { 734 System.out.println( 735 "rs.getInt(1) expected to return 6, not " + rs.getInt(1)); 736 passed = false; 737 } 738 if (rs.next()) 739 { 740 System.out.println("not expected to find another row"); 741 passed = false; 742 } 743 if (! rs.isAfterLast()) 744 { 745 System.out.println("expected to be after the last row"); 746 passed = false; 747 } 748 749 if (rs.isLast()) 752 { 753 System.out.println("not expected to be on the last row"); 754 passed = false; 755 } 756 if (rs.isFirst()) 757 { 758 System.out.println("not expected to be on the first row"); 759 passed = false; 760 } 761 if (rs.isBeforeFirst()) 762 { 763 System.out.println("not expected to be before the first row"); 764 passed = false; 765 } 766 767 if (rs.getFetchDirection() != ResultSet.FETCH_FORWARD) 769 { 770 System.out.println( 771 "getFetchDirection() expected to return FETCH_FORWARD, not " + 772 rs.getFetchDirection()); 773 passed = false; 774 } 775 rs.setFetchDirection(ResultSet.FETCH_UNKNOWN); 776 if (rs.getFetchDirection() != ResultSet.FETCH_UNKNOWN) 777 { 778 System.out.println( 779 "getFetchDirection() expected to return FETCH_UNKNOWN, not " + 780 rs.getFetchDirection()); 781 passed = false; 782 } 783 784 if ( 786 (rs.getFetchSize() != 1 && !isDerbyNetClient) || 787 (rs.getFetchSize() != 64 && isDerbyNetClient)) 788 { 789 if (!isDerbyNetClient) { 790 System.out.println( 791 "getFetchSize() expected to return 1, not " + rs.getFetchSize()); 792 } else { 793 System.out.println( 794 "getFetchSize() expected to return 64, not " + rs.getFetchSize()); 795 } 796 passed = false; 797 } 798 rs.setFetchSize(5); 799 if (rs.getFetchSize() != 5) 800 { 801 System.out.println( 802 "getFetchSize() expected to return 5, not " + rs.getFetchSize()); 803 passed = false; 804 } 805 rs.setFetchSize(0); 808 if ( 809 (rs.getFetchSize() != 5 && !isDerbyNetClient) || 810 (rs.getFetchSize() != 64 && isDerbyNetClient)) 811 { 812 if (!isDerbyNetClient) { 813 System.out.println( 814 "getFetchSize() expected to return 5, not " + rs.getFetchSize()); 815 } else { 816 System.out.println( 817 "getFetchSize() expected to return 64, not " + rs.getFetchSize()); 818 } 819 820 passed = false; 821 } 822 rs.close(); 824 825 826 rs = s_i_r.executeQuery("select * from t where 1=0"); 828 rs.afterLast(); 829 if (rs.isAfterLast()) { 830 System.out.println("afterLast() on empty RS should be no-op"); 831 } 832 833 rs.beforeFirst(); 834 if (rs.isBeforeFirst()) { 835 System.out.println("beforeFirst() on empty RS should be no-op"); 836 } 837 838 rs.close(); 839 840 ps_i_r = conn.prepareStatement( 841 "select * from t", 842 ResultSet.TYPE_SCROLL_INSENSITIVE, 843 ResultSet.CONCUR_READ_ONLY); 844 845 warning = conn.getWarnings(); 848 while (warning != null) 849 { 850 System.out.println("unexpected warning = " + warning); 851 warning = warning.getNextWarning(); 852 passed = false; 853 } 854 conn.clearWarnings(); 855 856 rs = ps_i_r.executeQuery(); 857 rs.last(); 859 rs.close(); 860 ps_i_r.close(); 861 862 if (s_i_r.getMaxRows() != 0) 864 { 865 System.out.println("getMaxRows() expected to return 0"); 866 passed = false; 867 } 868 s_i_r.setMaxRows(5); 869 if (s_i_r.getMaxRows() != 5) 870 { 871 System.out.println("getMaxRows() expected to return 5"); 872 passed = false; 873 } 874 rs = s_i_r.executeQuery("values 1, 2, 3, 4, 5, 6"); 875 if (rs == null) 876 { 877 System.out.println("rs expected to be non-null."); 878 passed = false; 879 } 880 for (int index = 1; index < 6; index++) 882 { 883 if (! rs.next()) 884 { 885 System.out.println("rs.next() failed, index = " + index); 886 passed = false; 887 break; 888 } 889 } 890 if (rs.next()) 892 { 893 System.out.println("rs.next() failed, should not have seen 6th row."); 894 passed = false; 895 } 896 rs.close(); 897 rs = s_i_r.executeQuery("values 1, 2, 3, 4, 5, 6"); 899 if (rs == null) 900 { 901 System.out.println("rs expected to be non-null."); 902 passed = false; 903 } 904 if (!rs.last()) 905 { 906 System.out.println("rs.last() failed."); 907 passed = false; 908 } 909 for (int index = 1; index < 5; index++) 911 { 912 if (! rs.previous()) 913 { 914 System.out.println("rs.previous() failed, index = " + index); 915 passed = false; 916 break; 917 } 918 } 919 if (rs.previous()) 921 { 922 System.out.println("rs.previous() failed, should not have seen 6th row."); 923 passed = false; 924 } 925 rs.close(); 926 rs = s_i_r.executeQuery("values 1, 2, 3, 4, 5, 6"); 927 if (rs == null) 928 { 929 System.out.println("rs expected to be non-null."); 930 passed = false; 931 } 932 rs.afterLast(); 933 for (int index = 1; index < 6; index++) 935 { 936 if (! rs.previous()) 937 { 938 System.out.println("rs.previous() failed, index = " + index); 939 passed = false; 940 break; 941 } 942 } 943 if (rs.previous()) 945 { 946 System.out.println("rs.previous() failed, should not have seen 6th row."); 947 passed = false; 948 } 949 rs.close(); 950 s_i_r.setMaxRows(0); 952 rs = s_i_r.executeQuery("values 1, 2, 3, 4, 5, 6"); 953 if (rs == null) 954 { 955 System.out.println("rs expected to be non-null."); 956 passed = false; 957 } 958 for (int index = 1; index < 7; index++) 960 { 961 if (! rs.next()) 962 { 963 System.out.println("rs.next() failed, index = " + index); 964 passed = false; 965 break; 966 } 967 } 968 if (rs.next()) 970 { 971 System.out.println("rs.next() failed, should not have seen another row."); 972 passed = false; 973 } 974 rs.close(); 975 976 return passed; 977 } 978 979 988 static boolean scrollInsensitiveNegative( Connection conn) 989 throws SQLException 990 { 991 boolean passed = true; 992 ResultSet rs; 993 SQLWarning warning; 994 Statement s_i_r = null; 996 s_i_r = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 997 ResultSet.CONCUR_READ_ONLY); 998 999 warning = conn.getWarnings(); 1002 while (warning != null) 1003 { 1004 System.out.println("unexpected warning = " + warning); 1005 warning = warning.getNextWarning(); 1006 passed = false; 1007 } 1008 conn.clearWarnings(); 1009 1010 try 1012 { 1013 s_i_r.setMaxRows(-1); 1014 System.out.println("setMaxRows(-1) expected to fail"); 1016 passed = false; 1017 } 1018 catch (SQLException sqle) 1019 { 1020 1021 if (!isDerbyNetClient) { 1022 passed = passed && checkException(sqle, "XJ063"); 1023 } else { 1024 System.out.println(sqle.getMessage()); 1025 } 1026 1027 } 1028 if (s_i_r.getMaxRows() != 0) 1030 { 1031 System.out.println("getMaxRows() expected to return 0"); 1032 passed = false; 1033 } 1034 1035 rs = s_i_r.executeQuery("select * from t where 1=0"); 1037 if (rs.isBeforeFirst()) 1040 { 1041 System.out.println("isBeforeFirst() expected to return false on empty result set"); 1042 passed = false; 1043 } 1044 if (rs.next()) 1045 { 1046 System.out.println("rs.next() expected to show result set is empty"); 1047 passed = false; 1048 } 1049 if (rs.previous()) 1050 { 1051 System.out.println("rs.previous() expected to show result set is empty"); 1052 passed = false; 1053 } 1054 if (rs.isAfterLast()) 1055 { 1056 System.out.println("isAfterLast() expected to return false on empty result set"); 1057 passed = false; 1058 } 1059 if (rs.isFirst()) 1060 { 1061 System.out.println("isFirst() expected to return false on empty result set"); 1062 passed = false; 1063 } 1064 if (rs.isLast()) 1065 { 1066 System.out.println("isLast() expected to return false on empty result set"); 1067 passed = false; 1068 } 1069 1070 if (rs.relative(0)) 1071 { 1072 System.out.println("relative(0) expected to return false on empty result set"); 1073 passed = false; 1074 } 1075 1076 if (rs.relative(1)) 1077 { 1078 System.out.println("relative(1) expected to return false on empty result set"); 1079 passed = false; 1080 } 1081 1082 if (rs.relative(-1)) 1083 { 1084 System.out.println("relative(-1) expected to return false on empty result set"); 1085 passed = false; 1086 } 1087 1088 if (rs.absolute(0)) 1089 { 1090 System.out.println("absolute(0) expected to return false on empty result set"); 1091 passed = false; 1092 } 1093 if (rs.absolute(1)) 1094 { 1095 System.out.println("absolute(1) expected to return false on empty result set"); 1096 passed = false; 1097 } 1098 1099 if (rs.absolute(-1)) 1100 { 1101 System.out.println("absolute(-1) expected to return false on empty result set"); 1102 passed = false; 1103 } 1104 1105 1106 rs.close(); 1107 1109 rs = s_i_r.executeQuery("select * from t"); 1111 try 1113 { 1114 rs.setFetchSize(-5); 1115 System.out.println("setFetchSize(-5) expected to fail"); 1116 passed = false; 1117 } 1118 catch (SQLException sqle) 1119 { 1120 1121 if (!isDerbyNetClient) { 1122 passed = passed && checkException(sqle, "XJ062"); 1123 } else { 1124 System.out.println(sqle.getMessage()); 1125 } 1126 1127 } 1128 1129 s_i_r.close(); 1130 1131 return passed; 1132 } 1133 1134 1135 1144 1145 public static boolean testCallableStatements( Connection conn) 1146 throws SQLException 1147 { 1148 boolean passed = true; 1149 int warningCount = 0; 1150 SQLWarning warning; 1151 CallableStatement cs_s_r = null; CallableStatement cs_s_u = null; CallableStatement cs_i_r = null; CallableStatement cs_f_r = null; 1156 cs_s_r = conn.prepareCall( 1157 "values cast (? as Integer)", 1158 ResultSet.TYPE_SCROLL_SENSITIVE, 1159 ResultSet.CONCUR_READ_ONLY); 1160 1161 warning = conn.getWarnings(); 1163 while (warning != null) 1164 { 1165 System.out.println("warning = " + warning); 1166 warning = warning.getNextWarning(); 1167 warningCount++; 1168 } 1169 if (warningCount != 1) 1170 { 1171 System.out.println("warningCount expected to be 1, not " + 1172 warningCount); 1173 passed = false; 1174 } 1175 conn.clearWarnings(); 1176 cs_s_r.close(); 1177 1178 cs_s_u = conn.prepareCall( 1179 "values cast (? as Integer)", 1180 ResultSet.TYPE_SCROLL_SENSITIVE, 1181 ResultSet.CONCUR_UPDATABLE); 1182 1183 warningCount = 0; 1185 warning = conn.getWarnings(); 1186 while (warning != null) 1187 { 1188 System.out.println("warning = " + warning); 1189 warning = warning.getNextWarning(); 1190 warningCount++; 1191 } 1192 if (warningCount != 1) 1194 { 1195 System.out.println("warningCount expected to be 1, not " + warningCount); 1196 passed = false; 1197 } 1198 conn.clearWarnings(); 1199 cs_s_u.close(); 1200 1201 cs_i_r = conn.prepareCall( 1202 "values cast (? as Integer)", 1203 ResultSet.TYPE_SCROLL_INSENSITIVE, 1204 ResultSet.CONCUR_READ_ONLY); 1205 1206 warningCount = 0; 1208 warning = conn.getWarnings(); 1209 while (warning != null) 1210 { 1211 System.out.println("warning = " + warning); 1212 warning = warning.getNextWarning(); 1213 warningCount++; 1214 } 1215 if (warningCount != 0) 1216 { 1217 System.out.println("warningCount expected to be 0, not " + 1218 warningCount); 1219 passed = false; 1220 } 1221 conn.clearWarnings(); 1222 cs_i_r.close(); 1223 1224 cs_f_r = conn.prepareCall( 1225 "values cast (? as Integer)", 1226 ResultSet.TYPE_FORWARD_ONLY, 1227 ResultSet.CONCUR_READ_ONLY); 1228 1229 warningCount = 0; 1231 warning = conn.getWarnings(); 1232 while (warning != null) 1233 { 1234 System.out.println("warning = " + warning); 1235 warning = warning.getNextWarning(); 1236 warningCount++; 1237 } 1238 if (warningCount != 0) 1239 { 1240 System.out.println("warningCount expected to be 0, not " + warningCount); 1241 passed = false; 1242 } 1243 conn.clearWarnings(); 1244 cs_f_r.close(); 1245 1246 return passed; 1247 } 1248 1249 1250 1259 static boolean getMetaDataTests( Connection conn) 1260 throws SQLException 1261 { 1262 boolean passed = true; 1263 PreparedStatement ps_f_r = null; ResultSet rs; 1265 ResultSetMetaData rsmd_ps; 1266 ResultSetMetaData rsmd_rs; 1267 SQLWarning warning; 1268 1269 1270 ps_f_r = conn.prepareStatement( 1271 "select c50, i, 43 from t", 1272 ResultSet.TYPE_FORWARD_ONLY, 1273 ResultSet.CONCUR_READ_ONLY); 1274 1275 rsmd_ps = ps_f_r.getMetaData(); 1276 if (rsmd_ps == null) 1277 { 1278 System.out.println("rsmd_ps expected to be non-null"); 1279 return false; 1280 } 1281 1282 rs = ps_f_r.executeQuery(); 1284 rsmd_rs = rs.getMetaData(); 1285 if (rsmd_rs == null) 1286 { 1287 System.out.println("rsmd_rs expected to be non-null"); 1288 return false; 1289 } 1290 1291 if (rsmd_ps.getColumnCount() != rsmd_rs.getColumnCount()) 1293 { 1294 System.out.println("column count expected to be same, not " + 1295 rsmd_ps.getColumnCount() + 1296 " and " + 1297 rsmd_rs.getColumnCount()); 1298 passed = false; 1299 } 1300 1301 if (! rsmd_ps.getColumnName(2).equals(rsmd_rs.getColumnName(2))) 1303 { 1304 System.out.println("column name expected to be same, not " + 1305 rsmd_ps.getColumnName(2) + 1306 " and " + 1307 rsmd_rs.getColumnName(2)); 1308 passed = false; 1309 } 1310 1311 if (rsmd_ps.isReadOnly(2) != rsmd_rs.isReadOnly(2)) 1312 { 1313 System.out.println("isReadOnly() expected to be same, not " + 1314 rsmd_ps.isReadOnly(2) + 1315 " and " + 1316 rsmd_rs.isReadOnly(2)); 1317 passed = false; 1318 } 1319 1320 rs.close(); 1321 ps_f_r.close(); 1322 1323 return passed; 1324 } 1325 1326 1327 1338 private static boolean scrollVerifyMaxRowWithFetchSize(Connection conn, int maxRows, int fetchSize) { 1339 ResultSet rs; 1340 boolean passed = true; 1341 Statement s_i_r = null; 1342 1343 try { 1344 s_i_r = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 1345 ResultSet.CONCUR_READ_ONLY); 1346 s_i_r.setMaxRows(maxRows); 1347 1348 rs = s_i_r.executeQuery("values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15"); 1350 rs.setFetchSize(fetchSize); 1351 1352 s_i_r.setMaxRows(2); 1354 if (maxRows == 0) 1355 maxRows = 15; 1356 1357 if (rs == null) 1358 { 1359 System.out.println("rs expected to be non-null."); 1360 passed = false; 1361 } 1362 for (int index = 1; index < maxRows + 1; index++) 1365 { 1366 if (! rs.next()) 1367 { 1368 System.out.println("rs.next() failed, index = " + index); 1369 passed = false; 1370 break; 1371 } else { 1372 if (index != rs.getInt(1)) { 1373 System.out.println("Expected: " + index + 1374 " not: " + rs.getInt(1)); 1375 } 1376 } 1377 } 1378 if (rs.next()) 1380 { 1381 System.out.println("Error with maxRows = " + maxRows + 1382 " and fetchSize = " + fetchSize + "\n" + 1383 "rs.next() failed, should not have seen " + 1384 (maxRows + 1) + "th row."); 1385 passed = false; 1386 } 1387 1388 if (!rs.first()) 1390 { 1391 System.out.println("rs.first() failed."); 1392 passed = false; 1393 } 1394 for (int index = 1; index < maxRows; index++) 1396 { 1397 if (! rs.next()) 1398 { 1399 System.out.println("rs.previous() failed, index = " + 1400 index); 1401 passed = false; 1402 break; 1403 } else { 1404 if ((index + 1) != rs.getInt(1)) 1405 System.out.println("Error with maxRows = " + 1406 maxRows + " and fetchSize = " + 1407 fetchSize + "\n" + "Error with maxRows = " + 1408 maxRows + " and fetchSize = " + fetchSize + 1409 "\n" + "Expected: " + (index + 1) + 1410 " not: " + rs.getInt(1)); 1411 } 1412 } 1413 if (rs.next()) 1415 { 1416 System.out.println("Error with maxRows = " + maxRows + 1417 " and fetchSize = " + fetchSize + "\n" + 1418 "rs.next() failed, should not have seen " + 1419 (maxRows + 1) + "th row."); 1420 passed = false; 1421 } 1422 1423 rs.afterLast(); 1425 for (int index = 1; index < maxRows + 1; index++) 1427 { 1428 if (! rs.previous()) 1429 { 1430 System.out.println("rs.previous() failed, index = " + 1431 index); 1432 passed = false; 1433 break; 1434 } else { 1435 if (((maxRows - index) + 1) != rs.getInt(1)) { 1436 System.out.println("Error with maxRows = " + maxRows + 1437 " and fetchSize = " + fetchSize + "\n" + 1438 "Expected: " + ((maxRows - index) + 1) + 1439 " not: " + rs.getInt(1)); 1440 } 1441 } 1442 } 1443 if (rs.previous()) 1445 { 1446 System.out.println("Error with maxRows = " + maxRows + 1447 " and fetchSize = " + fetchSize + "\n" + 1448 "rs.previous() failed, should not have seen " + 1449 (maxRows + 1) + "th row."); 1450 passed = false; 1451 } 1452 1453 if (!rs.last()) 1455 { 1456 System.out.println("rs.last() failed."); 1457 passed = false; 1458 } 1459 for (int index = 1; index < maxRows; index++) 1461 { 1462 if (! rs.previous()) 1463 { 1464 System.out.println("rs.previous() failed, index = " + 1465 index); 1466 passed = false; 1467 break; 1468 } else { 1469 if ((maxRows - index) != rs.getInt(1)) { 1470 System.out.println("Error with maxRows = " + maxRows + 1471 " and fetchSize = " + fetchSize + "\n" + 1472 "Expected: " + (maxRows - index) + " not: " + 1473 rs.getInt(1)); 1474 } 1475 } 1476 } 1477 if (rs.previous()) 1479 { 1480 System.out.println("Error with maxRows = " + maxRows + 1481 " and fetchSize = " + fetchSize + "\n" + 1482 "rs.previous() failed, should not have seen " + 1483 (maxRows + 1) + "th row."); 1484 passed = false; 1485 } 1486 1487 rs.last(); 1488 int rows = rs.getRow(); 1489 1490 rs.absolute(rows/2); 1491 if (rs.relative(-1 * (rows))) { 1492 System.out.println("relative(" + -1 * (rows) + ") should return false, position outside of the resultSet"); 1493 1494 } 1495 if (!rs.isBeforeFirst()) { 1496 System.out.println("isBeforeFirst should be true"); 1497 } 1498 1499 rs.absolute(rows/2); 1500 if (rs.relative(rows)) { 1501 System.out.println("relative(" + (rows) + ") should return false, position outside of the resultSet"); 1502 } 1503 if (!rs.isAfterLast()) { 1504 System.out.println("isAfterLast should be true"); 1505 } 1506 rs.absolute(rows/2); 1507 if (rs.absolute(rows + 1)) { 1508 System.out.println("absolute(" + (rows + 1) + ") should return false, position outside of the resultSet"); 1509 System.out.println("Current row: " + rs.getInt(1)); 1510 } 1511 if (!rs.isAfterLast()) { 1512 System.out.println("isAfterLast should be true"); 1513 } 1514 rs.absolute(rows/2); 1515 if (rs.absolute((-1) * (rows + 1))) { 1516 System.out.println("absolute(" + (((-1) * (rows + 1))) + ") should return false, position outside of the resultSet"); 1517 System.out.println("Current row: " + rs.getInt(1)); 1518 } 1519 if (!rs.isBeforeFirst()) { 1520 System.out.println("isBeforeFirst should be true"); 1521 } 1522 1523 rs.close(); 1524 1525 } catch (SQLException e) { 1526 System.out.println(e.getMessage()); 1527 } 1528 1529 return passed; 1530 } 1531 1532 1533 1534 1543 1544 private static boolean checkException(SQLException e, 1545 String SQLState) 1546 { 1547 String state; 1548 String nextState; 1549 SQLException next; 1550 boolean passed = true; 1551 1552 state = e.getSQLState(); 1553 1554 1555 if (! SQLState.equals(state)) { 1556 System.out.println("FAIL -- unexpected exception " + e + 1557 "sqlstate: " + state + SQLState); 1558 passed = false; 1559 } 1560 1561 return passed; 1562 } 1563 1564 1574 1575 static boolean cleanUp(Connection conn, Statement s) { 1576 try { 1577 1578 if (s == null) 1579 { 1580 s = conn.createStatement(); 1582 } 1583 s.execute("drop table t"); 1584 1585 conn.commit(); 1586 conn.close(); 1587 } catch (Throwable e) { 1588 System.out.println("FAIL -- unexpected exception caught in cleanup()"); 1589 JDBCDisplayUtil.ShowException(System.out, e); 1590 return false; 1591 } 1592 1593 return true; 1594 } 1595 1596 1600 static void cleanUp(Connection conn) throws SQLException { 1601 Statement cleanupStmt = conn.createStatement(); 1602 String [] testObjects = {"table t"}; 1603 TestUtil.cleanUpTest(cleanupStmt, testObjects); 1604 cleanupStmt.close(); 1605 } 1606 1607 1608} 1609 | Popular Tags |