001: package org.apache.ojb.odmg;
002:
003: import org.apache.ojb.broker.Contract;
004: import org.apache.ojb.broker.Identity;
005: import org.apache.ojb.junit.ODMGTestCase;
006: import org.apache.ojb.odmg.shared.DetailFKinPK;
007: import org.apache.ojb.odmg.shared.DetailFKnoPK;
008: import org.apache.ojb.odmg.shared.Master;
009: import org.odmg.Implementation;
010: import org.odmg.OQLQuery;
011: import org.odmg.Transaction;
012:
013: import java.util.Collection;
014: import java.util.Iterator;
015: import java.util.Vector;
016: import java.util.List;
017:
018: /** Demo Application that shows basic concepts for Applications using the OJB ODMG
019: * implementation as an transactional object server.
020: */
021: public class RITest extends ODMGTestCase {
022: public static void main(String[] args) {
023: String[] arr = { RITest.class.getName() };
024: junit.textui.TestRunner.main(arr);
025: }
026:
027: public RITest(String name) {
028: super (name);
029: }
030:
031: /**
032: * Test referential integrity with the ODMG-Layer, the foreign key
033: * is not a part of the primary key of the detail.
034: */
035: public void testStoreFKnoPK() throws Exception {
036: long timestamp = System.currentTimeMillis();
037: Transaction tx = odmg.newTransaction();
038: tx.begin();
039: Master master_1 = populatedMasterFKnoPK(tx, 5, timestamp);
040: Master master_2 = populatedMasterFKnoPK(tx, 5, timestamp);
041:
042: database.makePersistent(master_1);
043: database.makePersistent(master_2);
044: tx.commit();
045:
046: // Check stored objects
047: OQLQuery query = odmg.newOQLQuery();
048: query.create("select masters from " + Master.class.getName()
049: + " where masterText like $1");
050: query.bind("%" + timestamp);
051: List allMasters = (List) query.execute();
052: assertEquals("We should found master objects", 2, allMasters
053: .size());
054: Master lookup_1 = (Master) allMasters.get(0);
055:
056: Collection col_in = lookup_1.getCollDetailFKinPK();
057: Collection col_no = lookup_1.getCollDetailFKnoPK();
058: assertEquals("Should found none "
059: + DetailFKinPK.class.getName() + " objects", 0, col_in
060: .size());
061: assertEquals("Should found " + DetailFKnoPK.class.getName()
062: + " objects", 5, col_no.size());
063: }
064:
065: /**
066: * Test referential integrity with the ODMG-Layer, the foreign key
067: * is part of the primary key of the detail table in this case
068: */
069: public void testStoreFKinPK() throws Exception {
070: final long timestamp = System.currentTimeMillis();
071: Transaction tx = odmg.newTransaction();
072: tx.begin();
073:
074: Master master_1 = populatedMasterFKinPK(tx, 5, timestamp);
075: Master master_2 = populatedMasterFKinPK(tx, 5, timestamp);
076:
077: database.makePersistent(master_1);
078: database.makePersistent(master_2);
079: tx.commit();
080:
081: // Check stored objects
082: OQLQuery query = odmg.newOQLQuery();
083: query.create("select masters from " + Master.class.getName()
084: + " where masterText like $1");
085: query.bind("%" + timestamp);
086: List allMasters = (List) query.execute();
087: assertEquals("We should found master objects", 2, allMasters
088: .size());
089: Master lookup_1 = (Master) allMasters.get(0);
090: Collection col_in = lookup_1.getCollDetailFKinPK();
091: Collection col_no = lookup_1.getCollDetailFKnoPK();
092: assertEquals("Should found none "
093: + DetailFKnoPK.class.getName() + " objects", 0, col_no
094: .size());
095: assertEquals("Should found " + DetailFKinPK.class.getName()
096: + " objects", 5, col_in.size());
097: }
098:
099: private Master populatedMasterFKnoPK(Transaction tx,
100: int countDetailObjects, long timestamp) throws Exception {
101: Master master = new Master();
102: master.masterText = "Master_timestamp_" + timestamp;
103: master.collDetailFKnoPK = new Vector();
104: new Identity(master, ((HasBroker) tx).getBroker());
105: for (int i = 0; i < countDetailObjects; i++) {
106: DetailFKnoPK aDetail = new DetailFKnoPK();
107: aDetail.detailText = "DetailFK*no*PK count " + i
108: + ", associate master " + master.masterId
109: + " timestamp_" + timestamp;
110: aDetail.master = master;
111: master.collDetailFKnoPK.add(aDetail);
112: }
113: return master;
114: }
115:
116: private Master populatedMasterFKinPK(Transaction tx,
117: int countDetailObjects, long timestamp) throws Exception {
118: Master master = new Master();
119: master.masterText = "Master_timestamp_" + timestamp;
120: master.collDetailFKinPK = new Vector();
121: new Identity(master, ((HasBroker) tx).getBroker());
122: for (int i = 0; i < countDetailObjects; i++) {
123: DetailFKinPK aDetail = new DetailFKinPK();
124: aDetail.detailText = "DetailFKinPK count " + i
125: + ", associate master " + master.masterId
126: + " timestamp_" + timestamp;
127: aDetail.master = master;
128: master.collDetailFKinPK.add(aDetail);
129: }
130: return master;
131: }
132:
133: public void testDelete() throws Exception {
134: long timestamp = System.currentTimeMillis();
135: // 2. Get a list of all Masters
136: Transaction tx = odmg.newTransaction();
137: tx.begin();
138: // 1. Insert some objects into the database, some of them with
139: // details in DetailFKinPK, some of them in DetailFKnoPK
140: Master master_1 = populatedMasterFKinPK(tx, 7, timestamp);
141: Master master_2 = populatedMasterFKinPK(tx, 6, timestamp);
142: Master master_3 = populatedMasterFKnoPK(tx, 7, timestamp);
143: Master master_4 = populatedMasterFKnoPK(tx, 6, timestamp);
144:
145: tx.lock(master_1, Transaction.WRITE);
146: tx.lock(master_2, Transaction.WRITE);
147: tx.lock(master_3, Transaction.WRITE);
148: tx.lock(master_4, Transaction.WRITE);
149: tx.commit();
150:
151: tx.begin();
152: OQLQuery query = odmg.newOQLQuery();
153: query.create("select masters from " + Master.class.getName()
154: + " where masterText like $1");
155: query.bind("%" + timestamp);
156: List allMasters = (List) query.execute();
157:
158: // Iterator over all Master objects
159: Iterator it = allMasters.iterator();
160: int counter = 0;
161: while (it.hasNext()) {
162: ++counter;
163: Master aMaster = (Master) it.next();
164: Iterator it2 = aMaster.collDetailFKinPK.iterator();
165: while (it2.hasNext())
166: database.deletePersistent(it2.next());
167: it2 = aMaster.collDetailFKnoPK.iterator();
168: while (it2.hasNext())
169: database.deletePersistent(it2.next());
170: database.deletePersistent(aMaster);
171: }
172: tx.commit();
173: assertEquals("Wrong count of Master objects found", 4, counter);
174:
175: query = odmg.newOQLQuery();
176: query.create("select masters from " + Master.class.getName()
177: + " where masterText like $1");
178: query.bind("%" + timestamp);
179: allMasters = (List) query.execute();
180: assertEquals("Delete of Master objects failed", 0, allMasters
181: .size());
182: }
183:
184: public void testInsertAfterDelete() throws Exception {
185: final String contractPk = "The Contract_"
186: + System.currentTimeMillis();
187: Contract obj1 = new Contract();
188: Contract obj2 = new Contract();
189:
190: obj1.setPk(contractPk);
191: obj1.setContractValue2(1);
192:
193: obj2.setPk(contractPk);
194: obj2.setContractValue2(2);
195:
196: // 1. Insert object
197: Transaction tx = odmg.newTransaction();
198: tx.begin();
199: /*
200: arminw:
201: seems to have problems when within a tx a object
202: was stored/deleted.
203: Without obj1 test pass
204: TODO: fix this
205: */
206: database.makePersistent(obj1);
207: database.deletePersistent(obj2);
208: database.makePersistent(obj1);
209: /*
210: thma: I checked this, and don't see a problem here.
211: obj1 and obj2 have the same Identity. Thus the
212: calls database.makePersistent(obj1); and database.makePersistent(obj2);
213: will only register one instance to the transaction.
214: The second call does not add a second instance, but just marks the
215: existing instance as dirty a second time.
216: So it's no wonder why after deletePersistent(obj1); no contract is found.
217: Works as designed.
218: The Lesson to learn: never let business objects have the same primary key values!
219: * */
220: tx.commit();
221: Collection result = getContract(contractPk, odmg);
222: assertEquals("We should found exact one contract", 1, result
223: .size());
224: Contract newObj = (Contract) result.iterator().next();
225: assertNotNull("Object not found", newObj);
226: assertEquals(1, newObj.getContractValue2());
227:
228: // 2. Delete, then insert object with the same identity
229: tx.begin();
230: database.deletePersistent(newObj);
231: database.makePersistent(obj2);
232: tx.commit();
233: assertEquals(2, obj2.getContractValue2());
234:
235: result = getContract(contractPk, odmg);
236: assertEquals("We should found exact one contract", 1, result
237: .size());
238: newObj = (Contract) result.iterator().next();
239: assertNotNull("Object not found", newObj);
240: assertEquals(2, newObj.getContractValue2());
241:
242: // 3. Delete
243: tx.begin();
244: database.deletePersistent(obj1);
245: tx.commit();
246:
247: result = getContract(contractPk, odmg);
248: assertEquals(0, result.size());
249: }
250:
251: private Collection getContract(String pk, Implementation odmg)
252: throws Exception {
253: OQLQuery query = odmg.newOQLQuery();
254: query.create("select c from " + Contract.class.getName()
255: + " where pk like $1");
256: query.bind(pk);
257: return (Collection) query.execute();
258: }
259: }
|