001: package org.apache.ojb.odmg;
002:
003: import org.apache.ojb.broker.FarAwayClass;
004: import org.apache.ojb.broker.PBKey;
005: import org.apache.ojb.broker.PersistenceBroker;
006: import org.apache.ojb.broker.PersistenceBrokerFactory;
007: import org.apache.ojb.broker.TestHelper;
008: import org.apache.ojb.broker.metadata.MetadataManager;
009: import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
010: import org.apache.ojb.broker.metadata.ConnectionRepository;
011: import org.apache.ojb.broker.query.Criteria;
012: import org.apache.ojb.broker.query.Query;
013: import org.apache.ojb.broker.query.QueryByCriteria;
014: import org.apache.ojb.odmg.shared.Project;
015: import org.apache.ojb.odmg.shared.ODMGZoo;
016: import org.apache.ojb.junit.OJBTestCase;
017: import org.odmg.Database;
018: import org.odmg.Implementation;
019: import org.odmg.OQLQuery;
020: import org.odmg.Transaction;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: /**
028: * Do some rollback tests and check behavior within transactions.
029: * CAUTION: This tests works only against the default repository.
030: */
031: public class MultiDBUsageTest extends OJBTestCase {
032: private Implementation odmg_1;
033: private Database db_1;
034: private Implementation odmg_2;
035: private Database db_2;
036:
037: public static void main(String[] args) {
038: String[] arr = { MultiDBUsageTest.class.getName() };
039: junit.textui.TestRunner.main(arr);
040: }
041:
042: public MultiDBUsageTest(String s) {
043: super (s);
044: }
045:
046: public void setUp() throws Exception {
047: super .setUp();
048:
049: MetadataManager mm = MetadataManager.getInstance();
050: JdbcConnectionDescriptor jcd = mm.connectionRepository()
051: .getDescriptor(TestHelper.FAR_AWAY_KEY);
052: if (jcd == null) {
053: ConnectionRepository cr = mm
054: .readConnectionRepository(TestHelper.FAR_AWAY_CONNECTION_REPOSITORY);
055: mm.connectionRepository().addDescriptor(
056: cr.getDescriptor(TestHelper.FAR_AWAY_KEY));
057: }
058:
059: odmg_1 = OJB.getInstance();
060: db_1 = odmg_1.newDatabase();
061: db_1.open(TestHelper.DEF_DATABASE_NAME,
062: Database.OPEN_READ_WRITE);
063:
064: odmg_2 = OJB.getInstance();
065: db_2 = odmg_2.newDatabase();
066: db_2.open(TestHelper.FAR_AWAY_DATABASE_NAME,
067: Database.OPEN_READ_WRITE);
068: }
069:
070: protected void tearDown() throws Exception {
071: MetadataManager mm = MetadataManager.getInstance();
072: JdbcConnectionDescriptor jcd = mm.connectionRepository()
073: .getDescriptor(TestHelper.FAR_AWAY_KEY);
074: mm.connectionRepository().removeDescriptor(jcd);
075: try {
076: if (odmg_1.currentTransaction() != null) {
077: odmg_1.currentTransaction().abort();
078: }
079: db_1.close();
080: odmg_1 = null;
081: } catch (Exception e) {
082: // ignore
083: }
084:
085: try {
086: if (odmg_2.currentTransaction() != null) {
087: odmg_2.currentTransaction().abort();
088: }
089: db_2.close();
090: odmg_2 = null;
091: } catch (Exception e) {
092: // ignore
093: }
094: super .tearDown();
095: }
096:
097: /**
098: * Test store / delete objects to different db
099: */
100: public void testStore() throws Exception {
101: String name = "testStoreDelete_" + System.currentTimeMillis();
102:
103: // little hack for the test. use PB and ODMG api to verify results
104: int odmgZoosBefore = getDBObjectCountWithNewPB(
105: ((DatabaseImpl) db_1).getPBKey(), ODMGZoo.class);
106: int projectsBefore = getDBObjectCountWithNewPB(
107: ((DatabaseImpl) db_1).getPBKey(), Project.class);
108: int farAwaysBefore = getDBObjectCountWithNewPB(
109: ((DatabaseImpl) db_2).getPBKey(), FarAwayClass.class);
110:
111: Transaction tx_1 = odmg_1.newTransaction();
112: tx_1.begin();
113: //store
114: storeObjects(tx_1, getNewODMGZoos(name, 5));
115: storeObjects(tx_1, getNewProjects(name, 3));
116: //store more
117: storeObjects(tx_1, getNewODMGZoos(name, 5));
118: storeObjects(tx_1, getNewProjects(name, 2));
119: tx_1.commit();
120:
121: Transaction tx_2 = odmg_2.newTransaction();
122: tx_2.begin();
123: //store
124: storeObjects(tx_2, getNewFarAways(name, 9));
125: //store more
126: storeObjects(tx_2, getNewFarAways(name, 11));
127: tx_2.commit();
128:
129: int odmgZoosAfter = getDBObjectCountWithNewPB(
130: ((DatabaseImpl) db_1).getPBKey(), ODMGZoo.class);
131: int projectsAfter = getDBObjectCountWithNewPB(
132: ((DatabaseImpl) db_1).getPBKey(), Project.class);
133: int farAwaysAfter = getDBObjectCountWithNewPB(
134: ((DatabaseImpl) db_2).getPBKey(), FarAwayClass.class);
135: int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
136: odmg_1, ODMGZoo.class);
137: int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
138: odmg_1, Project.class);
139: int farAwaysAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
140: odmg_2, FarAwayClass.class);
141:
142: assertEquals("Wrong number of odmgZoos found",
143: (odmgZoosBefore + 10), odmgZoosAfter);
144: assertEquals("Wrong number of projects found",
145: (projectsBefore + 5), projectsAfter);
146: assertEquals("Wrong number of odmgZoos found",
147: (odmgZoosBefore + 10), odmgZoosAfterOQL);
148: assertEquals("Wrong number of projects found",
149: (projectsBefore + 5), projectsAfterOQL);
150: assertEquals("Wrong number of farAways found",
151: (farAwaysBefore + 20), farAwaysAfter);
152: assertEquals("Wrong number of farAways found",
153: (farAwaysBefore + 20), farAwaysAfterOQL);
154:
155: //************
156: // we do twice
157: //************
158: // little hack for the test
159: odmgZoosBefore = getDBObjectCountWithNewPB(
160: ((DatabaseImpl) db_1).getPBKey(), ODMGZoo.class);
161: projectsBefore = getDBObjectCountWithNewPB(
162: ((DatabaseImpl) db_1).getPBKey(), Project.class);
163: farAwaysBefore = getDBObjectCountWithNewPB(
164: ((DatabaseImpl) db_2).getPBKey(), FarAwayClass.class);
165:
166: tx_1.begin();
167: //store
168: storeObjects(tx_1, getNewODMGZoos(name, 5));
169: storeObjects(tx_1, getNewProjects(name, 3));
170: //store more
171: storeObjects(tx_1, getNewODMGZoos(name, 5));
172: storeObjects(tx_1, getNewProjects(name, 2));
173: tx_1.commit();
174:
175: tx_2.begin();
176: //store
177: storeObjects(tx_2, getNewFarAways(name, 9));
178: //store more
179: storeObjects(tx_2, getNewFarAways(name, 11));
180: tx_2.commit();
181:
182: odmgZoosAfter = getDBObjectCountWithNewPB(((DatabaseImpl) db_1)
183: .getPBKey(), ODMGZoo.class);
184: projectsAfter = getDBObjectCountWithNewPB(((DatabaseImpl) db_1)
185: .getPBKey(), Project.class);
186: farAwaysAfter = getDBObjectCountWithNewPB(((DatabaseImpl) db_2)
187: .getPBKey(), FarAwayClass.class);
188: odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
189: odmg_1, ODMGZoo.class);
190: projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
191: odmg_1, Project.class);
192: farAwaysAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(
193: odmg_2, FarAwayClass.class);
194:
195: assertEquals("Wrong number of odmgZoos found",
196: (odmgZoosBefore + 10), odmgZoosAfter);
197: assertEquals("Wrong number of projects found",
198: (projectsBefore + 5), projectsAfter);
199: assertEquals("Wrong number of odmgZoos found",
200: (odmgZoosBefore + 10), odmgZoosAfterOQL);
201: assertEquals("Wrong number of projects found",
202: (projectsBefore + 5), projectsAfterOQL);
203: assertEquals("Wrong number of farAways found",
204: (farAwaysBefore + 20), farAwaysAfter);
205: assertEquals("Wrong number of farAways found",
206: (farAwaysBefore + 20), farAwaysAfterOQL);
207:
208: List result;
209: tx_1.begin();
210: OQLQuery query = odmg_1.newOQLQuery();
211: query.create("select projects from " + Project.class.getName()
212: + " where title like $1");
213: query.bind(name);
214: result = (List) query.execute();
215: deleteObjects(db_1, result);
216: tx_1.commit();
217:
218: tx_1.begin();
219: query = odmg_1.newOQLQuery();
220: query.create("select projects from " + ODMGZoo.class.getName()
221: + " where name like $1");
222: query.bind(name);
223: result = (List) query.execute();
224: deleteObjects(db_1, result);
225: tx_1.commit();
226:
227: tx_2.begin();
228: query = odmg_2.newOQLQuery();
229: query.create("select projects from "
230: + FarAwayClass.class.getName() + " where name like $1");
231: query.bind(name);
232: result = (List) query.execute();
233: deleteObjects(db_2, result);
234: tx_2.commit();
235:
236: tx_1.begin();
237: query = odmg_1.newOQLQuery();
238: query.create("select projects from " + Project.class.getName()
239: + " where title like $1");
240: query.bind(name);
241: result = (List) query.execute();
242: tx_1.commit();
243: assertEquals(0, result.size());
244:
245: tx_1.begin();
246: query = odmg_1.newOQLQuery();
247: query.create("select projects from " + ODMGZoo.class.getName()
248: + " where name like $1");
249: query.bind(name);
250: result = (List) query.execute();
251: tx_1.commit();
252: assertEquals(0, result.size());
253:
254: tx_2.begin();
255: query = odmg_2.newOQLQuery();
256: query.create("select projects from "
257: + FarAwayClass.class.getName() + " where name like $1");
258: query.bind(name);
259: result = (List) query.execute();
260: tx_2.commit();
261: assertEquals(0, result.size());
262: }
263:
264: private void storeObjects(Transaction tx, Collection objects) {
265: for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
266: tx.lock(iterator.next(), Transaction.WRITE);
267: }
268: }
269:
270: private void deleteObjects(Database db, Collection objects) {
271: for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
272: db.deletePersistent(iterator.next());
273: }
274: }
275:
276: private static int counter;
277:
278: protected Collection getNewProjects(String name, int count) {
279: ArrayList list = new ArrayList();
280: for (int i = 0; i < count; i++) {
281: list.add(newProject(name));
282: }
283: return list;
284: }
285:
286: protected Project newProject(String name) {
287: Project p = new Project();
288: ++counter;
289: p.setDescription("Test project " + counter);
290: p.setTitle(name);
291: return p;
292: }
293:
294: protected Collection getNewODMGZoos(String name, int count) {
295: ArrayList list = new ArrayList();
296: for (int i = 0; i < count; i++) {
297: list.add(newODMGZoo(name));
298: }
299: return list;
300: }
301:
302: protected Collection getNewFarAways(String name, int count) {
303: ArrayList list = new ArrayList();
304: for (int i = 0; i < count; i++) {
305: list.add(newFarAway(name));
306: }
307: return list;
308: }
309:
310: protected ODMGZoo newODMGZoo(String name) {
311: ODMGZoo odmgZoo = new ODMGZoo();
312: ++counter;
313: odmgZoo.setName(name);
314: return odmgZoo;
315: }
316:
317: private FarAwayClass newFarAway(String name) {
318: FarAwayClass fa = new FarAwayClass();
319: counter++;
320: fa.setName(name);
321: fa.setDescription("so far away from " + counter);
322: return fa;
323: }
324:
325: protected int getDBObjectCountWithNewPB(PBKey pbKey, Class target)
326: throws Exception {
327: PersistenceBroker broker = PersistenceBrokerFactory
328: .createPersistenceBroker(pbKey);
329: Criteria c = new Criteria();
330: Query q = new QueryByCriteria(target, c);
331: int count = broker.getCount(q);
332: broker.close();
333: return count;
334: }
335:
336: protected int getDBObjectCount(PersistenceBroker broker,
337: Class target) throws Exception {
338: Criteria c = new Criteria();
339: Query q = new QueryByCriteria(target, c);
340: int count = broker.getCount(q);
341: return count;
342: }
343:
344: protected int getDBObjectCountViaOqlQueryUseNewTransaction(
345: Implementation ojb, Class target) throws Exception {
346: Transaction tx = ojb.newTransaction();
347: tx.begin();
348: OQLQuery query = ojb.newOQLQuery();
349: query.create("select allProjects from " + target.getName());
350: List list = (List) query.execute();
351: tx.commit();
352: return list.size();
353: }
354:
355: protected int getDBObjectCountViaOqlQuery(Implementation ojb,
356: Class target) throws Exception {
357: OQLQuery query = ojb.newOQLQuery();
358: query.create("select allObjects from " + target.getName());
359: List list = (List) query.execute();
360: return list.size();
361: }
362:
363: protected List getAllObjects(Implementation ojb, Class target)
364: throws Exception {
365: OQLQuery query = ojb.newOQLQuery();
366: query.create("select allObjects from " + target.getName());
367: List list = (List) query.execute();
368: return list;
369: }
370: }
|