KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2006 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenChronicle
5  *
6  * $Id: EntryDatabaseFactoryTest.java,v 1.4 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.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
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 /**
51  * Test functionality of EntryDatabaseFactory class.
52  *
53  * @version $Id: EntryDatabaseFactoryTest.java,v 1.4 2007/02/01 07:34:37 bastafidli Exp $
54  * @author Julian Legeny
55  * @code.reviewer TODO: Review this code
56  * @code.reviewed
57  */

58 public final class EntryDatabaseFactoryTest
59 {
60    // Constructors /////////////////////////////////////////////////////////////
61

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

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

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

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

94    public static class EntryDatabaseFactoryTestInternal extends DatabaseTest
95    {
96       // Cached values ////////////////////////////////////////////////////////////
97

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

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

106       protected BlogDatabaseFactory m_blogFactory;
107
108       /**
109        * Factory to manage entries.
110        */

111       protected EntryDatabaseFactory m_entryFactory;
112    
113       // Constructors /////////////////////////////////////////////////////////////
114

115       /**
116        * Static initializer.
117        */

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

138       public EntryDatabaseFactoryTestInternal(
139          String JavaDoc strTestName
140       ) throws Exception JavaDoc
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       // Tests ////////////////////////////////////////////////////////////////////
153

154       /**
155        * Test creation of entries.
156        *
157        * @throws Exception - an error has occured during test
158        */

159       public void testCreate(
160       ) throws Exception JavaDoc
161       {
162          Blog blog = null;
163          Entry data = null;
164          Entry testData = null;
165          Entry selectedData = null;
166          
167          PreparedStatement JavaDoc statement = null;
168          ResultSet JavaDoc results = null;
169          String JavaDoc 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                // create blog first
179
blog = (Blog)m_blogFactory.create(blog);
180                m_transaction.commit();
181             }
182             catch (Throwable JavaDoc thr)
183             {
184                m_transaction.rollback();
185                throw new Exception JavaDoc(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                // create test entry
195
testData = (Entry)m_entryFactory.create(data);
196                m_transaction.commit();
197             }
198             catch (Throwable JavaDoc thr)
199             {
200                m_transaction.rollback();
201                throw new Exception JavaDoc(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             // try to test inserted entry data from the DB
214
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             // delete inserted records
239
try
240             {
241                if ((data != null) && (data.getId() != DataObject.NEW_ID))
242                {
243                   // delete entry
244
m_entryFactory.delete(data.getId(), DataObject.NEW_ID);
245                }
246                if ((blog != null) && (blog.getId() != DataObject.NEW_ID))
247                {
248                   // delete blog
249
m_blogFactory.delete(blog.getId(), DataObject.NEW_ID);
250                }
251                m_transaction.commit();
252             }
253             catch (Throwable JavaDoc thr)
254             {
255                m_transaction.rollback();
256                throw new Exception JavaDoc(thr);
257             }
258          }
259       }
260       
261       /**
262        * Test for creating collection of entries
263        *
264        * @throws Exception - an error has occured
265        */

266       public void testCreateCollection(
267       ) throws Exception JavaDoc
268       {
269          PreparedStatement JavaDoc statement = null;
270          String JavaDoc strQuery;
271          ResultSet JavaDoc results = null;
272    
273          List JavaDoc lstData = new ArrayList JavaDoc();
274          Blog blog = null;
275          Entry testData = null;
276          Entry selectedData = null;
277          int iIndex;
278          int iInsertedCount = 0;
279          Iterator JavaDoc 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             // create blog first
287
blog = (Blog)m_blogFactory.create(blog);
288             m_transaction.commit();
289          }
290          catch (Throwable JavaDoc thr)
291          {
292             m_transaction.rollback();
293             throw new Exception JavaDoc(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                // try to create entries
312
iInsertedCount = m_entryFactory.create(lstData);
313                m_transaction.commit();
314             }
315             catch (Throwable JavaDoc thr)
316             {
317                m_transaction.rollback();
318                throw new Exception JavaDoc(thr);
319             }
320             
321             assertEquals("Number of inserted items is incorrect", lstData.size(), iInsertedCount);
322    
323             // try to test inserted entry data from the DB
324
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                   // Cannot use equals here since batch create doesn't fetch id and dates
341
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             // delete inserted records
354
m_transaction.begin();
355             try
356             {
357                if (iInsertedCount > 1)
358                {
359                   // delete all inserted entries
360
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                   // delete blog
368
m_blogFactory.delete(blog.getId(), DataObject.NEW_ID);
369                }
370                m_transaction.commit();
371             }
372             catch (Throwable JavaDoc thr)
373             {
374                m_transaction.rollback();
375                throw new Exception JavaDoc(thr);
376             }
377             finally
378             {
379                DatabaseUtils.closeStatement(statement);
380             }
381          }
382       }
383
384       /**
385        * Test Entry deletetion
386        * @throws Exception - an error has occured during test
387        */

388       public void testDelete(
389       ) throws Exception JavaDoc
390       {
391          PreparedStatement JavaDoc statement = null;
392          ResultSet JavaDoc results = null;
393          String JavaDoc 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                // create blog first
409
blog = (Blog)m_blogFactory.create(blog);
410                m_transaction.commit();
411             }
412             catch (Throwable JavaDoc thr)
413             {
414                m_transaction.rollback();
415                throw new Exception JavaDoc(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 JavaDoc thr)
432             {
433                m_transaction.rollback();
434                throw new Exception JavaDoc(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 JavaDoc thr)
444             {
445                m_transaction.rollback();
446                throw new Exception JavaDoc(thr);
447             }
448          
449             m_transaction.begin();
450             try
451             {
452                // try to delete entry 2
453
m_entryFactory.delete(data2.getId(), DataObject.NEW_ID);
454                m_transaction.commit();
455             }
456             catch (Throwable JavaDoc thr)
457             {
458                m_transaction.rollback();
459                throw new Exception JavaDoc(thr);
460             }
461          
462             // Here the delete is completed
463
// Try to get deleted data
464
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             // Try to get not deleted data
474
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                   // delete entry 1
496
m_entryFactory.delete(data1.getId(), DataObject.NEW_ID);
497                }
498                if ((data2 != null) && (data2.getId() != DataObject.NEW_ID))
499                {
500                   // delete entry 2
501
m_entryFactory.delete(data2.getId(), DataObject.NEW_ID);
502                }
503                if ((blog != null) && (blog.getId() != DataObject.NEW_ID))
504                {
505                   // delete blog 1
506
m_blogFactory.delete(blog.getId(), DataObject.NEW_ID);
507                }
508                m_transaction.commit();
509             }
510             catch (Throwable JavaDoc thr)
511             {
512                m_transaction.rollback();
513                throw new Exception JavaDoc(thr);
514             }
515             finally
516             {
517                DatabaseUtils.closeStatement(statement);
518             }
519          }
520       }
521    
522       /**
523        * Test of save entry
524        * @throws Exception - an error has occured during test
525        */

