1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
32
33
34
35
36 public
37 class TestSpringGenericDao
38 extends AbstractTransactionalSpringContextTests
39 {
40
41
42
43 public void
44 testFindByName()
45 {
46
47 IPersonDao myPersonDao = LocalDaoFactory.getInstance().
48 createPersonDao();
49
50
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
71 IPersonDao myPersonDao = LocalDaoFactory.getInstance().
72 createPersonDao();
73
74
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
97 IPersonDao myPersonDao = LocalDaoFactory.getInstance().
98 createPersonDao();
99
100
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
125 IPersonDao myPersonDao = LocalDaoFactory.getInstance().
126 createPersonDao();
127
128
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
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
157 IPersonDao myPersonDao = LocalDaoFactory.getInstance().
158 createPersonDao();
159 myPersonDao.deleteAll();
160 }
161
162
163
164
165
166
167
168 @SuppressWarnings("unused")
169 private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
170 }