KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > blog > persist > db > BlogDatabaseFactoryTest


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenChronicle
5  *
6  * $Id: BlogDatabaseFactoryTest.java,v 1.8 2007/02/01 07:34:37 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.blog.persist.db;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
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 /**
52  * Test functionality of BlogDatabaseFactory class.
53  *
54  * @version $Id: BlogDatabaseFactoryTest.java,v 1.8 2007/02/01 07:34:37 bastafidli Exp $
55  * @author Miro Halas
56  * @code.reviewer Miro Halas
57  * @code.reviewed Initial revision
58  */

59 public final class BlogDatabaseFactoryTest
60 {
61    // Constructors /////////////////////////////////////////////////////////////
62

63    /**
64     * Private constructor since this class cannot be instantiated
65     */

66    private BlogDatabaseFactoryTest(
67    )
68    {
69       // Do nothing
70
}
71    
72    // Public methods ///////////////////////////////////////////////////////////
73

74    /**
75     * Create the suite for this test since this is the only way how to create
76     * test setup which can initialize and shutdown the database for us
77     *
78     * @return Test - suite of tests to run for this database
79     */

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    /**
91     * Internal class which can be included in other test suites directly without
92     * including the above suite. This allows us to group multiple tests
93     * together and the execute the DatabaseTestSetup only once.
94     */

95    public static class BlogDatabaseFactoryTestInternal extends DatabaseTest
96    {
97       // Cached values ////////////////////////////////////////////////////////////
98

99       /**
100        * Schema to manage blogs.
101        */

102       protected BlogDatabaseSchema m_blogSchema;
103    
104       /**
105        * Factory to manage blogs.
106        */

107       protected BlogDatabaseFactory m_blogFactory;
108    
109       // Constructors /////////////////////////////////////////////////////////////
110

111       /**
112        * Static initializer.
113        */

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 JavaDoc("Unexpected exception.", bfeExc);
125          }
126       }
127    
128       /**
129        * Constructor
130        *
131        * @param strTestName - name of the test
132        * @throws Exception - an error has occured
133        */

134       public BlogDatabaseFactoryTestInternal(
135          String JavaDoc strTestName
136       ) throws Exception JavaDoc
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       // Tests ////////////////////////////////////////////////////////////////////
147

148       /**
149        * Test creation of blogs.
150        *
151        * @throws Exception - an error has occured during test
152        */

153       public void testCreate(
154       ) throws Exception JavaDoc
155       {
156          Blog data = null;
157          Blog testData = null;
158          Blog selectedData = null;
159          
160          PreparedStatement JavaDoc statement = null;
161          ResultSet JavaDoc results = null;
162          String JavaDoc 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 JavaDoc thr)
176             {
177                m_transaction.rollback();
178                throw new Exception JavaDoc(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             // try to test inserted blog data from the DB
191
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                // delete inserted records
218
try
219                {
220                   // delete blog
221
m_blogFactory.delete(data.getId(), DataObject.NEW_ID);
222                   m_transaction.commit();
223                }
224                catch (Throwable JavaDoc thr)
225                {
226                   m_transaction.rollback();
227                   throw new Exception JavaDoc(thr);
228                }
229             }
230          }
231       }
232       
233       /**
234        * Test for creating collection of blogs
235        *
236        * @throws Exception - an error has occured
237        */

238       public void testCreateCollection(
239       ) throws Exception JavaDoc
240       {
241          PreparedStatement JavaDoc statement = null;
242          String JavaDoc strQuery;
243          ResultSet JavaDoc results = null;
244    
245          List JavaDoc lstData = new ArrayList JavaDoc();
246          Blog testData = null;
247          Blog selectedData = null;
248          int iIndex;
249          int iInsertedCount = 0;
250          Iterator JavaDoc 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                // try to create blogs
266
iInsertedCount = m_blogFactory.create(lstData);
267                m_transaction.commit();
268             }
269             catch (Throwable JavaDoc thr)
270             {
271                m_transaction.rollback();
272                throw new Exception JavaDoc(thr);
273             }
274             
275             assertEquals("Number of inserted items is incorrect", lstData.size(), iInsertedCount);
276    
277             // try to test inserted blog data from the DB
278
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                   // Cannot use equals here since batch create doesn't fetch id and dates
294
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                // delete inserted records
310
try
311                {
312                   // delete all inserted blogs
313
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 JavaDoc thr)
319                {
320                   m_transaction.rollback();
321                   throw new Exception JavaDoc(thr);
322                }
323                finally
324                {
325                   DatabaseUtils.closeStatement(statement);
326                }
327             }
328          }
329       }
330    
331       /**
332        * Test Blog deletetion
333        * @throws Exception - an error has occured during test
334        */

