001: /**
002: * Author: Matthew Baird
003: * mattbaird@yahoo.com
004: */package org.apache.ojb.broker;
005:
006: import org.apache.ojb.broker.query.Criteria;
007: import org.apache.ojb.broker.query.Query;
008: import org.apache.ojb.broker.query.QueryFactory;
009: import org.apache.ojb.junit.PBTestCase;
010:
011: import java.sql.Timestamp;
012: import java.util.Iterator;
013:
014: /**
015: * This TestClass tests OJB ability to handle Contract Version Effectiveness patterns.
016: */
017: public class ComplexMultiMappedTableWithCollectionByQueryTest extends
018: PBTestCase {
019: private int COUNT = 10;
020: private static Class CLASS = ComplexMultiMappedTableWithCollectionByQueryTest.class;
021:
022: public static void main(String[] args) {
023: String[] arr = { CLASS.getName() };
024: junit.textui.TestRunner.main(arr);
025: }
026:
027: /**
028: * Insert the method's description here.
029: * Creation date: (24.12.2000 00:33:40)
030: */
031: public ComplexMultiMappedTableWithCollectionByQueryTest(String name) {
032: super (name);
033: }
034:
035: /**
036: * Insert the method's description here.
037: * Creation date: (06.12.2000 21:58:53)
038: */
039: public void setUp() throws Exception {
040: super .setUp();
041: createTestData();
042: }
043:
044: private int deleteAllData() {
045: int number_deleted = 0;
046: Criteria crit = new Criteria();
047: Query q = QueryFactory.newQuery(
048: ComplexMultiMapped.PersistentA.class, crit);
049: broker.beginTransaction();
050: Iterator iter = broker.getCollectionByQuery(q).iterator();
051: while (iter.hasNext()) {
052: broker.delete(iter.next());
053: number_deleted++;
054: }
055: /**
056: * will delete all B and D and E (both of which extends B)
057: */
058: crit = new Criteria();
059: q = QueryFactory.newQuery(ComplexMultiMapped.PersistentB.class,
060: crit);
061: iter = broker.getCollectionByQuery(q).iterator();
062:
063: while (iter.hasNext()) {
064: broker.delete(iter.next());
065: number_deleted++;
066: }
067: /**
068: * will delete all C
069: */
070: crit = new Criteria();
071: q = QueryFactory.newQuery(ComplexMultiMapped.PersistentC.class,
072: crit);
073: iter = broker.getCollectionByQuery(q).iterator();
074: while (iter.hasNext()) {
075: broker.delete(iter.next());
076: number_deleted++;
077: }
078: broker.commitTransaction();
079: return number_deleted;
080: }
081:
082: private void createTestData() {
083: /**
084: * create COUNT of each object
085: */
086: broker.beginTransaction();
087: for (int i = 0; i < COUNT; i++) {
088: ComplexMultiMapped.PersistentA a = new ComplexMultiMapped.PersistentA();
089: a.setValue1("a");
090: a.setValue2(i);
091: a.setValue3(new Timestamp(System.currentTimeMillis()));
092: broker.store(a);
093:
094: ComplexMultiMapped.PersistentB b = new ComplexMultiMapped.PersistentB();
095: b.setValue4("b");
096: b.setValue5(i);
097: b.setValue6(new Timestamp(System.currentTimeMillis()));
098: broker.store(b);
099:
100: ComplexMultiMapped.PersistentC c = new ComplexMultiMapped.PersistentC();
101: c.setValue1("c");
102: c.setValue2(i);
103: c.setValue3(new Timestamp(System.currentTimeMillis()));
104: c.setValue4("c");
105: c.setValue5(i);
106: c.setValue6(new Timestamp(System.currentTimeMillis()));
107: broker.store(c);
108:
109: ComplexMultiMapped.PersistentD d = new ComplexMultiMapped.PersistentD();
110: d.setValue1("d");
111: d.setValue2(i);
112: d.setValue3(new Timestamp(System.currentTimeMillis()));
113: d.setValue4("d");
114: d.setValue5(i);
115: d.setValue6(new Timestamp(System.currentTimeMillis()));
116: broker.store(d);
117:
118: ComplexMultiMapped.PersistentE e = new ComplexMultiMapped.PersistentE();
119: e.setValue1("e");
120: e.setValue2(i);
121: e.setValue3(new Timestamp(System.currentTimeMillis()));
122: e.setValue4("e");
123: e.setValue5(i);
124: e.setValue6(new Timestamp(System.currentTimeMillis()));
125: broker.store(e);
126:
127: ComplexMultiMapped.PersistentF f = new ComplexMultiMapped.PersistentF();
128: f.setValue1("f");
129: f.setValue2(i);
130: f.setValue3(new Timestamp(System.currentTimeMillis()));
131: f.setValue4("f");
132: f.setValue5(i);
133: f.setValue6(new Timestamp(System.currentTimeMillis()));
134: broker.store(f);
135: }
136: broker.commitTransaction();
137: }
138:
139: public void testCreate() {
140: createTestData();
141: }
142:
143: public void testGet() {
144: /**
145: * get to a clean, known state.
146: */
147: deleteAllData();
148: /**
149: * now create a bunch of test data.
150: */
151: createTestData();
152:
153: Criteria crit = new Criteria();
154: Query q;
155: Iterator iter;
156: int count;
157: /**
158: * check all A's
159: */
160: q = QueryFactory.newQuery(ComplexMultiMapped.PersistentA.class,
161: crit);
162: iter = broker.getCollectionByQuery(q).iterator();
163: ComplexMultiMapped.PersistentA a = null;
164: count = 0;
165: while (iter.hasNext()) {
166: a = (ComplexMultiMapped.PersistentA) iter.next();
167: if (!a.getValue1().equals("a")) {
168: fail("getValue1 should have returned 'a', it in fact returned '"
169: + a.getValue1() + "'");
170: }
171: count++;
172: }
173: if (count != COUNT)
174: fail("should have found "
175: + COUNT
176: + " ComplexMultiMapped.PersistentA's, in fact found "
177: + count);
178:
179: assertEquals("counted size", broker.getCount(q), count);
180:
181: /**
182: * check all B's
183: */
184: crit = new Criteria();
185: q = QueryFactory.newQuery(ComplexMultiMapped.PersistentB.class,
186: crit);
187: iter = broker.getCollectionByQuery(q).iterator();
188: ComplexMultiMapped.PersistentB b = null;
189: count = 0;
190: while (iter.hasNext()) {
191: b = (ComplexMultiMapped.PersistentB) iter.next();
192: if (!b.getValue4().equals("b")
193: && !b.getValue4().equals("d")
194: && !b.getValue4().equals("e")
195: && !b.getValue4().equals("f")) {
196: fail("getValue4 should have returned 'b' or 'd' or 'e' or 'f' (from extent), it in fact returned '"
197: + b.getValue4() + "'");
198: }
199: count++;
200: }
201: /**
202: * should find ALL b's, d's, e's and f's, so COUNT*4 is the expected result
203: */
204: if (count != COUNT * 4)
205: fail("should have found "
206: + (COUNT * 4)
207: + " ComplexMultiMapped.PersistentB's, in fact found "
208: + count);
209:
210: assertEquals("counted size", broker.getCount(q), count);
211:
212: /**
213: * check all C's
214: */
215: crit = new Criteria();
216: q = QueryFactory.newQuery(ComplexMultiMapped.PersistentC.class,
217: crit);
218: iter = broker.getCollectionByQuery(q).iterator();
219: ComplexMultiMapped.PersistentC c = null;
220: count = 0;
221: while (iter.hasNext()) {
222: c = (ComplexMultiMapped.PersistentC) iter.next();
223: if (!c.getValue1().equals("c")) {
224: fail("getValue1 should have returned 'c', it in fact returned '"
225: + c.getValue1() + "'");
226: }
227: count++;
228: }
229: if (count != COUNT)
230: fail("should have found "
231: + COUNT
232: + " ComplexMultiMapped.PersistentC's, in fact found "
233: + count);
234:
235: assertEquals("counted size", broker.getCount(q), count);
236:
237: /**
238: * check all D's
239: */
240: crit = new Criteria();
241: q = QueryFactory.newQuery(ComplexMultiMapped.PersistentD.class,
242: crit);
243: iter = broker.getCollectionByQuery(q).iterator();
244: ComplexMultiMapped.PersistentD d = null;
245: count = 0;
246: while (iter.hasNext()) {
247: d = (ComplexMultiMapped.PersistentD) iter.next();
248: if (!d.getValue1().equals("d")) {
249: fail("getValue1 should have returned 'd', it in fact returned '"
250: + d.getValue1() + "'");
251: }
252: count++;
253: }
254: if (count != COUNT)
255: fail("should have found "
256: + COUNT
257: + " ComplexMultiMapped.PersistentD's, in fact found "
258: + count);
259:
260: assertEquals("counted size", broker.getCount(q), count);
261:
262: /**
263: * check all E's
264: */
265: crit = new Criteria();
266: q = QueryFactory.newQuery(ComplexMultiMapped.PersistentE.class,
267: crit);
268: iter = broker.getCollectionByQuery(q).iterator();
269: ComplexMultiMapped.PersistentE e = null;
270: count = 0;
271: while (iter.hasNext()) {
272: e = (ComplexMultiMapped.PersistentE) iter.next();
273: if (!e.getValue1().equals("e")
274: && !e.getValue1().equals("f")) {
275: fail("getValue1 should have returned 'e' or 'f' (extent), it in fact returned '"
276: + e.getValue1() + "'");
277: }
278: count++;
279: }
280: if (count != COUNT * 2)
281: fail("should have found "
282: + (COUNT * 2)
283: + " ComplexMultiMapped.PersistentE's, in fact found "
284: + count);
285:
286: assertEquals("counted size", broker.getCount(q), count);
287:
288: /**
289: * check all F's NEeds to be figured out.
290: crit = new Criteria();
291: q = QueryFactory.newQuery(ComplexMultiMapped.PersistentF.class, crit);
292: iter = broker.getCollectionByQuery(q).iterator();
293: ComplexMultiMapped.PersistentF f = null;
294: count = 0;
295: while (iter.hasNext())
296: {
297: f = (ComplexMultiMapped.PersistentF) iter.next();
298: if (!f.getValue1().equals("f"))
299: {
300: fail("getValue1 should have returned 'f', it in fact returned '" + f.getValue1() + "'");
301: }
302: count++;
303: }
304: if (count != COUNT)
305: fail("should have found " + COUNT + " ComplexMultiMapped.PersistentF's, in fact found " + count);
306: */
307:
308: }
309:
310: public void testDeleteWithData() {
311: /**
312: * put some data in
313: */
314: createTestData();
315: /**
316: * then delete it.
317: */
318: int number_deleted = deleteAllData();
319: /**
320: * we should have the number of classes we put in (4) * the number we put in (COUNT of each)
321: */
322: if (number_deleted < (5 * COUNT)) {
323: fail("Should have deleted at least " + (4 * COUNT)
324: + " actually deleted " + number_deleted);
325: }
326: }
327:
328: public void testDeleteWithNoData() {
329: /**
330: * clear all data
331: */
332: deleteAllData();
333: /**
334: * call delete again: there should be nothing.
335: */
336: int number_deleted = deleteAllData();
337: if (number_deleted != 0) {
338: fail("Should have deleted 0, instead deleted "
339: + number_deleted);
340: }
341: }
342: }
|