526       public void testSave(
527       ) throws Exception JavaDoc
528       {
529          PreparedStatement JavaDoc statement = null;
530          ResultSet JavaDoc results = null;
531          String JavaDoc 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               // create blog first
547
blog = (Blog)m_blogFactory.create(blog);
548               m_transaction.commit();
549            }
550            catch (Throwable JavaDoc thr)
551            {
552               m_transaction.rollback();
553               throw new Exception JavaDoc(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 JavaDoc thr)
567             {
568                m_transaction.rollback();
569                throw new Exception JavaDoc(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 JavaDoc thr)
586             {
587                m_transaction.rollback();
588                throw new Exception JavaDoc(thr);
589             }
590          
591             // Here the save - update is completed
592
// We need to test
593
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                   // delete entry
623
m_entryFactory.delete(data1.getId(), DataObject.NEW_ID);
624                }
625                if ((blog != null) && (blog.getId() != DataObject.NEW_ID))
626                {
627                   // delete blog
628
m_blogFactory.delete(blog.getId(), DataObject.NEW_ID);
629                }
630                m_transaction.commit();
631             }
632             catch (Throwable JavaDoc thr)
633             {
634                m_transaction.rollback();
635                throw new Exception JavaDoc(thr);
636             }
637             finally
638             {
639                DatabaseUtils.closeStatement(statement);
640             }
641          }
642       }
643  
644       /**
645        * Test of get Entry by ID
646        * @throws Exception - an error has occured during test
647        */

