1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // TestHibernateReadOnlyDao.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:29:34 PM $
36 */
37 public
38 class TestHibernateGenericReadOnlyDao
39 extends TestCase
40 {
41 //------------------------------------------------------------------------
42 // public
43 //------------------------------------------------------------------------
44 public void
45 testContains()
46 {
47 // create generic read-only Dao instance
48 IGenericReadOnlyDao<Long, Person> myPersonDao = HibernateDaoFactory.
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 IGenericReadOnlyDao<Long, Person> myPersonDao = HibernateDaoFactory.
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 IGenericReadOnlyDao<Long, Person> myPersonDao = HibernateDaoFactory.
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 IGenericReadOnlyDao<Long, Person> myPersonDao = HibernateDaoFactory.
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 public void
110 testFindByExample()
111 {
112 // create generic read-only Dao instance
113 IGenericReadOnlyDao<Long, Person> myPersonDao = HibernateDaoFactory.
114 getInstance().createReadOnlyDao(Person.class);
115
116 // prepare example
117 Person myExample = new Person();
118 myExample.setName("Pedro");
119 myExample.setAge(45);
120
121 // find by example
122 List<Person> myPersons = myPersonDao.findByExample(myExample);
123
124 // run assertions
125 assertNotNull("findByExample did not find the expected Persons",
126 myPersons);
127 assertEquals("findByExample did not find the expected Persons", 1,
128 myPersons.size());
129 assertEquals("findByExample did not find the expected Persons",
130 "Pedro", myPersons.get(0).getName());
131
132 // find by example excluding properties
133 myExample.setAge(-1);
134 myPersons = myPersonDao.findByExample(myExample, "age");
135
136 // run assertions
137 assertNotNull("findByExample did not find the expected Persons",
138 myPersons);
139 assertEquals("findByExample did not find the expected Persons", 1,
140 myPersons.size());
141 assertEquals("findByExample did not find the expected Persons",
142 "Pedro", myPersons.get(0).getName());
143 }
144
145 //------------------------------------------------------------------------
146 // protected
147 //------------------------------------------------------------------------
148 @Override
149 protected void
150 setUp()
151 throws Exception
152 {
153 super.setUp();
154
155 // create the two example Persons
156 IGenericDao<Long, Person> myPersonDao = HibernateDaoFactory.
157 getInstance().createDao(Person.class);
158
159 if (myPersonDao.count() == 0)
160 {
161 myPersonDao.create(thePedro);
162 myPersonDao.create(theManuela);
163
164 myPersonDao.getTransaction().commit();
165 }
166 }
167
168 //------------------------------------------------------------------------
169 // members
170 //------------------------------------------------------------------------
171 // CHECKSTYLE:OFF
172 private static final Person thePedro = new Person("Pedro", 45);
173 private static final Person theManuela = new Person("Manuela", 23);
174 // CHECKSTYLE:ON
175
176 /**
177 * Provides logging services for this class.
178 */
179 @SuppressWarnings("unused")
180 private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
181 }