1 /*****************************************************************************
5 * Creation date 12 Oct 2012
8 * Generated by Siman Generator on $date
9 *****************************************************************************/
10 package test.splat.dao;
12 import java.util.Date;
14 import org.splat.dal.bo.kernel.User;
15 import org.splat.dal.bo.som.${EntityClass};
16 import org.splat.dal.dao.som.${EntityClass}DAO;
17 import org.splat.kernel.InvalidPropertyException;
18 import org.splat.kernel.MissedPropertyException;
19 import org.splat.kernel.MultiplyDefinedException;
20 import org.splat.log.AppLogger;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.beans.factory.annotation.Qualifier;
23 import org.springframework.orm.hibernate3.HibernateTemplate;
24 import org.testng.Assert;
25 import org.testng.annotations.Test;
27 import test.splat.common.BaseTest;
30 * Test class for ${EntityClass}DAO.
32 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
35 public class Test${EntityClass}DAO extends BaseTest {
38 * Logger for the class.
40 private static final AppLogger LOG = AppLogger
41 .getLogger(Test${EntityClass}DAO.class);
44 * The tested ${EntityClass}DAO. Later injected by Spring.
47 @Qualifier("${DAOBean}")
48 private transient ${EntityClass}DAO _${DAOBean};
51 * Test creation of an object.<BR>
52 * <B>Description :</B> <BR>
53 * <i>Create an object.</i><BR>
54 * <B>Action : </B><BR>
55 * <i>1. call DAO's create method for a good transient object.</i><BR>
56 * <i>2. call DAO's create method for an object with non-zero id.</i><BR>
57 * <B>Test data : </B><BR>
58 * <i>no input parameters</i><BR>
59 * <i>no input parameters</i><BR>
61 * <B>Outcome results:</B><BR>
64 * <li>Object is created in the database successfully<BR>
66 * <li>Exception is thrown<BR>
71 * @throws InvalidPropertyException
72 * if an invalid property is used when creating objects
73 * @throws MultiplyDefinedException
74 * when trying to create an object with already existing id
75 * @throws MissedPropertyException
76 * if a mandatory property is not defined for an object to be created
80 public void testCreate() throws InvalidPropertyException,
81 MissedPropertyException, MultiplyDefinedException {
82 LOG.debug(">>>>> BEGIN testCreate()");
84 ${EntityClass} anObject = get${EntityClass}();
85 // Call DAO's create method for a good transient object.
86 Long id = _${DAOBean}.create(anObject);
87 Assert.assertNotNull(id,
88 "Create method returns null instead of a new id.");
89 Assert.assertTrue(id > 0, "The new id is not a positive number.");
90 ${EntityClass} anObjectFound = getHibernateTemplate().get(
91 ${EntityClass}.class, id);
92 compareObjects(anObjectFound, anObject);
94 // Call DAO's create method for an object with non-zero id.
95 // TODO: Fill the object's properties
96 ${EntityClass} aBadObj = new ${EntityClass}(
97 (new ${EntityClass}.Properties()));
98 aBadObj.setIndex(anObjectFound.getIndex());
100 _${DAOBean}.create(aBadObj);
101 getHibernateTemplate().flush();
102 Assert.fail("Creation with existing id must be failed.");
103 } catch (Exception e) {
104 LOG.debug("Expected exception is thrown: "
105 + e.getClass().getSimpleName() + ": " + e.getMessage());
107 LOG.debug(">>>>> END testCreate()");
111 * Test of getting an object.<BR>
112 * <B>Description :</B> <BR>
113 * <i>Create an object and try to get it from the database.</i><BR>
114 * <B>Action : </B><BR>
115 * <i>1. call DAO's read method for an existing id.</i><BR>
116 * <i>2. call DAO's read method for a not existing id.</i><BR>
117 * <B>Test data : </B><BR>
118 * <i>no input parameters</i><BR>
119 * <i>no input parameters</i><BR>
121 * <B>Outcome results:</B><BR>
124 * <li>Object is found in the database successfully<BR>
126 * <li>Exception is thrown<BR>
131 * @throws InvalidPropertyException
132 * if an invalid property is used when creating objects
133 * @throws MultiplyDefinedException
134 * when trying to create an object with already existing id
135 * @throws MissedPropertyException
136 * if a mandatory property is not defined for an object to be created
140 public void testGet() throws InvalidPropertyException,
141 MissedPropertyException, MultiplyDefinedException {
142 LOG.debug(">>>>> BEGIN testGet()");
143 ${EntityClass} anObject = get${EntityClass}();
144 // Call DAO's create method for a good transient object.
145 Long id = _${DAOBean}.create(anObject);
146 Assert.assertNotNull(id,
147 "Create method returns null instead of a new id.");
148 Assert.assertTrue(id > 0, "The new id is not a positive number.");
150 // Call DAO's get method for an existing id.
151 ${EntityClass} anObjectFound = _${DAOBean}.get(id);
153 compareObjects(anObjectFound, anObject);
155 // Call DAO's get method for a not existing id.
157 anObjectFound = _${DAOBean}.get(-1L);
158 getHibernateTemplate().flush();
160 .fail("Getting an object with not existing id must be failed.");
161 } catch (Exception e) {
162 LOG.debug("Expected exception is thrown: "
163 + e.getClass().getSimpleName() + ": " + e.getMessage());
166 anObjectFound = _${DAOBean}.get(0L);
167 getHibernateTemplate().flush();
169 .fail("Getting an object with not existing id must be failed.");
170 } catch (Exception e) {
171 LOG.debug("Expected exception is thrown: "
172 + e.getClass().getSimpleName() + ": " + e.getMessage());
175 anObjectFound = _${DAOBean}.get(id + 1);
176 getHibernateTemplate().flush();
178 .fail("Getting an object with not existing id must be failed.");
179 } catch (Exception e) {
180 LOG.debug("Expected exception is thrown: "
181 + e.getClass().getSimpleName() + ": " + e.getMessage());
183 LOG.debug(">>>>> END testGet()");
187 * Test of updating an object.<BR>
188 * <B>Description :</B> <BR>
189 * <i>Create an object and try to update it with another data.</i><BR>
190 * <B>Action : </B><BR>
191 * <i>1. call DAO's update method for an existing id.</i><BR>
192 * <i>2. call DAO's update method for a not existing id.</i><BR>
193 * <i>3. call DAO's update method for wrong data.</i><BR>
194 * <B>Test data : </B><BR>
195 * <i>no input parameters</i><BR>
196 * <i>no input parameters</i><BR>
197 * <i>no input parameters</i><BR>
199 * <B>Outcome results:</B><BR>
202 * <li>Object is update in the database successfully<BR>
204 * <li>Exception is thrown<BR>
206 * <li>Exception is thrown<BR>
211 * @throws InvalidPropertyException
212 * if an invalid property is used when creating objects
213 * @throws MultiplyDefinedException
214 * when trying to create an object with already existing id
215 * @throws MissedPropertyException
216 * if a mandatory property is not defined for an object to be created
220 public void testUpdate() throws InvalidPropertyException,
221 MissedPropertyException, MultiplyDefinedException {
222 LOG.debug(">>>>> BEGIN testUpdate()");
223 ${EntityClass} anObject = get${EntityClass}();
224 // Call DAO's create method for a good transient object.
225 Long id = _${DAOBean}.create(anObject);
226 Assert.assertNotNull(id,
227 "Create method returns null instead of a new id.");
228 Assert.assertTrue(id > 0, "The new id is not a positive number.");
230 // Call DAO's update method for an existing id.
231 // TODO: Modify some object's properties to update it later
233 .assertTrue(anObject.getProgressState() != ProgressState.APPROVED,
234 "The initial state of the object should not be APPROVED.");
235 anObject.setProgressState(ProgressState.APPROVED);
236 anObject.setTitle(anObject.getTitle() + " updated");*/
238 _${DAOBean}.update(anObject);
240 // Check that the object has been updated.
241 ${EntityClass} anObjectFound = _${DAOBean}.get(id);
243 compareObjects(anObjectFound, anObject);
245 // Call DAO's create method for an object with non-zero id.
246 // TODO: Fill the object's properties
247 ${EntityClass} aBadObj = new ${EntityClass}(
248 (new ${EntityClass}.Properties()));
249 // aBadObj.setIndex(anObjectFound.getIndex());
251 _${DAOBean}.update(aBadObj);
252 getHibernateTemplate().flush();
253 Assert.fail("Update with not existing id must be failed.");
254 } catch (Exception e) {
255 LOG.debug("Expected exception is thrown: "
256 + e.getClass().getSimpleName() + ": " + e.getMessage());
258 // Call update with bad data (null title).
259 aBadObj.setIndex(anObjectFound.getIndex());
260 aBadObj.setTitle(null);
262 _${DAOBean}.update(aBadObj);
263 getHibernateTemplate().flush();
264 Assert.fail("Update with null title must be failed.");
265 } catch (Exception e) {
266 LOG.debug("Expected exception is thrown: "
267 + e.getClass().getSimpleName() + ": " + e.getMessage());
269 // Call update with bad data (null state).
270 aBadObj.setTitle(anObjectFound.getTitle());
271 aBadObj.setProgressState(null);
273 _${DAOBean}.update(aBadObj);
274 getHibernateTemplate().flush();
275 Assert.fail("Update with null state must be failed.");
276 } catch (Exception e) {
277 LOG.debug("Expected exception is thrown: "
278 + e.getClass().getSimpleName() + ": " + e.getMessage());
280 LOG.debug(">>>>> END testUpdate()");
284 * Test of deleting an object.<BR>
285 * <B>Description :</B> <BR>
286 * <i>Create an object and try to delete it.</i><BR>
287 * <B>Action : </B><BR>
288 * <i>1. call DAO's delete method for an existing id.</i><BR>
289 * <i>2. call DAO's delete method for a not existing id.</i><BR>
290 * <B>Test data : </B><BR>
291 * <i>no input parameters</i><BR>
292 * <i>no input parameters</i><BR>
294 * <B>Outcome results:</B><BR>
297 * <li>Object is found in the database successfully<BR>
299 * <li>Exception is thrown<BR>
304 * @throws InvalidPropertyException
305 * if an invalid property is used when creating objects
306 * @throws MultiplyDefinedException
307 * when trying to create an object with already existing id
308 * @throws MissedPropertyException
309 * if a mandatory property is not defined for an object to be created
313 public void testDelete() throws InvalidPropertyException,
314 MissedPropertyException, MultiplyDefinedException {
315 LOG.debug(">>>>> BEGIN testDelete()");
316 ${EntityClass} anObject = get${EntityClass}();
317 // Call DAO's create method for a good transient object.
318 Long id = _${DAOBean}.create(anObject);
319 Assert.assertNotNull(id,
320 "Create method returns null instead of a new id.");
321 Assert.assertTrue(id > 0, "The new id is not a positive number.");
323 // Call DAO's delete method for an existing id.
324 _${DAOBean}.delete(anObject);
326 // Check that the object has been deleted.
327 ${EntityClass} anObjectFound = _${DAOBean}.get(id);
329 Assert.assertNull(anObjectFound, "Deleted object must not be found.");
330 // Call DAO's delete method for a not existing id.
332 _${DAOBean}.delete(anObject);
333 getHibernateTemplate().flush();
334 Assert.fail("Delete with with not existing id must be failed.");
335 } catch (Exception e) {
336 LOG.debug("Expected exception is thrown: "
337 + e.getClass().getSimpleName() + ": " + e.getMessage());
339 LOG.debug(">>>>> END testDelete()");
343 * Create a transient ${EntityClass} for tests.
345 * @return a transient ${EntityClass}
346 * @throws InvalidPropertyException
347 * if an invalid property is used when creating objects
348 * @throws MultiplyDefinedException
349 * when trying to create an object with already existing id
350 * @throws MissedPropertyException
351 * if a mandatory property is not defined for an object to be created
353 private ${EntityClass} get${EntityClass}()
354 throws InvalidPropertyException, MissedPropertyException,
355 MultiplyDefinedException {
356 // Create a test knowledge type
357 HibernateTemplate ht = getHibernateTemplate();
359 ${EntityClass}.Properties kprops = new ${EntityClass}.Properties();
360 // Prepare a transient object
362 return new ${EntityClass}(kprops);
366 * Check that given objects are equal.
369 * the object to check
371 * the expected object
373 private void compareObjects(${EntityClass} anActual,
374 ${EntityClass} anExpected) {
375 Assert.assertNotNull(anActual,
376 "Created object is not found in the database.");
377 Assert.assertEquals(anActual.getAuthor(), anExpected.getAuthor(),
378 "object author is not saved.");
379 Assert.assertEquals(anActual.getDate(), anExpected.getDate(),
380 "object date is not saved.");
382 .assertEquals(anActual.getOwnerScenario(), anExpected
384 "object scenario is not saved.");
385 Assert.assertEquals(anActual.getProgressState(), anExpected
386 .getProgressState(), "object state is not saved.");
387 Assert.assertEquals(anActual.getTitle(), anExpected.getTitle(),
388 "object title is not saved.");
389 Assert.assertEquals(anActual.getType(), anExpected.getType(),
390 "object type is not saved.");
391 Assert.assertEquals(anActual.getValue(), anExpected.getValue(),
392 "object value is not saved.");
393 Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(),
394 "object index is not saved.");