648       public void testGetEntryByID(
649       ) throws Exception JavaDoc
650       {
651          PreparedStatement JavaDoc 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               // create blog first
667
blog = (Blog)m_blogFactory.create(blog);
668               m_transaction.commit();
669            }
670            catch (Throwable JavaDoc thr)
671            {
672               m_transaction.rollback();
673               throw new Exception JavaDoc(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 JavaDoc thr)
690            {
691               m_transaction.rollback();
692               throw new Exception JavaDoc(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 JavaDoc thr)
702            {
703               m_transaction.rollback();
704               throw new Exception JavaDoc(thr);
705            }
706             
707             // try to get entry 2
708
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             // try to get entry 1
716
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                   // delete entry 1
731
m_entryFactory.delete(data1.getId(), DataObject.NEW_ID);
732                }
733                if ((data2 != null) && (data2.getId() != DataObject.NEW_ID))
734                {
735                   // delete entry 2
736
m_entryFactory.delete(data2.getId(), DataObject.NEW_ID);
737                }
738                if ((blog != null) && (blog.getId() != DataObject.NEW_ID))
739                {
740                   // delete blog
741
m_blogFactory.delete(blog.getId(), DataObject.NEW_ID);
742                }
743                m_transaction.commit();
744             }
745             catch (Throwable JavaDoc thr)
746             {
747                m_transaction.rollback();
748                throw new Exception JavaDoc(thr);
749             }
750             finally
751             {
752                DatabaseUtils.closeStatement(statement);
753             }
754          }
755       }
756       
757       /**
758        * Test of get Entry by incorrect ID
759        * @throws Exception - an error has occured during test
760        */

761       public void testGetEntryByIncorrectID(
762       ) throws Exception JavaDoc
763       {
764          PreparedStatement JavaDoc 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                // create blog first
779
blog = (Blog)m_blogFactory.create(blog);
780                m_transaction.commit();
781             }
782             catch (Throwable JavaDoc thr)
783             {
784                m_transaction.rollback();
785                throw new Exception JavaDoc(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 JavaDoc thr)
799             {
800                m_transaction.rollback();
801                throw new Exception JavaDoc(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                   // delete entry
815
m_entryFactory.delete(data1.getId(), DataObject.NEW_ID);
816                }
817                if ((blog != null) && (blog.getId() != DataObject.NEW_ID))
818                {
819                   // delete blog
820
m_blogFactory.delete(blog.getId(), DataObject.NEW_ID);
821                }
822                m_transaction.commit();
823             }
824             catch (Throwable JavaDoc thr)
825             {
826                m_transaction.rollback();
827                throw new Exception JavaDoc(thr);
828             }
829             finally
830             {
831                DatabaseUtils.closeStatement(statement);
832             }
833          }
834       }
835
836       /**
837        * Test of get Entry by default NEW_ID
838        * @throws Exception - an error has occured during test
839        */

840       public void testGetBlogByNewID(
841       ) throws Exception JavaDoc
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       /**
853        * Test for getting of all entries
854        * @throws Exception - an error has occured during test
855        */

856       public void testGetAllEntries(
857       ) throws Exception JavaDoc
858       {
859          PreparedStatement JavaDoc statement = null;
860
861          int iCount1 = 123; // test this with real big amount of data
862
// - number of entries assigned to the blog 1
863
int iCount2 = 111; // - number of entries assigned to the blog 2
864

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                // create blog 1
878
blog1 = (Blog)m_blogFactory.create(blog1);
879                m_transaction.commit();
880             }
881             catch (Throwable JavaDoc thr)
882             {
883                m_transaction.rollback();
884                throw new Exception JavaDoc(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                // create blog 2
894
blog2 = (Blog)m_blogFactory.create(blog2);
895                m_transaction.commit();
896             }
897             catch (Throwable JavaDoc thr)
898             {
899                m_transaction.rollback();
900                throw new Exception JavaDoc(thr);
901             }
902
903             m_transaction.begin();
904             try
905             {
906                int iIndex;
907                for (iIndex = 0; iIndex < iCount1; iIndex++)
908                {
909                   // assign data to blog 1
910
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 JavaDoc thr)
921             {
922                m_transaction.rollback();
923                throw new Exception JavaDoc(thr);
924             }
925
926             m_transaction.begin();
927             try
928             {
929                int iIndex;
930                for (iIndex = 0; iIndex < iCount2; iIndex++)
931                {
932                   // assign data to blog 2
933
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 JavaDoc thr)
944             {
945                m_transaction.rollback();
946                throw new Exception JavaDoc(thr);
947             }
948       
949             List JavaDoc lstTestData;
950             // test entries for blog 1
951
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             // test entries for blog 2
956
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                   // delete blog 1
976
m_blogFactory.delete(blog1.getId(), DataObject.NEW_ID);
977                }
978                if ((blog2 != null) && (blog2.getId() != DataObject.NEW_ID))
979                {
980                   // delete blog 2
981
m_blogFactory.delete(blog2.getId(), DataObject.NEW_ID);
982                }
983                m_transaction.commit();
984             }
985             catch (Throwable JavaDoc thr)
986             {
987                m_transaction.rollback();
988                throw new Exception JavaDoc(thr);
989             }
990             finally
991             {
992                DatabaseUtils.closeStatement(statement);
993             }
994          }
995       }
996
997       /**
998        * Test getting all entries for empty table
999        * @throws Exception - an error has occured during test
1000       */