335       public void testDelete(
336       ) throws Exception JavaDoc
337       {
338          PreparedStatement JavaDoc statement = null;
339          ResultSet JavaDoc results = null;
340          String JavaDoc 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 JavaDoc thr)
358             {
359                m_transaction.rollback();
360                throw new Exception JavaDoc(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 JavaDoc thr)
370             {
371                m_transaction.rollback();
372                throw new Exception JavaDoc(thr);
373             }
374          
375             m_transaction.begin();
376             try
377             {
378                // try to delete blog 2
379
m_blogFactory.delete(data2.getId(), DataObject.NEW_ID);
380                m_transaction.commit();
381             }
382             catch (Throwable JavaDoc thr)
383             {
384                m_transaction.rollback();
385                throw new Exception JavaDoc(thr);
386             }
387          
388             // Here the delete is completed
389
// Try to get deleted data
390
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             // Try to get not deleted data
399
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                   // delete blog 1
421
m_blogFactory.delete(data1.getId(), DataObject.NEW_ID);
422                }
423                if ((data2 != null) && (data2.getId() != DataObject.NEW_ID))
424                {
425                   // delete blog 2
426
m_blogFactory.delete(data2.getId(), DataObject.NEW_ID);
427                }
428                m_transaction.commit();
429             }
430             catch (Throwable JavaDoc thr)
431             {
432                m_transaction.rollback();
433                throw new Exception JavaDoc(thr);
434             }
435             finally
436             {
437                DatabaseUtils.closeStatement(statement);
438             }
439          }
440       }
441
442       /**
443        * Test of save blog
444        * @throws Exception - an error has occured during test
445        */

446       public void testSave(
447       ) throws Exception JavaDoc
448       {
449          PreparedStatement JavaDoc statement = null;
450          ResultSet JavaDoc results = null;
451          String JavaDoc 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 JavaDoc thr)
467             {
468                m_transaction.rollback();
469                throw new Exception JavaDoc(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 JavaDoc thr)
487             {
488                m_transaction.rollback();
489                throw new Exception JavaDoc(thr);
490             }
491          
492             // Here the save - update is completed
493
// We need to test
494
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                   // delete blog 1
530
m_blogFactory.delete(data1.getId(), DataObject.NEW_ID);
531                }
532                m_transaction.commit();
533             }
534             catch (Throwable JavaDoc thr)
535             {
536                m_transaction.rollback();
537                throw new Exception JavaDoc(thr);
538             }
539             finally
540             {
541                DatabaseUtils.closeStatement(statement);
542             }
543          }
544       }
545  
546       /**
547        * Test of get Blog by ID
548        * @throws Exception - an error has occured during test
549        */

550       public void testGetBlogByID(
551       ) throws Exception JavaDoc
552       {
553          PreparedStatement JavaDoc 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 JavaDoc thr)
572             {
573                m_transaction.rollback();
574                throw new Exception JavaDoc(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 JavaDoc thr)
584             {
585                m_transaction.rollback();
586                throw new Exception JavaDoc(thr);
587             }
588             
589             // try to get blog 2
590
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             // try to get blog 1
607
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                   // delete blog 1
631
m_blogFactory.delete(data1.getId(), DataObject.NEW_ID);
632                }
633                if ((data2 != null) && (data2.getId() != DataObject.NEW_ID))
634                {
635                   // delete blog 2
636
m_blogFactory.delete(data2.getId(), DataObject.NEW_ID);
637                }
638                m_transaction.commit();
639             }
640             catch (Throwable JavaDoc thr)
641             {
642                m_transaction.rollback();
643                throw new Exception JavaDoc(thr);
644             }
645             finally
646             {
647                DatabaseUtils.closeStatement(statement);
648             }
649          }
650       }
651       
652       /**
653        * Test of get Blog by incorrect ID
654        * @throws Exception - an error has occured during test
655        */

