001: /*
002: * BatchModeTest.java
003: * JUnit based test
004: *
005: * Created on February 15, 2003, 12:47 AM
006: */
007:
008: package org.apache.ojb.broker;
009:
010: import java.util.ArrayList;
011: import java.util.Collection;
012: import java.util.Iterator;
013:
014: import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
015: import org.apache.ojb.broker.query.Criteria;
016: import org.apache.ojb.broker.query.Query;
017: import org.apache.ojb.broker.query.QueryFactory;
018: import org.apache.ojb.junit.PBTestCase;
019:
020: /**
021: * @author Oleg Nitz
022: */
023: public class BatchModeTest extends PBTestCase {
024: private static long timestamp = System.currentTimeMillis();
025:
026: public BatchModeTest(String testName) {
027: super (testName);
028: }
029:
030: private long getNewId() {
031: return timestamp++;
032: }
033:
034: public static void main(String[] args) {
035: String[] arr = { BatchModeTest.class.getName() };
036: junit.textui.TestRunner.main(arr);
037: }
038:
039: public void setUp() throws Exception {
040: super .setUp();
041: broker.serviceConnectionManager().setBatchMode(true);
042: // lookup connection to enable batch mode
043: broker.serviceConnectionManager().getConnection();
044: }
045:
046: boolean batchModeDisabled() {
047: if (broker.serviceConnectionManager().isBatchMode()) {
048: return false;
049: } else {
050: System.out
051: .println("["
052: + BatchModeTest.class.getName()
053: + "] Skip test, batch mode was not enabled or supported");
054: return true;
055: }
056: }
057:
058: /**
059: * A common (no specific batch) test
060: */
061: public void testDelete() {
062: if (batchModeDisabled())
063: return;
064:
065: long pk = getNewId();
066: String nameMain = "testDelete_Main_" + pk;
067: String nameSub = "testDelete_Sub_" + pk;
068:
069: MainObject main1 = new MainObject(pk, nameMain);
070: SubObject sub = new SubObject(nameSub);
071: main1.add(sub);
072: main1.add(new SubObject(nameSub));
073:
074: broker.beginTransaction();
075: broker.store(main1);
076: Identity oid_main = broker.serviceIdentity().buildIdentity(
077: main1);
078: Identity oid_sub = broker.serviceIdentity().buildIdentity(sub);
079: broker.delete(main1);
080: broker.commitTransaction();
081:
082: MainObject newMain = (MainObject) broker
083: .getObjectByIdentity(oid_main);
084: assertNull(newMain);
085: SubObject newSub = (SubObject) broker
086: .getObjectByIdentity(oid_sub);
087: assertNull(newSub);
088:
089: Criteria crit = new Criteria();
090: crit.addLike("name", nameSub);
091: Query q = QueryFactory.newQuery(SubObject.class, crit);
092: Collection result = broker.getCollectionByQuery(q);
093: assertEquals(0, result.size());
094: }
095:
096: /**
097: * A common (no specific batch) test
098: */
099: public void testEquals() throws Exception {
100: if (batchModeDisabled())
101: return;
102:
103: long pk = getNewId();
104: String nameMain = "testEquals_Main_" + pk;
105: String nameSub = "testEquals_Sub_" + pk;
106:
107: MainObject main1 = new MainObject(pk, nameMain);
108: main1.add(new SubObject(nameSub));
109: MainObject main2 = new MainObject(pk, nameMain);
110: main2.add(new SubObject(nameSub));
111:
112: broker.beginTransaction();
113: broker.store(main1);
114: // delete object before add new instance with same PK
115: broker.delete(main1);
116: broker.store(main2);
117: broker.commitTransaction();
118:
119: // new PB instance
120: super .tearDown();
121: super .setUp();
122:
123: MainObject main3 = new MainObject(pk, nameMain);
124: main3.add(new SubObject(nameSub));
125:
126: Identity oid = broker.serviceIdentity().buildIdentity(main1);
127: broker.clearCache();
128: MainObject main4 = (MainObject) broker.getObjectByIdentity(oid);
129:
130: assertEquals(main3, main4);
131: }
132:
133: public void testDeleteInsert() {
134: if (batchModeDisabled())
135: return;
136:
137: long pk = getNewId();
138: String nameMain = "testDeleteInsert_Main_" + pk;
139: String nameSub = "testDeleteInsert_Sub_" + pk;
140:
141: MainObject main1 = new MainObject(pk, nameMain);
142: main1.add(new SubObject("normal_" + nameSub));
143: broker.beginTransaction();
144: broker.store(main1);
145: broker.commitTransaction();
146:
147: // enable batch mode before start tx
148: broker.serviceConnectionManager().setBatchMode(true);
149: Identity oid = broker.serviceIdentity().buildIdentity(main1);
150: broker.beginTransaction();
151: broker.delete(main1);
152: MainObject main2 = new MainObject(pk, nameMain);
153: main2.add(new SubObject("updated_" + nameSub));
154: broker.store(main2);
155:
156: broker.commitTransaction();
157:
158: broker.clearCache();
159: MainObject newMain = (MainObject) broker
160: .getObjectByIdentity(oid);
161: assertNotNull(newMain);
162: assertNotNull(newMain.getSubObjects());
163: assertEquals(1, newMain.getSubObjects().size());
164: }
165:
166: public void testBatchStatementsOrder() {
167: if (batchModeDisabled())
168: return;
169:
170: String name = "testBatchStatementsOrder_"
171: + System.currentTimeMillis();
172: ConnectionManagerIF conMan = broker.serviceConnectionManager();
173: // try to enable batch mode
174: conMan.setBatchMode(true);
175: broker.beginTransaction();
176:
177: ProductGroup pg1 = new ProductGroup();
178: pg1.setName("ProductGroup#1_" + name);
179: broker.store(pg1);
180:
181: conMan.executeBatch();
182:
183: Article a1 = new Article();
184: a1.setArticleName(name);
185: a1.setProductGroup(pg1);
186: pg1.add(a1);
187: broker.store(pg1);
188: broker.store(a1);
189:
190: ProductGroup pg2 = new ProductGroup();
191: pg2.setName("ProductGroup #2_" + name);
192: broker.store(pg2);
193:
194: Article a2 = new Article();
195: a2.setArticleName(name);
196: a2.setProductGroup(pg2);
197: pg2.add(a2);
198: broker.store(a2);
199:
200: ProductGroup pg3 = new ProductGroup();
201: pg3.setName("ProductGroup #3_" + name);
202: broker.store(pg3);
203:
204: Article a3 = new Article();
205: a3.setArticleName(name);
206: a3.setProductGroup(pg3);
207: pg3.add(a3);
208: broker.store(a3);
209:
210: conMan.executeBatch();
211:
212: broker.delete(a1);
213:
214: conMan.executeBatch();
215:
216: broker.delete(pg1);
217: broker.delete(a2);
218: broker.delete(pg2);
219: broker.delete(a3);
220: broker.delete(pg3);
221: broker.commitTransaction();
222:
223: broker.beginTransaction();
224: pg3.getAllArticles().clear();
225: broker.store(pg3);
226: broker.delete(pg3);
227: broker.store(pg3);
228: broker.delete(pg3);
229: conMan.executeBatch();
230: broker.commitTransaction();
231: }
232:
233: /**
234: * collection-descriptor without inverse reference-descriptor
235: */
236: public void testBatchStatementsOrder2() {
237: if (batchModeDisabled())
238: return;
239:
240: ConnectionManagerIF conMan = broker.serviceConnectionManager();
241: broker.beginTransaction();
242:
243: Zoo zoo1 = new Zoo();
244: zoo1.setName("BatchModeTest Zoo #1");
245: broker.store(zoo1);
246:
247: conMan.executeBatch();
248:
249: Mammal m1 = new Mammal();
250: m1.setName("BatchModeTest Mammal #1");
251: m1.setAge(5);
252: m1.setNumLegs(4);
253: m1.setZooId(zoo1.getZooId());
254: zoo1.getAnimals().add(m1);
255: broker.store(m1);
256:
257: Zoo zoo2 = new Zoo();
258: zoo2.setName("BatchModeTest Zoo #2");
259: broker.store(zoo2);
260:
261: Mammal m2 = new Mammal();
262: m2.setName("BatchModeTest Mammal #2");
263: m2.setAge(5);
264: m2.setNumLegs(4);
265: m2.setZooId(zoo2.getZooId());
266: zoo2.getAnimals().add(m2);
267: broker.store(m2);
268:
269: Zoo zoo3 = new Zoo();
270: zoo3.setName("BatchModeTest Zoo #3");
271: broker.store(zoo3);
272:
273: Mammal m3 = new Mammal();
274: m3.setName("BatchModeTest Mammal #3");
275: m3.setAge(5);
276: m3.setNumLegs(4);
277: m3.setZooId(zoo3.getZooId());
278: zoo3.getAnimals().add(m3);
279: broker.store(m3);
280:
281: conMan.executeBatch();
282:
283: broker.delete(m1);
284:
285: conMan.executeBatch();
286:
287: broker.delete(zoo1);
288: broker.delete(m2);
289: broker.delete(zoo2);
290: broker.delete(m3);
291: broker.delete(zoo3);
292:
293: conMan.executeBatch();
294: broker.commitTransaction();
295: }
296:
297: public void testMassInsertDelete() {
298: if (batchModeDisabled())
299: return;
300:
301: String name = "testMassInsert_" + System.currentTimeMillis();
302:
303: broker.serviceConnectionManager().setBatchMode(true);
304: broker.beginTransaction();
305: for (int i = 200 - 1; i >= 0; i--) {
306: Person p = new Person();
307: p.setFirstname("a mass test_" + i);
308: p.setLastname(name);
309: broker.store(p);
310: }
311: broker.commitTransaction();
312:
313: Criteria crit = new Criteria();
314: crit.addLike("lastname", name);
315: Query q = QueryFactory.newQuery(Person.class, crit);
316: Collection result = broker.getCollectionByQuery(q);
317: assertEquals(200, result.size());
318:
319: broker.beginTransaction();
320: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
321: broker.delete(iterator.next());
322: }
323: broker.commitTransaction();
324:
325: crit = new Criteria();
326: crit.addLike("lastname", name);
327: q = QueryFactory.newQuery(Person.class, crit);
328: result = broker.getCollectionByQuery(q);
329: assertEquals(0, result.size());
330: }
331:
332: public void testBatchModeDeclaration() throws Exception {
333: if (batchModeDisabled())
334: return;
335:
336: String name = "testBatchModeDeclaration_"
337: + System.currentTimeMillis();
338:
339: broker.serviceConnectionManager().setBatchMode(true);
340: broker.beginTransaction();
341: Person p = new Person();
342: p.setFirstname("a mass test");
343: p.setLastname(name);
344: broker.store(p);
345: broker.commitTransaction();
346:
347: // new PB instance
348: tearDown();
349: setUp();
350:
351: broker.beginTransaction();
352: broker.serviceConnectionManager().setBatchMode(true);
353: p = new Person();
354: p.setFirstname("a mass test");
355: p.setLastname(name);
356: broker.store(p);
357: broker.commitTransaction();
358:
359: // new PB instance
360: tearDown();
361: setUp();
362: broker.serviceConnectionManager().setBatchMode(true);
363: broker.serviceConnectionManager().getConnection();
364: broker.beginTransaction();
365: broker.commitTransaction();
366:
367: // new PB instance
368: tearDown();
369: setUp();
370: broker.serviceConnectionManager().setBatchMode(true);
371: broker.serviceConnectionManager().getConnection();
372: broker.beginTransaction();
373: broker.abortTransaction();
374:
375: // new PB instance
376: tearDown();
377: setUp();
378: broker.beginTransaction();
379: broker.serviceConnectionManager().setBatchMode(true);
380: broker.serviceConnectionManager().getConnection();
381: broker.commitTransaction();
382:
383: // new PB instance
384: tearDown();
385: setUp();
386: broker.beginTransaction();
387: broker.serviceConnectionManager().setBatchMode(true);
388: broker.serviceConnectionManager().getConnection();
389: broker.abortTransaction();
390: }
391:
392: /**
393: * A common (no specific batch) test
394: */
395: public void testStoreDeleteStore() {
396: if (batchModeDisabled())
397: return;
398:
399: long pk = getNewId();
400: String nameMain = "testDelete_Main_" + pk;
401: String nameSub = "testDelete_Sub_" + pk;
402:
403: MainObject main1 = new MainObject(pk, nameMain);
404: main1.add(new SubObject(nameSub));
405: main1.add(new SubObject(nameSub));
406:
407: broker.beginTransaction();
408: broker.store(main1);
409: broker.delete(main1);
410: broker.store(main1);
411: broker.delete(main1);
412: broker.store(main1);
413: broker.commitTransaction();
414:
415: Identity oid = broker.serviceIdentity().buildIdentity(main1);
416: broker.clearCache();
417: MainObject newMain = (MainObject) broker
418: .getObjectByIdentity(oid);
419: assertNotNull(newMain);
420: assertEquals(2, newMain.getSubObjects().size());
421: broker.clearCache();
422: Criteria crit = new Criteria();
423: crit.addLike("name", nameSub);
424: Query q = QueryFactory.newQuery(SubObject.class, crit);
425: Collection result = broker.getCollectionByQuery(q);
426: assertEquals(2, result.size());
427: }
428:
429: //==========================================================
430: // inner classes used for testing
431: //==========================================================
432: public static class MainObject {
433: private Long id;
434: private String name;
435: private Collection subObjects;
436:
437: public MainObject() {
438: }
439:
440: public MainObject(long id, String name) {
441: setId(new Long(id));
442: setName(name);
443: }
444:
445: public Long getId() {
446: return id;
447: }
448:
449: public void setId(Long id) {
450: this .id = id;
451: }
452:
453: public String getName() {
454: return name;
455: }
456:
457: public void setName(String name) {
458: this .name = name;
459: }
460:
461: public Collection getSubObjects() {
462: return subObjects;
463: }
464:
465: public void setSubObjects(Collection subObjects) {
466: this .subObjects = subObjects;
467: }
468:
469: public void add(SubObject obj) {
470: if (subObjects == null) {
471: subObjects = new ArrayList();
472: }
473: subObjects.add(obj);
474: }
475:
476: public boolean equals(Object other) {
477: if (other instanceof MainObject) {
478: MainObject main = (MainObject) other;
479: return ((name == null) ? main.name == null : name
480: .equals(main.name))
481: && ((subObjects == null || subObjects.isEmpty()) ? (main.subObjects == null || main.subObjects
482: .isEmpty())
483: : subObjects.equals(main.subObjects));
484: } else {
485: return false;
486: }
487: }
488: }
489:
490: public static class SubObject {
491: private Long id;
492: private String name;
493: private Long mainId;
494:
495: public SubObject() {
496: }
497:
498: public SubObject(String name) {
499: setName(name);
500: }
501:
502: public Long getId() {
503: return id;
504: }
505:
506: public void setId(Long id) {
507: this .id = id;
508: }
509:
510: public Long getMainId() {
511: return mainId;
512: }
513:
514: public void setMainId(Long mainId) {
515: this .mainId = mainId;
516: }
517:
518: public String getName() {
519: return name;
520: }
521:
522: public void setName(String name) {
523: this .name = name;
524: }
525:
526: public boolean equals(Object other) {
527: if (other instanceof SubObject) {
528: SubObject sub = (SubObject) other;
529: return (name == null) ? sub.name == null : name
530: .equals(sub.name);
531: } else {
532: return false;
533: }
534: }
535:
536: }
537: }
|