001: package org.springunit.framework.samples.jpetstore.dao;
002:
003: import java.util.List;
004: import java.util.Map;
005:
006: import org.hibernate.SessionFactory;
007: import org.springunit.framework.SpringUnitContext;
008: import org.springunit.framework.SpringUnitTransactionalTest;
009: import org.springunit.framework.samples.jpetstore.dao.hibernate.AbstractHibernateSpringDao;
010: import org.springunit.framework.samples.jpetstore.domain.Persistable;
011:
012: public abstract class AbstractDaoTest<V extends Persistable, D extends AbstractDao<V>>
013: extends SpringUnitTransactionalTest {
014:
015: public AbstractDaoTest() {
016: this (null);
017: }
018:
019: public AbstractDaoTest(String name) {
020: super (name);
021: }
022:
023: protected void onSetUpInTransaction() throws Exception {
024: this .subject = (D) getObject("subject");
025: this .dependencies = getObject("dependencies");
026: populateDependencies();
027: this .prepopulated = (List<? extends V>) getObject("prepopulated");
028: prepopulate();
029: }
030:
031: protected D getSubject() {
032: return this .subject;
033: }
034:
035: protected List<? extends V> getPrepopulated() {
036: return this .prepopulated;
037: }
038:
039: protected Map<String, List<? extends Object>> getDependencies() {
040: return this .dependencies;
041: }
042:
043: public SpringUnitContext getAbstractDaoTest() {
044: return this .abstractDaoTest;
045: }
046:
047: public void setAbstractDaoTest(SpringUnitContext abstractDaoTest) {
048: this .abstractDaoTest = abstractDaoTest;
049: }
050:
051: public void testCreateOne() throws Exception {
052: runCreateOne();
053: }
054:
055: public void testCreateMany() throws Exception {
056: runCreateMany();
057: }
058:
059: public void testDeleteOne() throws Exception {
060: runDeleteOne();
061: }
062:
063: public void testDeleteMany() throws Exception {
064: runDeleteMany();
065: }
066:
067: public void testReadOne() throws Exception {
068: runReadOne();
069: }
070:
071: public void testReadMany() throws Exception {
072: runReadMany();
073: }
074:
075: public void testUpdateOne() throws Exception {
076: runUpdateOne();
077: }
078:
079: public void testUpdateMany() throws Exception {
080: runUpdateMany();
081: }
082:
083: public void testUpdateNone() throws Exception {
084: runUpdateMany();
085: }
086:
087: public void testGetSessionFactory() throws Exception {
088: runGetSessionFactory();
089: }
090:
091: protected void runCreateOne() throws Exception {
092: V item = (V) getObject("item");
093: V expected = (V) getObject("expected");
094: Exception expectedException = getObject("expectedException");
095: try {
096: V actual = getSubject().create(item);
097: if (expectedException != null) {
098: fail("Exception not thrown.");
099: }
100: assertTrue(isEqual(expected, actual));
101: } catch (Exception ex) {
102: if (expectedException == null
103: || !expectedException.getClass().isAssignableFrom(
104: ex.getClass())) {
105: throw ex;
106: }
107: }
108: }
109:
110: protected void runCreateMany() throws Exception {
111: List<? extends V> items = (List<? extends V>) getObject("items");
112: List<? extends V> expecteds = (List<? extends V>) getObject("expecteds");
113: Exception expectedException = getObject("expectedException");
114: try {
115: List<? extends V> actuals = getSubject().create(items);
116: if (expectedException != null) {
117: fail("Exception not thrown.");
118: }
119: assertTrue(areEqual(expecteds, actuals));
120: } catch (Exception ex) {
121: if (expectedException == null
122: || !expectedException.getClass().isAssignableFrom(
123: ex.getClass())) {
124: throw ex;
125: }
126: }
127: }
128:
129: protected void runDeleteOne() throws Exception {
130: V item = (V) getPrepopulated().get(0);
131: Exception expectedException = getObject("expectedException");
132: try {
133: getSubject().delete(item);
134: if (expectedException != null) {
135: fail("Exception not thrown.");
136: }
137: } catch (Exception ex) {
138: if (expectedException == null
139: || !expectedException.getClass().isAssignableFrom(
140: ex.getClass())) {
141: throw ex;
142: }
143: }
144: }
145:
146: protected void runDeleteMany() throws Exception {
147: List<? extends V> items = getPrepopulated();
148: Exception expectedException = getObject("expectedException");
149: try {
150: getSubject().delete(items);
151: if (expectedException != null) {
152: fail("Exception not thrown.");
153: }
154: } catch (Exception ex) {
155: if (expectedException == null
156: || !expectedException.getClass().isAssignableFrom(
157: ex.getClass())) {
158: throw ex;
159: }
160: }
161: }
162:
163: protected void runReadOne() throws Exception {
164: V expected = (V) getPrepopulated().get(0);
165: int id = expected.getId();
166: Exception expectedException = getObject("expectedException");
167: try {
168: V actual = getSubject().read(id);
169: if (expectedException != null) {
170: fail("Exception not thrown.");
171: }
172: assertEquals(expected, actual);
173: } catch (Exception ex) {
174: if (expectedException == null
175: || !expectedException.getClass().isAssignableFrom(
176: ex.getClass())) {
177: throw ex;
178: }
179: }
180: }
181:
182: protected void runReadMany() throws Exception {
183: List<? extends V> expecteds = getPrepopulated();
184: Exception expectedException = getObject("expectedException");
185: try {
186: List<? extends V> actuals = getSubject().read();
187: if (expectedException != null) {
188: fail("Exception not thrown.");
189: }
190: assertTrue(areEqualSets(expecteds, actuals));
191: } catch (Exception ex) {
192: if (expectedException == null
193: || !expectedException.getClass().isAssignableFrom(
194: ex.getClass())) {
195: throw ex;
196: }
197: }
198: }
199:
200: protected void runUpdateOne() throws Exception {
201: V item = getPrepopulated().get(0);
202: Map<String, Object> values = getObject("values");
203: modify(item, values);
204: Exception expectedException = getObject("expectedException");
205: try {
206: getSubject().update(item);
207: if (expectedException != null) {
208: fail("Exception not thrown.");
209: }
210: } catch (Exception ex) {
211: if (expectedException == null
212: || !expectedException.getClass().isAssignableFrom(
213: ex.getClass())) {
214: throw ex;
215: }
216: }
217: }
218:
219: protected void runUpdateMany() throws Exception {
220: List<? extends V> items = getPrepopulated();
221: List<Map<String, Object>> values = getObject("values");
222: modify(items, values);
223: Exception expectedException = getObject("expectedException");
224: try {
225: getSubject().update(items);
226: if (expectedException != null) {
227: fail("Exception not thrown.");
228: }
229: } catch (Exception ex) {
230: if (expectedException == null
231: || !expectedException.getClass().isAssignableFrom(
232: ex.getClass())) {
233: throw ex;
234: }
235: }
236: }
237:
238: protected void runGetSessionFactory() throws Exception {
239: SessionFactory actual = ((AbstractHibernateSpringDao<V>) getSubject())
240: .getSessionFactory();
241: assertNotNull(actual);
242: }
243:
244: protected void prepopulate() throws Exception {
245: if (getPrepopulated() != null && getPrepopulated().size() > 0) {
246: getSubject().create(getPrepopulated());
247: }
248: }
249:
250: protected void populateDependencies() throws Exception {
251: }
252:
253: protected boolean isEqual(V left, V right) {
254: if (left == null && right == null) {
255: return true;
256: } else if (left == null && right != null || left != null
257: && right == null) {
258: return false;
259: } else {
260: return left.equals(right);
261: }
262: }
263:
264: protected boolean areEqual(List<? extends V> left,
265: List<? extends V> right) {
266: boolean result = left.size() == right.size();
267: if (result) {
268: for (int i = 0; i < left.size(); i++) {
269: V l = left.get(i);
270: V r = right.get(i);
271: result &= isEqual(l, r);
272: if (!result) {
273: break;
274: }
275: }
276: }
277: return result;
278: }
279:
280: protected boolean areEqualSets(List<? extends V> left,
281: List<? extends V> right) {
282: boolean result = left.size() == right.size();
283: if (result) {
284: for (V l : left) {
285: boolean found = false;
286: for (V r : right) {
287: found = isEqual(l, r);
288: if (found) {
289: break;
290: }
291: }
292: result &= found;
293: if (!result) {
294: break;
295: }
296: }
297: }
298: return result;
299: }
300:
301: protected boolean areEqualSetsString(List<String> left,
302: List<String> right) {
303: boolean result = left.size() == right.size();
304: if (result) {
305: for (String l : left) {
306: boolean found = false;
307: for (String r : right) {
308: found = l.equals(r);
309: if (found) {
310: break;
311: }
312: }
313: result &= found;
314: if (!result) {
315: break;
316: }
317: }
318: }
319: return result;
320: }
321:
322: protected abstract void modify(V item, Map<String, Object> values);
323:
324: protected void modify(List<? extends V> items,
325: List<Map<String, Object>> values) {
326: for (int i = 0; i < items.size(); i++) {
327: modify(items.get(i), values.get(i));
328: }
329: }
330:
331: private SpringUnitContext abstractDaoTest;
332:
333: private Map<String, List<? extends Object>> dependencies;
334:
335: private D subject;
336:
337: private List<? extends V> prepopulated;
338:
339: }
|