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