1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // TestSpringGenericDao.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 org.perfectjpattern.example.datamodel.*;
26  import org.perfectjpattern.jee.api.integration.dao.*;
27  import org.slf4j.*;
28  import org.springframework.test.*;
29  
30  /**
31   * Test Suite for the {@link SpringGenericDao} implementation
32   * 
33   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
34   * @version $ $Date: Nov 6, 2008 2:20:15 PM $
35   */
36  public 
37  class TestSpringGenericDao
38  extends AbstractTransactionalSpringContextTests
39  {
40      //------------------------------------------------------------------------
41      // public
42      //------------------------------------------------------------------------
43      public void
44      testFindByName()
45      {
46          // create Generic DAO instance
47          IPersonDao myPersonDao = LocalDaoFactory.getInstance().
48              createPersonDao();
49          
50          // create fixture Person instances
51          Person myErnesto = new Person("Ernesto", 35);        
52          myPersonDao.create(myErnesto);
53          Person myGiovanni = new Person("Giovanni", 33);        
54          myPersonDao.create(myGiovanni);
55          Person myAlberto = new Person("Alberto", 33);        
56          myPersonDao.create(myAlberto);
57          
58          List<Person> myMatches = myPersonDao.findByName("Ernesto");
59          assertNotNull("findByName did not work as expected", myMatches);
60          assertEquals("findByName did not work as expected", 1, myMatches.
61              size());
62          assertEquals("findByName did not work as expected", myErnesto, 
63              myMatches.get(0));
64      }
65      
66      //------------------------------------------------------------------------
67      public void
68      testFindByAge()
69      {
70          // create Generic DAO instance
71          IPersonDao myPersonDao = LocalDaoFactory.getInstance().
72              createPersonDao();
73          
74          // create fixture Person instances
75          Person myErnesto = new Person("Ernesto", 35);        
76          myPersonDao.create(myErnesto);
77          Person myGiovanni = new Person("Giovanni", 33);        
78          myPersonDao.create(myGiovanni);
79          Person myAlberto = new Person("Alberto", 33);        
80          myPersonDao.create(myAlberto);
81          
82          List<Person> myMatches = myPersonDao.findByAge(33);
83          assertNotNull("findByAge did not work as expected", myMatches);
84          assertEquals("findByAge did not work as expected", 2, myMatches.
85              size());
86          assertEquals("findByAge did not work as expected", 33, 
87              myMatches.get(0).getAge());        
88          assertEquals("findByAge did not work as expected", 33, 
89              myMatches.get(1).getAge());        
90      }
91      
92      //------------------------------------------------------------------------
93      public void
94      testFindByExample()
95      {
96          // create Generic DAO instance
97          IPersonDao myPersonDao = LocalDaoFactory.getInstance().
98              createPersonDao();
99          
100         // create fixture Person instances
101         Person myErnesto = new Person("Ernesto", 35);        
102         myPersonDao.create(myErnesto);
103         Person myGiovanni = new Person("Giovanni", 33);        
104         myPersonDao.create(myGiovanni);
105         Person myAlberto = new Person("Alberto", 33);        
106         myPersonDao.create(myAlberto);
107         
108         Person myExample = new Person();
109         myExample.setAge(33);        
110         List<Person> myMatches = myPersonDao.findByExample(myExample);
111         assertNotNull("findByExample did not work as expected", myMatches);
112         assertEquals("findByExample did not work as expected", 2, myMatches.
113             size());
114         assertEquals("findByExample did not work as expected", 33, 
115             myMatches.get(0).getAge());        
116         assertEquals("findByExample did not work as expected", 33, 
117             myMatches.get(1).getAge());        
118     }
119 
120     //------------------------------------------------------------------------
121     public void
122     testFindById()
123     {
124         // create Generic DAO instance
125         IPersonDao myPersonDao = LocalDaoFactory.getInstance().
126             createPersonDao();
127         
128         // create fixture Person instances
129         Person myErnesto = new Person("Ernesto", 35);        
130         myPersonDao.create(myErnesto);
131         
132         Person myMatch = myPersonDao.findById(myErnesto.getId());
133         assertNotNull("findById did not work as expected", myMatch);
134         assertEquals("findById did not work as expected", myErnesto, 
135             myMatch);        
136     }
137 
138     //------------------------------------------------------------------------
139     // protected
140     //------------------------------------------------------------------------
141     protected String[] 
142     getConfigLocations() 
143     {
144         return new String[] 
145         {
146             "file:src/main/resources/genericDao-applicationContext.xml",
147             "file:src/test/resources/test-applicationContext.xml"
148         };
149     }    
150     
151     //------------------------------------------------------------------------
152     protected void 
153     onTearDownAfterTransaction() 
154     throws Exception 
155     {
156         // empty the Person table
157         IPersonDao myPersonDao = LocalDaoFactory.getInstance().
158             createPersonDao();
159         myPersonDao.deleteAll();
160     }
161     
162     //------------------------------------------------------------------------
163     // members
164     //------------------------------------------------------------------------
165     /**
166      * Provides logging services for this class
167      */
168     @SuppressWarnings("unused")
169     private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
170 }