KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > RITest


1 package org.apache.ojb.odmg;
2
3 import org.apache.ojb.broker.Contract;
4 import org.apache.ojb.broker.Identity;
5 import org.apache.ojb.junit.ODMGTestCase;
6 import org.apache.ojb.odmg.shared.DetailFKinPK;
7 import org.apache.ojb.odmg.shared.DetailFKnoPK;
8 import org.apache.ojb.odmg.shared.Master;
9 import org.odmg.Implementation;
10 import org.odmg.OQLQuery;
11 import org.odmg.Transaction;
12
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Vector JavaDoc;
16 import java.util.List JavaDoc;
17
18 /** Demo Application that shows basic concepts for Applications using the OJB ODMG
19  * implementation as an transactional object server.
20  */

21 public class RITest extends ODMGTestCase
22 {
23     public static void main(String JavaDoc[] args)
24     {
25         String JavaDoc[] arr = {RITest.class.getName()};
26         junit.textui.TestRunner.main(arr);
27     }
28
29     public RITest(String JavaDoc name)
30     {
31         super(name);
32     }
33
34     /**
35      * Test referential integrity with the ODMG-Layer, the foreign key
36      * is not a part of the primary key of the detail.
37      */

38     public void testStoreFKnoPK() throws Exception JavaDoc
39     {
40         long timestamp = System.currentTimeMillis();
41         Transaction tx = odmg.newTransaction();
42         tx.begin();
43         Master master_1 = populatedMasterFKnoPK(tx, 5, timestamp);
44         Master master_2 = populatedMasterFKnoPK(tx, 5, timestamp);
45
46         database.makePersistent(master_1);
47         database.makePersistent(master_2);
48         tx.commit();
49
50         // Check stored objects
51
OQLQuery query = odmg.newOQLQuery();
52         query.create("select masters from " + Master.class.getName() + " where masterText like $1");
53         query.bind("%" + timestamp);
54         List JavaDoc allMasters = (List JavaDoc) query.execute();
55         assertEquals("We should found master objects", 2, allMasters.size());
56         Master lookup_1 = (Master) allMasters.get(0);
57
58         Collection JavaDoc col_in = lookup_1.getCollDetailFKinPK();
59         Collection JavaDoc col_no = lookup_1.getCollDetailFKnoPK();
60         assertEquals("Should found none " + DetailFKinPK.class.getName() + " objects", 0, col_in.size());
61         assertEquals("Should found " + DetailFKnoPK.class.getName() + " objects", 5, col_no.size());
62     }
63
64     /**
65      * Test referential integrity with the ODMG-Layer, the foreign key
66      * is part of the primary key of the detail table in this case
67      */

68     public void testStoreFKinPK() throws Exception JavaDoc
69     {
70         final long timestamp = System.currentTimeMillis();
71         Transaction tx = odmg.newTransaction();
72         tx.begin();
73
74         Master master_1 = populatedMasterFKinPK(tx, 5, timestamp);
75         Master master_2 = populatedMasterFKinPK(tx, 5, timestamp);
76
77         database.makePersistent(master_1);
78         database.makePersistent(master_2);
79         tx.commit();
80
81         // Check stored objects
82
OQLQuery query = odmg.newOQLQuery();
83         query.create("select masters from " + Master.class.getName() + " where masterText like $1");
84         query.bind("%" + timestamp);
85         List JavaDoc allMasters = (List JavaDoc) query.execute();
86         assertEquals("We should found master objects", 2, allMasters.size());
87         Master lookup_1 = (Master) allMasters.get(0);
88         Collection JavaDoc col_in = lookup_1.getCollDetailFKinPK();
89         Collection JavaDoc col_no = lookup_1.getCollDetailFKnoPK();
90         assertEquals("Should found none " + DetailFKnoPK.class.getName() + " objects", 0, col_no.size());
91         assertEquals("Should found " + DetailFKinPK.class.getName() + " objects", 5, col_in.size());
92     }
93
94     private Master populatedMasterFKnoPK(Transaction tx, int countDetailObjects, long timestamp)
95             throws Exception JavaDoc
96     {
97         Master master = new Master();
98         master.masterText = "Master_timestamp_" + timestamp;
99         master.collDetailFKnoPK = new Vector JavaDoc();
100         new Identity(master, ((HasBroker) tx).getBroker());
101         for (int i = 0; i < countDetailObjects; i++)
102         {
103             DetailFKnoPK aDetail = new DetailFKnoPK();
104             aDetail.detailText = "DetailFK*no*PK count " + i + ", associate master " + master.masterId + " timestamp_" + timestamp;
105             aDetail.master = master;
106             master.collDetailFKnoPK.add(aDetail);
107         }
108         return master;
109     }
110
111     private Master populatedMasterFKinPK(Transaction tx, int countDetailObjects, long timestamp)
112             throws Exception JavaDoc
113     {
114         Master master = new Master();
115         master.masterText = "Master_timestamp_" + timestamp;
116         master.collDetailFKinPK = new Vector JavaDoc();
117         new Identity(master, ((HasBroker) tx).getBroker());
118         for (int i = 0; i < countDetailObjects; i++)
119         {
120             DetailFKinPK aDetail = new DetailFKinPK();
121             aDetail.detailText = "DetailFKinPK count " + i + ", associate master " + master.masterId + " timestamp_" + timestamp;
122             aDetail.master = master;
123             master.collDetailFKinPK.add(aDetail);
124         }
125         return master;
126     }
127
128     public void testDelete() throws Exception JavaDoc
129     {
130         long timestamp = System.currentTimeMillis();
131         // 2. Get a list of all Masters
132
Transaction tx = odmg.newTransaction();
133         tx.begin();
134         // 1. Insert some objects into the database, some of them with
135
// details in DetailFKinPK, some of them in DetailFKnoPK
136
Master master_1 = populatedMasterFKinPK(tx, 7, timestamp);
137         Master master_2 = populatedMasterFKinPK(tx, 6, timestamp);
138         Master master_3 = populatedMasterFKnoPK(tx, 7, timestamp);
139         Master master_4 = populatedMasterFKnoPK(tx, 6, timestamp);
140
141         tx.lock(master_1, Transaction.WRITE);
142         tx.lock(master_2, Transaction.WRITE);
143         tx.lock(master_3, Transaction.WRITE);
144         tx.lock(master_4, Transaction.WRITE);
145         tx.commit();
146
147         tx.begin();
148         OQLQuery query = odmg.newOQLQuery();
149         query.create("select masters from " + Master.class.getName() + " where masterText like $1");
150         query.bind("%" + timestamp);
151         List JavaDoc allMasters = (List JavaDoc) query.execute();
152
153         // Iterator over all Master objects
154
Iterator JavaDoc it = allMasters.iterator();
155         int counter = 0;
156         while (it.hasNext())
157         {
158             ++counter;
159             Master aMaster = (Master) it.next();
160             Iterator JavaDoc it2 = aMaster.collDetailFKinPK.iterator();
161             while (it2.hasNext())
162                 database.deletePersistent(it2.next());
163             it2 = aMaster.collDetailFKnoPK.iterator();
164             while (it2.hasNext())
165                 database.deletePersistent(it2.next());
166             database.deletePersistent(aMaster);
167         }
168         tx.commit();
169         assertEquals("Wrong count of Master objects found", 4, counter);
170
171         query = odmg.newOQLQuery();
172         query.create("select masters from " + Master.class.getName() + " where masterText like $1");
173         query.bind("%" + timestamp);
174         allMasters = (List JavaDoc) query.execute();
175         assertEquals("Delete of Master objects failed", 0, allMasters.size());
176     }
177
178     public void testInsertAfterDelete() throws Exception JavaDoc
179     {
180         final String JavaDoc contractPk = "The Contract_" + System.currentTimeMillis();
181         Contract obj1 = new Contract();
182         Contract obj2 = new Contract();
183
184         obj1.setPk(contractPk);
185         obj1.setContractValue2(1);
186
187         obj2.setPk(contractPk);
188         obj2.setContractValue2(2);
189
190         // 1. Insert object
191
Transaction tx = odmg.newTransaction();
192         tx.begin();
193         /*
194         arminw:
195         seems to have problems when within a tx a object
196         was stored/deleted.
197         Without obj1 test pass
198         TODO: fix this
199         */

200         database.makePersistent(obj1);
201         database.deletePersistent(obj2);
202         database.makePersistent(obj1);
203         /*
204          thma: I checked this, and don't see a problem here.
205         obj1 and obj2 have the same Identity. Thus the
206         calls database.makePersistent(obj1); and database.makePersistent(obj2);
207         will only register one instance to the transaction.
208         The second call does not add a second instance, but just marks the
209         existing instance as dirty a second time.
210         So it's no wonder why after deletePersistent(obj1); no contract is found.
211         Works as designed.
212         The Lesson to learn: never let business objects have the same primary key values!
213          * */

214         tx.commit();
215         Collection JavaDoc result = getContract(contractPk, odmg);
216         assertEquals("We should found exact one contract", 1, result.size());
217         Contract newObj = (Contract) result.iterator().next();
218         assertNotNull("Object not found", newObj);
219         assertEquals(1, newObj.getContractValue2());
220
221         // 2. Delete, then insert object with the same identity
222
tx.begin();
223         database.deletePersistent(newObj);
224         database.makePersistent(obj2);
225         tx.commit();
226         assertEquals(2, obj2.getContractValue2());
227
228         result = getContract(contractPk, odmg);
229         assertEquals("We should found exact one contract", 1, result.size());
230         newObj = (Contract) result.iterator().next();
231         assertNotNull("Object not found", newObj);
232         assertEquals(2, newObj.getContractValue2());
233
234         // 3. Delete
235
tx.begin();
236         database.deletePersistent(obj1);
237         tx.commit();
238
239         result = getContract(contractPk, odmg);
240         assertEquals(0, result.size());
241     }
242
243     private Collection JavaDoc getContract(String JavaDoc pk, Implementation odmg)
244             throws Exception JavaDoc
245     {
246         OQLQuery query = odmg.newOQLQuery();
247         query.create("select c from " + Contract.class.getName() + " where pk like $1");
248         query.bind(pk);
249         return (Collection JavaDoc) query.execute();
250     }
251 }
252
Popular Tags