001: package org.apache.ojb.broker;
002:
003: import junit.framework.Assert;
004: import junit.framework.TestCase;
005: import org.apache.ojb.broker.query.Criteria;
006: import org.apache.ojb.broker.query.QueryByCriteria;
007: import org.apache.ojb.broker.metadata.MetadataManager;
008: import org.apache.ojb.broker.metadata.ConnectionRepository;
009: import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
010: import org.apache.commons.lang.builder.ToStringBuilder;
011: import org.apache.commons.lang.builder.ToStringStyle;
012:
013: import java.sql.Connection;
014: import java.util.Collection;
015:
016: /**
017: * Test to check support for multiple DB.
018: *
019: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
020: */
021: public class MultipleDBTest extends TestCase {
022: private static final String REFERENCE_NAME = "Test reference";
023:
024: public MultipleDBTest(String s) {
025: super (s);
026: }
027:
028: protected void setUp() throws Exception {
029: super .setUp();
030: MetadataManager mm = MetadataManager.getInstance();
031: JdbcConnectionDescriptor jcd = mm.connectionRepository()
032: .getDescriptor(TestHelper.FAR_AWAY_KEY);
033: if (jcd == null) {
034: ConnectionRepository cr = mm
035: .readConnectionRepository(TestHelper.FAR_AWAY_CONNECTION_REPOSITORY);
036: mm.connectionRepository().addDescriptor(
037: cr.getDescriptor(TestHelper.FAR_AWAY_KEY));
038: }
039: }
040:
041: protected void tearDown() throws Exception {
042: MetadataManager mm = MetadataManager.getInstance();
043: JdbcConnectionDescriptor jcd = mm.connectionRepository()
044: .getDescriptor(TestHelper.FAR_AWAY_KEY);
045: mm.connectionRepository().removeDescriptor(jcd);
046: super .tearDown();
047: }
048:
049: public static void main(String[] args) {
050: String[] arr = { MultipleDBTest.class.getName() };
051: junit.textui.TestRunner.main(arr);
052: }
053:
054: /**
055: * This test show how it is possible to materialize an object
056: * with a collection of objects retrieved from a different DB
057: * NOTE: This is not a recommended design, but it works.
058: */
059: public void testMaterializeFromDifferentDB() {
060: String name = "testMaterializeFromDifferentDB"
061: + System.currentTimeMillis();
062: PersistenceBroker broker = PersistenceBrokerFactory
063: .defaultPersistenceBroker();
064: PersistenceBroker brokerFarAway = PersistenceBrokerFactory
065: .createPersistenceBroker(TestHelper.FAR_AWAY_KEY);
066:
067: MultipleObject obj = new MultipleObject();
068: obj.setName(name);
069: broker.beginTransaction();
070: broker.store(obj);
071: broker.commitTransaction();
072:
073: Identity oid = new Identity(obj, broker);
074:
075: MultipleObjectRef ref_1 = new MultipleObjectRef();
076: MultipleObjectRef ref_2 = new MultipleObjectRef();
077: ref_1.setName(name);
078: ref_1.setRefId(obj.getId());
079: ref_2.setName(name);
080: ref_2.setRefId(obj.getId());
081:
082: brokerFarAway.beginTransaction();
083: brokerFarAway.store(ref_1);
084: brokerFarAway.store(ref_2);
085: brokerFarAway.commitTransaction();
086:
087: broker.clearCache();
088: brokerFarAway.clearCache();
089:
090: MultipleObject newObj = (MultipleObject) broker
091: .getObjectByIdentity(oid);
092: brokerFarAway.retrieveAllReferences(newObj);
093:
094: assertNotNull(newObj.getReferences());
095: assertEquals(2, newObj.getReferences().size());
096: // System.out.println("## " + newObj);
097: }
098:
099: /**
100: * test PB instance lookup using different
101: * PBKey constructors + databases
102: */
103: public void testLookupByPBKey() {
104: PBKey pb_1a = new PBKey(TestHelper.DEF_JCD_ALIAS);
105: PBKey pb_1b = new PBKey(TestHelper.DEF_JCD_ALIAS, null, null);
106: PBKey pb_1c = new PBKey(TestHelper.DEF_JCD_ALIAS,
107: TestHelper.DEF_USER, TestHelper.DEF_PASSWORD);
108:
109: PBKey pb_2a = new PBKey(TestHelper.FAR_AWAY_JCD_ALIAS);
110: PBKey pb_2b = new PBKey(TestHelper.FAR_AWAY_JCD_ALIAS, null,
111: null);
112:
113: PersistenceBroker b1a = PersistenceBrokerFactory
114: .createPersistenceBroker(pb_1a);
115: PersistenceBroker b1b = PersistenceBrokerFactory
116: .createPersistenceBroker(pb_1b);
117: PersistenceBroker b1c = PersistenceBrokerFactory
118: .createPersistenceBroker(pb_1c);
119: PersistenceBroker b2a = PersistenceBrokerFactory
120: .createPersistenceBroker(pb_2a);
121: PersistenceBroker b2b = PersistenceBrokerFactory
122: .createPersistenceBroker(pb_2b);
123:
124: assertNotNull(b1a);
125: assertNotNull(b1b);
126: assertNotNull(b1c);
127: assertNotNull(b2a);
128: assertNotNull(b2b);
129:
130: if (b1a != null)
131: b1a.close();
132: if (b1b != null)
133: b1b.close();
134: if (b1c != null)
135: b1c.close();
136: if (b2a != null)
137: b2a.close();
138: if (b2b != null)
139: b2b.close();
140: }
141:
142: public void testPBLookupConnection() throws Exception {
143: PBKey key = new PBKey(TestHelper.FAR_AWAY_JCD_ALIAS);
144: PersistenceBroker broker = PersistenceBrokerFactory
145: .createPersistenceBroker(key);
146: // get connection to check lookup
147: Connection con = broker.serviceConnectionManager()
148: .getConnection();
149: con.isClosed();
150: assertNotNull(broker);
151: assertEquals(key.getAlias(), broker.getPBKey().getAlias());
152: broker.close();
153:
154: key = new PBKey(TestHelper.DEF_JCD_ALIAS);
155: broker = PersistenceBrokerFactory.createPersistenceBroker(key);
156: // get connection to check lookup
157: con = broker.serviceConnectionManager().getConnection();
158: con.isClosed();
159: assertNotNull(broker);
160: assertEquals(key.getAlias(), broker.getPBKey().getAlias());
161: broker.close();
162: }
163:
164: public void testPBCreation() throws Exception {
165: PersistenceBroker defPB = null;
166: PersistenceBroker secPB = null;
167:
168: try {
169: defPB = PersistenceBrokerFactory.defaultPersistenceBroker();
170: PBKey secKey = TestHelper.FAR_AWAY_KEY;
171: secPB = PersistenceBrokerFactory
172: .createPersistenceBroker(secKey);
173:
174: Assert.assertNotNull("Cannot lookup default PB", defPB);
175: Assert.assertNotNull("Cannot lookup PB for PBKey: "
176: + secKey, secPB);
177: Assert.assertEquals(
178: "Different repository files for second db", secPB
179: .getPBKey().getAlias(), secKey.getAlias());
180: } finally {
181: if (defPB != null)
182: defPB.close();
183: if (secPB != null)
184: secPB.close();
185: }
186:
187: }
188:
189: /**
190: * Insert/delete the same object with given id in two different DB
191: */
192: public void testInsertDeleteNoAutoSequence() throws Exception {
193: Article article = createArticleWithId(Integer.MAX_VALUE - 1001);
194: PBKey secKey = TestHelper.FAR_AWAY_KEY;
195:
196: FarAwayClass fa = createFarAwayObjectWithId(Integer.MAX_VALUE - 1002);
197: PersistenceBroker secPB = PersistenceBrokerFactory
198: .createPersistenceBroker(secKey);
199: secPB.beginTransaction();
200: secPB.store(fa);
201: secPB.commitTransaction();
202: secPB.close();
203:
204: PersistenceBroker defPB = PersistenceBrokerFactory
205: .defaultPersistenceBroker();
206: defPB.beginTransaction();
207: defPB.store(article);
208: defPB.commitTransaction();
209: defPB.close();
210:
211: secPB = PersistenceBrokerFactory
212: .createPersistenceBroker(secKey);
213: secPB.clearCache();
214: Object[] pks = { new Integer(fa.getId()) };
215: Identity oid = new Identity(FarAwayClass.class,
216: FarAwayClass.class, pks);
217: FarAwayClass fa2 = (FarAwayClass) secPB
218: .getObjectByIdentity(oid);
219: Assert.assertNotNull("Lookup for article in second DB failed",
220: fa2);
221: Assert.assertEquals(fa.toString(), fa2.toString());
222: secPB.close();
223:
224: defPB = PersistenceBrokerFactory.defaultPersistenceBroker();
225: defPB.clearCache();
226: Identity oid2 = defPB.serviceIdentity().buildIdentity(
227: Article.class, article.getArticleId());
228: Article article2 = (Article) defPB.getObjectByIdentity(oid2);
229: Assert.assertNotNull("Lookup for article in default DB failed",
230: article2);
231: defPB.close();
232:
233: secPB = PersistenceBrokerFactory
234: .createPersistenceBroker(secKey);
235: secPB.beginTransaction();
236: secPB.delete(fa);
237: secPB.commitTransaction();
238: secPB.close();
239:
240: defPB = PersistenceBrokerFactory.defaultPersistenceBroker();
241: defPB.beginTransaction();
242: defPB.delete(article);
243: defPB.commitTransaction();
244: defPB.close();
245: }
246:
247: /**
248: * Insert/delete objects in two different DB, use auto-generated keys
249: */
250: public void testInsertDeleteAutoSequenceClearCache()
251: throws Exception {
252: Article article = createArticle();
253: PBKey secKey = TestHelper.FAR_AWAY_KEY;
254:
255: FarAwayClass fa = createFarAwayObject();
256: PersistenceBroker farAwayPB = PersistenceBrokerFactory
257: .createPersistenceBroker(secKey);
258: farAwayPB.clearCache();
259: farAwayPB.beginTransaction();
260: farAwayPB.store(fa);
261: farAwayPB.commitTransaction();
262: farAwayPB.close();
263:
264: PersistenceBroker defaultPB = PersistenceBrokerFactory
265: .defaultPersistenceBroker();
266: defaultPB.clearCache();
267: defaultPB.beginTransaction();
268: defaultPB.store(article);
269: defaultPB.commitTransaction();
270: defaultPB.close();
271:
272: farAwayPB = PersistenceBrokerFactory
273: .createPersistenceBroker(secKey);
274: farAwayPB.clearCache();
275: Object[] pks = { new Integer(fa.getId()) };
276: Identity oid = new Identity(FarAwayClass.class,
277: FarAwayClass.class, pks);
278: FarAwayClass fa2 = (FarAwayClass) farAwayPB
279: .getObjectByIdentity(oid);
280: Assert.assertNotNull("Lookup for article in second DB failed",
281: fa2);
282: Assert.assertEquals(fa.toString(), fa2.toString());
283: farAwayPB.close();
284:
285: defaultPB = PersistenceBrokerFactory.defaultPersistenceBroker();
286: defaultPB.clearCache();
287: Identity oid2 = defaultPB.serviceIdentity().buildIdentity(
288: Article.class, article.getArticleId());
289: Article article2 = (Article) defaultPB
290: .getObjectByIdentity(oid2);
291: Assert.assertNotNull("Lookup for article in default DB failed",
292: article2);
293: defaultPB.close();
294:
295: farAwayPB = PersistenceBrokerFactory
296: .createPersistenceBroker(secKey);
297: farAwayPB.clearCache();
298: farAwayPB.beginTransaction();
299: farAwayPB.delete(fa);
300: farAwayPB.commitTransaction();
301: farAwayPB.close();
302:
303: defaultPB = PersistenceBrokerFactory.defaultPersistenceBroker();
304: defaultPB.clearCache();
305: defaultPB.beginTransaction();
306: defaultPB.delete(article);
307: defaultPB.commitTransaction();
308: defaultPB.close();
309: }
310:
311: /**
312: * Insert/delete objects in two different DB, use auto-generated keys
313: */
314: public void testInsertDeleteAutoSequence() throws Exception {
315: Article article = createArticle();
316: PBKey secKey = TestHelper.FAR_AWAY_KEY;
317:
318: PersistenceBroker defaultPB = PersistenceBrokerFactory
319: .defaultPersistenceBroker();
320: defaultPB.beginTransaction();
321: defaultPB.store(article);
322: defaultPB.commitTransaction();
323: defaultPB.close();
324:
325: FarAwayClass fa = createFarAwayObject();
326: PersistenceBroker farAwayPB = PersistenceBrokerFactory
327: .createPersistenceBroker(secKey);
328: farAwayPB.beginTransaction();
329: farAwayPB.store(fa);
330: farAwayPB.commitTransaction();
331: farAwayPB.close();
332:
333: farAwayPB = PersistenceBrokerFactory
334: .createPersistenceBroker(secKey);
335: Object[] pks = { new Integer(fa.getId()) };
336: Identity oid = new Identity(FarAwayClass.class,
337: FarAwayClass.class, pks);
338: FarAwayClass fa2 = (FarAwayClass) farAwayPB
339: .getObjectByIdentity(oid);
340: Assert.assertNotNull("Lookup for article in second DB failed",
341: fa2);
342: Assert.assertEquals(fa.toString(), fa2.toString());
343: farAwayPB.close();
344:
345: defaultPB = PersistenceBrokerFactory.defaultPersistenceBroker();
346: Identity oid2 = defaultPB.serviceIdentity().buildIdentity(
347: Article.class, article.getArticleId());
348: Article article2 = (Article) defaultPB
349: .getObjectByIdentity(oid2);
350: Assert.assertNotNull("Lookup for article in default DB failed",
351: article2);
352: defaultPB.close();
353:
354: farAwayPB = PersistenceBrokerFactory
355: .createPersistenceBroker(secKey);
356: farAwayPB.beginTransaction();
357: farAwayPB.delete(fa);
358: farAwayPB.commitTransaction();
359: farAwayPB.close();
360:
361: defaultPB = PersistenceBrokerFactory.defaultPersistenceBroker();
362: defaultPB.beginTransaction();
363: defaultPB.delete(article);
364: defaultPB.commitTransaction();
365: defaultPB.close();
366: }
367:
368: /**
369: * tests if references work on second database
370: */
371: public void testWithReference() throws Exception {
372: PBKey secKey = TestHelper.FAR_AWAY_KEY;
373: FarAwayClass f1 = doReferenceMatchingStore(secKey);
374: FarAwayClass f2 = doReferenceMatchingStore(secKey);
375:
376: PersistenceBroker broker = null;
377: try {
378: broker = PersistenceBrokerFactory
379: .createPersistenceBroker(secKey);
380: broker.clearCache();
381: Identity oid = new Identity(f1, broker);
382: FarAwayClass fac = (FarAwayClass) broker
383: .getObjectByIdentity(oid);
384: FarAwayReferenceIF ref = fac.getReference();
385: assertNotNull(ref);
386: assertEquals(REFERENCE_NAME, ref.getName());
387: } finally {
388: if (broker != null)
389: broker.close();
390: }
391:
392: doReferenceMatchingDelete(secKey, f1);
393: doReferenceMatchingDelete(secKey, f2);
394: }
395:
396: private FarAwayClass doReferenceMatchingStore(PBKey key)
397: throws Exception {
398: FarAwayClass fa = createFarAwayObject();
399: FarAwayReferenceIF ref = new FarAwayReference();
400: ref.setName(REFERENCE_NAME);
401:
402: PersistenceBroker farAwayPB = PersistenceBrokerFactory
403: .createPersistenceBroker(key);
404: farAwayPB.beginTransaction();
405: farAwayPB.store(ref);
406: fa.setReference(ref);
407: farAwayPB.store(fa);
408: farAwayPB.commitTransaction();
409: farAwayPB.close();
410:
411: farAwayPB = PersistenceBrokerFactory
412: .createPersistenceBroker(key);
413: Criteria criteria = new Criteria();
414: criteria.addEqualTo("id", new Integer(fa.getId()));
415: FarAwayClass result = (FarAwayClass) farAwayPB
416: .getObjectByQuery(new QueryByCriteria(
417: FarAwayClass.class, criteria));
418: farAwayPB.close();
419:
420: int refId = result.getReference().getId();
421: assertEquals(ref.getId(), refId);
422: return result;
423: }
424:
425: private void doReferenceMatchingDelete(PBKey key,
426: FarAwayClass farAwayClass) throws Exception {
427: Integer refId = farAwayClass.getReferenceId();
428: Integer mainId = new Integer(farAwayClass.getId());
429: PersistenceBroker broker = PersistenceBrokerFactory
430: .createPersistenceBroker(key);
431: try {
432: Criteria criteria = new Criteria();
433: criteria.addEqualTo("id", mainId);
434: FarAwayClass result = (FarAwayClass) broker
435: .getObjectByQuery(new QueryByCriteria(
436: FarAwayClass.class, criteria));
437: assertNotNull("Object not found", result);
438:
439: Criteria criteriaRef = new Criteria();
440: criteriaRef.addEqualTo("id", refId);
441: FarAwayReferenceIF resultRef = (FarAwayReferenceIF) broker
442: .getObjectByQuery(new QueryByCriteria(
443: FarAwayReference.class, criteriaRef));
444: assertNotNull("Object not found", result);
445:
446: broker.beginTransaction();
447: broker.delete(farAwayClass);
448: broker.commitTransaction();
449:
450: criteria = new Criteria();
451: criteria.addEqualTo("id", mainId);
452: result = (FarAwayClass) broker
453: .getObjectByQuery(new QueryByCriteria(
454: FarAwayClass.class, criteria));
455: assertNull("Object was not deleted", result);
456:
457: criteriaRef = new Criteria();
458: criteriaRef.addEqualTo("id", refId);
459: resultRef = (FarAwayReferenceIF) broker
460: .getObjectByQuery(new QueryByCriteria(
461: FarAwayReference.class, criteriaRef));
462: assertNull("Reference object was not deleted", resultRef);
463: } finally {
464: if (broker != null)
465: broker.close();
466: }
467: }
468:
469: /**
470: * factory method that createa an PerformanceArticle
471: * @return the created PerformanceArticle object
472: * @param id the primary key value for the new object
473: */
474: private Article createArticleWithId(int id) {
475: Article ret = createArticle();
476: ret.setArticleId(new Integer(id));
477: return ret;
478: }
479:
480: private FarAwayClass createFarAwayObjectWithId(int id) {
481: FarAwayClass fa = createFarAwayObject();
482: fa.setId(id);
483: return fa;
484: }
485:
486: private FarAwayClass createFarAwayObject() {
487: FarAwayClass fa = new FarAwayClass();
488: fa.setName("away from " + counter);
489: fa.setDescription("so far away from " + counter);
490: return fa;
491: }
492:
493: private static int counter;
494:
495: /**
496: * factory method that createa an PerformanceArticle
497: * @return the created PerformanceArticle object
498: */
499: private Article createArticle() {
500: Article a = new Article();
501: a.setArticleName("New Performance Article " + (++counter));
502: a.setMinimumStock(100);
503: a.setOrderedUnits(17);
504: a.setPrice(0.45);
505: a.setStock(234);
506: a.setSupplierId(4);
507: a.setUnit("bottle");
508: return a;
509: }
510:
511: //***********************************************************************
512: // inner classes used by test case
513: //***********************************************************************
514: public static class MultipleObject {
515: Integer id;
516: String name;
517: Collection references;
518:
519: public MultipleObject() {
520: }
521:
522: public Integer getId() {
523: return id;
524: }
525:
526: public void setId(Integer id) {
527: this .id = id;
528: }
529:
530: public String getName() {
531: return name;
532: }
533:
534: public void setName(String name) {
535: this .name = name;
536: }
537:
538: public Collection getReferences() {
539: return references;
540: }
541:
542: public void setReferences(Collection references) {
543: this .references = references;
544: }
545:
546: public String toString() {
547: return ToStringBuilder.reflectionToString(this ,
548: ToStringStyle.MULTI_LINE_STYLE);
549: }
550: }
551:
552: public static class MultipleObjectRef {
553: Integer id;
554: Integer refId;
555: String name;
556:
557: public MultipleObjectRef() {
558: }
559:
560: public Integer getId() {
561: return id;
562: }
563:
564: public void setId(Integer id) {
565: this .id = id;
566: }
567:
568: public Integer getRefId() {
569: return refId;
570: }
571:
572: public void setRefId(Integer refId) {
573: this .refId = refId;
574: }
575:
576: public String getName() {
577: return name;
578: }
579:
580: public void setName(String name) {
581: this .name = name;
582: }
583:
584: public String toString() {
585: return ToStringBuilder.reflectionToString(this,
586: ToStringStyle.DEFAULT_STYLE);
587: }
588: }
589: }
|