1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package tests.sql; 18 19 import dalvik.annotation.KnownFailure; 20 import dalvik.annotation.TestTargetClass; 21 import dalvik.annotation.TestTargets; 22 import dalvik.annotation.TestLevel; 23 import dalvik.annotation.TestTargetNew; 24 25 import java.io.IOException; 26 import java.sql.Connection; 27 import java.sql.DatabaseMetaData; 28 import java.sql.DriverManager; 29 import java.sql.SQLWarning; 30 import java.sql.Savepoint; 31 import java.sql.Statement; 32 import java.sql.PreparedStatement; 33 import java.sql.ResultSet; 34 import java.sql.SQLException; 35 36 import java.sql.CallableStatement; 37 import java.util.Map; 38 39 import junit.framework.Test; 40 41 @TestTargetClass(Connection.class) 42 public class ConnectionTest extends SQLTest { 43 44 /** 45 * @test {@link java.sql.Connection#createStatement()} 46 */ 47 @TestTargetNew( 48 level = TestLevel.COMPLETE, 49 notes = "", 50 method = "createStatement", 51 args = {} 52 ) testCreateStatement()53 public void testCreateStatement() { 54 55 Statement statement = null; 56 try { 57 statement = conn.createStatement(); 58 assertNotNull(statement); 59 //check default values 60 assertEquals(ResultSet.FETCH_UNKNOWN, statement.getFetchDirection()); 61 assertNull(statement.getWarnings()); 62 assertTrue(statement.getQueryTimeout() > 0); 63 } catch (SQLException sqle) { 64 fail("SQL Exception was thrown: " + sqle.getMessage()); 65 } catch (Exception e) { 66 fail("Unexpected Exception " + e.getMessage()); 67 } 68 try { 69 conn.close(); 70 statement.executeQuery("select * from zoo"); 71 fail("SQLException is not thrown after close"); 72 } catch (SQLException e) { 73 // expected 74 } 75 } 76 77 /** 78 * @test {@link java.sql.Connection#createStatement(int resultSetType, int 79 * resultSetConcurrency)} 80 */ 81 @TestTargetNew( 82 level = TestLevel.COMPLETE, 83 notes = "Exception tests fail.", 84 method = "createStatement", 85 args = {int.class, int.class} 86 ) 87 @KnownFailure("Scrolling on a forward only RS not allowed. conn.close() does not wrap up") testCreateStatement_int_int()88 public void testCreateStatement_int_int() throws SQLException { 89 Statement st = null; 90 ResultSet rs = null; 91 92 // test read only 93 try { 94 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 95 ResultSet.CONCUR_READ_ONLY); 96 st.execute("select id, name from zoo"); 97 rs = st.getResultSet(); 98 try { 99 rs.deleteRow(); 100 fail("Could delete row for READ_ONLY ResultSet"); 101 } catch (SQLException sqle) { 102 // expected 103 } 104 105 } catch (SQLException e) { 106 fail("SQLException was thrown: " + e.getMessage()); 107 } finally { 108 try { 109 rs.close(); 110 st.close(); 111 } catch (Exception ee) { 112 } 113 } 114 115 // test forward only: scrolling not allowed 116 try { 117 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 118 ResultSet.CONCUR_READ_ONLY); 119 st.execute("select id, name from zoo"); 120 rs = st.getResultSet(); 121 try { 122 rs.absolute(1); 123 rs.previous(); 124 fail("Could scroll backwards"); 125 } catch (SQLException sqle) { 126 // expected 127 } 128 129 } catch (SQLException e) { 130 fail("SQLException was thrown: " + e.getMessage()); 131 } finally { 132 try { 133 rs.close(); 134 st.close(); 135 } catch (Exception ee) { 136 } 137 } 138 139 // test forward only 140 try { 141 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 142 ResultSet.CONCUR_READ_ONLY); 143 st.execute("select id, name from zoo"); 144 rs = st.getResultSet(); 145 try { 146 rs.last(); 147 rs.first(); 148 fail("Could scroll backwards"); 149 } catch (SQLException sqle) { 150 // expected 151 } 152 153 } catch (SQLException e) { 154 fail("SQLException was thrown: " + e.getMessage()); 155 } finally { 156 try { 157 rs.close(); 158 st.close(); 159 } catch (Exception ee) { 160 } 161 } 162 163 164 // test updating ResultSets 165 try { 166 st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 167 ResultSet.CONCUR_UPDATABLE); 168 st.execute("select name, family from zoo"); 169 rs = st.getResultSet(); 170 try { 171 rs.insertRow(); 172 rs.updateObject("family", "bird"); 173 rs.next(); 174 rs.previous(); 175 assertEquals("parrot", (rs.getString(1))); 176 fail("SQLException was not thrown"); 177 } catch (SQLException sqle) { 178 // expected 179 } 180 181 } catch (SQLException e) { 182 fail("SQLException was thrown: " + e.getMessage()); 183 } finally { 184 try { 185 rs.close(); 186 st.close(); 187 } catch (Exception ee) { 188 } 189 } 190 191 try { 192 st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 193 ResultSet.CONCUR_UPDATABLE); 194 st.execute("select name, family from zoo"); 195 rs = st.getResultSet(); 196 try { 197 rs.insertRow(); 198 rs.updateObject("family", "bird"); 199 rs.next(); 200 rs.previous(); 201 assertEquals("bird", (rs.getString(1))); 202 fail("SQLException was not thrown"); 203 } catch (SQLException sqle) { 204 // expected 205 } 206 207 } catch (SQLException e) { 208 fail("SQLException was thrown: " + e.getMessage()); 209 } finally { 210 try { 211 rs.close(); 212 st.close(); 213 } catch (Exception ee) { 214 } 215 } 216 217 conn.close(); 218 219 try { 220 // exception is not specified for this case 221 conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, -1); 222 fail("Illigal arguments: should return exception."); 223 } catch (SQLException sqle) { 224 // expected 225 } 226 227 try { 228 // exception is not specified for this case 229 conn.createStatement(Integer.MIN_VALUE, ResultSet.CONCUR_READ_ONLY); 230 fail("Illigal arguments: should return exception."); 231 } catch (SQLException sqle) { 232 // expected 233 } 234 } 235 236 /** 237 * @test java.sql.Connection#createStatement(int resultSetType, int 238 * resultSetConcurrency, int resultSetHoldability) 239 */ 240 @TestTargetNew( 241 level = TestLevel.PARTIAL_COMPLETE, 242 notes = "ResultSet.HOLD_CURSORS_AT_COMMIT", 243 method = "createStatement", 244 args = {int.class, int.class, int.class} 245 ) testCreateStatement_int_int_int()246 public void testCreateStatement_int_int_int() { 247 Statement st = null; 248 try { 249 assertNotNull(conn); 250 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 251 ResultSet.CONCUR_READ_ONLY, 252 ResultSet.HOLD_CURSORS_OVER_COMMIT); 253 assertNotNull(st); 254 st.execute("select id, name from zoo"); 255 ResultSet rs = st.getResultSet(); 256 rs.next(); 257 int pos = rs.getRow(); 258 conn.commit(); 259 assertEquals("ResultSet cursor position has changed",pos, rs.getRow()); 260 try { 261 rs.close(); 262 } catch (SQLException sqle) { 263 fail("Unexpected exception was thrown during closing ResultSet"); 264 } 265 } catch (SQLException e) { 266 fail("SQLException was thrown: " + e.getMessage()); 267 } finally { 268 try { 269 if (st != null) st.close(); 270 } catch (SQLException ee) { 271 } 272 } 273 274 try { 275 conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 276 ResultSet.CONCUR_READ_ONLY, -100); 277 fail("SQLException was not thrown"); 278 } catch (SQLException sqle) { 279 //ok 280 } 281 282 } 283 284 @TestTargetNew( 285 level = TestLevel.PARTIAL_COMPLETE, 286 notes = "ResultSet.CLOSE_CURSORS_AT_COMMIT as argument is not supported", 287 method = "createStatement", 288 args = {int.class, int.class, int.class} 289 ) 290 @KnownFailure("not supported") testCreateStatementIntIntIntNotSupported()291 public void testCreateStatementIntIntIntNotSupported() { 292 Statement st = null; 293 try { 294 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 295 ResultSet.CONCUR_READ_ONLY, 296 ResultSet.CLOSE_CURSORS_AT_COMMIT); 297 assertNotNull(st); 298 st.execute("select id, name from zoo"); 299 ResultSet rs = st.getResultSet(); 300 301 try { 302 rs.close(); 303 fail("SQLException was not thrown"); 304 } catch (SQLException sqle) { 305 // expected 306 } 307 308 } catch (SQLException e) { 309 fail("SQLException was thrown: " + e.getMessage()); 310 } finally { 311 if (st != null) { 312 try { 313 st.close(); 314 } catch (SQLException ee) { 315 } 316 } 317 } 318 } 319 320 /** 321 * @test java.sql.Connection#getMetaData() 322 */ 323 @TestTargetNew( 324 level = TestLevel.COMPLETE, 325 notes = "SQLException test fails", 326 method = "getMetaData", 327 args = {} 328 ) 329 @KnownFailure("conn.close() does not wrap up") testGetMetaData()330 public void testGetMetaData() throws SQLException{ 331 try { 332 DatabaseMetaData md = conn.getMetaData(); 333 Connection con = md.getConnection(); 334 assertEquals(conn, con); 335 } catch (SQLException e) { 336 fail("SQLException is thrown"); 337 } 338 339 conn.close(); 340 try { 341 conn.getMetaData(); 342 fail("Exception expected"); 343 } catch (SQLException e) { 344 //ok 345 } 346 } 347 348 /** 349 * @throws SQLException 350 * @test java.sql.Connection#clearWarnings() 351 * 352 * TODO clearWarnings is not supported 353 */ 354 @TestTargetNew( 355 level = TestLevel.SUFFICIENT, 356 notes = "test fails. not supported. always returns null.", 357 method = "clearWarnings", 358 args = {} 359 ) 360 @KnownFailure("not supported") testClearWarnings()361 public void testClearWarnings() throws SQLException { 362 363 try { 364 SQLWarning w = conn.getWarnings(); 365 assertNull(w); 366 } catch (Exception e) { 367 fail("Unexpected Exception: " + e.getMessage()); 368 } 369 370 371 Statement st = null; 372 try { 373 st = conn.createStatement(); 374 st.execute("select animals from zoo"); 375 fail("SQLException was not thrown"); 376 } catch (SQLException e) { 377 assertNotNull(conn.getWarnings()); 378 } finally { 379 try { 380 st.close(); 381 } catch (SQLException ee) { 382 } 383 } 384 385 try { 386 conn.clearWarnings(); 387 SQLWarning w = conn.getWarnings(); 388 assertNull(w); 389 } catch (Exception e) { 390 fail("Unexpected Exception: " + e.getMessage()); 391 } 392 393 try { 394 st = conn.createStatement(); 395 st.execute("select monkey from zoo"); 396 fail("SQLException was not thrown"); 397 } catch (SQLException e) { 398 assertEquals("SQLite.Exception: error in prepare/compile",e.getMessage()); 399 } finally { 400 try { 401 st.close(); 402 } catch (SQLException ee) { 403 } 404 } 405 406 //Test for correct functionality 407 try { 408 SQLWarning w = conn.getWarnings(); 409 assertNotNull(w); 410 } catch (Exception e) { 411 fail("Unexpected Exception: " + e.getMessage()); 412 } 413 414 conn.close(); 415 try { 416 conn.clearWarnings(); 417 fail("Exception expected"); 418 } catch (SQLException e) { 419 //ok 420 } 421 422 } 423 424 425 /** 426 * @throws SQLException 427 * @test java.sql.Connection#getWarnings() 428 * 429 * TODO GetWarnings is not supported: returns null 430 */ 431 @TestTargetNew( 432 level = TestLevel.COMPLETE, 433 notes = "not supported. always returns null. SQLException test fails", 434 method = "getWarnings", 435 args = {} 436 ) 437 @KnownFailure("not supported") testGetWarnings()438 public void testGetWarnings() throws SQLException { 439 Statement st = null; 440 int errorCode1 = -1; 441 int errorCode2 = -1; 442 443 try { 444 st = conn.createStatement(); 445 st.execute("select animals from zoooo"); 446 fail("SQLException was not thrown"); 447 } catch (SQLException e) { 448 // expected 449 errorCode1 = e.getErrorCode(); 450 } 451 452 try { 453 SQLWarning wrs = conn.getWarnings(); 454 assertNull(wrs); 455 } catch (Exception e) { 456 fail("Change test implementation: get warnings is supported now"); 457 } 458 459 // tests implementation: but errorcodes need to change too -> change impl. 460 /* 461 Statement st = null; 462 int errorCode1 = -1; 463 int errorCode2 = -1; 464 465 try { 466 st = conn.createStatement(); 467 st.execute("select animals from zoooo"); 468 fail("SQLException was not thrown"); 469 } catch (SQLException e) { 470 // expected 471 errorCode1 = e.getErrorCode(); 472 } 473 474 try { 475 SQLWarning wrs = conn.getWarnings(); 476 assertNotNull(wrs); 477 assertEquals(errorCode1, wrs.getErrorCode()); 478 assertNull(wrs.getNextWarning()); 479 } catch (Exception e) { 480 fail("Unexpected Exception: " + e.getMessage()); 481 } 482 try { 483 st.execute("select horse from zoooooo"); 484 } catch (SQLException e) { 485 // expected 486 errorCode2 = e.getErrorCode(); 487 } 488 489 try { 490 SQLWarning wrs = conn.getWarnings(); 491 assertEquals(errorCode1, wrs.getErrorCode()); 492 assertNotNull(wrs.getNextWarning()); 493 assertEquals(errorCode2, wrs.getErrorCode()); 494 } catch (Exception e) { 495 fail("Unexpected Exception: " + e.getMessage()); 496 } 497 498 try { 499 st.close(); 500 } catch (SQLException ee) { 501 } 502 503 */ 504 505 conn.close(); 506 try { 507 conn.getWarnings(); 508 fail("Exception expected"); 509 } catch (SQLException e) { 510 //ok 511 } 512 } 513 514 /** 515 * @test java.sql.Connection#getAutoCommit() 516 */ 517 @TestTargetNew( 518 level = TestLevel.COMPLETE, 519 notes = "SQLException checking missed", 520 method = "getAutoCommit", 521 args = {} 522 ) testGetAutoCommit()523 public void testGetAutoCommit() { 524 try { 525 conn.setAutoCommit(true); 526 assertTrue(conn.getAutoCommit()); 527 conn.setAutoCommit(false); 528 assertFalse(conn.getAutoCommit()); 529 conn.setAutoCommit(true); 530 assertTrue(conn.getAutoCommit()); 531 532 } catch (SQLException e) { 533 fail("SQLException is thrown: " + e.getMessage()); 534 } 535 } 536 537 /** 538 * @test java.sql.Connection#setAutoCommit(boolean) 539 */ 540 @TestTargetNew( 541 level = TestLevel.COMPLETE, 542 notes = "SQLException test throws exception", 543 method = "setAutoCommit", 544 args = {boolean.class} 545 ) 546 @KnownFailure("conn.close() does not wrap up") testSetAutoCommit()547 public void testSetAutoCommit() throws SQLException { 548 549 Statement st = null; 550 ResultSet rs = null; 551 ResultSet rs1 = null; 552 try { 553 conn.setAutoCommit(true); 554 st = conn.createStatement(); 555 st 556 .execute("insert into zoo (id, name, family) values (3, 'Chichichi', 'monkey');"); 557 conn.commit(); 558 } catch (SQLException e) { 559 //ok 560 } finally { 561 try { 562 st.close(); 563 } catch (SQLException ee) { 564 } 565 } 566 // even though exception was thrown value is committed 567 try { 568 st = conn.createStatement(); 569 st.execute("select * from zoo"); 570 rs = st.getResultSet(); 571 assertEquals(3, getCount(rs)); 572 } catch (SQLException e) { 573 fail("Unexpected Exception thrown"); 574 } finally { 575 try { 576 st.close(); 577 } catch (SQLException ee) { 578 } 579 } 580 581 582 try { 583 conn.setAutoCommit(false); 584 st = conn.createStatement(); 585 st 586 .execute("insert into zoo (id, name, family) values (4, 'Burenka', 'cow');"); 587 st.execute("select * from zoo"); 588 rs = st.getResultSet(); 589 assertEquals(4, getCount(rs)); 590 conn.commit(); 591 // Check cursors closed after commit 592 rs1 = st.getResultSet(); 593 assertEquals(0, getCount(rs1)); 594 595 } catch (SQLException e) { 596 fail("SQLException is thrown: " + e.getMessage()); 597 } finally { 598 try { 599 rs.close(); 600 rs1.close(); 601 st.close(); 602 } catch (SQLException ee) { 603 } 604 } 605 606 conn.close(); 607 608 try { 609 conn.setAutoCommit(true); 610 fail("Exception expected"); 611 } catch (SQLException e) { 612 //ok 613 } 614 } 615 616 /** 617 * @throws SQLException 618 * @test java.sql.Connection#isReadOnly() 619 */ 620 @TestTargetNew( 621 level = TestLevel.COMPLETE, 622 notes = "Instead of SQLException nullpointer exception is thrown.", 623 method = "isReadOnly", 624 args = {} 625 ) 626 @KnownFailure("conn.close() does not wrap up") testIsReadOnly()627 public void testIsReadOnly() throws SQLException { 628 try { 629 conn.setReadOnly(true); 630 assertTrue(conn.isReadOnly()); 631 conn.setReadOnly(false); 632 assertFalse(conn.isReadOnly()); 633 } catch (SQLException sqle) { 634 fail("SQLException was thrown: " + sqle.getMessage()); 635 } 636 637 conn.close(); 638 try { 639 conn.isReadOnly(); 640 fail("Exception expected"); 641 } catch (SQLException e) { 642 //ok 643 } 644 } 645 646 /** 647 * @throws SQLException 648 * @test java.sql.Connection#setReadOnly(boolean) 649 */ 650 @TestTargetNew( 651 level = TestLevel.COMPLETE, 652 notes = "Not supported. test fails", 653 method = "setReadOnly", 654 args = {boolean.class} 655 ) 656 @KnownFailure("not supported") testSetReadOnly()657 public void testSetReadOnly() throws SQLException { 658 659 // Pseudo test: not supported test 660 Statement st = null; 661 try { 662 conn.setReadOnly(true); 663 st = conn.createStatement(); 664 st.execute("insert into zoo (id, name, family) values (3, 'ChiChiChi', 'monkey');"); 665 // fail("SQLException is not thrown"); 666 } catch (SQLException sqle) { 667 fail("Set readonly is actually implemented: activate correct test"); 668 } finally { 669 try { 670 st.close(); 671 } catch (SQLException ee) { 672 } 673 } 674 675 // test for correct implementation 676 st = null; 677 try { 678 conn.setReadOnly(true); 679 st = conn.createStatement(); 680 st.execute("insert into zoo (id, name, family) values (3, 'ChiChiChi', 'monkey');"); 681 fail("SQLException is not thrown"); 682 } catch (SQLException sqle) { 683 // expected 684 } finally { 685 try { 686 st.close(); 687 } catch (SQLException ee) { 688 } 689 } 690 691 try { 692 conn.setReadOnly(true); 693 st = conn.createStatement(); 694 st.executeUpdate("insert into zoo (id, name, family) values (4, 'ChaChaCha', 'monkey');"); 695 fail("SQLException is not thrown"); 696 } catch (SQLException sqle) { 697 // expected 698 } finally { 699 try { 700 st.close(); 701 } catch (SQLException ee) { 702 } 703 } 704 705 try { 706 conn.setReadOnly(false); 707 st = conn.createStatement(); 708 st.execute("insert into zoo (id, name, family) values (4, 'ChiChiChi', 'monkey');"); 709 } catch (SQLException sqle) { 710 fail("SQLException was thrown: " + sqle.getMessage()); 711 } finally { 712 try { 713 st.close(); 714 } catch (SQLException ee) { 715 } 716 } 717 718 conn.close(); 719 try { 720 conn.setReadOnly(true); 721 fail("Exception expected"); 722 } catch (SQLException e) { 723 //ok 724 } 725 } 726 727 /** 728 * @throws SQLException 729 * @test java.sql.Connection#getHoldability() 730 * 731 * TODO ResultSet.CLOSE_CURSORS_AT_COMMIT is not supported 732 */ 733 @TestTargetNew( 734 level = TestLevel.COMPLETE, 735 notes = "+option ResultSet.CLOSE_CURSORS_AT_COMMIT not supported. SQLException test fails.", 736 method = "getHoldability", 737 args = {} 738 ) 739 @KnownFailure("not supported") testGetHoldability()740 public void testGetHoldability() throws SQLException { 741 try { 742 conn.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT); 743 assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, conn 744 .getHoldability()); 745 } catch (SQLException sqle) { 746 fail("SQLException was thrown: " + sqle.getMessage()); 747 } 748 749 try { 750 conn.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT); 751 assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, conn 752 .getHoldability()); 753 } catch (SQLException e) { 754 assertEquals("not supported", e.getMessage()); 755 } 756 757 // Exception checking 758 759 conn.close(); 760 761 try { 762 conn.getHoldability(); 763 fail("Could execute statement on closed connection."); 764 } catch (SQLException e) { 765 //ok 766 } 767 } 768 769 /** 770 * @test java.sql.Connection#setHoldability(int) 771 * 772 * TODO ResultSet.CLOSE_CURSORS_AT_COMMIT is not supported 773 */ 774 @TestTargetNew( 775 level = TestLevel.COMPLETE, 776 notes = "ResultSet.CLOSE_CURSORS_AT_COMMIT is not supported", 777 method = "setHoldability", 778 args = {int.class} 779 ) 780 @KnownFailure("not supported") testSetHoldability()781 public void testSetHoldability() { 782 Statement st = null; 783 try { 784 conn.setAutoCommit(false); 785 conn.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT); 786 assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, conn 787 .getHoldability()); 788 st = conn.createStatement(); 789 st.execute("insert into zoo (id, name, family) values (4, 'ChiChiChi', 'monkey');"); 790 ResultSet rs = st.getResultSet(); 791 conn.commit(); 792 try { 793 rs.next(); 794 } catch (SQLException sqle) { 795 //ok 796 } 797 conn.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT); 798 assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, conn 799 .getHoldability()); 800 st = conn.createStatement(); 801 st.execute("insert into zoo (id, name, family) values (4, 'ChiChiChi', 'monkey');"); 802 rs = st.getResultSet(); 803 conn.commit(); 804 try { 805 rs.next(); 806 } catch (SQLException sqle) { 807 fail("SQLException was thrown: " + sqle.getMessage()); 808 } 809 } catch (SQLException sqle) { 810 fail("SQLException was thrown: " + sqle.getMessage()); 811 } finally { 812 try { 813 st.close(); 814 } catch (Exception ee) { 815 } 816 } 817 818 try { 819 conn.setHoldability(-1); 820 fail("SQLException is not thrown"); 821 } catch (SQLException sqle) { 822 // expected 823 } 824 } 825 826 /** 827 * @throws SQLException 828 * @test java.sql.Connection#getTransactionIsolation() 829 * 830 * TODO only Connection.TRANSACTION_SERIALIZABLE is supported 831 */ 832 @TestTargetNew( 833 level = TestLevel.SUFFICIENT, 834 notes = "SQLException testing throws exception. Connection.TRANSACTION_SERIALIZABLE.", 835 method = "getTransactionIsolation", 836 args = {} 837 ) 838 @KnownFailure("not supported") testGetTransactionIsolation()839 public void testGetTransactionIsolation() throws SQLException { 840 try { 841 conn 842 .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 843 assertEquals(Connection.TRANSACTION_READ_UNCOMMITTED, conn 844 .getTransactionIsolation()); 845 conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 846 assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn 847 .getTransactionIsolation()); 848 conn 849 .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 850 assertEquals(Connection.TRANSACTION_REPEATABLE_READ, conn 851 .getTransactionIsolation()); 852 conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); 853 assertEquals(Connection.TRANSACTION_SERIALIZABLE, conn 854 .getTransactionIsolation()); 855 } catch (SQLException sqle) { 856 fail("SQLException is thrown: " + sqle.toString()); 857 } 858 859 // Exception checking 860 861 conn.close(); 862 863 try { 864 conn.getTransactionIsolation(); 865 fail("Could execute statement on closed connection."); 866 } catch (SQLException e) { 867 //ok 868 } 869 } 870 871 /** 872 * @throws SQLException 873 * @test java.sql.Connection#getTransactionIsolation() 874 * 875 * TODO only Connection.TRANSACTION_SERIALIZABLE is supported 876 */ 877 @TestTargetNew( 878 level = TestLevel.PARTIAL_COMPLETE, 879 notes = "not supported options", 880 method = "getTransactionIsolation", 881 args = {} 882 ) testGetTransactionIsolationNotSupported()883 public void testGetTransactionIsolationNotSupported() throws SQLException { 884 /* 885 try { 886 conn 887 .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 888 assertEquals(Connection.TRANSACTION_READ_UNCOMMITTED, conn 889 .getTransactionIsolation()); 890 conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 891 assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn 892 .getTransactionIsolation()); 893 conn 894 .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 895 assertEquals(Connection.TRANSACTION_REPEATABLE_READ, conn 896 .getTransactionIsolation()); 897 } catch (SQLException sqle) { 898 fail("SQLException is thrown: " + sqle.toString()); 899 } 900 */ 901 } 902 903 /** 904 * @test java.sql.Connection#setTransactionIsolation(int) 905 * 906 * TODO only Connection.TRANSACTION_SERIALIZABLE is supported 907 */ 908 @TestTargetNew( 909 level = TestLevel.SUFFICIENT, 910 notes = "not fully supported", 911 method = "setTransactionIsolation", 912 args = {int.class} 913 ) testSetTransactionIsolation()914 public void testSetTransactionIsolation() { 915 try { 916 // conn 917 // .setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 918 // assertEquals(Connection.TRANSACTION_READ_UNCOMMITTED, conn 919 // .getTransactionIsolation()); 920 // conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 921 // assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn 922 // .getTransactionIsolation()); 923 // conn 924 // .setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); 925 // assertEquals(Connection.TRANSACTION_REPEATABLE_READ, conn 926 // .getTransactionIsolation()); 927 conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); 928 assertEquals(Connection.TRANSACTION_SERIALIZABLE, conn 929 .getTransactionIsolation()); 930 } catch (SQLException sqle) { 931 fail("SQLException is thrown: " + sqle.toString()); 932 } 933 934 try { 935 conn.setTransactionIsolation(0); 936 fail("SQLException is not thrown"); 937 } catch (SQLException sqle) { 938 // expected 939 } 940 } 941 942 /** 943 * @test java.sql.Connection#setCatalog(String catalog) 944 * 945 * TODO setCatalog method does nothing: Hint default catalog sqlite_master. 946 */ 947 @TestTargetNew( 948 level = TestLevel.COMPLETE, 949 notes = "not supported", 950 method = "setCatalog", 951 args = {java.lang.String.class} 952 ) testSetCatalog()953 public void testSetCatalog() { 954 955 String[] catalogs = { "test", "test1", "test" }; 956 Statement st = null; 957 try { 958 for (int i = 0; i < catalogs.length; i++) { 959 conn.setCatalog(catalogs[i]); 960 assertNull(catalogs[i], conn.getCatalog()); 961 st = conn.createStatement(); 962 st 963 .equals("create table test_table (id integer not null, name varchar(20), primary key(id));"); 964 st.equals("drop table test_table;"); 965 966 } 967 } catch (SQLException sqle) { 968 fail("SQLException is thrown"); 969 } finally { 970 try { 971 st.close(); 972 } catch (Exception ee) { 973 } 974 } 975 976 /* 977 String[] catalogs = { "test"}; 978 Statement st = null; 979 try { 980 for (int i = 0; i < catalogs.length; i++) { 981 conn.setCatalog(catalogs[i]); 982 fail("illegal catalog name"); 983 assertEquals(catalogs[i], conn.getCatalog()); 984 st = conn.createStatement(); 985 st 986 .equals("create table test_table (id integer not null, name varchar(20), primary key(id));"); 987 st.equals("drop table test_table;"); 988 } 989 } catch (SQLException sqle) { 990 System.out.println("TODO: Test for correct error message: name with ,\"sqlite_\" prefix expected"); 991 } finally { 992 try { 993 st.close(); 994 } catch (Exception ee) { 995 } 996 } 997 998 String[] catalogs = { "sqlite_test", "sqlite_test1", "sqlite_test" }; 999 Statement st = null; 1000 try { 1001 for (int i = 0; i < catalogs.length; i++) { 1002 conn.setCatalog(catalogs[i]); 1003 assertEquals(catalogs[i], conn.getCatalog()); 1004 st = conn.createStatement(); 1005 st 1006 .equals("create table test_table (id integer not null, name varchar(20), primary key(id));"); 1007 st.equals("drop table test_table;"); 1008 1009 } 1010 } catch (SQLException sqle) { 1011 fail("SQLException is thrown"); 1012 } finally { 1013 try { 1014 st.close(); 1015 } catch (Exception ee) { 1016 } 1017 } 1018 1019 try { 1020 conn.setCatalog(null); 1021 fail("SQLException is not thrown"); 1022 } catch (SQLException e) { 1023 // expected 1024 } 1025 1026 try { 1027 conn.setCatalog("not_exist"); 1028 fail("SQLException is not thrown"); 1029 } catch (SQLException e) { 1030 // expected 1031 } 1032 */ 1033 } 1034 1035 /** 1036 * @throws SQLException 1037 * @test java.sql.Connection#getCatalog() 1038 * 1039 */ 1040 @TestTargetNew( 1041 level = TestLevel.COMPLETE, 1042 notes = "not supported. test fails", 1043 method = "getCatalog", 1044 args = {} 1045 ) 1046 @KnownFailure("not supported") testGetCatalog()1047 public void testGetCatalog() throws SQLException { 1048 1049 1050 // test default catalog 1051 try { 1052 assertEquals("sqlite_master", conn.getCatalog()); 1053 } catch (SQLException sqle) { 1054 fail("SQL Exception " + sqle.getMessage()); 1055 } catch (Exception e) { 1056 fail("Unexpected Exception " + e.getMessage()); 1057 } 1058 1059 1060 String[] catalogs = { "sqlite_test", "sqlite_test1", "sqlite_test" }; 1061 Statement st = null; 1062 try { 1063 for (int i = 0; i < catalogs.length; i++) { 1064 conn.setCatalog(catalogs[i]); 1065 assertNull(conn.getCatalog()); 1066 } 1067 } catch (SQLException sqle) { 1068 fail("SQL Exception " + sqle.getMessage()); 1069 } catch (Exception e) { 1070 fail("Reeimplement tests now that the method is implemented"); 1071 } 1072 1073 // Exception checking 1074 1075 conn.close(); 1076 1077 try { 1078 conn.getCatalog(); 1079 fail("Could execute statement on closed connection."); 1080 } catch (SQLException e) { 1081 //ok 1082 } 1083 } 1084 1085 /** 1086 * @test java.sql.Connection#setTypeMap(Map<String,Class<?>> map) 1087 * 1088 * TODO setTypeMap is not supported 1089 */ 1090 @TestTargetNew( 1091 level = TestLevel.COMPLETE, 1092 notes = "not supported", 1093 method = "setTypeMap", 1094 args = {java.util.Map.class} 1095 ) testSetTypeMap()1096 public void testSetTypeMap() { 1097 /* 1098 try { 1099 java.util.Map map = conn.getTypeMap(); 1100 map 1101 .put( 1102 "org.apache.harmony.sql.tests.java.sql.TestHelper_Connection1", 1103 Class.forName("TestHelper_Connection1")); 1104 conn.setTypeMap(map); 1105 assertEquals(map, conn.getTypeMap()); 1106 } catch (SQLException sqle) { 1107 //ok 1108 } catch (Exception e) { 1109 fail("Unexpected Exception " + e.getMessage()); 1110 } 1111 1112 try { 1113 conn.setTypeMap(null); 1114 fail("SQLException is not thrown"); 1115 } catch (SQLException e) { 1116 // expected 1117 } 1118 */ 1119 } 1120 1121 /** 1122 * @throws SQLException 1123 * @test java.sql.Connection#getTypeMap() 1124 * 1125 * TODO getTypeMap is not supported 1126 */ 1127 @TestTargetNew( 1128 level = TestLevel.COMPLETE, 1129 notes = "not supported", 1130 method = "getTypeMap", 1131 args = {} 1132 ) testGetTypeMap()1133 public void testGetTypeMap() throws SQLException { 1134 /* 1135 try { 1136 java.util.Map map = conn.getTypeMap(); 1137 map 1138 .put( 1139 "org.apache.harmony.sql.tests.java.sql.TestHelper_Connection1", 1140 Class.forName("TestHelper_Connection1")); 1141 conn.setTypeMap(map); 1142 assertEquals(map, conn.getTypeMap()); 1143 } catch (SQLException sqle) { 1144 //ok 1145 } catch (Exception e) { 1146 fail("Unexpected Exception " + e.getMessage()); 1147 } 1148 1149 // Exception checking 1150 1151 conn.close(); 1152 1153 try { 1154 conn.setTypeMap(null); 1155 fail("Could execute statement on closed connection."); 1156 } catch (SQLException e) { 1157 //ok 1158 } 1159 */ 1160 } 1161 1162 /** 1163 * @test java.sql.Connection#nativeSQL(String sql) 1164 * 1165 * TODO nativeSQL is not supported 1166 */ 1167 @TestTargetNew( 1168 level = TestLevel.COMPLETE, 1169 notes = "not supported", 1170 method = "nativeSQL", 1171 args = {java.lang.String.class} 1172 ) testNativeSQL()1173 public void testNativeSQL() throws SQLException{ 1174 String[] queries = { 1175 "select * from zoo;", 1176 "insert into zoo (id, name, family) values (3, 'Chichichi', 'monkey');", 1177 "create table zoo_office(id integer not null, name varchar(20), primary key(id));", 1178 "drop table zoo_office;" }; 1179 String[] native_queries = { 1180 "select * from zoo;", 1181 "insert into zoo (id, name, family) values (3, 'Chichichi', 'monkey');", 1182 "create table zoo_office(id integer not null, name varchar(20), primary key(id));", 1183 "drop table zoo_office;" }; 1184 Statement st = null; 1185 String nativeQuery = ""; 1186 try { 1187 for (int i = 0; i < queries.length; i++) { 1188 nativeQuery = conn.nativeSQL(queries[i]); 1189 assertEquals(native_queries[i], nativeQuery); 1190 st = conn.createStatement(); 1191 st.execute(nativeQuery); 1192 } 1193 } catch (SQLException sqle) { 1194 //ok 1195 } catch (Exception e) { 1196 fail("Unexpected Exception " + e.getMessage()); 1197 } finally { 1198 try { 1199 st.close(); 1200 } catch (Exception ee) { 1201 } 1202 } 1203 1204 String[] inc_queries = { "", " ", "not query" }; 1205 for (int i = 0; i < inc_queries.length; i++) { 1206 try { 1207 nativeQuery = conn.nativeSQL(inc_queries[i]); 1208 assertEquals(inc_queries[i], nativeQuery); 1209 } catch (SQLException e) { 1210 assertEquals("not supported",e.getMessage()); 1211 } 1212 } 1213 1214 // Exception checking 1215 1216 conn.close(); 1217 1218 try { 1219 conn.nativeSQL(inc_queries[0]); 1220 fail("Could execute statement on closed connection."); 1221 } catch (SQLException e) { 1222 //ok 1223 } 1224 1225 } 1226 1227 /** 1228 * @test java.sql.Connection#prepareCall(String sql) 1229 * 1230 * TODO prepareCall is not supported 1231 */ 1232 @TestTargetNew( 1233 level = TestLevel.COMPLETE, 1234 notes = "not supported", 1235 method = "prepareCall", 1236 args = {java.lang.String.class} 1237 ) testPrepareCall()1238 public void testPrepareCall() throws SQLException { 1239 CallableStatement cstmt = null; 1240 ResultSet rs = null; 1241 ResultSet rs1 = null; 1242 Statement st = null; 1243 Statement st1 = null; 1244 try { 1245 cstmt = conn.prepareCall("call welcomeAnimal(3, 'Petya', 'Cock')"); 1246 st = conn.createStatement(); 1247 st.execute("select * from zoo"); 1248 rs = st.getResultSet(); 1249 assertEquals(2, getCount(rs)); 1250 cstmt.execute(); 1251 st1 = conn.createStatement(); 1252 st1.execute("select * from zoo"); 1253 rs1 = st1.getResultSet(); 1254 assertEquals(3, getCount(rs1)); 1255 1256 } catch (SQLException e) { 1257 //ok not supported 1258 } finally { 1259 try { 1260 st.close(); 1261 st1.close(); 1262 rs.close(); 1263 rs1.close(); 1264 cstmt.close(); 1265 } catch (Exception ee) { 1266 } 1267 } 1268 1269 1270 try { 1271 conn.prepareCall("welcomeAnimal(4, 'Petya', 'Cock')"); 1272 fail("SQL Exception is not thrown"); 1273 } catch (SQLException e) { 1274 // expected 1275 } 1276 1277 try { 1278 conn.prepareCall(null); 1279 fail("SQL Exception is not thrown"); 1280 } catch (SQLException e) { 1281 // expected 1282 } 1283 1284 // Exception checking 1285 1286 conn.close(); 1287 1288 try { 1289 conn.prepareCall(""); 1290 fail("Could execute statement on closed connection."); 1291 } catch (SQLException e) { 1292 //ok 1293 } 1294 1295 } 1296 1297 /** 1298 * @test java.sql.Connection#prepareCall(String sql, int resultSetType, int 1299 * resultSetConcurrency) 1300 * 1301 * TODO prepareCall is not supported 1302 */ 1303 @TestTargetNew( 1304 level = TestLevel.COMPLETE, 1305 notes = "not supported", 1306 method = "prepareCall", 1307 args = {java.lang.String.class, int.class, int.class} 1308 ) testPrepareCall_String_int_int()1309 public void testPrepareCall_String_int_int() { 1310 CallableStatement cstmt = null; 1311 ResultSet rs = null; 1312 1313 try { 1314 String query = "call welcomeAnimal(3, 'Petya', 'Cock')"; 1315 cstmt = conn.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY, 1316 ResultSet.CONCUR_READ_ONLY); 1317 } catch (SQLException e) { 1318 //ok 1319 } 1320 1321 /* 1322 try { 1323 String query = "call welcomeAnimal(3, 'Petya', 'Dino')"; 1324 cstmt = conn.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY, 1325 ResultSet.CONCUR_READ_ONLY); 1326 cstmt.execute("select id, name from zoo"); 1327 rs = cstmt.getResultSet(); 1328 try { 1329 rs.deleteRow(); 1330 fail("Can delete row for READ_ONLY ResultSet"); 1331 } catch (SQLException sqle) { 1332 // expected 1333 } 1334 1335 try { 1336 rs.absolute(0); 1337 fail("Can move cursor to the last position for TYPE_FORWARD_ONLY ResultSet"); 1338 } catch (SQLException sqle) { 1339 // expected 1340 } 1341 1342 } catch (SQLException e) { 1343 fail("SQLException was thrown: " + e.getMessage()); 1344 } finally { 1345 try { 1346 rs.close(); 1347 cstmt.close(); 1348 } catch (Exception ee) { 1349 } 1350 } 1351 Statement st = null; 1352 try { 1353 st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 1354 ResultSet.CONCUR_UPDATABLE); 1355 st.execute("select name, family from zoo"); 1356 rs = st.getResultSet(); 1357 try { 1358 rs.insertRow(); 1359 rs.updateObject("family", "bird"); 1360 rs.next(); 1361 rs.previous(); 1362 assertEquals("parrot", (rs.getString(1))); 1363 fail("SQLException was not thrown"); 1364 } catch (SQLException sqle) { 1365 // expected 1366 } 1367 1368 } catch (SQLException e) { 1369 fail("SQLException was thrown: " + e.getMessage()); 1370 } finally { 1371 try { 1372 rs.close(); 1373 st.close(); 1374 } catch (SQLException ee) { 1375 } 1376 } 1377 1378 try { 1379 st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 1380 ResultSet.CONCUR_UPDATABLE); 1381 st.execute("select name, family from zoo"); 1382 rs = st.getResultSet(); 1383 try { 1384 rs.insertRow(); 1385 rs.updateObject("family", "bird"); 1386 rs.next(); 1387 rs.previous(); 1388 assertEquals("bird", (rs.getString(1))); 1389 fail("SQLException was not thrown"); 1390 } catch (SQLException sqle) { 1391 // expected 1392 } 1393 1394 } catch (SQLException e) { 1395 fail("SQLException was thrown: " + e.getMessage()); 1396 } finally { 1397 try { 1398 rs.close(); 1399 st.close(); 1400 } catch (SQLException ee) { 1401 } 1402 } 1403 1404 try { 1405 conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, -1); 1406 fail("SQLException was not thrown"); 1407 } catch (SQLException sqle) { 1408 // expected 1409 } 1410 1411 try { 1412 conn.createStatement(Integer.MIN_VALUE, ResultSet.CONCUR_READ_ONLY); 1413 fail("SQLException was not thrown"); 1414 } catch (SQLException sqle) { 1415 // expected 1416 } 1417 1418 */ 1419 } 1420 1421 /** 1422 * @test java.sql.Connection#prepareCall(String sql, int resultSetType, int 1423 * resultSetConcurrency, int resultSetHoldability) 1424 * 1425 * TODO prepareCall is not supported 1426 */ 1427 @TestTargetNew( 1428 level = TestLevel.COMPLETE, 1429 notes = "not supported", 1430 method = "prepareCall", 1431 args = {java.lang.String.class, int.class, int.class, int.class} 1432 ) testPrepareCall_String_int_int_int()1433 public void testPrepareCall_String_int_int_int() { 1434 CallableStatement cstmt = null; 1435 ResultSet rs = null; 1436 1437 try { 1438 String query = "call welcomeAnimal(?, ?, ?)"; 1439 cstmt = conn.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY, 1440 ResultSet.CONCUR_READ_ONLY, 1441 ResultSet.HOLD_CURSORS_OVER_COMMIT); 1442 } catch (SQLException e) { 1443 //ok 1444 } 1445 /* 1446 try { 1447 String query = "call welcomeAnimal(?, ?, ?)"; 1448 cstmt = conn.prepareCall(query, ResultSet.TYPE_FORWARD_ONLY, 1449 ResultSet.CONCUR_READ_ONLY, 1450 ResultSet.HOLD_CURSORS_OVER_COMMIT); 1451 cstmt.setInt(1, 3); 1452 cstmt.setString(2, "Petya"); 1453 cstmt.setString(3, "Cock"); 1454 cstmt.execute("select id, name from zoo"); 1455 rs = cstmt.getResultSet(); 1456 try { 1457 rs.close(); 1458 fail("SQLException was not thrown"); 1459 } catch (SQLException sqle) { 1460 fail("Unexpected exception was thrown during closing ResultSet"); 1461 } 1462 } catch (SQLException e) { 1463 fail("SQLException was thrown: " + e.getMessage()); 1464 } finally { 1465 try { 1466 rs.close(); 1467 cstmt.close(); 1468 } catch (Exception ee) { 1469 } 1470 } 1471 1472 Statement st = null; 1473 1474 try { 1475 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 1476 ResultSet.CONCUR_READ_ONLY, 1477 ResultSet.CLOSE_CURSORS_AT_COMMIT); 1478 st.execute("select id, name from zoo"); 1479 rs = st.getResultSet(); 1480 try { 1481 rs.close(); 1482 fail("SQLException was not thrown"); 1483 } catch (SQLException sqle) { 1484 // expected 1485 } 1486 } catch (SQLException e) { 1487 fail("SQLException was thrown: " + e.getMessage()); 1488 } 1489 1490 try { 1491 conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 1492 ResultSet.CONCUR_READ_ONLY, -100); 1493 fail("SQLException was not thrown"); 1494 } catch (SQLException sqle) { 1495 // expected 1496 } 1497 */ 1498 1499 } 1500 1501 /** 1502 * @test java.sql.Connection#prepareStatement(String sql) 1503 */ 1504 @TestTargetNew( 1505 level = TestLevel.COMPLETE, 1506 notes = "", 1507 method = "prepareStatement", 1508 args = {java.lang.String.class} 1509 ) testPrepareStatement()1510 public void testPrepareStatement() { 1511 PreparedStatement prst = null; 1512 Statement st = null; 1513 ResultSet rs = null; 1514 ResultSet rs1 = null; 1515 try { 1516 String update = "update zoo set family = ? where name = ?;"; 1517 prst = conn.prepareStatement(update); 1518 prst.setString(1, "cat"); 1519 prst.setString(2, "Yasha"); 1520 st = conn.createStatement(); 1521 st.execute("select * from zoo where family = 'cat'"); 1522 rs = st.getResultSet(); 1523 assertEquals(0, getCount(rs)); 1524 prst.executeUpdate(); 1525 st.execute("select * from zoo where family = 'cat'"); 1526 rs1 = st.getResultSet(); 1527 assertEquals(1, getCount(rs1)); 1528 } catch (SQLException e) { 1529 fail("SQLException is thrown: " + e.getMessage()); 1530 } finally { 1531 try { 1532 rs.close(); 1533 rs1.close(); 1534 prst.close(); 1535 st.close(); 1536 } catch (SQLException ee) { 1537 } 1538 } 1539 1540 try { 1541 prst = conn.prepareStatement(""); 1542 prst.execute(); 1543 fail("SQLException is not thrown"); 1544 } catch (SQLException e) { 1545 //ok 1546 } 1547 1548 try { 1549 conn.prepareStatement(null); 1550 fail("SQLException is not thrown"); 1551 } catch (Exception e) { 1552 //ok 1553 } 1554 1555 1556 } 1557 1558 1559 /** 1560 * @test { @link java.sql.Connection#prepareStatement(String sql, int 1561 * autoGeneratedKeys) } 1562 */ 1563 // TODO Crashes VM. Fix later. 1564 @TestTargetNew( 1565 level = TestLevel.COMPLETE, 1566 notes = "Statment.Return_generated_keys/getGeneratedKeys is not supported", 1567 method = "prepareStatement", 1568 args = {java.lang.String.class, int.class} 1569 ) 1570 @KnownFailure("not supported") testPrepareStatement_String_int()1571 public void testPrepareStatement_String_int() { 1572 PreparedStatement prst = null; 1573 PreparedStatement prst1 = null; 1574 Statement st = null; 1575 ResultSet rs = null; 1576 ResultSet rs1 = null; 1577 ResultSet rs4 = null; 1578 ResultSet rs5 = null; 1579 1580 1581 try { 1582 String insert = "insert into zoo (id, name, family) values (?, ?, ?);"; 1583 prst = conn.prepareStatement(insert, 1584 Statement.RETURN_GENERATED_KEYS); 1585 fail("Fail: prepareStatement does not fail"); 1586 } catch (SQLException e) { 1587 //ok not supported 1588 } 1589 1590 1591 try { 1592 String insert = "insert into zoo (id, name, family) values (?, ?, ?);"; 1593 1594 prst = conn.prepareStatement(insert, 1595 Statement.NO_GENERATED_KEYS); 1596 prst.setInt(1, 8); 1597 prst.setString(2, "Tuzik"); 1598 prst.setString(3, "dog"); 1599 st = conn.createStatement(); 1600 st.execute("select * from zoo"); 1601 rs = st.getResultSet(); 1602 assertEquals(2, getCount(rs)); 1603 prst.execute(); 1604 st.execute("select * from zoo where family = 'dog'"); 1605 rs1 = st.getResultSet(); 1606 assertEquals(1, getCount(rs1)); 1607 // TODO getGeneratedKeys is not supported 1608 rs4 = prst.getGeneratedKeys(); 1609 assertEquals(0, getCount(rs4)); 1610 1611 1612 1613 prst1 = conn.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS); 1614 prst1.setInt(1, 5); 1615 prst1.setString(2, "Layka"); 1616 prst1.setString(3, "dog"); 1617 1618 prst1.execute(); 1619 1620 1621 1622 rs5 = prst1.getGeneratedKeys(); 1623 assertEquals(0, getCount(rs5)); 1624 1625 } catch (SQLException e) { 1626 fail("SQLException is thrown: " + e.getMessage()); 1627 } finally { 1628 try { 1629 rs.close(); 1630 rs1.close(); 1631 prst.close(); 1632 st.close(); 1633 } catch (Exception ee) { 1634 } 1635 } 1636 1637 1638 } 1639 1640 /** 1641 * @test java.sql.Connection#commit() 1642 */ 1643 @TestTargetNew( 1644 level = TestLevel.COMPLETE, 1645 notes = "", 1646 method = "commit", 1647 args = {} 1648 ) testCommit()1649 public void testCommit() { 1650 Statement st = null; 1651 Statement st1 = null; 1652 Statement st2 = null; 1653 Statement st3 = null; 1654 Statement st4 = null; 1655 ResultSet rs1 = null; 1656 ResultSet rs2 = null; 1657 ResultSet rs3 = null; 1658 ResultSet rs4 = null; 1659 try { 1660 conn.setAutoCommit(false); 1661 1662 st = conn.createStatement(); 1663 st 1664 .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');"); 1665 st 1666 .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');"); 1667 1668 st1 = conn.createStatement(); 1669 st1.execute("select * from zoo"); 1670 rs1 = st1.getResultSet(); 1671 assertEquals(4, getCount(rs1)); 1672 try { 1673 conn.commit(); 1674 st2 = conn.createStatement(); 1675 st2.execute("select * from zoo"); 1676 rs2 = st2.getResultSet(); 1677 assertEquals(4, getCount(rs2)); 1678 } catch (SQLException e) { 1679 fail("SQLException is thrown: " + e.toString()); 1680 } finally { 1681 try { 1682 rs2.close(); 1683 st2.close(); 1684 } catch (SQLException ee) { 1685 } 1686 } 1687 1688 try { 1689 st3 = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, 1690 ResultSet.CONCUR_READ_ONLY, 1691 ResultSet.HOLD_CURSORS_OVER_COMMIT); 1692 st3.execute("select * from zoo"); 1693 rs3 = st3.getResultSet(); 1694 conn.commit(); 1695 assertEquals(4, getCount(rs3)); 1696 } catch (SQLException e) { 1697 fail("SQLException is thrown: " + e.toString()); 1698 } finally { 1699 try { 1700 if (rs3 != null) rs3.close(); 1701 if (st3 != null) st3.close(); 1702 } catch (SQLException ee) { 1703 } 1704 } 1705 } catch (SQLException sqle) { 1706 fail("SQLException was thrown: " + sqle.toString()); 1707 } finally { 1708 try { 1709 rs1.close(); 1710 st.close(); 1711 st1.close(); 1712 } catch (Exception ee) { 1713 } 1714 } 1715 1716 1717 } 1718 1719 /** 1720 * @throws SQLException 1721 * @test java.sql.Connection#rollback() 1722 */ 1723 @TestTargetNew( 1724 level = TestLevel.COMPLETE, 1725 notes = "", 1726 method = "rollback", 1727 args = {} 1728 ) testRollback()1729 public void testRollback() throws SQLException { 1730 Statement st = null; 1731 Statement st1 = null; 1732 ResultSet rs1 = null; 1733 ResultSet rs2 = null; 1734 ResultSet rs3 = null; 1735 1736 try { 1737 conn.setAutoCommit(false); 1738 st = conn.createStatement(); 1739 st 1740 .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');"); 1741 st 1742 .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');"); 1743 conn.rollback(); 1744 st1 = conn.createStatement(); 1745 st1.execute("select * from zoo"); 1746 rs1 = st1.getResultSet(); 1747 assertEquals("Rollback was ineffective",2, getCount(rs1)); 1748 1749 } catch (SQLException sqle) { 1750 fail("SQLException is thrown: " + sqle.toString()); 1751 } finally { 1752 conn.setAutoCommit(true); 1753 try { 1754 st.close(); 1755 st1.close(); 1756 rs1.close(); 1757 } catch (SQLException ee) { 1758 } 1759 } 1760 try { 1761 conn.setAutoCommit(false); 1762 1763 st = conn.createStatement(); 1764 st 1765 .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');"); 1766 st 1767 .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');"); 1768 1769 if (!conn.getAutoCommit()) { 1770 st1 = conn.createStatement(); 1771 st1.execute("select * from zoo"); 1772 rs1 = st1.getResultSet(); 1773 assertEquals(4, getCount(rs1)); 1774 Statement st2 = null; 1775 Statement st3 = null; 1776 try { 1777 conn.commit(); 1778 st2 = conn.createStatement(); 1779 st2.execute("select * from zoo"); 1780 rs2 = st2.getResultSet(); 1781 assertEquals(4, getCount(rs2)); 1782 // rollback after commit ineffective 1783 conn.rollback(); 1784 st3 = conn.createStatement(); 1785 st3.execute("select * from zoo"); 1786 rs3 = st3.getResultSet(); 1787 assertEquals(4, getCount(rs3)); 1788 } catch (SQLException e) { 1789 fail("SQLException is thrown: " + e.toString()); 1790 } finally { 1791 conn.setAutoCommit(true); 1792 try { 1793 rs2.close(); 1794 rs3.close(); 1795 st2.close(); 1796 st3.close(); 1797 } catch (SQLException ee) { 1798 } 1799 } 1800 } else { 1801 fail("Error in test setup: cannot turn autocommit off."); 1802 } 1803 } catch (SQLException sqle) { 1804 fail("SQLException is thrown: " + sqle.toString()); 1805 } finally { 1806 try { 1807 st.close(); 1808 st1.close(); 1809 rs1.close(); 1810 } catch (SQLException ee) { 1811 } 1812 } 1813 1814 conn.close(); 1815 try { 1816 conn.rollback(); 1817 fail("SQLException expected"); 1818 } catch (SQLException e) { 1819 // ok 1820 } 1821 } 1822 1823 /** 1824 * @test java.sql.Connection#setSavepoint() 1825 * 1826 * TODO setSavepoint is not supported 1827 */ 1828 @TestTargetNew( 1829 level = TestLevel.COMPLETE, 1830 notes = "not supported", 1831 method = "setSavepoint", 1832 args = {} 1833 ) testSetSavepoint()1834 public void testSetSavepoint() { 1835 1836 try { 1837 conn.setAutoCommit(false); 1838 1839 try { 1840 Savepoint sp = conn.setSavepoint(); 1841 } catch (SQLException e) { 1842 // ok not supported 1843 } 1844 } catch (SQLException sqle) { 1845 fail("SQLException is thrown: " + sqle.toString()); 1846 } 1847 1848 1849 //Complete test but: not supported exception is thrown 1850 /* 1851 try { 1852 conn.setAutoCommit(false); 1853 1854 st = conn.createStatement(); 1855 st 1856 .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');"); 1857 st 1858 .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');"); 1859 1860 if (!conn.getAutoCommit()) { 1861 st1 = conn.createStatement(); 1862 st1.execute("select * from zoo"); 1863 rs1 = st1.getResultSet(); 1864 assertEquals(4, getCount(rs1)); 1865 Statement st2 = null; 1866 ResultSet rs2 = null; 1867 try { 1868 Savepoint sp = conn.setSavepoint(); 1869 st 1870 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 1871 conn.rollback(sp); 1872 st2 = conn.createStatement(); 1873 st2.execute("select * from zoo"); 1874 rs2 = st2.getResultSet(); 1875 assertEquals(4, getCount(rs2)); 1876 } catch (SQLException e) { 1877 fail("SQLException is thrown: " + e.toString()); 1878 } finally { 1879 try { 1880 rs2.close(); 1881 st2.close(); 1882 } catch (Exception ee) { 1883 } 1884 } 1885 1886 try { 1887 Savepoint sp1 = conn.setSavepoint(); 1888 assertNotNull(sp1); 1889 st 1890 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 1891 Savepoint sp2 = conn.setSavepoint(); 1892 assertNotNull(sp2); 1893 st 1894 .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');"); 1895 conn.rollback(sp1); 1896 st2 = conn.createStatement(); 1897 st2.execute("select * from zoo"); 1898 rs2 = st2.getResultSet(); 1899 assertEquals(4, getCount(rs2)); 1900 } catch (SQLException e) { 1901 fail("SQLException is thrown: " + e.toString()); 1902 } finally { 1903 try { 1904 rs2.close(); 1905 st2.close(); 1906 } catch (SQLException ee) { 1907 } 1908 } 1909 1910 try { 1911 Savepoint sp1 = conn.setSavepoint(); 1912 assertNotNull(sp1); 1913 st 1914 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 1915 Savepoint sp2 = conn.setSavepoint(); 1916 assertNotNull(sp2); 1917 st 1918 .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');"); 1919 conn.rollback(); 1920 st2 = conn.createStatement(); 1921 st2.execute("select * from zoo"); 1922 rs2 = st2.getResultSet(); 1923 assertEquals(4, getCount(rs2)); 1924 } catch (SQLException e) { 1925 fail("SQLException is thrown: " + e.toString()); 1926 } finally { 1927 try { 1928 rs2.close(); 1929 st2.close(); 1930 } catch (SQLException ee) { 1931 } 1932 } 1933 1934 } else { 1935 st1 = conn.createStatement(); 1936 st1.execute("select * from zoo"); 1937 rs1 = st1.getResultSet(); 1938 assertEquals(4, getCount(rs1)); 1939 try { 1940 Savepoint sp = conn.setSavepoint(); 1941 st 1942 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 1943 conn.rollback(sp); 1944 fail("SQLException is not thrown"); 1945 } catch (SQLException sqle) { 1946 // expected 1947 } 1948 } 1949 } catch (SQLException sqle) { 1950 fail("SQLException is thrown: " + sqle.toString()); 1951 } finally { 1952 try { 1953 rs1.close(); 1954 st.close(); 1955 st1.close(); 1956 } catch (SQLException ee) { 1957 } 1958 } 1959 */ 1960 } 1961 1962 /** 1963 * @test java.sql.Connection#setSavepoint(String name) 1964 * 1965 * TODO setSavepoint is not supported 1966 */ 1967 @TestTargetNew( 1968 level = TestLevel.COMPLETE, 1969 notes = "not supported", 1970 method = "setSavepoint", 1971 args = {java.lang.String.class} 1972 ) testSetSavepoint_String()1973 public void testSetSavepoint_String() { 1974 1975 String testSavepoint = "testSavepoint"; 1976 1977 try { 1978 conn.setAutoCommit(false); 1979 1980 try { 1981 Savepoint sp = conn.setSavepoint(testSavepoint); 1982 } catch (SQLException e) { 1983 // ok not supported 1984 } 1985 } catch (SQLException sqle) { 1986 fail("SQLException is thrown: " + sqle.toString()); 1987 } 1988 1989 /* 1990 Statement st = null; 1991 Statement st1 = null; 1992 ResultSet rs1 = null; 1993 try { 1994 conn.setAutoCommit(false); 1995 1996 st = conn.createStatement(); 1997 st 1998 .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');"); 1999 st 2000 .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');"); 2001 2002 if (!conn.getAutoCommit()) { 2003 st1 = conn.createStatement(); 2004 st1.execute("select * from zoo"); 2005 rs1 = st1.getResultSet(); 2006 assertEquals(4, getCount(rs1)); 2007 Statement st2 = null; 2008 ResultSet rs2 = null; 2009 try { 2010 Savepoint sp = conn.setSavepoint("one"); 2011 st 2012 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2013 conn.rollback(sp); 2014 st2 = conn.createStatement(); 2015 st2.execute("select * from zoo"); 2016 rs2 = st2.getResultSet(); 2017 assertEquals(4, getCount(rs2)); 2018 } catch (SQLException e) { 2019 fail("SQLException is thrown: " + e.toString()); 2020 } finally { 2021 try { 2022 rs2.close(); 2023 st2.close(); 2024 } catch (Exception ee) { 2025 } 2026 } 2027 2028 try { 2029 Savepoint sp1 = conn.setSavepoint("one"); 2030 st 2031 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2032 Savepoint sp2 = conn.setSavepoint("two"); 2033 st 2034 .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');"); 2035 conn.rollback(sp1); 2036 st2 = conn.createStatement(); 2037 st2.execute("select * from zoo"); 2038 rs2 = st2.getResultSet(); 2039 assertEquals(4, getCount(rs2)); 2040 } catch (SQLException e) { 2041 fail("SQLException is thrown: " + e.toString()); 2042 } finally { 2043 try { 2044 rs2.close(); 2045 st2.close(); 2046 } catch (SQLException ee) { 2047 } 2048 } 2049 2050 try { 2051 Savepoint sp1 = conn.setSavepoint("three"); 2052 st 2053 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2054 Savepoint sp2 = conn.setSavepoint("four"); 2055 st 2056 .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');"); 2057 conn.rollback(); 2058 st2 = conn.createStatement(); 2059 st2.execute("select * from zoo"); 2060 rs2 = st2.getResultSet(); 2061 assertEquals(4, getCount(rs2)); 2062 } catch (SQLException e) { 2063 fail("SQLException is thrown: " + e.toString()); 2064 } finally { 2065 try { 2066 rs2.close(); 2067 st2.close(); 2068 } catch (SQLException ee) { 2069 } 2070 } 2071 2072 } else { 2073 st1 = conn.createStatement(); 2074 st1.execute("select * from zoo"); 2075 rs1 = st1.getResultSet(); 2076 assertEquals(4, getCount(rs1)); 2077 try { 2078 Savepoint sp = conn.setSavepoint("five"); 2079 st 2080 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2081 conn.rollback(sp); 2082 fail("SQLException is not thrown"); 2083 } catch (SQLException sqle) { 2084 // expected 2085 } 2086 } 2087 } catch (SQLException sqle) { 2088 fail("SQLException is thrown: " + sqle.toString()); 2089 } finally { 2090 try { 2091 rs1.close(); 2092 st.close(); 2093 st1.close(); 2094 } catch (SQLException ee) { 2095 } 2096 } 2097 */ 2098 } 2099 2100 /** 2101 * @test java.sql.Connection#rollback(Savepoint savepoint) 2102 * 2103 * TODO Savepoint is not supported 2104 */ 2105 @TestTargetNew( 2106 level = TestLevel.COMPLETE, 2107 notes = "not supported", 2108 method = "rollback", 2109 args = {java.sql.Savepoint.class} 2110 ) testRollback_Savepoint()2111 public void testRollback_Savepoint() { 2112 Savepoint sp = new DummySavePoint(); 2113 try { 2114 conn.setAutoCommit(false); 2115 2116 try { 2117 conn.rollback(sp); 2118 } catch (SQLException e) { 2119 //ok 2120 } 2121 } catch (SQLException sqle) { 2122 fail("SQLException is thrown: " + sqle.toString()); 2123 } 2124 /* 2125 Statement st = null; 2126 Statement st1 = null; 2127 ResultSet rs1 = null; 2128 try { 2129 conn.setAutoCommit(false); 2130 2131 st = conn.createStatement(); 2132 st 2133 .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');"); 2134 st 2135 .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');"); 2136 2137 if (!conn.getAutoCommit()) { 2138 st1 = conn.createStatement(); 2139 st1.execute("select * from zoo"); 2140 rs1 = st1.getResultSet(); 2141 assertEquals(4, getCount(rs1)); 2142 Statement st2 = null; 2143 ResultSet rs2 = null; 2144 try { 2145 Savepoint sp = conn.setSavepoint("one"); 2146 st 2147 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2148 conn.rollback(sp); 2149 st2 = conn.createStatement(); 2150 st2.execute("select * from zoo"); 2151 rs2 = st2.getResultSet(); 2152 assertEquals(4, getCount(rs2)); 2153 } catch (SQLException e) { 2154 fail("SQLException is thrown: " + e.toString()); 2155 } finally { 2156 try { 2157 rs2.close(); 2158 st2.close(); 2159 } catch (Exception ee) { 2160 } 2161 } 2162 2163 try { 2164 Savepoint sp1 = conn.setSavepoint("one"); 2165 st 2166 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2167 Savepoint sp2 = conn.setSavepoint("two"); 2168 st 2169 .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');"); 2170 conn.rollback(sp1); 2171 st2 = conn.createStatement(); 2172 st2.execute("select * from zoo"); 2173 rs2 = st2.getResultSet(); 2174 assertEquals(4, getCount(rs2)); 2175 } catch (SQLException e) { 2176 fail("SQLException is thrown: " + e.toString()); 2177 } finally { 2178 try { 2179 rs2.close(); 2180 st2.close(); 2181 } catch (SQLException ee) { 2182 } 2183 } 2184 2185 try { 2186 Savepoint sp1 = conn.setSavepoint("three"); 2187 st 2188 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2189 Savepoint sp2 = conn.setSavepoint("four"); 2190 st 2191 .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');"); 2192 conn.rollback(); 2193 st2 = conn.createStatement(); 2194 st2.execute("select * from zoo"); 2195 rs2 = st2.getResultSet(); 2196 assertEquals(4, getCount(rs2)); 2197 } catch (SQLException e) { 2198 fail("SQLException is thrown: " + e.toString()); 2199 } finally { 2200 try { 2201 rs2.close(); 2202 st2.close(); 2203 } catch (SQLException ee) { 2204 } 2205 } 2206 2207 } else { 2208 st1 = conn.createStatement(); 2209 st1.execute("select * from zoo"); 2210 rs1 = st1.getResultSet(); 2211 assertEquals(4, getCount(rs1)); 2212 try { 2213 Savepoint sp = conn.setSavepoint("five"); 2214 st 2215 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2216 conn.rollback(sp); 2217 fail("SQLException is not thrown"); 2218 } catch (SQLException sqle) { 2219 // expected 2220 } 2221 } 2222 } catch (SQLException sqle) { 2223 fail("SQLException is thrown: " + sqle.toString()); 2224 } finally { 2225 try { 2226 rs1.close(); 2227 st.close(); 2228 st1.close(); 2229 } catch (SQLException ee) { 2230 } 2231 } 2232 */ 2233 } 2234 2235 /** 2236 * @test java.sql.Connection#releaseSavepoint(Savepoint savepoint) 2237 * 2238 * TODO Savepoint is not supported 2239 */ 2240 @TestTargetNew( 2241 level = TestLevel.COMPLETE, 2242 notes = "not supported", 2243 method = "releaseSavepoint", 2244 args = {java.sql.Savepoint.class} 2245 ) testReleaseSavepoint_Savepoint()2246 public void testReleaseSavepoint_Savepoint() { 2247 Savepoint sp = new DummySavePoint(); 2248 try { 2249 conn.setAutoCommit(false); 2250 2251 try { 2252 conn.releaseSavepoint(sp); 2253 } catch (SQLException e) { 2254 //ok 2255 } 2256 } catch (SQLException sqle) { 2257 fail("SQLException is thrown: " + sqle.toString()); 2258 } 2259 /* 2260 2261 Statement st = null; 2262 Statement st1 = null; 2263 ResultSet rs1 = null; 2264 try { 2265 conn.setAutoCommit(false); 2266 2267 st = conn.createStatement(); 2268 st 2269 .execute("insert into zoo (id, name, family) values (3, 'Vorobey', 'sparrow');"); 2270 st 2271 .execute("insert into zoo (id, name, family) values (4, 'Orel', 'eagle');"); 2272 2273 if (!conn.getAutoCommit()) { 2274 st1 = conn.createStatement(); 2275 st1.execute("select * from zoo"); 2276 rs1 = st1.getResultSet(); 2277 assertEquals(4, getCount(rs1)); 2278 Statement st2 = null; 2279 ResultSet rs2 = null; 2280 try { 2281 Savepoint sp = conn.setSavepoint("one"); 2282 st 2283 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2284 conn.rollback(sp); 2285 st2 = conn.createStatement(); 2286 st2.execute("select * from zoo"); 2287 rs2 = st2.getResultSet(); 2288 assertEquals(4, getCount(rs2)); 2289 st 2290 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2291 conn.releaseSavepoint(sp); 2292 try { 2293 conn.rollback(sp); 2294 fail("SQLException is not thrown"); 2295 } catch (SQLException sqle) { 2296 // expected 2297 } 2298 conn.rollback(); 2299 } catch (SQLException e) { 2300 fail("SQLException is thrown: " + e.toString()); 2301 } finally { 2302 try { 2303 rs2.close(); 2304 st2.close(); 2305 } catch (Exception ee) { 2306 } 2307 } 2308 2309 try { 2310 Savepoint sp1 = conn.setSavepoint("one"); 2311 st 2312 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2313 Savepoint sp2 = conn.setSavepoint("two"); 2314 st 2315 .execute("insert into zoo (id, name, family) values (6, 'grach', 'rook');"); 2316 conn.releaseSavepoint(sp1); 2317 try { 2318 conn.rollback(sp1); 2319 fail("SQLException is not thrown"); 2320 } catch (SQLException sqle) { 2321 // expected 2322 } 2323 conn.commit(); 2324 conn.rollback(sp2); 2325 st2 = conn.createStatement(); 2326 st2.execute("select * from zoo"); 2327 rs2 = st2.getResultSet(); 2328 assertEquals(4, getCount(rs2)); 2329 } catch (SQLException e) { 2330 fail("SQLException is thrown: " + e.toString()); 2331 } finally { 2332 try { 2333 rs2.close(); 2334 st2.close(); 2335 } catch (SQLException ee) { 2336 } 2337 } 2338 2339 } else { 2340 st1 = conn.createStatement(); 2341 st1.execute("select * from zoo"); 2342 rs1 = st1.getResultSet(); 2343 assertEquals(4, getCount(rs1)); 2344 try { 2345 Savepoint sp = conn.setSavepoint("five"); 2346 st 2347 .execute("insert into zoo (id, name, family) values (5, 'chayka', 'gull');"); 2348 conn.releaseSavepoint(sp); 2349 fail("SQLException is not thrown"); 2350 } catch (SQLException sqle) { 2351 // expected 2352 } 2353 } 2354 } catch (SQLException sqle) { 2355 fail("SQLException is thrown: " + sqle.toString()); 2356 } finally { 2357 try { 2358 rs1.close(); 2359 st.close(); 2360 st1.close(); 2361 } catch (SQLException ee) { 2362 } 2363 } 2364 */ 2365 } 2366 2367 /** 2368 * @test java.sql.Connection#prepareStatement(String sql, int[] 2369 * columnIndexes) 2370 * 2371 * TODO prepareStatement(String sql, int[] columnIndexes) is not 2372 * supported 2373 */ 2374 @TestTargetNew( 2375 level = TestLevel.COMPLETE, 2376 notes = "not supported", 2377 method = "prepareStatement", 2378 args = {java.lang.String.class, int[].class} 2379 ) testPrepareStatement_String_intArray()2380 public void testPrepareStatement_String_intArray() { 2381 PreparedStatement prst = null; 2382 try { 2383 String insert = "insert into zoo (id, name, family) values (?, ?, ?);"; 2384 prst = conn.prepareStatement(insert, new int[] { 0, 1, 2 }); 2385 } catch (SQLException e) { 2386 //ok not supported 2387 } finally { 2388 try { 2389 prst.close(); 2390 } catch (Exception ee) { 2391 } 2392 } 2393 /* 2394 2395 Statement st = null; 2396 PreparedStatement prst1 = null; 2397 PreparedStatement prst = null; 2398 ResultSet rs = null; 2399 ResultSet rs1 = null; 2400 ResultSet rs4 = null; 2401 ResultSet rs5 = null; 2402 try { 2403 String insert = "insert into zoo (id, name, family) values (?, ?, ?);"; 2404 prst = conn.prepareStatement(insert, new int[] { 0, 1, 2 }); 2405 prst.setInt(1, 8); 2406 prst.setString(2, "Tuzik"); 2407 prst.setString(3, "dog"); 2408 2409 st = conn.createStatement(); 2410 st.execute("select * from zoo"); 2411 rs = st.getResultSet(); 2412 assertEquals(2, getCount(rs)); 2413 prst.execute(); 2414 st.execute("select * from zoo where family = 'dog'"); 2415 rs1 = st.getResultSet(); 2416 assertEquals(1, getCount(rs1)); 2417 2418 rs4 = prst.getGeneratedKeys(); 2419 assertEquals(0, getCount(rs4)); 2420 2421 prst1 = conn.prepareStatement(insert, new int[] { 0, 1, 2, 10 }); 2422 prst1.setInt(1, 5); 2423 prst1.setString(2, "Layka"); 2424 prst1.setString(3, "dog"); 2425 2426 prst1.execute(); 2427 2428 rs5 = prst1.getGeneratedKeys(); 2429 assertEquals(0, getCount(rs5)); 2430 2431 } catch (SQLException e) { 2432 fail("SQLException is thrown: " + e.getMessage()); 2433 } finally { 2434 try { 2435 rs.close(); 2436 rs1.close(); 2437 rs4.close(); 2438 rs5.close(); 2439 st.close(); 2440 prst1.close(); 2441 prst.close(); 2442 } catch (Exception ee) { 2443 } 2444 } 2445 2446 try { 2447 String insert = "insert into zoo (id, name, family) values (?, ?, ?);"; 2448 conn.prepareStatement(insert, new int[] {}); 2449 } catch (SQLException e) { 2450 fail("SQLException is thrown"); 2451 } 2452 2453 try { 2454 String insert = "insert into zoo (id, name, family) values (?, ?, ?);"; 2455 conn.prepareStatement(insert, (int[]) null); 2456 } catch (SQLException e) { 2457 fail("SQLException is thrown"); 2458 } 2459 */ 2460 } 2461 2462 /** 2463 * @test java.sql.Connection#prepareStatement(String sql, int resultSetType, 2464 * int resultSetConcurrency) 2465 */ 2466 @TestTargetNew( 2467 level = TestLevel.PARTIAL_COMPLETE, 2468 notes = "not fully supported", 2469 method = "prepareStatement", 2470 args = {java.lang.String.class, int.class, int.class} 2471 ) testPrepareStatement_String_int_int()2472 public void testPrepareStatement_String_int_int() { 2473 String query = "insert into zoo (id, name, family) values (?, ?, ?);"; 2474 PreparedStatement st = null; 2475 ResultSet rs = null; 2476 try { 2477 2478 st = conn.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, 2479 ResultSet.CONCUR_READ_ONLY); 2480 st.execute("select id, name from zoo"); 2481 rs = st.getResultSet(); 2482 try { 2483 rs.deleteRow(); 2484 fail("Can delete row for READ_ONLY ResultSet"); 2485 } catch (SQLException sqle) { 2486 // expected 2487 } 2488 2489 } catch (SQLException e) { 2490 fail("SQLException was thrown: " + e.getMessage()); 2491 } finally { 2492 try { 2493 if (rs != null) rs.close(); 2494 if (st != null) st.close(); 2495 } catch (SQLException ee) { 2496 } 2497 } 2498 2499 try { 2500 st = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, 2501 ResultSet.CONCUR_UPDATABLE); 2502 st.execute("select name, family from zoo"); 2503 rs = st.getResultSet(); 2504 try { 2505 rs.insertRow(); 2506 rs.updateObject("family", "bird"); 2507 rs.next(); 2508 rs.previous(); 2509 assertEquals("bird", (rs.getString(1))); 2510 } catch (SQLException sqle) { 2511 // expected 2512 } 2513 2514 } catch (SQLException e) { 2515 fail("SQLException was thrown: " + e.getMessage()); 2516 } finally { 2517 try { 2518 rs.close(); 2519 st.close(); 2520 } catch (SQLException ee) { 2521 } 2522 } 2523 2524 try { 2525 conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, -1); 2526 } catch (SQLException sqle) { 2527 // expected 2528 } 2529 2530 try { 2531 conn.prepareStatement(query, Integer.MIN_VALUE, 2532 ResultSet.CONCUR_READ_ONLY); 2533 } catch (SQLException sqle) { 2534 // expected 2535 } 2536 } 2537 2538 @TestTargetNew( 2539 level = TestLevel.PARTIAL_COMPLETE, 2540 notes = "not supported options: ResultSet.TYPE_SCROLL_INSENSITIVE," + 2541 "ResultSet.CONCUR_UPDATABLE", 2542 method = "prepareStatement", 2543 args = {java.lang.String.class, int.class, int.class} 2544 ) 2545 @KnownFailure("not supported") testPrepareStatementNotSupported()2546 public void testPrepareStatementNotSupported() { 2547 String query = "insert into zoo (id, name, family) values (?, ?, ?);"; 2548 PreparedStatement st = null; 2549 ResultSet rs = null; 2550 try { 2551 st = conn.prepareStatement(query, 2552 ResultSet.TYPE_SCROLL_INSENSITIVE, 2553 ResultSet.CONCUR_UPDATABLE); 2554 st.execute("select name, family from zoo"); 2555 rs = st.getResultSet(); 2556 try { 2557 rs.insertRow(); 2558 rs.updateObject("family", "bird"); 2559 rs.next(); 2560 rs.previous(); 2561 assertEquals("parrot", (rs.getString(1))); 2562 } catch (SQLException sqle) { 2563 fail("Got Exception "+sqle.getMessage()); 2564 } 2565 2566 } catch (SQLException e) { 2567 fail("SQLException was thrown: " + e.getMessage()); 2568 } finally { 2569 try { 2570 if (rs != null) rs.close(); 2571 if (st != null) st.close(); 2572 } catch (SQLException ee) { 2573 } 2574 } 2575 2576 } 2577 2578 2579 2580 /** 2581 * @test java.sql.Connection#prepareStatement(String sql, int resultSetType, 2582 * int resultSetConcurrency, int resultSetHoldability) 2583 */ 2584 // TODO Crashes VM. Fix later. 2585 @TestTargetNew( 2586 level = TestLevel.SUFFICIENT, 2587 notes = "Not fully implemented: ResultSet.CLOSE_CURSORS_AT_COMMIT not supported", 2588 method = "prepareStatement", 2589 args = {java.lang.String.class, int.class, int.class, int.class} 2590 ) testPrepareStatement_String_int_int_int()2591 public void testPrepareStatement_String_int_int_int() { 2592 String query = "insert into zoo (id, name, family) values (?, ?, ?);"; 2593 PreparedStatement st = null; 2594 ResultSet rs = null; 2595 try { 2596 st = conn.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, 2597 ResultSet.CONCUR_READ_ONLY, 2598 ResultSet.HOLD_CURSORS_OVER_COMMIT); 2599 st.setInt(1, 3); 2600 st.setString(2, "Petya"); 2601 st.setString(3, "Cock"); 2602 st.execute("select id, name from zoo"); 2603 rs = st.getResultSet(); 2604 try { 2605 rs.close(); 2606 } catch (SQLException sqle) { 2607 fail("Unexpected exception was thrown during closing ResultSet"); 2608 } 2609 } catch (SQLException e) { 2610 fail("SQLException was thrown: " + e.getMessage()); 2611 } finally { 2612 try { 2613 if (rs != null) rs.close(); 2614 if (st != null) st.close(); 2615 } catch (SQLException ee) { 2616 } 2617 } 2618 /* 2619 //TODO ResultSet.CLOSE_CURSORS_AT_COMMIT is not supported 2620 try { 2621 st = conn.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, 2622 ResultSet.CONCUR_READ_ONLY, 2623 ResultSet.CLOSE_CURSORS_AT_COMMIT); 2624 st.execute("select id, name from zoo"); 2625 rs = st.getResultSet(); 2626 try { 2627 rs.close(); 2628 fail("SQLException was not thrown"); 2629 } catch (SQLException sqle) { 2630 // expected 2631 } 2632 } catch (SQLException e) { 2633 fail("SQLException was thrown: " + e.getMessage()); 2634 } finally { 2635 try { 2636 st.close(); 2637 rs.close(); 2638 } catch (SQLException ee) { 2639 } 2640 } 2641 */ 2642 2643 try { 2644 conn.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, 2645 ResultSet.CONCUR_READ_ONLY, -100); 2646 fail("SQLException was not thrown"); 2647 } catch (SQLException sqle) { 2648 // expected 2649 } 2650 2651 } 2652 2653 /** 2654 * @test java.sql.Connection#prepareStatement(String sql, String[] 2655 * columnNames) 2656 * 2657 * TODO prepareStatement(String sql, String[] columnNames) method is 2658 * not supported 2659 */ 2660 @TestTargetNew( 2661 level = TestLevel.COMPLETE, 2662 notes = "not supported", 2663 method = "prepareStatement", 2664 args = {java.lang.String.class, java.lang.String[].class} 2665 ) testPrepareStatement_String_StringArray()2666 public void testPrepareStatement_String_StringArray() { 2667 PreparedStatement prst = null; 2668 PreparedStatement prst1 = null; 2669 ResultSet rs = null; 2670 ResultSet rs1 = null; 2671 ResultSet rs4 = null; 2672 ResultSet rs5 = null; 2673 2674 try { 2675 String insert = "insert into zoo (id, name, family) values (?, ?, ?);"; 2676 conn.prepareStatement(insert, new String[] { "id", "name", 2677 "family" }); 2678 } catch (SQLException e) { 2679 //ok not supported 2680 } 2681 2682 /* 2683 try { 2684 String insert = "insert into zoo (id, name, family) values (?, ?, ?);"; 2685 conn.prepareStatement(insert, new String[] {}); 2686 } catch (SQLException e) { 2687 fail("SQLException is thrown"); 2688 } 2689 2690 try { 2691 String insert = "insert into zoo (id, name, family) values (?, ?, ?);"; 2692 conn.prepareStatement(insert, (String[]) null); 2693 } catch (SQLException e) { 2694 fail("SQLException is thrown"); 2695 } 2696 2697 try { 2698 String insert = "insert into zoo (id, name, family) values (?, ?, ?);"; 2699 prst = conn.prepareStatement(insert, new String[] { "id", "name", 2700 "family" }); 2701 prst.setInt(1, 8); 2702 prst.setString(2, "Tuzik"); 2703 prst.setString(3, "dog"); 2704 2705 Statement st = conn.createStatement(); 2706 st.execute("select * from zoo"); 2707 rs = st.getResultSet(); 2708 assertEquals(2, getCount(rs)); 2709 prst.execute(); 2710 st.execute("select * from zoo where family = 'dog'"); 2711 rs1 = st.getResultSet(); 2712 assertEquals(1, getCount(rs1)); 2713 2714 rs4 = prst.getGeneratedKeys(); 2715 assertEquals(0, getCount(rs4)); 2716 2717 prst1 = conn.prepareStatement(insert, new String[] { "id", "name", "" }); 2718 prst1.setInt(1, 5); 2719 prst1.setString(2, "Layka"); 2720 prst1.setString(3, "dog"); 2721 2722 prst1.execute(); 2723 2724 rs5 = prst1.getGeneratedKeys(); 2725 assertEquals(0, getCount(rs5)); 2726 2727 } catch (SQLException e) { 2728 fail("SQLException is thrown: " + e.getMessage()); 2729 } finally { 2730 try { 2731 rs.close(); 2732 rs1.close(); 2733 rs4.close(); 2734 rs5.close(); 2735 prst.close(); 2736 prst1.close(); 2737 } catch (Exception ee) { 2738 } 2739 } 2740 */ 2741 2742 2743 } 2744 2745 2746 @TestTargetNew( 2747 level = TestLevel.COMPLETE, 2748 notes = "not supported: it should release all resources but it doesn't", 2749 method = "close", 2750 args = {} 2751 ) testClose()2752 public void testClose() { 2753 try { 2754 2755 2756 2757 if (! conn.isClosed()) { 2758 conn.close(); 2759 } 2760 assertTrue(conn.isClosed()); 2761 2762 try { 2763 conn.prepareCall("select * from zoo"); 2764 fail("Should not be able to prepare query closed connection"); 2765 } catch (SQLException e) { 2766 //ok 2767 } 2768 } catch (SQLException e) { 2769 fail("Error in implementation"); 2770 e.printStackTrace(); 2771 } 2772 2773 } 2774 2775 @TestTargetNew( 2776 level = TestLevel.COMPLETE, 2777 notes = "not supported", 2778 method = "isClosed", 2779 args = {} 2780 ) testIsClosed()2781 public void testIsClosed() throws Exception { 2782 2783 assertFalse(conn.isClosed()); 2784 conn.close(); 2785 assertTrue(conn.isClosed()); 2786 2787 conn = DriverManager.getConnection("jdbc:sqlite:/" + dbFile.getPath()); 2788 assertFalse(conn.isClosed()); 2789 Statement st = conn.createStatement(); 2790 st.execute("select * from zoo"); 2791 } 2792 2793 2794 private static class DummySavePoint implements Savepoint{ 2795 getSavepointId()2796 public int getSavepointId() { 2797 // TODO Auto-generated method stub 2798 return 0; 2799 } 2800 getSavepointName()2801 public String getSavepointName() { 2802 // TODO Auto-generated method stub 2803 return "NoName"; 2804 } 2805 2806 } 2807 } 2808