656       public void testGetBlogByIncorrectID(
657       ) throws Exception JavaDoc
658       {
659          PreparedStatement JavaDoc 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 JavaDoc thr)
675             {
676                m_transaction.rollback();
677                throw new Exception JavaDoc(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                   // delete blog 1
691
m_blogFactory.delete(data1.getId(), DataObject.NEW_ID);
692                }
693                m_transaction.commit();
694             }
695             catch (Throwable JavaDoc thr)
696             {
697                m_transaction.rollback();
698                throw new Exception JavaDoc(thr);
699             }
700             finally
701             {
702                DatabaseUtils.closeStatement(statement);
703             }
704          }
705       }
706
707       /**
708        * Test of get Blog by default NEW_ID
709        * @throws Exception - an error has occured during test
710        */

711       public void testGetBlogByNewID(
712       ) throws Exception JavaDoc
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       /**
724        * Test of getting Blog by unique Folder name
725        * @throws Exception - an error has occured during test
726        */

727       public void testGetBlogByFolder(
728       ) throws Exception JavaDoc
729       {
730          PreparedStatement JavaDoc 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 JavaDoc thr)
750             {
751                m_transaction.rollback();
752                throw new Exception JavaDoc(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 JavaDoc thr)
762             {
763                m_transaction.rollback();
764                throw new Exception JavaDoc(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                   // delete blog 1
791
m_blogFactory.delete(data1.getId(), DataObject.NEW_ID);
792                }
793                if ((data2 != null) && (data2.getId() != DataObject.NEW_ID))
794                {
795                   // delete blog 2
796
m_blogFactory.delete(data2.getId(), DataObject.NEW_ID);
797                }
798                m_transaction.commit();
799             }
800             catch (Throwable JavaDoc thr)
801             {
802                m_transaction.rollback();
803                throw new Exception JavaDoc(thr);
804             }
805             finally
806             {
807                DatabaseUtils.closeStatement(statement);
808             }
809          }
810       }
811
812       /**
813        * Test of get Blog by incorrect folder name
814        * @throws Exception - an error has occured during test
815        */

816       public void testGetBlogByIncorrectFolder(
817       ) throws Exception JavaDoc
818       {
819          Blog data = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
820                               "testfolder", "testcaption",
821                               "testcomment", null, null);
822    
823          PreparedStatement JavaDoc 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 JavaDoc thr)
833             {
834                m_transaction.rollback();
835                throw new Exception JavaDoc(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                   // delete blog
851
m_blogFactory.delete(data.getId(), DataObject.NEW_ID);
852                }
853                m_transaction.commit();
854             }
855             catch (Throwable JavaDoc thr)
856             {
857                m_transaction.rollback();
858                throw new Exception JavaDoc(thr);
859             }
860             finally
861             {
862                DatabaseUtils.closeStatement(statement);
863             }
864          }
865       }
866
867       /**
868        * Test for checking unique constraint for Folder attribute
869        * @throws Exception - an error has occured during test
870        */

