1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // TestHibernateGenericDao.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.jee.integration.dao;
22  
23  import java.util.*;
24  
25  import junit.framework.*;
26  
27  import org.perfectjpattern.example.datamodel.*;
28  import org.perfectjpattern.jee.api.integration.dao.*;
29  import org.slf4j.*;
30  
31  /**
32   * Test Suite for the {@link HibernateGenericDao} implementation.
33   * 
34   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
35   * @version $Revision: 1.0 $ $Date: Nov 26, 2007 9:31:46 PM $
36   */
37  public 
38  class TestJpaGenericDao
39  extends TestCase
40  {
41      //------------------------------------------------------------------------
42      // public
43      //------------------------------------------------------------------------
44      public void
45      testCreate()
46      {
47          // create generic read-only Dao instance
48          IBaseDao<Long, Person> myPersonDao = JpaDaoFactory.
49              getInstance().createDao(Person.class);
50          
51          // create a new Person
52          Person myGiovanni = new Person("Giovanni", 32);
53          
54          myPersonDao.create(myGiovanni);
55          
56          // make sure it exists in the persistent storage
57          List<Person> myPersons = myPersonDao.findAll();
58          
59          assertNotNull("create seems not to work, record was not found.", 
60              myPersons);
61          assertEquals("create seems not to work, record was not found.", 
62              1, myPersons.size());
63          assertEquals("create seems not to work, record was not found.", 
64              "Giovanni", myPersons.get(0).getName());  
65      }
66      
67      //------------------------------------------------------------------------
68      public void
69      testUpdate()
70      {
71          // create generic read-only Dao instance
72          IBaseDao<Long, Person> myPersonDao = JpaDaoFactory.
73              getInstance().createDao(Person.class);
74          
75          // create a new Person
76          Person myRosa = new Person("Rosa", 27);        
77          myPersonDao.create(myRosa);
78          
79          // retrieve the record just created 
80          List<Person> myPersons = myPersonDao.findAll();
81          
82          Person myUpdate = myPersons.get(0);
83          myUpdate.setAge(28);
84          myPersonDao.update(myUpdate);
85          
86          // retrieve the record just edited
87          myPersons = myPersonDao.findAll();
88  
89          assertNotNull("create seems not to work, record was not found.", 
90              myPersons);
91          assertEquals("create seems not to work, record was not found.", 
92              "Rosa", myPersons.get(0).getName());        
93          assertEquals("create seems not to work, record was not found.", 
94              28, myPersons.get(0).getAge());        
95      }
96  
97      //------------------------------------------------------------------------
98      public void
99      testDelete()
100     {
101         // create generic read-only Dao instance
102         IBaseDao<Long, Person> myPersonDao = JpaDaoFactory.getInstance().
103             createDao(Person.class);
104         
105         // create a new Person
106         Person myErnesto = new Person("Ernesto", 35);        
107         myPersonDao.create(myErnesto);
108         
109         // commit so the Id will be assigned
110         myPersonDao.getTransaction().commit();
111         
112         Long myId = myErnesto.getId();
113         
114         Person myDelete = myPersonDao.findById(myId);
115         
116         // delete Ernesto
117         myPersonDao.delete(myDelete);
118         
119         boolean myContains = myPersonDao.contains(myDelete);
120         
121         // run assertions on it
122         assertFalse("There must be no Elements left matching the deleted.", 
123             myContains);
124     }
125     
126     //------------------------------------------------------------------------
127     // protected
128     //------------------------------------------------------------------------
129     @Override
130     protected void 
131     setUp() 
132     throws Exception
133     {
134         super.setUp();
135         
136         // create the two example Persons
137         IBaseDao<Long, Person> myPersonDao = JpaDaoFactory.
138             getInstance().createDao(Person.class);
139         
140         ITransaction myTransaction = myPersonDao.getTransaction(); 
141         if (myTransaction.isActive())
142         {
143             myTransaction.rollback();
144         }
145 
146         if (myPersonDao.count() > 0)
147         {        
148             myPersonDao.deleteAll();
149             
150             myTransaction.commit();
151         }               
152     }
153     
154     //------------------------------------------------------------------------
155     // members
156     //------------------------------------------------------------------------
157     /**
158      * Provides logging services for this class.
159      */
160     @SuppressWarnings("unused")
161     private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
162 }