KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * (C) 2003 ppi Media
3  * User: om
4  */

5
6 package org.apache.ojb.odmg;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.List JavaDoc;
12
13 import org.apache.ojb.junit.ODMGTestCase;
14 import org.apache.ojb.odmg.shared.Person;
15 import org.apache.ojb.odmg.shared.PersonImpl;
16 import org.apache.commons.lang.ArrayUtils;
17 import org.apache.commons.collections.ListUtils;
18 import org.odmg.OQLQuery;
19 import org.odmg.Transaction;
20
21 /**
22  * class PersonWithArrayTest
23  *
24  * @author <a HREF="mailto:om@ppi.de">Oliver Matz</a>
25  * @version $Id: $
26  */

27 public class PersonWithArrayTest extends ODMGTestCase
28 {
29     public static void main(String JavaDoc[] args)
30     {
31         String JavaDoc[] arr = {PersonWithArrayTest.class.getName()};
32         junit.textui.TestRunner.main(arr);
33     }
34
35     /*
36      * lock only the father, let OJB do the rest
37      * delete father then children
38      */

39     public void testStoreDeleteThreePersons_1() throws Exception JavaDoc
40     {
41         String JavaDoc postfix = "_" + System.currentTimeMillis();
42         String JavaDoc firstnameFather = "Father" + postfix;
43         String JavaDoc firstnameChild_1 = "Child_One" + postfix;
44         String JavaDoc firstnameChild_2 = "Child_Two" + postfix;
45         String JavaDoc lastname = "testStoreThreePersons_1_" + postfix;
46
47         Person father = createPerson(firstnameFather, lastname, null, null);
48         Person child_1 = createPerson(firstnameChild_1, lastname, null, null);
49         Person child_2 = createPerson(firstnameChild_2, lastname, null, null);
50
51         Person[] children = new Person[]{child_1, child_2};
52         father.setChildren(children);
53         child_1.setFather(father);
54         child_2.setFather(father);
55
56         /*
57          * lock only the father, let OJB do the rest
58          */

59         TransactionExt tx = (TransactionExt) odmg.newTransaction();
60         tx.begin();
61         tx.lock(father, Transaction.WRITE);
62         tx.commit();
63
64         tx.begin();
65         // make sure all objects are retrieved freshly in subsequent transactions
66
((TransactionImpl) tx).getBroker().clearCache();
67         OQLQuery qry = odmg.newOQLQuery();
68         qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1");
69         qry.bind(firstnameFather);
70         Collection JavaDoc result = (Collection JavaDoc) qry.execute();
71
72         assertEquals("Exactly one element in result set", 1, result.size());
73         Person returnedFather = (Person) result.iterator().next();
74         // should retrieve new instance
75
assertTrue("not same", returnedFather != father);
76         Person[] returnedChildren = returnedFather.getChildren();
77         assertNotNull(returnedChildren);
78         assertEquals(2, returnedChildren.length);
79         Person child = returnedChildren[0];
80         Person lookupFather = child.getFather();
81         assertNotNull(lookupFather);
82         assertEquals(returnedFather.getFirstname(), lookupFather.getFirstname());
83         // unfortunately, PersonImpl does not have a suitable equals method.
84
assertEquals(
85                 "children's names are equal",
86                 Arrays.asList(getFirstNames(returnedChildren)),
87                 Arrays.asList(getFirstNames(children)));
88         tx.commit();
89
90         /*
91          delete father then children
92          fk-constraints?
93          */

94         tx.begin();
95         database.deletePersistent(returnedFather);
96         database.deletePersistent(returnedFather.getChildren()[0]);
97         database.deletePersistent(returnedFather.getChildren()[1]);
98
99         tx.commit();
100
101         qry = odmg.newOQLQuery();
102         qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1");
103         qry.bind(firstnameFather);
104         result = (Collection JavaDoc) qry.execute();
105         assertEquals(0, result.size());
106
107         qry = odmg.newOQLQuery();
108         qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1");
109         qry.bind(firstnameChild_1);
110         result = (Collection JavaDoc) qry.execute();
111         // System.out.println("child: "+ new ArrayList(result));
112
assertEquals(0, result.size());
113     }
114
115     /*
116      lock father then all childs
117      delete children then father
118      */

119     public void testStoreDeleteThreePersons_2() throws Exception JavaDoc
120     {
121         String JavaDoc postfix = "_" + System.currentTimeMillis();
122         String JavaDoc firstnameFather = "Father" + postfix;
123         String JavaDoc firstnameChild_1 = "Child_One" + postfix;
124         String JavaDoc firstnameChild_2 = "Child_Two" + postfix;
125         String JavaDoc lastname = "testStoreThreePersons_2_" + postfix;
126
127         Person father = createPerson(firstnameFather, lastname, null, null);
128         Person child_1 = createPerson(firstnameChild_1, lastname, null, null);
129         Person child_2 = createPerson(firstnameChild_2, lastname, null, null);
130
131         Person[] children = new Person[]{child_1, child_2};
132         father.setChildren(children);
133         child_1.setFather(father);
134         child_2.setFather(father);
135
136         /*
137          lock father then all childs
138          */

139         TransactionExt tx = (TransactionExt) odmg.newTransaction();
140         tx.begin();
141         tx.lock(father, Transaction.WRITE);
142         tx.lock(child_2, Transaction.WRITE);
143         tx.lock(child_1, Transaction.WRITE);
144
145         tx.commit();
146         assertEquals(2, father.getChildren().length);
147
148         tx.begin();
149         // make sure all objects are retrieved freshly in subsequent transactions
150
((TransactionImpl) tx).getBroker().clearCache();
151         OQLQuery qry = odmg.newOQLQuery();
152         qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1");
153         qry.bind(firstnameFather);
154         Collection JavaDoc result = (Collection JavaDoc) qry.execute();
155
156         assertEquals("Exactly one element in result set", 1, result.size());
157         Person returnedFather = (Person) result.iterator().next();
158         // should retrieve new instance
159
assertTrue("not same", returnedFather != father);
160         Person[] returnedChildren = returnedFather.getChildren();
161         assertNotNull(returnedChildren);
162         // check original instance again
163
assertEquals(2, father.getChildren().length);
164         assertEquals(2, returnedChildren.length);
165         Person child = returnedChildren[0];
166         Person lookupFather = child.getFather();
167         assertNotNull(lookupFather);
168         assertEquals(returnedFather.getFirstname(), lookupFather.getFirstname());
169         // unfortunately, PersonImpl does not have a suitable equals method.
170
assertEquals(
171                 "children's names are equal",
172                 Arrays.asList(getFirstNames(returnedChildren)),
173                 Arrays.asList(getFirstNames(children)));
174         // System.out.println(Arrays.asList(getFirstNames(returnedChildren)));
175
tx.commit();
176
177         /*
178          delete father only and disable cascading delete
179          */

180         tx.begin();
181         /*
182         by default cascading delete is enabled for 1:n relations, but we want to
183         delete the father without deleting the dependent childs, so change runtime
184         behavior of cascading delete
185         */

186         tx.setCascadingDelete(PersonImpl.class, "children", false);
187         database.deletePersistent(returnedFather);
188         tx.commit();
189
190         qry = odmg.newOQLQuery();
191         qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1");
192         qry.bind(firstnameFather);
193         result = (Collection JavaDoc) qry.execute();
194         assertEquals("Exactly one element in result set", 0, result.size());
195
196         qry = odmg.newOQLQuery();
197         qry.create("select a from " + PersonImpl.class.getName() + " where lastname=$1");
198         qry.bind(lastname);
199         result = (Collection JavaDoc) qry.execute();
200         assertEquals("Expected the two children objects of deleted main object", 2, result.size());
201     }
202
203     /**
204      * Seems the locking order of objects is mandatory in this
205      * case. This test fails
206      */

207     public void testStoreDeleteThreePersons_3() throws Exception JavaDoc
208     {
209         String JavaDoc postfix = "_" + System.currentTimeMillis();
210         String JavaDoc firstnameFather = "Father" + postfix;
211         String JavaDoc firstnameChild_1 = "Child_One" + postfix;
212         String JavaDoc firstnameChild_2 = "Child_Two" + postfix;
213         String JavaDoc lastname = "testStoreThreePersons_3" + postfix;
214
215         Person father = createPerson(firstnameFather, lastname, null, null);
216         Person child_1 = createPerson(firstnameChild_1, lastname, null, null);
217         Person child_2 = createPerson(firstnameChild_2, lastname, null, null);
218
219         Person[] children = new Person[]{child_1, child_2};
220         father.setChildren(children);
221         child_1.setFather(father);
222         child_2.setFather(father);
223
224         /*
225          lock childs first, then lock father
226          TODO: Does not pass - why? A defined lock
227          order necessary?
228          if this doesn't make sense remove the test
229          */

230         Transaction tx = odmg.newTransaction();
231         tx.begin();
232         tx.lock(child_1, Transaction.WRITE);
233         tx.lock(child_2, Transaction.WRITE);
234         tx.lock(father, Transaction.WRITE);
235         tx.commit();
236
237         tx = odmg.newTransaction();
238         tx.begin();
239         // make sure all objects are retrieved freshly in subsequent transactions
240
((TransactionImpl) tx).getBroker().clearCache();
241
242         OQLQuery qry = odmg.newOQLQuery();
243         qry.create("select a from " + PersonImpl.class.getName() + " where lastname=$1");
244         qry.bind(lastname);
245         Collection JavaDoc result = (Collection JavaDoc) qry.execute();
246         assertEquals(3, new ArrayList JavaDoc(result).size());
247
248
249         qry = odmg.newOQLQuery();
250         qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1");
251         qry.bind(firstnameFather);
252         result = (Collection JavaDoc) qry.execute();
253         tx.commit();
254         assertEquals("Exactly one element in result set", 1, result.size());
255
256         tx.begin();
257         Person returnedFather = (Person) result.iterator().next();
258         // should retrieve new instance, cause we clear the cache
259
assertTrue("not same instance expected", returnedFather != father);
260         Person[] returnedChildren = returnedFather.getChildren();
261         assertNotNull(returnedChildren);
262         assertEquals(2, returnedChildren.length);
263         Person child = returnedChildren[0];
264         Person lookupFather = child.getFather();
265         assertNotNull(lookupFather);
266         assertEquals(returnedFather.getFirstname(), lookupFather.getFirstname());
267         // unfortunately, PersonImpl does not have a suitable equals method.
268
// comment out, because of child object order problem (it's not a bug, it's bad test writing)
269
// assertEquals(
270
// "children's names are equal",
271
// Arrays.asList(getFirstNames(returnedChildren)),
272
// Arrays.asList(getFirstNames(children)));
273
// we expect the same names in both array, thus intersection result have to be '2'
274
List JavaDoc list = ListUtils.intersection(Arrays.asList(getFirstNames(returnedChildren)), Arrays.asList(getFirstNames(children)));
275         assertEquals(2, list.size());
276         // System.out.println(Arrays.asList(getFirstNames(returnedChildren)));
277
tx.commit();
278
279         /*
280          delete father then children
281          fk-constraints?
282          Delete calls in wrong order
283          */

284         tx.begin();
285         database.deletePersistent(returnedFather);
286         database.deletePersistent(returnedFather.getChildren()[0]);
287         database.deletePersistent(returnedFather.getChildren()[1]);
288         tx.commit();
289
290         qry = odmg.newOQLQuery();
291         qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1");
292         qry.bind(firstnameFather);
293         result = (Collection JavaDoc) qry.execute();
294         assertEquals("Exactly one element in result set", 0, result.size());
295
296         qry = odmg.newOQLQuery();
297         qry.create("select a from " + PersonImpl.class.getName() + " where firstname=$1");
298         qry.bind(firstnameChild_1);
299         result = (Collection JavaDoc) qry.execute();
300         // System.out.println("child: "+result.iterator().next());
301
assertEquals("Exactly one element in result set", 0, result.size());
302     }
303
304     private Person createPerson(String JavaDoc firstname, String JavaDoc lastname, Person father, Person mother)
305     {
306         Person p = new PersonImpl();
307         p.setFirstname(firstname);
308         p.setLastname(lastname);
309         p.setFather(father);
310         p.setMother(mother);
311         // p.setChildren(null);
312
return p;
313     }
314
315     private static String JavaDoc[] getFirstNames(Person[] persons)
316     {
317         int length = persons == null ? 0 : persons.length;
318         String JavaDoc[] ret = new String JavaDoc[length];
319         for (int i = 0; i < ret.length; i++)
320         {
321             ret[i] = persons[i].getFirstname();
322         }
323         return ret;
324     }
325 }
326
Popular Tags