871       public void testCheckUniqueConstraint(
872       ) throws Exception JavaDoc
873       {
874          PreparedStatement JavaDoc 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                 // create blog 1
889
testdata = (Blog)m_blogFactory.create(data1);
890                 m_transaction.commit();
891              }
892              catch (Throwable JavaDoc thr)
893              {
894                 m_transaction.rollback();
895                 throw new Exception JavaDoc(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              // At this point we have created blog 1. Now we will try to create another blog
907
// with the same folder name. There should be thrown unique constraint exception
908
data2 = new Blog(DataObject.NEW_ID, DataObject.NEW_ID,
909                                "testfolder1", "testcaption2",
910                               "testcomment2", null, null);
911              m_transaction.begin();
912              try
913              {
914                 // try to create Blog with the same Folder name
915
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                 // this exception is expected, there should be not inserted blog
922
// with the same folder name as already exists within the DB,
923
// while folder name is unique
924
m_transaction.rollback();
925                 
926                 StringWriter JavaDoc swStringWriter = new StringWriter JavaDoc();
927                 PrintWriter JavaDoc pwPrintWritter = new PrintWriter JavaDoc(swStringWriter);
928                 
929                 String JavaDoc 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                            // MySQL handles blog folder unique constraint exception as 'KEY 2'
935
|| (swStringWriter.toString().toUpperCase().indexOf("KEY 2") > -1)
936                            // IBM DB2 handles blog folder unique constraint exception as "2"
937
|| ((swStringWriter.toString().toUpperCase()).indexOf("\"2\"") > -1))
938                          && (strAllMessages.indexOf("Folder has to be unique.") > -1))
939                 );
940              }
941              catch (Throwable JavaDoc thr)
942              {
943                 m_transaction.rollback();
944                 throw new Exception JavaDoc(thr);
945              }
946          }
947          finally
948          {
949             m_transaction.begin();
950             try
951             {
952                if ((testdata != null) && (testdata.getId() != DataObject.NEW_ID))
953                {
954                   // delete blog
955
m_blogFactory.delete(testdata.getId(), DataObject.NEW_ID);
956                }
957                m_transaction.commit();
958             }
959             catch (Throwable JavaDoc thr)
960             {
961                m_transaction.rollback();
962                throw new Exception JavaDoc(thr);
963             }
964             finally
965             {
966                DatabaseUtils.closeStatement(statement);
967             }
968          }
969       }
970
971       /**
972        * Test for getting of all blogs
973        * @throws Exception - an error has occured during test
974        */

975       public void testGetAllBlogs(
976       ) throws Exception JavaDoc
977       {
978          Blog data;
979          int iCount = 123; // test this with real big amount of data
980

981          PreparedStatement JavaDoc 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 JavaDoc thr)
999             {
1000               m_transaction.rollback();
1001               throw new Exception JavaDoc(thr);
1002            }
1003      
1004            List JavaDoc 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 JavaDoc thr)
1023            {
1024               m_transaction.rollback();
1025               throw new Exception JavaDoc(thr);
1026            }
1027            finally
1028            {
1029               DatabaseUtils.closeStatement(statement);
1030            }
1031         }
1032      }
1033   
1034      /**
1035       * Test getting all blogs for empty table
1036       * @throws Exception - an error has occured during test
1037       */

1038      public void testGetAllBlogsNoData(
1039      ) throws Exception JavaDoc
1040      {
1041         List JavaDoc lstTestData;
1042   
1043         lstTestData = m_blogFactory.getAll();
1044            
1045         assertNull("Loaded blog list is not null for empty DB", lstTestData);
1046      }
1047
1048      /**
1049       * Test of load Blog utility
1050       * @throws Exception - an error has occured during test
1051       */

1052      public void testLoadBlog(
1053      ) throws Exception JavaDoc
1054      {
1055         PreparedStatement JavaDoc statement = null;
1056         ResultSet JavaDoc results = null;
1057         String JavaDoc 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 JavaDoc thr)
1074            {
1075               m_transaction.rollback();
1076               throw new Exception JavaDoc(thr);
1077            }
1078      
1079            // We need to test
1080
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                  // delete blog
1120
m_blogFactory.delete(data.getId(), DataObject.NEW_ID);
1121               }
1122               m_transaction.commit();
1123            }
1124            catch (Throwable JavaDoc thr)
1125            {
1126               m_transaction.rollback();
1127               throw new Exception JavaDoc(thr);
1128            }
1129            finally
1130            {
1131               DatabaseUtils.closeStatement(statement);
1132            }
1133         }
1134      }
1135   }
1136}
1137
Popular Tags