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