1 21 22 package org.opensubsystems.blog.persist.db; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import junit.extensions.TestSetup; 31 import junit.framework.Test; 32 import junit.framework.TestSuite; 33 34 import org.opensubsystems.blog.data.Blog; 35 import org.opensubsystems.blog.data.Entry; 36 import org.opensubsystems.blog.persist.BlogFactory; 37 import org.opensubsystems.blog.persist.EntryFactory; 38 import org.opensubsystems.core.data.DataObject; 39 import org.opensubsystems.core.error.OSSException; 40 import org.opensubsystems.core.persist.DataFactoryManager; 41 import org.opensubsystems.core.persist.db.Database; 42 import org.opensubsystems.core.persist.db.DatabaseImpl; 43 import org.opensubsystems.core.persist.db.DatabaseSchemaManager; 44 import org.opensubsystems.core.persist.db.DatabaseTest; 45 import org.opensubsystems.core.persist.db.DatabaseTestSetup; 46 import org.opensubsystems.core.persist.db.DatabaseTestSuite; 47 import org.opensubsystems.core.util.CallContext; 48 import org.opensubsystems.core.util.DatabaseUtils; 49 50 58 public final class EntryDatabaseFactoryTest 59 { 60 62 65 private EntryDatabaseFactoryTest( 66 ) 67 { 68 } 70 71 73 79 public static Test suite( 80 ) 81 { 82 TestSuite suite = new DatabaseTestSuite("EntryDatabaseFactoryTest"); 83 suite.addTestSuite(EntryDatabaseFactoryTestInternal.class); 84 TestSetup wrapper = new DatabaseTestSetup(suite); 85 86 return wrapper; 87 } 88 89 94 public static class EntryDatabaseFactoryTestInternal extends DatabaseTest 95 { 96 98 101 protected BlogDatabaseSchema m_blogSchema; 102 103 106 protected BlogDatabaseFactory m_blogFactory; 107 108 111 protected EntryDatabaseFactory m_entryFactory; 112 113 115 118 static 119 { 120 Database dbDatabase; 121 try 122 { 123 dbDatabase = DatabaseImpl.getInstance(); 124 dbDatabase.add(DatabaseSchemaManager.getInstance(BlogDatabaseSchema.class)); 125 } 126 catch (OSSException bfeExc) 127 { 128 throw new RuntimeException ("Unexpected exception.", bfeExc); 129 } 130 } 131 132 138 public EntryDatabaseFactoryTestInternal( 139 String strTestName 140 ) throws Exception 141 { 142 super(strTestName); 143 144 m_blogSchema = (BlogDatabaseSchema)DatabaseSchemaManager.getInstance( 145 BlogDatabaseSchema.class); 146 m_blogFactory = (BlogDatabaseFactory)DataFactoryManager.getInstance( 147 BlogFactory.class); 148 m_entryFactory = (EntryDatabaseFactory)DataFactoryManager.getInstance( 149 EntryFactory.class); 150 } 151 152 154 159 public void testCreate( 160 ) throws Exception 161 { 162 Blog blog = null; 163 Entry data = null; 164 Entry testData = null; 165 Entry selectedData = null; 166 167 PreparedStatement statement = null; 168 ResultSet results = null; 169 String strQuery; 170 171 try 172 { 173 blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder", 174 "testcaption", "testcomment", null, null); 175 m_transaction.begin(); 176 try 177 { 178 blog = (Blog)m_blogFactory.create(blog); 180 m_transaction.commit(); 181 } 182 catch (Throwable thr) 183 { 184 m_transaction.rollback(); 185 throw new Exception (thr); 186 } 187 188 data = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 189 "test_entry_caption", "test_entry_comment", 190 "test_image_url", "test_target_url", null, null); 191 m_transaction.begin(); 192 try 193 { 194 testData = (Entry)m_entryFactory.create(data); 196 m_transaction.commit(); 197 } 198 catch (Throwable thr) 199 { 200 m_transaction.rollback(); 201 throw new Exception (thr); 202 } 203 204 assertNotNull("Entry should not be null", testData); 205 assertTrue("Id was not generated", testData.getId() != DataObject.NEW_ID); 206 assertNotNull("Creation timestamp was not generated", 207 testData.getCreationTimestamp()); 208 assertNotNull("Modification timestamp are not the same", 209 testData.getModificationTimestamp()); 210 assertTrue("Entry is not the same", data.isSame(testData)); 211 212 213 strQuery = m_blogSchema.getSelectEntryById( 215 BlogDatabaseSchema.ENTRY_COLUMNS); 216 217 try 218 { 219 statement = m_connection.prepareStatement(strQuery); 220 statement.setInt(1, testData.getId()); 221 statement.setInt(2, CallContext.getInstance().getCurrentDomainId()); 222 results = statement.executeQuery(); 223 224 assertTrue("Inserted data were not found.", results.next()); 225 selectedData = (Entry)m_entryFactory.load(results, 1); 226 assertNotNull("Etry should not be null", selectedData); 227 assertTrue("Entry is not the same", testData.isSame(selectedData)); 228 assertFalse("Only one data should have been created.", results.next()); 229 } 230 finally 231 { 232 DatabaseUtils.closeResultSetAndStatement(results, statement); 233 } 234 } 235 finally 236 { 237 m_transaction.begin(); 238 try 240 { 241 if ((data != null) && (data.getId() != DataObject.NEW_ID)) 242 { 243 m_entryFactory.delete(data.getId(), DataObject.NEW_ID); 245 } 246 if ((blog != null) && (blog.getId() != DataObject.NEW_ID)) 247 { 248 m_blogFactory.delete(blog.getId(), DataObject.NEW_ID); 250 } 251 m_transaction.commit(); 252 } 253 catch (Throwable thr) 254 { 255 m_transaction.rollback(); 256 throw new Exception (thr); 257 } 258 } 259 } 260 261 266 public void testCreateCollection( 267 ) throws Exception 268 { 269 PreparedStatement statement = null; 270 String strQuery; 271 ResultSet results = null; 272 273 List lstData = new ArrayList (); 274 Blog blog = null; 275 Entry testData = null; 276 Entry selectedData = null; 277 int iIndex; 278 int iInsertedCount = 0; 279 Iterator items; 280 281 blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder", 282 "testcaption", "testcomment", null, null); 283 m_transaction.begin(); 284 try 285 { 286 blog = (Blog)m_blogFactory.create(blog); 288 m_transaction.commit(); 289 } 290 catch (Throwable thr) 291 { 292 m_transaction.rollback(); 293 throw new Exception (thr); 294 } 295 296 for (iIndex = 1; iIndex < 234; iIndex++) 297 { 298 lstData.add(new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 299 "test_entry_caption_batch_" + iIndex, 300 "test_entry_comment_batch_" + iIndex, 301 "test_image_url_batch_" + iIndex, 302 "test_target_url_batch_" + iIndex, 303 null, null)); 304 } 305 306 try 307 { 308 m_transaction.begin(); 309 try 310 { 311 iInsertedCount = m_entryFactory.create(lstData); 313 m_transaction.commit(); 314 } 315 catch (Throwable thr) 316 { 317 m_transaction.rollback(); 318 throw new Exception (thr); 319 } 320 321 assertEquals("Number of inserted items is incorrect", lstData.size(), iInsertedCount); 322 323 strQuery = "select " + BlogDatabaseSchema.ENTRY_COLUMNS 325 + " from BF_BLOG_ENTRY WHERE DOMAIN_ID = ? and BLOG_ID = ? order by ID"; 326 327 try 328 { 329 statement = m_connection.prepareStatement(strQuery); 330 statement.setInt(1, CallContext.getInstance().getCurrentDomainId()); 331 statement.setInt(2, blog.getId()); 332 results = statement.executeQuery(); 333 334 for (items = lstData.iterator(), iIndex = 1; items.hasNext(); iIndex++) 335 { 336 testData = (Entry)items.next(); 337 assertTrue("Inserted data were not found.", results.next()); 338 selectedData = (Entry)m_entryFactory.load(results, 1); 339 assertNotNull("Entry should not be null", selectedData); 340 assertTrue("Entry " + iIndex + " is not the same", 342 testData.isSame(selectedData)); 343 } 344 assertFalse("Only one data should have been created.", results.next()); 345 } 346 finally 347 { 348 DatabaseUtils.closeResultSetAndStatement(results, statement); 349 } 350 } 351 finally 352 { 353 m_transaction.begin(); 355 try 356 { 357 if (iInsertedCount > 1) 358 { 359 strQuery = "delete from BF_BLOG_ENTRY where CAPTION " + 361 "like 'test_entry_caption_batch_%'"; 362 statement = m_connection.prepareStatement(strQuery); 363 statement.executeUpdate(); 364 } 365 if ((blog != null) && (blog.getId() != DataObject.NEW_ID)) 366 { 367 m_blogFactory.delete(blog.getId(), DataObject.NEW_ID); 369 } 370 m_transaction.commit(); 371 } 372 catch (Throwable thr) 373 { 374 m_transaction.rollback(); 375 throw new Exception (thr); 376 } 377 finally 378 { 379 DatabaseUtils.closeStatement(statement); 380 } 381 } 382 } 383 384 388 public void testDelete( 389 ) throws Exception 390 { 391 PreparedStatement statement = null; 392 ResultSet results = null; 393 String strQuery; 394 395 Blog blog = null; 396 Entry data1 = null; 397 Entry data2 = null; 398 Entry testdata = null; 399 400 try 401 { 402 blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder", 403 "testcaption", "testcomment", null, null); 404 405 m_transaction.begin(); 406 try 407 { 408 blog = (Blog)m_blogFactory.create(blog); 410 m_transaction.commit(); 411 } 412 catch (Throwable thr) 413 { 414 m_transaction.rollback(); 415 throw new Exception (thr); 416 } 417 418 data1 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 419 "test_entry_caption1", "test_entry_comment1", 420 "test_image_url1", "test_target_url1", null, null); 421 data2 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 422 "test_entry_caption2", "test_entry_comment2", 423 "test_image_url2", "test_target_url2", null, null); 424 425 m_transaction.begin(); 426 try 427 { 428 data1 = (Entry)m_entryFactory.create(data1); 429 m_transaction.commit(); 430 } 431 catch (Throwable thr) 432 { 433 m_transaction.rollback(); 434 throw new Exception (thr); 435 } 436 437 m_transaction.begin(); 438 try 439 { 440 data2 = (Entry)m_entryFactory.create(data2); 441 m_transaction.commit(); 442 } 443 catch (Throwable thr) 444 { 445 m_transaction.rollback(); 446 throw new Exception (thr); 447 } 448 449 m_transaction.begin(); 450 try 451 { 452 m_entryFactory.delete(data2.getId(), DataObject.NEW_ID); 454 m_transaction.commit(); 455 } 456 catch (Throwable thr) 457 { 458 m_transaction.rollback(); 459 throw new Exception (thr); 460 } 461 462 strQuery = "select * from BF_BLOG_ENTRY where BLOG_ID = ? and ID = ?"; 465 statement = m_connection.prepareStatement(strQuery); 466 statement.setInt(1, blog.getId()); 467 statement.setInt(2, data2.getId()); 468 results = statement.executeQuery(); 469 470 assertFalse("Entry 2 has to be deleted but was not", results.next()); 471 data2 = null; 472 473 strQuery = "select * from BF_BLOG_ENTRY where BLOG_ID = ? and ID = ?"; 475 statement = m_connection.prepareStatement(strQuery); 476 statement.setInt(1, blog.getId()); 477 statement.setInt(2, data1.getId()); 478 results = statement.executeQuery(); 479 480 assertTrue("Entry 1 has not be deleted but it was", results.next()); 481 482 testdata = (Entry)m_entryFactory.load(results, 1); 483 assertEquals("ID from database not equal to inserted Entry ID", 484 data1.getId(), testdata.getId()); 485 assertTrue("Inserted Entry is not the same as retrieved one", 486 data1.isSame(testdata)); 487 } 488 finally 489 { 490 m_transaction.begin(); 491 try 492 { 493 if ((data1 != null) && (data1.getId() != DataObject.NEW_ID)) 494 { 495 m_entryFactory.delete(data1.getId(), DataObject.NEW_ID); 497 } 498 if ((data2 != null) && (data2.getId() != DataObject.NEW_ID)) 499 { 500 m_entryFactory.delete(data2.getId(), DataObject.NEW_ID); 502 } 503 if ((blog != null) && (blog.getId() != DataObject.NEW_ID)) 504 { 505 m_blogFactory.delete(blog.getId(), DataObject.NEW_ID); 507 } 508 m_transaction.commit(); 509 } 510 catch (Throwable thr) 511 { 512 m_transaction.rollback(); 513 throw new Exception (thr); 514 } 515 finally 516 { 517 DatabaseUtils.closeStatement(statement); 518 } 519 } 520 } 521 522 526 public void testSave( 527 ) throws Exception 528 { 529 PreparedStatement statement = null; 530 ResultSet results = null; 531 String strQuery; 532 533 Blog blog = null; 534 Entry data1 = null; 535 Entry data2 = null; 536 Entry testdata = null; 537 538 try 539 { 540 blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder", 541 "testcaption", "testcomment", null, null); 542 543 m_transaction.begin(); 544 try 545 { 546 blog = (Blog)m_blogFactory.create(blog); 548 m_transaction.commit(); 549 } 550 catch (Throwable thr) 551 { 552 m_transaction.rollback(); 553 throw new Exception (thr); 554 } 555 556 data1 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 557 "test_entry_caption1", "test_entry_comment1", 558 "test_image_url1", "test_target_url1", null, null); 559 560 m_transaction.begin(); 561 try 562 { 563 data1 = (Entry)m_entryFactory.create(data1); 564 m_transaction.commit(); 565 } 566 catch (Throwable thr) 567 { 568 m_transaction.rollback(); 569 throw new Exception (thr); 570 } 571 572 data2 = new Entry(data1.getId(), DataObject.NEW_ID, blog.getId(), 573 "test_entry_caption2", "test_entry_comment2", 574 "test_image_url2", "test_target_url2", 575 data1.getCreationTimestamp(), data1.getModificationTimestamp()); 576 577 Thread.sleep(1000); 578 579 m_transaction.begin(); 580 try 581 { 582 testdata = (Entry)m_entryFactory.save(data2); 583 m_transaction.commit(); 584 } 585 catch (Throwable thr) 586 { 587 m_transaction.rollback(); 588 throw new Exception (thr); 589 } 590 591 assertTrue("Modification timestamp not changed", 594 testdata.getModificationTimestamp().getTime() 595 > data1.getModificationTimestamp().getTime()); 596 597 strQuery = "select * from BF_BLOG_ENTRY where BLOG_ID = ? and ID = ?"; 598 statement = m_connection.prepareStatement(strQuery); 599 statement.setInt(1, blog.getId()); 600 statement.setInt(2, data2.getId()); 601 results = statement.executeQuery(); 602 603 assertTrue("Updated item not found", results.next()); 604 605 assertEquals("ID from database not equal to inserted Entry ID", 606 data1.getId(), testdata.getId()); 607 assertTrue("Modification timestamp was not changed", 608 testdata.getModificationTimestamp().getTime() 609 > data1.getModificationTimestamp().getTime()); 610 assertTrue("Inserted Entry is not the same as retrieved one", 611 data2.isSame(testdata)); 612 613 assertFalse("More items in database", results.next()); 614 } 615 finally 616 { 617 m_transaction.begin(); 618 try 619 { 620 if ((data1 != null) && (data1.getId() != DataObject.NEW_ID)) 621 { 622 m_entryFactory.delete(data1.getId(), DataObject.NEW_ID); 624 } 625 if ((blog != null) && (blog.getId() != DataObject.NEW_ID)) 626 { 627 m_blogFactory.delete(blog.getId(), DataObject.NEW_ID); 629 } 630 m_transaction.commit(); 631 } 632 catch (Throwable thr) 633 { 634 m_transaction.rollback(); 635 throw new Exception (thr); 636 } 637 finally 638 { 639 DatabaseUtils.closeStatement(statement); 640 } 641 } 642 } 643 644 648 public void testGetEntryByID( 649 ) throws Exception 650 { 651 PreparedStatement statement = null; 652 653 Blog blog = null; 654 Entry data1 = null; 655 Entry data2 = null; 656 Entry testdata = null; 657 658 try 659 { 660 blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder", 661 "testcaption", "testcomment", null, null); 662 663 m_transaction.begin(); 664 try 665 { 666 blog = (Blog)m_blogFactory.create(blog); 668 m_transaction.commit(); 669 } 670 catch (Throwable thr) 671 { 672 m_transaction.rollback(); 673 throw new Exception (thr); 674 } 675 676 data1 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 677 "test_entry_caption1", "test_entry_comment1", 678 "test_image_url1", "test_target_url1", null, null); 679 data2 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 680 "test_entry_caption2", "test_entry_comment2", 681 "test_image_url2", "test_target_url2", null, null); 682 683 m_transaction.begin(); 684 try 685 { 686 data1 = (Entry)m_entryFactory.create(data1); 687 m_transaction.commit(); 688 } 689 catch (Throwable thr) 690 { 691 m_transaction.rollback(); 692 throw new Exception (thr); 693 } 694 695 m_transaction.begin(); 696 try 697 { 698 data2 = (Entry)m_entryFactory.create(data2); 699 m_transaction.commit(); 700 } 701 catch (Throwable thr) 702 { 703 m_transaction.rollback(); 704 throw new Exception (thr); 705 } 706 707 testdata = (Entry)m_entryFactory.get(data2.getId(), DataObject.NEW_ID); 709 710 assertEquals("Loaded ID not equal to inserted ID", 711 data2.getId(), testdata.getId()); 712 assertTrue("Loaded Entry 2 is not the same as inserted one", 713 data2.isSame(testdata)); 714 715 testdata = (Entry)m_entryFactory.get(data1.getId(), DataObject.NEW_ID); 717 718 assertEquals("Loaded ID not equal to inserted ID", 719 data1.getId(), testdata.getId()); 720 assertTrue("Loaded Entry 1 is not the same as inserted one", 721 data1.isSame(testdata)); 722 } 723 finally 724 { 725 m_transaction.begin(); 726 try 727 { 728 if ((data1 != null) && (data1.getId() != DataObject.NEW_ID)) 729 { 730 m_entryFactory.delete(data1.getId(), DataObject.NEW_ID); 732 } 733 if ((data2 != null) && (data2.getId() != DataObject.NEW_ID)) 734 { 735 m_entryFactory.delete(data2.getId(), DataObject.NEW_ID); 737 } 738 if ((blog != null) && (blog.getId() != DataObject.NEW_ID)) 739 { 740 m_blogFactory.delete(blog.getId(), DataObject.NEW_ID); 742 } 743 m_transaction.commit(); 744 } 745 catch (Throwable thr) 746 { 747 m_transaction.rollback(); 748 throw new Exception (thr); 749 } 750 finally 751 { 752 DatabaseUtils.closeStatement(statement); 753 } 754 } 755 } 756 757 761 public void testGetEntryByIncorrectID( 762 ) throws Exception 763 { 764 PreparedStatement statement = null; 765 766 Blog blog = null; 767 Entry data1 = null; 768 Entry testdata = null; 769 770 try 771 { 772 blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder", 773 "testcaption", "testcomment", null, null); 774 775 m_transaction.begin(); 776 try 777 { 778 blog = (Blog)m_blogFactory.create(blog); 780 m_transaction.commit(); 781 } 782 catch (Throwable thr) 783 { 784 m_transaction.rollback(); 785 throw new Exception (thr); 786 } 787 788 data1 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 789 "test_entry_caption1", "test_entry_comment1", 790 "test_image_url1", "test_target_url1", null, null); 791 792 m_transaction.begin(); 793 try 794 { 795 data1 = (Entry)m_entryFactory.create(data1); 796 m_transaction.commit(); 797 } 798 catch (Throwable thr) 799 { 800 m_transaction.rollback(); 801 throw new Exception (thr); 802 } 803 804 testdata = (Entry)m_entryFactory.get(data1.getId() + 1234 * 2, DataObject.NEW_ID); 805 assertNull("Entry with specified incorrect ID should not exist", testdata); 806 } 807 finally 808 { 809 m_transaction.begin(); 810 try 811 { 812 if ((data1 != null) && (data1.getId() != DataObject.NEW_ID)) 813 { 814 m_entryFactory.delete(data1.getId(), DataObject.NEW_ID); 816 } 817 if ((blog != null) && (blog.getId() != DataObject.NEW_ID)) 818 { 819 m_blogFactory.delete(blog.getId(), DataObject.NEW_ID); 821 } 822 m_transaction.commit(); 823 } 824 catch (Throwable thr) 825 { 826 m_transaction.rollback(); 827 throw new Exception (thr); 828 } 829 finally 830 { 831 DatabaseUtils.closeStatement(statement); 832 } 833 } 834 } 835 836 840 public void testGetBlogByNewID( 841 ) throws Exception 842 { 843 Entry testdata; 844 845 testdata = (Entry)m_entryFactory.get(DataObject.NEW_ID, DataObject.NEW_ID); 846 847 assertNotNull("Entry called by NEW_ID is null", testdata); 848 assertEquals("New Entry ID is not default NEW_ID", 849 testdata.getId(), DataObject.NEW_ID); 850 } 851 852 856 public void testGetAllEntries( 857 ) throws Exception 858 { 859 PreparedStatement statement = null; 860 861 int iCount1 = 123; int iCount2 = 111; 865 Blog blog1 = null; 866 Blog blog2 = null; 867 Entry data = null; 868 869 try 870 { 871 blog1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder1", 872 "testcaption1", "testcomment1", null, null); 873 874 m_transaction.begin(); 875 try 876 { 877 blog1 = (Blog)m_blogFactory.create(blog1); 879 m_transaction.commit(); 880 } 881 catch (Throwable thr) 882 { 883 m_transaction.rollback(); 884 throw new Exception (thr); 885 } 886 887 blog2 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder2", 888 "testcaption2", "testcomment2", null, null); 889 890 m_transaction.begin(); 891 try 892 { 893 blog2 = (Blog)m_blogFactory.create(blog2); 895 m_transaction.commit(); 896 } 897 catch (Throwable thr) 898 { 899 m_transaction.rollback(); 900 throw new Exception (thr); 901 } 902 903 m_transaction.begin(); 904 try 905 { 906 int iIndex; 907 for (iIndex = 0; iIndex < iCount1; iIndex++) 908 { 909 data = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog1.getId(), 911 "test_entry_caption_batch_" + iIndex, 912 "test_entry_comment_batch_" + iIndex, 913 "test_image_url_batch_" + iIndex, 914 "test_target_url_batch_" + iIndex, 915 null, null); 916 m_entryFactory.create(data); 917 } 918 m_transaction.commit(); 919 } 920 catch (Throwable thr) 921 { 922 m_transaction.rollback(); 923 throw new Exception (thr); 924 } 925 926 m_transaction.begin(); 927 try 928 { 929 int iIndex; 930 for (iIndex = 0; iIndex < iCount2; iIndex++) 931 { 932 data = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog2.getId(), 934 "test_entry_caption_batch_" + iIndex, 935 "test_entry_comment_batch_" + iIndex, 936 "test_image_url_batch_" + iIndex, 937 "test_target_url_batch_" + iIndex, 938 null, null); 939 m_entryFactory.create(data); 940 } 941 m_transaction.commit(); 942 } 943 catch (Throwable thr) 944 { 945 m_transaction.rollback(); 946 throw new Exception (thr); 947 } 948 949 List lstTestData; 950 lstTestData = m_entryFactory.getAll(blog1.getId()); 952 assertNotNull("Loaded entry list for blog 1 is null", lstTestData); 953 assertEquals("Number of loaded entries for blog 1 is incorrect", 954 iCount1, lstTestData.size()); 955 lstTestData = m_entryFactory.getAll(blog2.getId()); 957 assertNotNull("Loaded entry list for blog 2 is null", lstTestData); 958 assertEquals("Number of loaded entries for blog 2 is incorrect", 959 iCount2, lstTestData.size()); 960 } 961 finally 962 { 963 m_transaction.begin(); 964 try 965 { 966 statement = m_connection.prepareStatement( 967 "delete from BF_BLOG_ENTRY where BLOG_ID in (?, ?) " + 968 "and CAPTION like 'test_entry_caption_batch_%'"); 969 statement.setInt(1, blog1.getId()); 970 statement.setInt(2, blog2.getId()); 971 statement.executeUpdate(); 972 973 if ((blog1 != null) && (blog1.getId() != DataObject.NEW_ID)) 974 { 975 m_blogFactory.delete(blog1.getId(), DataObject.NEW_ID); 977 } 978 if ((blog2 != null) && (blog2.getId() != DataObject.NEW_ID)) 979 { 980 m_blogFactory.delete(blog2.getId(), DataObject.NEW_ID); 982 } 983 m_transaction.commit(); 984 } 985 catch (Throwable thr) 986 { 987 m_transaction.rollback(); 988 throw new Exception (thr); 989 } 990 finally 991 { 992 DatabaseUtils.closeStatement(statement); 993 } 994 } 995 } 996 997 1001 public void testGetAllEntriesNoData( 1002 ) throws Exception 1003 { 1004 Blog blog = null; 1005 1006 try 1007 { 1008 blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder1", 1009 "testcaption1", "testcomment1", null, null); 1010 1011 m_transaction.begin(); 1012 try 1013 { 1014 blog = (Blog)m_blogFactory.create(blog); 1016 m_transaction.commit(); 1017 } 1018 catch (Throwable thr) 1019 { 1020 m_transaction.rollback(); 1021 throw new Exception (thr); 1022 } 1023 1024 List lstTestData; 1025 lstTestData = m_entryFactory.getAll(blog.getId()); 1026 assertNull("Loaded entry list is not null for empty DB", lstTestData); 1027 } 1028 finally 1029 { 1030 m_transaction.begin(); 1031 try 1032 { 1033 if ((blog != null) && (blog.getId() != DataObject.NEW_ID)) 1034 { 1035 m_blogFactory.delete(blog.getId(), DataObject.NEW_ID); 1037 } 1038 m_transaction.commit(); 1039 } 1040 catch (Throwable thr) 1041 { 1042 m_transaction.rollback(); 1043 throw new Exception (thr); 1044 } 1045 } 1046 } 1047 1048 1052 public void testGetLastEntry( 1053 ) throws Exception 1054 { 1055 PreparedStatement statement = null; 1056 1057 Blog blog = null; 1058 Entry data1 = null; 1059 Entry data2 = null; 1060 Entry data3 = null; 1061 Entry data4 = null; 1062 Entry testData = null; 1063 1064 try 1065 { 1066 blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder1", 1067 "testcaption1", "testcomment1", null, null); 1068 1069 m_transaction.begin(); 1070 try 1071 { 1072 blog = (Blog)m_blogFactory.create(blog); 1074 m_transaction.commit(); 1075 } 1076 catch (Throwable thr) 1077 { 1078 m_transaction.rollback(); 1079 throw new Exception (thr); 1080 } 1081 1082 data3 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 1083 "test_entry_caption3", "test_entry_comment3", 1084 "test_image_url3", "test_target_url3", null, null); 1085 m_transaction.begin(); 1086 try 1087 { 1088 data3 = (Entry)m_entryFactory.create(data3); 1089 m_transaction.commit(); 1090 } 1091 catch (Throwable thr) 1092 { 1093 m_transaction.rollback(); 1094 throw new Exception (thr); 1095 } 1096 1097 data1 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 1098 "test_entry_caption1", "test_entry_comment1", 1099 "test_image_url1", "test_target_url1", null, null); 1100 m_transaction.begin(); 1101 try 1102 { 1103 data1 = (Entry)m_entryFactory.create(data1); 1104 m_transaction.commit(); 1105 } 1106 catch (Throwable thr) 1107 { 1108 m_transaction.rollback(); 1109 throw new Exception (thr); 1110 } 1111 1112 data4 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 1113 "test_entry_caption4", "test_entry_comment4", 1114 "test_image_url4", "test_target_url4", null, null); 1115 m_transaction.begin(); 1116 try 1117 { 1118 data4 = (Entry)m_entryFactory.create(data4); 1119 m_transaction.commit(); 1120 } 1121 catch (Throwable thr) 1122 { 1123 m_transaction.rollback(); 1124 throw new Exception (thr); 1125 } 1126 1127 data2 = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 1128 "test_entry_caption2", "test_entry_comment2", 1129 "test_image_url2", "test_target_url2", null, null); 1130 m_transaction.begin(); 1131 try 1132 { 1133 data2 = (Entry)m_entryFactory.create(data2); 1134 m_transaction.commit(); 1135 } 1136 catch (Throwable thr) 1137 { 1138 m_transaction.rollback(); 1139 throw new Exception (thr); 1140 } 1141 1142 testData = m_entryFactory.getLast(blog.getId()); 1144 assertNotNull("Last entry is null", testData); 1145 assertTrue("Last entry is incorrect", data2.isSame(testData)); 1146 } 1147 finally 1148 { 1149 m_transaction.begin(); 1150 try 1151 { 1152 if ((data1 != null) && (data1.getId() != DataObject.NEW_ID)) 1153 { 1154 m_entryFactory.delete(data1.getId(), DataObject.NEW_ID); 1156 } 1157 if ((data2 != null) && (data2.getId() != DataObject.NEW_ID)) 1158 { 1159 m_entryFactory.delete(data2.getId(), DataObject.NEW_ID); 1161 } 1162 if ((data3 != null) && (data3.getId() != DataObject.NEW_ID)) 1163 { 1164 m_entryFactory.delete(data3.getId(), DataObject.NEW_ID); 1166 } 1167 if ((data4 != null) && (data4.getId() != DataObject.NEW_ID)) 1168 { 1169 m_entryFactory.delete(data4.getId(), DataObject.NEW_ID); 1171 } 1172 if ((blog != null) && (blog.getId() != DataObject.NEW_ID)) 1173 { 1174 m_blogFactory.delete(blog.getId(), DataObject.NEW_ID); 1176 } 1177 m_transaction.commit(); 1178 } 1179 catch (Throwable thr) 1180 { 1181 m_transaction.rollback(); 1182 throw new Exception (thr); 1183 } 1184 finally 1185 { 1186 DatabaseUtils.closeStatement(statement); 1187 } 1188 } 1189 } 1190 1191 1195 public void testLoadEntry( 1196 ) throws Exception 1197 { 1198 PreparedStatement statement = null; 1199 ResultSet results = null; 1200 String strQuery; 1201 1202 Blog blog = null; 1203 Entry data = null; 1204 Entry testdata = null; 1205 Entry loadData = null; 1206 1207 try 1208 { 1209 blog = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder", 1210 "testcaption", "testcomment", null, null); 1211 1212 m_transaction.begin(); 1213 try 1214 { 1215 blog = (Blog)m_blogFactory.create(blog); 1217 m_transaction.commit(); 1218 } 1219 catch (Throwable thr) 1220 { 1221 m_transaction.rollback(); 1222 throw new Exception (thr); 1223 } 1224 1225 data = new Entry(DataObject.NEW_ID, DataObject.NEW_ID, blog.getId(), 1226 "test_entry_caption1", "test_entry_comment1", 1227 "test_image_url1", "test_target_url1", null, null); 1228 1229 m_transaction.begin(); 1230 try 1231 { 1232 data = (Entry)m_entryFactory.create(data); 1233 m_transaction.commit(); 1234 } 1235 catch (Throwable thr) 1236 { 1237 m_transaction.rollback(); 1238 throw new Exception (thr); 1239 } 1240 1241 strQuery = "select * from BF_BLOG_ENTRY where BLOG_ID = ? and ID = ?"; 1243 statement = m_connection.prepareStatement(strQuery); 1244 statement.setInt(1, blog.getId()); 1245 statement.setInt(2, data.getId()); 1246 results = statement.executeQuery(); 1247 1248 assertTrue("Inserted entry was not found", results.next()); 1249 1250 loadData = (Entry)m_entryFactory.load(results, 1); 1251 testdata = new Entry(results.getInt("ID"), 1252 results.getInt("DOMAIN_ID"), 1253 results.getInt("BLOG_ID"), 1254 results.getString("CAPTION"), 1255 results.getString("COMMENTS"), 1256 results.getString("IMAGEURL"), 1257 results.getString("TARGETURL"), 1258 results.getTimestamp("CREATION_DATE"), 1259 results.getTimestamp("MODIFICATION_DATE") 1260 ); 1261 1262 assertEquals("Loaded ID is incorrect", 1263 testdata.getId(), loadData.getId()); 1264 assertTrue("Loaded Entry is not the same", 1265 testdata.isSame(loadData)); 1266 assertFalse("More items in database", results.next()); 1267 } 1268 finally 1269 { 1270 m_transaction.begin(); 1271 try 1272 { 1273 if ((data != null) && (data.getId() != DataObject.NEW_ID)) 1274 { 1275 m_entryFactory.delete(data.getId(), DataObject.NEW_ID); 1277 } 1278 if ((blog != null) && (blog.getId() != DataObject.NEW_ID)) 1279 { 1280 m_blogFactory.delete(blog.getId(), DataObject.NEW_ID); 1282 } 1283 m_transaction.commit(); 1284 } 1285 catch (Throwable thr) 1286 { 1287 m_transaction.rollback(); 1288 throw new Exception (thr); 1289 } 1290 finally 1291 { 1292 DatabaseUtils.closeStatement(statement); 1293 } 1294 } 1295 } 1296 } 1297} 1298 | Popular Tags |