1 package org.itracker.persistence.dao;
2
3 import org.hibernate.SessionFactory;
4 import org.itracker.AbstractDependencyInjectionTest;
5 import org.itracker.model.Language;
6 import org.junit.Test;
7 import org.springframework.dao.DataAccessException;
8
9
10
11
12
13
14 public class BaseHibernateDaoImplIT extends AbstractDependencyInjectionTest {
15
16 BaseHibernateDAOImpl<Language> dao;
17 LanguageDAO languageDAO;
18
19 @Test
20 public void testSave() {
21
22 try {
23 dao.save(null);
24 fail("save of null entity must throw DataAccessException");
25 } catch (DataAccessException dae) {
26
27 } catch (Throwable t) {
28 fail("save of null entity must throw DataAccessException, not " + t);
29 }
30
31 Language entity = new Language();
32 entity.setLocale("en_US");
33 entity.setResourceKey("org.itracker.test.BaseHibernateDAOImpl");
34
35 try {
36 dao.save(null);
37 fail("save of null not-null property must throw DataAccessException");
38 } catch (DataAccessException dae) {
39
40 } catch (Throwable t) {
41 fail("save of null not-null property must throw DataAccessException, not "
42 + t);
43 }
44
45 entity.setResourceValue("BaseHibernateDAOImpl");
46 assertTrue("entity.new", entity.isNew());
47 dao.save(entity);
48 assertTrue("entity.!new", !entity.isNew());
49 try {
50 Language l = languageDAO.findByKeyAndLocale(
51 entity.getResourceKey(), entity.getLocale());
52
53 assertEquals("entity", entity, l);
54
55
56
57
58
59
60
61
62 } catch (DataAccessException dae) {
63 fail("failed to load entity from languageDAO");
64 }
65
66 }
67
68 @Test
69 public void testSaveOrUpdate() {
70
71 try {
72 dao.saveOrUpdate(null);
73 fail("saveOrUpdate of null entity must throw DataAccessException");
74 } catch (DataAccessException dae) {
75
76 } catch (Throwable t) {
77 fail("saveOrUpdate of null entity must throw DataAccessException, not "
78 + t);
79 }
80
81 Language entity = new Language();
82 entity.setLocale("en_US");
83 entity.setResourceKey("org.itracker.test.BaseHibernateDAOImpl");
84
85 try {
86 dao.saveOrUpdate(null);
87 fail("saveOrUpdate of null not-null property must throw DataAccessException");
88 } catch (DataAccessException dae) {
89
90 } catch (Throwable t) {
91 fail("saveOrUpdate of null not-null property must throw DataAccessException, not "
92 + t);
93 }
94
95 entity.setResourceValue("BaseHibernateDAOImpl");
96 dao.saveOrUpdate(entity);
97
98 try {
99 Language l = languageDAO.findByKeyAndLocale(
100 entity.getResourceKey(), entity.getLocale());
101
102 assertEquals("entity", entity, l);
103 try {
104 dao.saveOrUpdate(entity);
105 } catch (Throwable t) {
106 fail("saveOrUpdate of persisted entity must not throw " + t);
107 }
108 } catch (DataAccessException dae) {
109 fail("failed to load entity from languageDAO");
110 }
111 }
112
113 @Test
114 public void testDelete() throws Exception {
115
116 try {
117 dao.delete(null);
118 fail("delete of null entity must throw DataAccessException");
119 } catch (DataAccessException dae) {
120
121 }
122
123 Language entity = new Language();
124 entity.setLocale("en_US");
125 entity.setResourceKey("org.itracker.test.BaseHibernateDAOImpl");
126 entity.setResourceValue("BaseHibernateDAOImpl");
127
128 dao.save(entity);
129
130 entity = languageDAO.findByKeyAndLocale(entity.getResourceKey(), entity
131 .getLocale());
132 dao.delete(entity);
133
134 try {
135 entity = languageDAO.findByKeyAndLocale(entity.getResourceKey(),
136 entity.getLocale());
137 fail("expecting to catch NoSuchEntityException");
138 } catch (NoSuchEntityException dae) {
139
140 }
141 }
142
143 @Test
144 public void testDetach() throws Exception {
145 try {
146 dao.detach(null);
147 fail("detach of null entity must throw DataAccessException");
148 } catch (DataAccessException dae) {
149
150 }
151
152 Language entity = new Language();
153 entity.setLocale("en_US");
154 entity.setResourceKey("org.itracker.test.BaseHibernateDAOImpl");
155 entity.setResourceValue("BaseHibernateDAOImpl");
156
157 dao.save(entity);
158
159 entity = languageDAO.findByKeyAndLocale(entity.getResourceKey(), entity
160 .getLocale());
161 dao.detach(entity);
162
163 Language l = languageDAO.findByKeyAndLocale(entity.getResourceKey(),
164 entity.getLocale());
165 assertNotSame("detached object", l, entity);
166
167 }
168
169 @Test
170 public void testRefresh() throws Exception {
171
172 try {
173 dao.refresh(null);
174 fail("Should throw DataAccessException");
175 } catch (DataAccessException e) {
176
177 } catch (Exception e) {
178 fail("Should throw DataAccessException not " + e);
179 }
180
181 Language entity = new Language();
182 entity.setLocale("en_US");
183 entity.setResourceKey("org.itracker.test.BaseHibernateDAOImpl");
184 entity.setResourceValue("BaseHibernateDAOImpl");
185
186 dao.save(entity);
187 dao.detach(entity);
188
189 Language l = languageDAO.findByKeyAndLocale(entity.getResourceKey(),
190 entity.getLocale());
191 dao.detach(l);
192
193 entity.setResourceValue("org.itracker.test.BaseHibernateDAO2");
194 dao.merge(entity);
195 assertNotSame("detached and managed entity are same", l, entity);
196
197 assertTrue("entity.resourceValue not equals l.resourceValue", !entity
198 .getResourceValue().equals(l.getResourceValue()));
199
200 l = languageDAO.findByKeyAndLocale(entity.getResourceKey(), entity
201 .getLocale());
202 assertEquals("entity.resourceValue", l.getResourceValue(), entity
203 .getResourceValue());
204
205 }
206
207 @Test
208 public void testMerge() throws Exception {
209 try {
210 dao.merge(null);
211 fail("Should throw DataAccessException");
212 } catch (DataAccessException e) {
213
214 } catch (Exception e) {
215 fail("Should throw DataAccessException not " + e);
216 }
217
218 Language entity = new Language();
219 entity.setLocale("en_US");
220 entity.setResourceKey("org.itracker.test.BaseHibernateDAOImpl");
221 entity.setResourceValue("BaseHibernateDAOImpl");
222
223 dao.save(entity);
224 dao.detach(entity);
225
226 Language l = languageDAO.findByKeyAndLocale(entity.getResourceKey(),
227 entity.getLocale());
228 l.setResourceValue("test");
229 dao.saveOrUpdate(l);
230
231 entity.setResourceValue("org.itracker.test.BaseHibernateDAOImpl2");
232
233 assertTrue("entity.resourceValue not equals l.resourceValue", !l
234 .getResourceValue().equals(entity.getResourceValue()));
235
236 dao.detach(l);
237 dao.merge(entity);
238
239 assertTrue("entity.resourceValue not equals l.resourceValue", !l
240 .getResourceValue().equals(entity.getResourceValue()));
241
242 l = languageDAO.findByKeyAndLocale(entity.getResourceKey(), entity
243 .getLocale());
244
245 assertEquals("entity.resourceKey", l.getResourceKey(), entity
246 .getResourceKey());
247 assertEquals("entity.resourceValue", l.getResourceValue(), entity
248 .getResourceValue());
249 }
250
251 @Override
252 public void onSetUp() throws Exception {
253 super.onSetUp();
254
255 languageDAO = (LanguageDAO) applicationContext.getBean("languageDAO");
256 dao = new BaseHibernateDAOImpl<Language>() {
257 };
258 dao.setSessionFactory((SessionFactory) applicationContext
259 .getBean("sessionFactory"));
260 }
261
262 @Override
263 protected String[] getDataSetFiles() {
264 return new String[]{"dataset/languagebean_dataset.xml"};
265 }
266
267 @Override
268 protected String[] getConfigLocations() {
269 return new String[]{"application-context.xml"};
270 }
271 }