1001      public void testGetAllEntriesNoData(
1002      ) throws Exception JavaDoc
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               // create blog
1015
blog = (Blog)m_blogFactory.create(blog);
1016               m_transaction.commit();
1017            }
1018            catch (Throwable JavaDoc thr)
1019            {
1020               m_transaction.rollback();
1021               throw new Exception JavaDoc(thr);
1022            }
1023
1024            List JavaDoc 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                  // delete blog
1036
m_blogFactory.delete(blog.getId(), DataObject.NEW_ID);
1037               }
1038               m_transaction.commit();
1039            }
1040            catch (Throwable JavaDoc thr)
1041            {
1042               m_transaction.rollback();
1043               throw new Exception JavaDoc(thr);
1044            }
1045         }
1046      }
1047
1048      /**
1049       * Test for getting of the last entry
1050       * @throws Exception - an error has occured during test
1051       */

1052      public void testGetLastEntry(
1053      ) throws Exception JavaDoc
1054      {
1055         PreparedStatement JavaDoc 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               // create blog
1073
blog = (Blog)m_blogFactory.create(blog);
1074               m_transaction.commit();
1075            }
1076            catch (Throwable JavaDoc thr)
1077            {
1078               m_transaction.rollback();
1079               throw new Exception JavaDoc(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 JavaDoc thr)
1092            {
1093               m_transaction.rollback();
1094               throw new Exception JavaDoc(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 JavaDoc thr)
1107            {
1108               m_transaction.rollback();
1109               throw new Exception JavaDoc(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 JavaDoc thr)
1122            {
1123               m_transaction.rollback();
1124               throw new Exception JavaDoc(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 JavaDoc thr)
1137            {
1138               m_transaction.rollback();
1139               throw new Exception JavaDoc(thr);
1140            }
1141
1142            // get last entry for particular blog
1143
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                  // delete entry 1
1155
m_entryFactory.delete(data1.getId(), DataObject.NEW_ID);
1156               }
1157               if ((data2 != null) && (data2.getId() != DataObject.NEW_ID))
1158               {
1159                  // delete entry 2
1160
m_entryFactory.delete(data2.getId(), DataObject.NEW_ID);
1161               }
1162               if ((data3 != null) && (data3.getId() != DataObject.NEW_ID))
1163               {
1164                  // delete entry 3
1165
m_entryFactory.delete(data3.getId(), DataObject.NEW_ID);
1166               }
1167               if ((data4 != null) && (data4.getId() != DataObject.NEW_ID))
1168               {
1169                  // delete entry 4
1170
m_entryFactory.delete(data4.getId(), DataObject.NEW_ID);
1171               }
1172               if ((blog != null) && (blog.getId() != DataObject.NEW_ID))
1173               {
1174                  // delete blog
1175
m_blogFactory.delete(blog.getId(), DataObject.NEW_ID);
1176               }
1177               m_transaction.commit();
1178            }
1179            catch (Throwable JavaDoc thr)
1180            {
1181               m_transaction.rollback();
1182               throw new Exception JavaDoc(thr);
1183            }
1184            finally
1185            {
1186               DatabaseUtils.closeStatement(statement);
1187            }
1188         }
1189      }
1190
1191      /**
1192       * Test of load Entry utility
1193       * @throws Exception - an error has occured during test
1194       */

1195      public void testLoadEntry(
1196      ) throws Exception JavaDoc
1197      {
1198         PreparedStatement JavaDoc statement = null;
1199         ResultSet JavaDoc results = null;
1200         String JavaDoc 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               // create blog first
1216
blog = (Blog)m_blogFactory.create(blog);
1217               m_transaction.commit();
1218            }
1219            catch (Throwable JavaDoc thr)
1220            {
1221               m_transaction.rollback();
1222               throw new Exception JavaDoc(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 JavaDoc thr)
1236            {
1237               m_transaction.rollback();
1238               throw new Exception JavaDoc(thr);
1239            }
1240      
1241            // We need to test
1242
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                  // delete entry
1276
m_entryFactory.delete(data.getId(), DataObject.NEW_ID);
1277               }
1278               if ((blog != null) && (blog.getId() != DataObject.NEW_ID))
1279               {
1280                  // delete blog
1281
m_blogFactory.delete(blog.getId(), DataObject.NEW_ID);
1282               }
1283               m_transaction.commit();
1284            }
1285            catch (Throwable JavaDoc thr)
1286            {
1287               m_transaction.rollback();
1288               throw new Exception JavaDoc(thr);
1289            }
1290            finally
1291            {
1292               DatabaseUtils.closeStatement(statement);
1293            }
1294         }
1295      }
1296   }
1297}
1298
Popular Tags