1 21 22 package org.opensubsystems.blog.persist.db; 23 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import junit.extensions.TestSetup; 33 import junit.framework.Test; 34 import junit.framework.TestSuite; 35 36 import org.opensubsystems.blog.data.Blog; 37 import org.opensubsystems.blog.persist.BlogFactory; 38 import org.opensubsystems.core.data.DataObject; 39 import org.opensubsystems.core.error.OSSException; 40 import org.opensubsystems.core.error.OSSInvalidDataException; 41 import org.opensubsystems.core.persist.DataFactoryManager; 42 import org.opensubsystems.core.persist.db.Database; 43 import org.opensubsystems.core.persist.db.DatabaseImpl; 44 import org.opensubsystems.core.persist.db.DatabaseSchemaManager; 45 import org.opensubsystems.core.persist.db.DatabaseTest; 46 import org.opensubsystems.core.persist.db.DatabaseTestSetup; 47 import org.opensubsystems.core.persist.db.DatabaseTestSuite; 48 import org.opensubsystems.core.util.CallContext; 49 import org.opensubsystems.core.util.DatabaseUtils; 50 51 59 public final class BlogDatabaseFactoryTest 60 { 61 63 66 private BlogDatabaseFactoryTest( 67 ) 68 { 69 } 71 72 74 80 public static Test suite( 81 ) 82 { 83 TestSuite suite = new DatabaseTestSuite("BlogDatabaseFactoryTest"); 84 suite.addTestSuite(BlogDatabaseFactoryTestInternal.class); 85 TestSetup wrapper = new DatabaseTestSetup(suite); 86 87 return wrapper; 88 } 89 90 95 public static class BlogDatabaseFactoryTestInternal extends DatabaseTest 96 { 97 99 102 protected BlogDatabaseSchema m_blogSchema; 103 104 107 protected BlogDatabaseFactory m_blogFactory; 108 109 111 114 static 115 { 116 Database dbDatabase; 117 try 118 { 119 dbDatabase = DatabaseImpl.getInstance(); 120 dbDatabase.add(DatabaseSchemaManager.getInstance(BlogDatabaseSchema.class)); 121 } 122 catch (OSSException bfeExc) 123 { 124 throw new RuntimeException ("Unexpected exception.", bfeExc); 125 } 126 } 127 128 134 public BlogDatabaseFactoryTestInternal( 135 String strTestName 136 ) throws Exception 137 { 138 super(strTestName); 139 140 m_blogSchema = (BlogDatabaseSchema)DatabaseSchemaManager.getInstance( 141 BlogDatabaseSchema.class); 142 m_blogFactory = (BlogDatabaseFactory)DataFactoryManager.getInstance( 143 BlogFactory.class); 144 } 145 146 148 153 public void testCreate( 154 ) throws Exception 155 { 156 Blog data = null; 157 Blog testData = null; 158 Blog selectedData = null; 159 160 PreparedStatement statement = null; 161 ResultSet results = null; 162 String strQuery; 163 164 data = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, "testfolder", 165 "testcaption", "testcomment", null, null); 166 167 try 168 { 169 m_transaction.begin(); 170 try 171 { 172 testData = (Blog)m_blogFactory.create(data); 173 m_transaction.commit(); 174 } 175 catch (Throwable thr) 176 { 177 m_transaction.rollback(); 178 throw new Exception (thr); 179 } 180 181 assertNotNull("Blog should not be null", testData); 182 assertTrue("Id was not generated", testData.getId() != DataObject.NEW_ID); 183 assertNotNull("Creation timestamp was not generated", 184 testData.getCreationTimestamp()); 185 assertNotNull("Modification timestamp are not the same", 186 testData.getModificationTimestamp()); 187 assertTrue("Blog is not the same", data.isSame(testData)); 188 189 190 strQuery = m_blogSchema.getSelectBlogById( 192 BlogDatabaseSchema.BLOG_COLUMNS); 193 194 try 195 { 196 statement = m_connection.prepareStatement(strQuery); 197 statement.setInt(1, testData.getId()); 198 statement.setInt(2, CallContext.getInstance().getCurrentDomainId()); 199 results = statement.executeQuery(); 200 201 assertTrue("Inserted data were not found.", results.next()); 202 selectedData = (Blog)m_blogFactory.load(results, 1); 203 assertNotNull("Blog should not be null", selectedData); 204 assertEquals("Blog is not the same", testData, selectedData); 205 assertFalse("Only one data should have been created.", results.next()); 206 } 207 finally 208 { 209 DatabaseUtils.closeResultSetAndStatement(results, statement); 210 } 211 } 212 finally 213 { 214 if ((data != null) && (data.getId() != DataObject.NEW_ID)) 215 { 216 m_transaction.begin(); 217 try 219 { 220 m_blogFactory.delete(data.getId(), DataObject.NEW_ID); 222 m_transaction.commit(); 223 } 224 catch (Throwable thr) 225 { 226 m_transaction.rollback(); 227 throw new Exception (thr); 228 } 229 } 230 } 231 } 232 233 238 public void testCreateCollection( 239 ) throws Exception 240 { 241 PreparedStatement statement = null; 242 String strQuery; 243 ResultSet results = null; 244 245 List lstData = new ArrayList (); 246 Blog testData = null; 247 Blog selectedData = null; 248 int iIndex; 249 int iInsertedCount = 0; 250 Iterator items; 251 252 for (iIndex = 1; iIndex < 234; iIndex++) 253 { 254 lstData.add(new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 255 "testfolder_batch_" + iIndex, 256 "testcaption_batch_" + iIndex, 257 "testcomment_batch_" + iIndex, null, null)); 258 } 259 260 try 261 { 262 m_transaction.begin(); 263 try 264 { 265 iInsertedCount = m_blogFactory.create(lstData); 267 m_transaction.commit(); 268 } 269 catch (Throwable thr) 270 { 271 m_transaction.rollback(); 272 throw new Exception (thr); 273 } 274 275 assertEquals("Number of inserted items is incorrect", lstData.size(), iInsertedCount); 276 277 strQuery = "select " + BlogDatabaseSchema.BLOG_COLUMNS 279 + " from BF_BLOG WHERE DOMAIN_ID = ? order by ID"; 280 281 try 282 { 283 statement = m_connection.prepareStatement(strQuery); 284 statement.setInt(1, CallContext.getInstance().getCurrentDomainId()); 285 results = statement.executeQuery(); 286 287 for (items = lstData.iterator(), iIndex = 1; items.hasNext(); iIndex++) 288 { 289 testData = (Blog)items.next(); 290 assertTrue("Inserted data were not found.", results.next()); 291 selectedData = (Blog)m_blogFactory.load(results, 1); 292 assertNotNull("Blog should not be null", selectedData); 293 assertTrue("Blog " + iIndex + " is not the same", 295 testData.isSame(selectedData)); 296 } 297 assertFalse("Only one data should have been created.", results.next()); 298 } 299 finally 300 { 301 DatabaseUtils.closeResultSetAndStatement(results, statement); 302 } 303 } 304 finally 305 { 306 if (iInsertedCount > 1) 307 { 308 m_transaction.begin(); 309 try 311 { 312 strQuery = "delete from BF_BLOG where FOLDER like 'testfolder_batch_%'"; 314 statement = m_connection.prepareStatement(strQuery); 315 statement.executeUpdate(); 316 m_transaction.commit(); 317 } 318 catch (Throwable thr) 319 { 320 m_transaction.rollback(); 321 throw new Exception (thr); 322 } 323 finally 324 { 325 DatabaseUtils.closeStatement(statement); 326 } 327 } 328 } 329 } 330 331 335 public void testDelete( 336 ) throws Exception 337 { 338 PreparedStatement statement = null; 339 ResultSet results = null; 340 String strQuery; 341 342 Blog data1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 343 "testfolder1", "testcaption1", 344 "testcomment1", null, null); 345 Blog data2 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 346 "testfolder2", "testcaption2", 347 "testcomment2", null, null); 348 349 try 350 { 351 m_transaction.begin(); 352 try 353 { 354 data1 = (Blog)m_blogFactory.create(data1); 355 m_transaction.commit(); 356 } 357 catch (Throwable thr) 358 { 359 m_transaction.rollback(); 360 throw new Exception (thr); 361 } 362 363 m_transaction.begin(); 364 try 365 { 366 data2 = (Blog)m_blogFactory.create(data2); 367 m_transaction.commit(); 368 } 369 catch (Throwable thr) 370 { 371 m_transaction.rollback(); 372 throw new Exception (thr); 373 } 374 375 m_transaction.begin(); 376 try 377 { 378 m_blogFactory.delete(data2.getId(), DataObject.NEW_ID); 380 m_transaction.commit(); 381 } 382 catch (Throwable thr) 383 { 384 m_transaction.rollback(); 385 throw new Exception (thr); 386 } 387 388 strQuery = "select * from BF_BLOG where ID = ?"; 391 statement = m_connection.prepareStatement(strQuery); 392 statement.setInt(1, data2.getId()); 393 results = statement.executeQuery(); 394 395 assertFalse("Blog 2 has to be deleted but was not", results.next()); 396 data2 = null; 397 398 strQuery = "select * from BF_BLOG where ID = ?"; 400 statement = m_connection.prepareStatement(strQuery); 401 statement.setInt(1, data1.getId()); 402 results = statement.executeQuery(); 403 404 assertTrue("Blog 1 has not be deleted but it was", results.next()); 405 406 Blog testdata; 407 testdata = (Blog)m_blogFactory.load(results, 1); 408 assertEquals("ID from database not equal to inserted Blog ID", 409 data1.getId(), testdata.getId()); 410 assertEquals("Folder from database not equal to inserted Blog folder", 411 data1.getFolder(), testdata.getFolder()); 412 } 413 finally 414 { 415 m_transaction.begin(); 416 try 417 { 418 if ((data1 != null) && (data1.getId() != DataObject.NEW_ID)) 419 { 420 m_blogFactory.delete(data1.getId(), DataObject.NEW_ID); 422 } 423 if ((data2 != null) && (data2.getId() != DataObject.NEW_ID)) 424 { 425 m_blogFactory.delete(data2.getId(), DataObject.NEW_ID); 427 } 428 m_transaction.commit(); 429 } 430 catch (Throwable thr) 431 { 432 m_transaction.rollback(); 433 throw new Exception (thr); 434 } 435 finally 436 { 437 DatabaseUtils.closeStatement(statement); 438 } 439 } 440 } 441 442 446 public void testSave( 447 ) throws Exception 448 { 449 PreparedStatement statement = null; 450 ResultSet results = null; 451 String strQuery; 452 453 Blog data1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 454 "testfolder1", "testcaption1", 455 "testcomment1", null, null); 456 Blog data2 = null; 457 458 try 459 { 460 m_transaction.begin(); 461 try 462 { 463 data1 = (Blog)m_blogFactory.create(data1); 464 m_transaction.commit(); 465 } 466 catch (Throwable thr) 467 { 468 m_transaction.rollback(); 469 throw new Exception (thr); 470 } 471 472 data2 = new Blog(data1.getId(), DataObject.NEW_ID, 473 "testfolder2", "testcaption2", 474 "testcomment2", data1.getCreationTimestamp(), 475 data1.getModificationTimestamp()); 476 Blog testdata; 477 478 Thread.sleep(1000); 479 480 m_transaction.begin(); 481 try 482 { 483 testdata = (Blog)m_blogFactory.save(data2); 484 m_transaction.commit(); 485 } 486 catch (Throwable thr) 487 { 488 m_transaction.rollback(); 489 throw new Exception (thr); 490 } 491 492 assertTrue("Modification timestamp not changed", 495 testdata.getModificationTimestamp().getTime() 496 > data1.getModificationTimestamp().getTime()); 497 498 strQuery = "select * from BF_BLOG where ID = ?"; 499 statement = m_connection.prepareStatement(strQuery); 500 statement.setInt(1, data2.getId()); 501 results = statement.executeQuery(); 502 503 assertTrue("Updated item not found", results.next()); 504 505 assertEquals("ID from database not equal to inserted Blog ID", 506 data1.getId(), testdata.getId()); 507 assertEquals("Folder from database was not updated", 508 data2.getFolder(), testdata.getFolder()); 509 assertEquals("Caption from database was not updated", 510 data2.getCaption(), testdata.getCaption()); 511 assertEquals("Comments from database was not updated", 512 data2.getComments(), testdata.getComments()); 513 assertEquals("Creation Timestamp not equals", 514 data1.getCreationTimestamp(), testdata.getCreationTimestamp()); 515 assertNotNull("Modification timestamp is null", testdata.getModificationTimestamp()); 516 assertTrue("Modification timestamp was not changed", 517 testdata.getModificationTimestamp().getTime() 518 > data1.getModificationTimestamp().getTime()); 519 520 assertFalse("More items in database", results.next()); 521 } 522 finally 523 { 524 m_transaction.begin(); 525 try 526 { 527 if ((data1 != null) && (data1.getId() != DataObject.NEW_ID)) 528 { 529 m_blogFactory.delete(data1.getId(), DataObject.NEW_ID); 531 } 532 m_transaction.commit(); 533 } 534 catch (Throwable thr) 535 { 536 m_transaction.rollback(); 537 throw new Exception (thr); 538 } 539 finally 540 { 541 DatabaseUtils.closeStatement(statement); 542 } 543 } 544 } 545 546 550 public void testGetBlogByID( 551 ) throws Exception 552 { 553 PreparedStatement statement = null; 554 555 Blog data1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 556 "testfolder1", "testcaption1", 557 "testcomment1", null, null); 558 559 Blog data2 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 560 "testfolder2", "testcaption2", 561 "testcomment2", null, null); 562 Blog testdata = null; 563 try 564 { 565 m_transaction.begin(); 566 try 567 { 568 data1 = (Blog)m_blogFactory.create(data1); 569 m_transaction.commit(); 570 } 571 catch (Throwable thr) 572 { 573 m_transaction.rollback(); 574 throw new Exception (thr); 575 } 576 577 m_transaction.begin(); 578 try 579 { 580 data2 = (Blog)m_blogFactory.create(data2); 581 m_transaction.commit(); 582 } 583 catch (Throwable thr) 584 { 585 m_transaction.rollback(); 586 throw new Exception (thr); 587 } 588 589 testdata = (Blog)m_blogFactory.get(data2.getId(), DataObject.NEW_ID); 591 592 assertEquals("Loaded ID not equal to inserted ID", 593 data2.getId(), testdata.getId()); 594 assertEquals("Loaded Folder not equal to inserted", 595 data2.getFolder(), testdata.getFolder()); 596 assertEquals("Loaded Caption not equal to inserted", 597 data2.getCaption(), testdata.getCaption()); 598 assertEquals("Loaded Comments not equal to inserted", 599 data2.getComments(), testdata.getComments()); 600 assertEquals("Loaded Creation Timestamp not equal to inserted", 601 data2.getCreationTimestamp(), testdata.getCreationTimestamp()); 602 assertEquals("Loaded Modification Timestamp not equal to inserted", 603 data2.getModificationTimestamp(), 604 testdata.getModificationTimestamp()); 605 606 testdata = (Blog)m_blogFactory.get(data1.getId(), DataObject.NEW_ID); 608 609 assertEquals("Loaded ID not equal to inserted ID", 610 data1.getId(), testdata.getId()); 611 assertEquals("Loaded Folder not equal to inserted", 612 data1.getFolder(), testdata.getFolder()); 613 assertEquals("Loaded Caption not equal to inserted", 614 data1.getCaption(), testdata.getCaption()); 615 assertEquals("Loaded Comments not equal to inserted", 616 data1.getComments(), testdata.getComments()); 617 assertEquals("Loaded Creation Timestamp not equal to inserted", 618 data1.getCreationTimestamp(), testdata.getCreationTimestamp()); 619 assertEquals("Loaded Modification Timestamp not equal to inserted", 620 data1.getModificationTimestamp(), 621 testdata.getModificationTimestamp()); 622 } 623 finally 624 { 625 m_transaction.begin(); 626 try 627 { 628 if ((data1 != null) && (data1.getId() != DataObject.NEW_ID)) 629 { 630 m_blogFactory.delete(data1.getId(), DataObject.NEW_ID); 632 } 633 if ((data2 != null) && (data2.getId() != DataObject.NEW_ID)) 634 { 635 m_blogFactory.delete(data2.getId(), DataObject.NEW_ID); 637 } 638 m_transaction.commit(); 639 } 640 catch (Throwable thr) 641 { 642 m_transaction.rollback(); 643 throw new Exception (thr); 644 } 645 finally 646 { 647 DatabaseUtils.closeStatement(statement); 648 } 649 } 650 } 651 652 656 public void testGetBlogByIncorrectID( 657 ) throws Exception 658 { 659 PreparedStatement statement = null; 660 661 Blog data1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 662 "testfolder1", "testcaption1", 663 "testcomment1", null, null); 664 665 Blog testdata = null; 666 try 667 { 668 m_transaction.begin(); 669 try 670 { 671 data1 = (Blog)m_blogFactory.create(data1); 672 m_transaction.commit(); 673 } 674 catch (Throwable thr) 675 { 676 m_transaction.rollback(); 677 throw new Exception (thr); 678 } 679 680 testdata = (Blog)m_blogFactory.get(data1.getId() + 1234 * 2, DataObject.NEW_ID); 681 assertNull("Blog with specified incorrect ID should not exist", testdata); 682 } 683 finally 684 { 685 m_transaction.begin(); 686 try 687 { 688 if ((data1 != null) && (data1.getId() != DataObject.NEW_ID)) 689 { 690 m_blogFactory.delete(data1.getId(), DataObject.NEW_ID); 692 } 693 m_transaction.commit(); 694 } 695 catch (Throwable thr) 696 { 697 m_transaction.rollback(); 698 throw new Exception (thr); 699 } 700 finally 701 { 702 DatabaseUtils.closeStatement(statement); 703 } 704 } 705 } 706 707 711 public void testGetBlogByNewID( 712 ) throws Exception 713 { 714 Blog testdata; 715 716 testdata = (Blog)m_blogFactory.get(DataObject.NEW_ID, DataObject.NEW_ID); 717 718 assertNotNull("Blog called by NEW_ID is null", testdata); 719 assertEquals("New Blog ID is not default NEW_ID", 720 testdata.getId(), DataObject.NEW_ID); 721 } 722 723 727 public void testGetBlogByFolder( 728 ) throws Exception 729 { 730 PreparedStatement statement = null; 731 732 Blog data1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 733 "testfolder1", "testcaption1", 734 "testcomment1", null, null); 735 736 Blog data2 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 737 "testfolder2", "testcaption2", 738 "testcomment2", null, null); 739 Blog testdata; 740 741 try 742 { 743 m_transaction.begin(); 744 try 745 { 746 data1 = (Blog)m_blogFactory.create(data1); 747 m_transaction.commit(); 748 } 749 catch (Throwable thr) 750 { 751 m_transaction.rollback(); 752 throw new Exception (thr); 753 } 754 755 m_transaction.begin(); 756 try 757 { 758 data2 = (Blog)m_blogFactory.create(data2); 759 m_transaction.commit(); 760 } 761 catch (Throwable thr) 762 { 763 m_transaction.rollback(); 764 throw new Exception (thr); 765 } 766 767 testdata = (Blog)m_blogFactory.get(data2.getFolder()); 768 769 assertEquals("Loaded ID not equal to inserted ID", 770 data2.getId(), testdata.getId()); 771 assertEquals("Loaded Folder not equal to inserted", 772 data2.getFolder(), testdata.getFolder()); 773 assertEquals("Loaded Caption not equal to inserted", 774 data2.getCaption(), testdata.getCaption()); 775 assertEquals("Loaded Comments not equal to inserted", 776 data2.getComments(), testdata.getComments()); 777 assertEquals("Loaded Creation Timestamp not equal to inserted", 778 data2.getCreationTimestamp(), testdata.getCreationTimestamp()); 779 assertEquals("Loaded Modification Timestamp not equal to inserted", 780 data2.getModificationTimestamp(), 781 testdata.getModificationTimestamp()); 782 } 783 finally 784 { 785 m_transaction.begin(); 786 try 787 { 788 if ((data1 != null) && (data1.getId() != DataObject.NEW_ID)) 789 { 790 m_blogFactory.delete(data1.getId(), DataObject.NEW_ID); 792 } 793 if ((data2 != null) && (data2.getId() != DataObject.NEW_ID)) 794 { 795 m_blogFactory.delete(data2.getId(), DataObject.NEW_ID); 797 } 798 m_transaction.commit(); 799 } 800 catch (Throwable thr) 801 { 802 m_transaction.rollback(); 803 throw new Exception (thr); 804 } 805 finally 806 { 807 DatabaseUtils.closeStatement(statement); 808 } 809 } 810 } 811 812 816 public void testGetBlogByIncorrectFolder( 817 ) throws Exception 818 { 819 Blog data = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 820 "testfolder", "testcaption", 821 "testcomment", null, null); 822 823 PreparedStatement statement = null; 824 try 825 { 826 m_transaction.begin(); 827 try 828 { 829 data = (Blog)m_blogFactory.create(data); 830 m_transaction.commit(); 831 } 832 catch (Throwable thr) 833 { 834 m_transaction.rollback(); 835 throw new Exception (thr); 836 } 837 838 Blog testdata; 839 840 testdata = m_blogFactory.get(data.getFolder() + "_incorrect"); 841 assertNull("Blog with called Folder name should not exist", testdata); 842 } 843 finally 844 { 845 m_transaction.begin(); 846 try 847 { 848 if ((data != null) && (data.getId() != DataObject.NEW_ID)) 849 { 850 m_blogFactory.delete(data.getId(), DataObject.NEW_ID); 852 } 853 m_transaction.commit(); 854 } 855 catch (Throwable thr) 856 { 857 m_transaction.rollback(); 858 throw new Exception (thr); 859 } 860 finally 861 { 862 DatabaseUtils.closeStatement(statement); 863 } 864 } 865 } 866 867 871 public void testCheckUniqueConstraint( 872 ) throws Exception 873 { 874 PreparedStatement statement = null; 875 876 Blog data1 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 877 "testfolder1", "testcaption1", 878 "testcomment1", null, null); 879 880 Blog data2 = null; 881 Blog testdata = null; 882 883 try 884 { 885 m_transaction.begin(); 886 try 887 { 888 testdata = (Blog)m_blogFactory.create(data1); 890 m_transaction.commit(); 891 } 892 catch (Throwable thr) 893 { 894 m_transaction.rollback(); 895 throw new Exception (thr); 896 } 897 898 assertNotNull("Blog 1 should be created but was not", testdata); 899 assertTrue("Id was not generated", testdata.getId() != DataObject.NEW_ID); 900 assertNotNull("Creation timestamp was not generated", 901 testdata.getCreationTimestamp()); 902 assertNotNull("Modification timestamp was not generated", 903 testdata.getModificationTimestamp()); 904 assertTrue("Blog 1 is not the same", data1.isSame(testdata)); 905 906 data2 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 909 "testfolder1", "testcaption2", 910 "testcomment2", null, null); 911 m_transaction.begin(); 912 try 913 { 914 data2 = (Blog)m_blogFactory.create(data2); 916 m_transaction.commit(); 917 fail("There has to be thrown blog folder name unique constraint exception."); 918 } 919 catch (OSSInvalidDataException ideExc) 920 { 921 m_transaction.rollback(); 925 926 StringWriter swStringWriter = new StringWriter (); 927 PrintWriter pwPrintWritter = new PrintWriter (swStringWriter); 928 929 String strAllMessages = ideExc.getErrorMessages().getAllErrorMessages(";"); 930 931 ideExc.printStackTrace(pwPrintWritter); 932 assertTrue("There shoul be thrown BF_BLOG_FLDR_UQ unique constraint SQL exception", 933 (((swStringWriter.toString().toUpperCase().indexOf("BF_BLOG_FLDR_UQ") > -1) 934 || (swStringWriter.toString().toUpperCase().indexOf("KEY 2") > -1) 936 || ((swStringWriter.toString().toUpperCase()).indexOf("\"2\"") > -1)) 938 && (strAllMessages.indexOf("Folder has to be unique.") > -1)) 939 ); 940 } 941 catch (Throwable thr) 942 { 943 m_transaction.rollback(); 944 throw new Exception (thr); 945 } 946 } 947 finally 948 { 949 m_transaction.begin(); 950 try 951 { 952 if ((testdata != null) && (testdata.getId() != DataObject.NEW_ID)) 953 { 954 m_blogFactory.delete(testdata.getId(), DataObject.NEW_ID); 956 } 957 m_transaction.commit(); 958 } 959 catch (Throwable thr) 960 { 961 m_transaction.rollback(); 962 throw new Exception (thr); 963 } 964 finally 965 { 966 DatabaseUtils.closeStatement(statement); 967 } 968 } 969 } 970 971 975 public void testGetAllBlogs( 976 ) throws Exception 977 { 978 Blog data; 979 int iCount = 123; 981 PreparedStatement statement = null; 982 try 983 { 984 m_transaction.begin(); 985 try 986 { 987 int iIndex; 988 for (iIndex = 0; iIndex < iCount; iIndex++) 989 { 990 991 data = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 992 "testfolder_" + iIndex, "testcaption" + iIndex, 993 "testcomment" + iIndex, null, null); 994 m_blogFactory.create(data); 995 } 996 m_transaction.commit(); 997 } 998 catch (Throwable thr) 999 { 1000 m_transaction.rollback(); 1001 throw new Exception (thr); 1002 } 1003 1004 List lstTestData; 1005 1006 lstTestData = m_blogFactory.getAll(); 1007 1008 assertNotNull("Loaded blog list is null", lstTestData); 1009 assertEquals("Number of loaded blogs is incorrect", 1010 iCount, lstTestData.size()); 1011 } 1012 finally 1013 { 1014 m_transaction.begin(); 1015 try 1016 { 1017 statement = m_connection.prepareStatement("delete from BF_BLOG where FOLDER " + 1018 "like 'testfolder_%'"); 1019 statement.executeUpdate(); 1020 m_transaction.commit(); 1021 } 1022 catch (Throwable thr) 1023 { 1024 m_transaction.rollback(); 1025 throw new Exception (thr); 1026 } 1027 finally 1028 { 1029 DatabaseUtils.closeStatement(statement); 1030 } 1031 } 1032 } 1033 1034 1038 public void testGetAllBlogsNoData( 1039 ) throws Exception 1040 { 1041 List lstTestData; 1042 1043 lstTestData = m_blogFactory.getAll(); 1044 1045 assertNull("Loaded blog list is not null for empty DB", lstTestData); 1046 } 1047 1048 1052 public void testLoadBlog( 1053 ) throws Exception 1054 { 1055 PreparedStatement statement = null; 1056 ResultSet results = null; 1057 String strQuery; 1058 1059 Blog data = new Blog(DataObject.NEW_ID, DataObject.NEW_ID, 1060 "testfolder1", "testcaption1", 1061 "testcomment1", null, null); 1062 Blog testdata = null; 1063 Blog loadData = null; 1064 1065 try 1066 { 1067 m_transaction.begin(); 1068 try 1069 { 1070 data = (Blog)m_blogFactory.create(data); 1071 m_transaction.commit(); 1072 } 1073 catch (Throwable thr) 1074 { 1075 m_transaction.rollback(); 1076 throw new Exception (thr); 1077 } 1078 1079 strQuery = "select * from BF_BLOG where ID = ?"; 1081 statement = m_connection.prepareStatement(strQuery); 1082 statement.setInt(1, data.getId()); 1083 results = statement.executeQuery(); 1084 1085 assertTrue("Inserted blog was not found", results.next()); 1086 1087 loadData = (Blog)m_blogFactory.load(results, 1); 1088 testdata = new Blog(results.getInt("ID"), 1089 results.getInt("DOMAIN_ID"), 1090 results.getString("FOLDER"), 1091 results.getString("CAPTION"), 1092 results.getString("COMMENTS"), 1093 results.getTimestamp("CREATION_DATE"), 1094 results.getTimestamp("MODIFICATION_DATE") 1095 ); 1096 1097 assertEquals("Loaded ID not correct", 1098 testdata.getId(), loadData.getId()); 1099 assertEquals("Loaded Folder not correct", 1100 testdata.getFolder(), loadData.getFolder()); 1101 assertEquals("Loaded Caption not correct", 1102 testdata.getCaption(), loadData.getCaption()); 1103 assertEquals("Loaded Comments not correct", 1104 testdata.getComments(), loadData.getComments()); 1105 assertEquals("Loaded Creation Timestamp not correct", 1106 testdata.getCreationTimestamp(), loadData.getCreationTimestamp()); 1107 assertEquals("Loaded Modification timestamp not correct", 1108 testdata.getModificationTimestamp(), 1109 loadData.getModificationTimestamp()); 1110 assertFalse("More items in database", results.next()); 1111 } 1112 finally 1113 { 1114 m_transaction.begin(); 1115 try 1116 { 1117 if ((data != null) && (data.getId() != DataObject.NEW_ID)) 1118 { 1119 m_blogFactory.delete(data.getId(), DataObject.NEW_ID); 1121 } 1122 m_transaction.commit(); 1123 } 1124 catch (Throwable thr) 1125 { 1126 m_transaction.rollback(); 1127 throw new Exception (thr); 1128 } 1129 finally 1130 { 1131 DatabaseUtils.closeStatement(statement); 1132 } 1133 } 1134 } 1135 } 1136} 1137 | Popular Tags |