1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // TestJpaGenericReadOnlyDao.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 JpaBaseDao} implementation.
33   * 
34   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
35   * @version $Revision: 1.0 $ $Date: Nov 26, 2007 9:29:34 PM $
36   */
37  public 
38  class TestJpaGenericReadOnlyDao
39  extends TestCase
40  {
41      //------------------------------------------------------------------------
42      // public
43      //------------------------------------------------------------------------
44      public void
45      testContains()
46      {        
47          // create generic read-only Dao instance
48          IBaseReadOnlyDao<Long, Person> myPersonDao = JpaDaoFactory.
49              getInstance().createReadOnlyDao(Person.class);
50       
51          // contains
52          List<Person> myPersons = myPersonDao.findAll();
53          boolean myContains = myPersonDao.contains(myPersons.get(0));
54  
55          // run assertions
56          assertTrue("contains did not work as expected", myContains);
57      }
58  
59      //------------------------------------------------------------------------
60      public void
61      testCount()
62      {        
63          // create generic read-only Dao instance
64          IBaseReadOnlyDao<Long, Person> myPersonDao = JpaDaoFactory.
65              getInstance().createReadOnlyDao(Person.class);
66       
67          // contains
68          int myCount = myPersonDao.count();
69  
70          // run assertions
71          assertEquals("count did not work as expected", 2, myCount);
72      }
73  
74      //------------------------------------------------------------------------
75      public void
76      testFindById()
77      {        
78          // create generic read-only Dao instance
79          IBaseReadOnlyDao<Long, Person> myPersonDao = JpaDaoFactory.
80              getInstance().createReadOnlyDao(Person.class);
81       
82          // find by Id
83          Person myPedro = myPersonDao.findById(thePedro.getId());
84  
85          // run assertions
86          assertNotNull("findById did not find the expected Person", myPedro);
87          assertEquals("findById did not find the expected Person", "Pedro", 
88              myPedro.getName());
89      }
90      
91      //------------------------------------------------------------------------
92      public void
93      testFindAll()
94      {
95          // create generic read-only Dao instance
96          IBaseReadOnlyDao<Long, Person> myPersonDao = JpaDaoFactory.
97              getInstance().createReadOnlyDao(Person.class);
98       
99          // find All
100         List<Person> myPersons = myPersonDao.findAll();
101 
102         // run assertions
103         assertNotNull("findAll did not find the expected Persons", myPersons);
104         assertEquals("findAll did not find the expected Persons", 2, myPersons.
105             size());
106     }
107     
108     //------------------------------------------------------------------------
109     // protected
110     //------------------------------------------------------------------------
111     @Override
112     protected void 
113     setUp() 
114     throws Exception
115     {
116         super.setUp();
117      
118         // create the two example Persons
119         IBaseDao<Long, Person> myPersonDao = JpaDaoFactory.
120             getInstance().createDao(Person.class);
121         
122         if (myPersonDao.count() == 0)
123         {        
124             myPersonDao.create(thePedro);            
125             myPersonDao.create(theManuela);
126             
127             myPersonDao.getTransaction().commit();
128         }               
129     }
130     
131     //------------------------------------------------------------------------
132     // members
133     //------------------------------------------------------------------------
134     // CHECKSTYLE:OFF
135     private static final Person thePedro = new Person("Pedro", 45);
136     private static final Person theManuela = new Person("Manuela", 23);
137     // CHECKSTYLE:ON
138     
139     /**
140      * Provides logging services for this class.
141      */
142     @SuppressWarnings("unused")
143     private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
144 }