|
30 | 30 | * @author Cynick Young |
31 | 31 | */ |
32 | 32 | public abstract class AbstractTransactionalDaoTests<DAO extends SimpleDao<OBJECT>, OBJECT extends Persistable> |
33 | | - extends AbstractTransactionalDbTests { |
34 | | - |
35 | | - protected DAO dao; |
36 | | - |
37 | | - protected OBJECT dataObject; |
38 | | - |
39 | | - private static final Long NON_EXISTENT_PK = new Long(666); |
40 | | - |
41 | | - /** |
42 | | - * Test method for |
43 | | - * {@link org.wise.portal.dao.impl.AbstractHibernateDao#delete(java.lang.Object)}. |
44 | | - */ |
45 | | - public void testDelete() { |
46 | | - this.verifyDataStoreIsEmpty(); |
47 | | - |
48 | | - // save and delete the data object using dao |
49 | | - this.dao.save(this.dataObject); |
50 | | - this.dao.delete(this.dataObject); |
51 | | - |
52 | | - // * NOTE * must flush to test delete |
53 | | - // see http://forum.springframework.org/showthread.php?t=18263 for |
54 | | - // explanation |
55 | | - this.toilet.flush(); |
56 | | - |
57 | | - this.verifyDataStoreIsEmpty(); |
58 | | - } |
59 | | - |
60 | | - /** |
61 | | - * Test method for |
62 | | - * {@link org.wise.portal.dao.impl.AbstractHibernateDao#save(java.lang.Object)}. |
63 | | - */ |
64 | | - public abstract void testSave(); |
65 | | - |
66 | | - /** |
67 | | - * Test method for |
68 | | - * {@link org.wise.portal.dao.impl.AbstractHibernateDao#getList()}. |
69 | | - */ |
70 | | - public void testGetList() { |
71 | | - this.verifyDataStoreIsEmpty(); |
72 | | - List<OBJECT> actualList = this.dao.getList(); |
73 | | - assertTrue(actualList.isEmpty()); |
74 | | - |
75 | | - this.dao.save(this.dataObject); |
76 | | - List<?> expectedList = this.retrieveDataObjectListFromDb(); |
77 | | - assertEquals(1, expectedList.size()); |
78 | | - |
79 | | - actualList = this.dao.getList(); |
80 | | - assertEquals(1, actualList.size()); |
81 | | - assertEquals(this.dataObject, actualList.get(0)); |
82 | | - } |
83 | | - |
84 | | - /** |
85 | | - * Test method for |
86 | | - * {@link org.wise.portal.dao.impl.AbstractHibernateDao#getById(java.lang.Long)}. |
87 | | - */ |
88 | | - public void testGetById() throws Exception { |
89 | | - this.verifyDataStoreIsEmpty(); |
90 | | - try { |
91 | | - this.dao.getById(NON_EXISTENT_PK); |
92 | | - fail("Expected ObjectNotFoundException"); |
93 | | - } catch (ObjectNotFoundException e) { |
94 | | - |
95 | | - } |
96 | | - this.dao.save(this.dataObject); |
97 | | - List<OBJECT> actualList = this.dao.getList(); |
98 | | - OBJECT actualObject = actualList.get(0); |
99 | | - |
100 | | - assertTrue(this.dataObject.getId() instanceof Long); |
101 | | - assertEquals(actualObject, this.dao.getById((Long) this.dataObject |
102 | | - .getId())); |
103 | | - } |
104 | | - |
105 | | - protected final void verifyDataStoreIsEmpty() { |
106 | | - assertTrue(retrieveDataObjectListFromDb().isEmpty()); |
107 | | - } |
108 | | - |
109 | | - protected abstract List<?> retrieveDataObjectListFromDb(); |
110 | | - |
111 | | - /** |
112 | | - * @see org.springframework.test.AbstractTransactionalSpringContextTests#onTearDownAfterTransaction() |
113 | | - */ |
114 | | - @Override |
115 | | - protected void onTearDownAfterTransaction() throws Exception { |
116 | | - super.onTearDownAfterTransaction(); |
117 | | - this.dao = null; |
118 | | - this.dataObject = null; |
119 | | - } |
| 33 | + extends AbstractTransactionalDbTests { |
| 34 | + |
| 35 | + protected DAO dao; |
| 36 | + |
| 37 | + protected OBJECT dataObject; |
| 38 | + |
| 39 | + private static final Long NON_EXISTENT_PK = new Long(666); |
| 40 | + |
| 41 | + /** |
| 42 | + * Test method for |
| 43 | + * {@link org.wise.portal.dao.impl.AbstractHibernateDao#delete(java.lang.Object)}. |
| 44 | + */ |
| 45 | + public void testDelete() { |
| 46 | + this.verifyDataStoreIsEmpty(); |
| 47 | + |
| 48 | + // save and delete the data object using dao |
| 49 | + this.dao.save(this.dataObject); |
| 50 | + this.dao.delete(this.dataObject); |
| 51 | + |
| 52 | + // * NOTE * must flush to test delete |
| 53 | + // see http://forum.springframework.org/showthread.php?t=18263 for |
| 54 | + // explanation |
| 55 | + this.flush(); |
| 56 | + |
| 57 | + this.verifyDataStoreIsEmpty(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test method for |
| 62 | + * {@link org.wise.portal.dao.impl.AbstractHibernateDao#save(java.lang.Object)}. |
| 63 | + */ |
| 64 | + public abstract void testSave(); |
| 65 | + |
| 66 | + /** |
| 67 | + * Test method for |
| 68 | + * {@link org.wise.portal.dao.impl.AbstractHibernateDao#getList()}. |
| 69 | + */ |
| 70 | + public void testGetList() { |
| 71 | + this.verifyDataStoreIsEmpty(); |
| 72 | + List<OBJECT> actualList = this.dao.getList(); |
| 73 | + assertTrue(actualList.isEmpty()); |
| 74 | + |
| 75 | + this.dao.save(this.dataObject); |
| 76 | + List<?> expectedList = this.retrieveDataObjectListFromDb(); |
| 77 | + assertEquals(1, expectedList.size()); |
| 78 | + |
| 79 | + actualList = this.dao.getList(); |
| 80 | + assertEquals(1, actualList.size()); |
| 81 | + assertEquals(this.dataObject, actualList.get(0)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Test method for |
| 86 | + * {@link org.wise.portal.dao.impl.AbstractHibernateDao#getById(java.lang.Long)}. |
| 87 | + */ |
| 88 | + public void testGetById() throws Exception { |
| 89 | + this.verifyDataStoreIsEmpty(); |
| 90 | + try { |
| 91 | + this.dao.getById(NON_EXISTENT_PK); |
| 92 | + fail("Expected ObjectNotFoundException"); |
| 93 | + } catch (ObjectNotFoundException e) { |
| 94 | + |
| 95 | + } |
| 96 | + this.dao.save(this.dataObject); |
| 97 | + List<OBJECT> actualList = this.dao.getList(); |
| 98 | + OBJECT actualObject = actualList.get(0); |
| 99 | + |
| 100 | + assertTrue(this.dataObject.getId() instanceof Long); |
| 101 | + assertEquals(actualObject, this.dao.getById((Long) this.dataObject.getId())); |
| 102 | + } |
| 103 | + |
| 104 | + protected final void verifyDataStoreIsEmpty() { |
| 105 | + assertTrue(retrieveDataObjectListFromDb().isEmpty()); |
| 106 | + } |
| 107 | + |
| 108 | + protected abstract List<?> retrieveDataObjectListFromDb(); |
| 109 | + |
| 110 | + /** |
| 111 | + * @see org.springframework.test.AbstractTransactionalSpringContextTests#onTearDownAfterTransaction() |
| 112 | + */ |
| 113 | + @Override |
| 114 | + protected void onTearDownAfterTransaction() throws Exception { |
| 115 | + super.onTearDownAfterTransaction(); |
| 116 | + this.dao = null; |
| 117 | + this.dataObject = null; |
| 118 | + } |
120 | 119 | } |
0 commit comments