KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > cache > LocalCacheTest


1 package org.apache.ojb.broker.cache;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.ojb.broker.Identity;
24 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
25 import org.apache.ojb.broker.query.Criteria;
26 import org.apache.ojb.broker.query.Query;
27 import org.apache.ojb.broker.query.QueryFactory;
28 import org.apache.ojb.junit.PBTestCase;
29
30 /**
31  * Tests both caching level (LocalCache + real OjbjectCache) using circular
32  * relationship tests.
33  *
34  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
35  * @version $Id: LocalCacheTest.java,v 1.2.2.2 2005/12/21 22:31:24 tomdz Exp $
36  */

37 public class LocalCacheTest extends PBTestCase
38 {
39 // private static final int CASCADE_NONE = ObjectReferenceDescriptor.CASCADE_NONE;
40
// private static final int CASCADE_LINK = ObjectReferenceDescriptor.CASCADE_LINK;
41
private static final int CASCADE_OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT;
42
43     public static void main(String JavaDoc[] args)
44     {
45         String JavaDoc[] arr = {LocalCacheTest.class.getName()};
46         junit.textui.TestRunner.main(arr);
47     }
48
49     public void tearDown() throws Exception JavaDoc
50     {
51         super.tearDown();
52     }
53
54     public void testCircularStore()
55     {
56         // prepare test
57
ojbChangeReferenceSetting(Person.class, "father", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
58         ojbChangeReferenceSetting(Person.class, "grandfather", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
59         ojbChangeReferenceSetting(Person.class, "childs", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
60         ojbChangeReferenceSetting(Person.class, "grandchilds", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
61
62         String JavaDoc postfix = "_testCircularStore_" + System.currentTimeMillis();
63         Person junior = createComplexFamily(postfix);
64         broker.beginTransaction();
65         broker.store(junior);
66         broker.commitTransaction();
67         Identity oidJunior = new Identity(junior, broker);
68         Identity oidSenior = new Identity(junior.getFather(), broker);
69         broker.clearCache();
70
71         Criteria crit = new Criteria();
72         crit.addLike("name", "jeffChild_%" + postfix);
73         Query q = QueryFactory.newQuery(Person.class, crit);
74
75         Person newJunior = (Person) broker.getObjectByIdentity(oidJunior);
76         assertNotNull(newJunior);
77         assertNotNull(newJunior.getFather());
78         assertNotNull(newJunior.getChilds());
79         assertEquals(2, newJunior.getChilds().size());
80
81         Person newSenior = (Person) broker.getObjectByIdentity(oidSenior);
82         assertNotNull(newSenior);
83         assertNotNull(newSenior.getChilds());
84         assertEquals(1, newSenior.getChilds().size());
85         assertNotNull(newSenior.getGrandchilds());
86         assertEquals(2, newSenior.getGrandchilds().size());
87
88         Collection JavaDoc result = broker.getCollectionByQuery(q);
89         assertEquals(2, result.size());
90         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
91         {
92             Person p = (Person) iterator.next();
93             assertNotNull(p.getFather());
94             assertEquals("jeffJunior" + postfix, p.getFather().getName());
95             assertNotNull(p.getGrandfather());
96             assertEquals("jeffSenior" + postfix, p.getGrandfather().getName());
97         }
98     }
99
100     /**
101      * same as above but without clearing the cache
102      */

103     public void testCircularStore_2()
104     {
105         // prepare test
106
ojbChangeReferenceSetting(Person.class, "father", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
107         ojbChangeReferenceSetting(Person.class, "grandfather", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
108         ojbChangeReferenceSetting(Person.class, "childs", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
109         ojbChangeReferenceSetting(Person.class, "grandchilds", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
110
111         String JavaDoc postfix = "_testCircularStore_2_" + System.currentTimeMillis();
112         Person junior = createComplexFamily(postfix);
113         broker.beginTransaction();
114         broker.store(junior);
115         broker.commitTransaction();
116         Identity oidJunior = new Identity(junior, broker);
117         Identity oidSenior = new Identity(junior.getFather(), broker);
118
119         Criteria crit = new Criteria();
120         crit.addLike("name", "jeffChild_%" + postfix);
121         Query q = QueryFactory.newQuery(Person.class, crit);
122
123         Person newJunior = (Person) broker.getObjectByIdentity(oidJunior);
124         assertNotNull(newJunior);
125         assertNotNull(newJunior.getFather());
126         assertNotNull(newJunior.getChilds());
127         assertEquals(2, newJunior.getChilds().size());
128
129         Person newSenior = (Person) broker.getObjectByIdentity(oidSenior);
130         assertNotNull(newSenior);
131         assertNotNull(newSenior.getChilds());
132         assertEquals(1, newSenior.getChilds().size());
133         assertNotNull(newSenior.getGrandchilds());
134         assertEquals(2, newSenior.getGrandchilds().size());
135
136         Collection JavaDoc result = broker.getCollectionByQuery(q);
137         assertEquals(2, result.size());
138         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
139         {
140             Person p = (Person) iterator.next();
141             assertNotNull(p.getFather());
142             assertEquals("jeffJunior" + postfix, p.getFather().getName());
143             assertNotNull(p.getGrandfather());
144             assertEquals("jeffSenior" + postfix, p.getGrandfather().getName());
145         }
146     }
147
148     public void testCircularStoreUpdate()
149     {
150         // prepare test
151
ojbChangeReferenceSetting(Person.class, "father", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
152         ojbChangeReferenceSetting(Person.class, "grandfather", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
153         ojbChangeReferenceSetting(Person.class, "childs", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
154         ojbChangeReferenceSetting(Person.class, "grandchilds", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
155
156         String JavaDoc postfix = "_testCircularStore_" + System.currentTimeMillis();
157         Person junior = createComplexFamily(postfix);
158         broker.beginTransaction();
159         broker.store(junior);
160         broker.commitTransaction();
161         Identity oidJunior = new Identity(junior, broker);
162         Identity oidSenior = new Identity(junior.getFather(), broker);
163         broker.clearCache();
164
165         Criteria crit = new Criteria();
166         crit.addLike("name", "jeffChild_%" + postfix);
167         Query q = QueryFactory.newQuery(Person.class, crit);
168
169         Person newJunior = (Person) broker.getObjectByIdentity(oidJunior);
170         assertNotNull(newJunior);
171         assertNotNull(newJunior.getFather());
172         assertNotNull(newJunior.getChilds());
173         assertEquals(2, newJunior.getChilds().size());
174
175         Person newSenior = (Person) broker.getObjectByIdentity(oidSenior);
176         assertNotNull(newSenior);
177         assertNotNull(newSenior.getChilds());
178         assertEquals(1, newSenior.getChilds().size());
179         assertNotNull(newSenior.getGrandchilds());
180         assertEquals(2, newSenior.getGrandchilds().size());
181
182         Collection JavaDoc result = broker.getCollectionByQuery(q);
183         assertEquals(2, result.size());
184         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
185         {
186             Person p = (Person) iterator.next();
187             assertNotNull(p.getFather());
188             assertEquals("jeffJunior" + postfix, p.getFather().getName());
189             assertNotNull(p.getGrandfather());
190             assertEquals("jeffSenior" + postfix, p.getGrandfather().getName());
191         }
192
193         broker.beginTransaction();
194         Person newChild = new Person("newGrandChild" + postfix, null, newSenior, null);
195         newSenior.addGranschild(newChild);
196         broker.store(newSenior);
197         broker.commitTransaction();
198         broker.clearCache();
199
200         newSenior = (Person) broker.getObjectByIdentity(oidSenior);
201         assertNotNull(newSenior);
202         assertNotNull(newSenior.getChilds());
203         assertEquals(1, newSenior.getChilds().size());
204         assertNotNull(newSenior.getGrandchilds());
205         assertEquals(3, newSenior.getGrandchilds().size());
206     }
207
208     /**
209      * same as above, but without clearing the cache
210      */

211     public void testCircularStoreUpdate_2()
212     {
213         // prepare test
214
ojbChangeReferenceSetting(Person.class, "father", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
215         ojbChangeReferenceSetting(Person.class, "grandfather", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
216         ojbChangeReferenceSetting(Person.class, "childs", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
217         ojbChangeReferenceSetting(Person.class, "grandchilds", true, CASCADE_OBJECT, CASCADE_OBJECT, false);
218
219         String JavaDoc postfix = "_testCircularStore_2_" + System.currentTimeMillis();
220         Person junior = createComplexFamily(postfix);
221         broker.beginTransaction();
222         broker.store(junior);
223         broker.commitTransaction();
224         Identity oidJunior = new Identity(junior, broker);
225         Identity oidSenior = new Identity(junior.getFather(), broker);
226
227         Criteria crit = new Criteria();
228         crit.addLike("name", "jeffChild_%" + postfix);
229         Query q = QueryFactory.newQuery(Person.class, crit);
230
231         Person newJunior = (Person) broker.getObjectByIdentity(oidJunior);
232         assertNotNull(newJunior);
233         assertNotNull(newJunior.getFather());
234         assertNotNull(newJunior.getChilds());
235         assertEquals(2, newJunior.getChilds().size());
236
237         Person newSenior = (Person) broker.getObjectByIdentity(oidSenior);
238         assertNotNull(newSenior);
239         assertNotNull(newSenior.getChilds());
240         assertEquals(1, newSenior.getChilds().size());
241         assertNotNull(newSenior.getGrandchilds());
242         assertEquals(2, newSenior.getGrandchilds().size());
243
244         Collection JavaDoc result = broker.getCollectionByQuery(q);
245         assertEquals(2, result.size());
246         for(Iterator JavaDoc iterator = result.iterator(); iterator.hasNext();)
247         {
248             Person p = (Person) iterator.next();
249             assertNotNull(p.getFather());
250             assertEquals("jeffJunior" + postfix, p.getFather().getName());
251             assertNotNull(p.getGrandfather());
252             assertEquals("jeffSenior" + postfix, p.getGrandfather().getName());
253         }
254
255         broker.beginTransaction();
256         Person newChild = new Person("newGrandChild" + postfix, null, newSenior, null);
257         newSenior.addGranschild(newChild);
258         broker.store(newSenior);
259         broker.commitTransaction();
260
261         newSenior = (Person) broker.getObjectByIdentity(oidSenior);
262         assertNotNull(newSenior);
263         assertNotNull(newSenior.getChilds());
264         assertEquals(1, newSenior.getChilds().size());
265         assertNotNull(newSenior.getGrandchilds());
266         assertEquals(3, newSenior.getGrandchilds().size());
267     }
268
269     /**
270      * Creates an circular object hierarchy
271      */

272     private Person createComplexFamily(String JavaDoc postfix)
273     {
274         Person jeffJunior = new Person();
275         jeffJunior.setName("jeffJunior" + postfix);
276
277         Person jeffSenior = new Person();
278         jeffSenior.setName("jeffSenior" + postfix);
279
280         Person jeffChild_1 = new Person();
281         jeffChild_1.setName("jeffChild_1" + postfix);
282         jeffChild_1.setFather(jeffJunior);
283         jeffChild_1.setGrandfather(jeffSenior);
284
285         Person jeffChild_2 = new Person();
286         jeffChild_2.setName("jeffChild_2" + postfix);
287         jeffChild_2.setFather(jeffJunior);
288         jeffChild_2.setGrandfather(jeffSenior);
289
290         jeffJunior.setFather(jeffSenior);
291         jeffJunior.addChild(jeffChild_1);
292         jeffJunior.addChild(jeffChild_2);
293
294         jeffSenior.addChild(jeffJunior);
295         jeffSenior.addGranschild(jeffChild_1);
296         jeffSenior.addGranschild(jeffChild_2);
297
298         return jeffJunior;
299     }
300
301     public static class Person
302     {
303         private Integer JavaDoc id;
304         private String JavaDoc name;
305         private Person father;
306         private Person grandfather;
307         private List JavaDoc childs;
308         private List JavaDoc grandchilds;
309         private Integer JavaDoc fkChild;
310         private Integer JavaDoc fkGrandchild;
311
312         public Person()
313         {
314         }
315
316         public Person(String JavaDoc name, Person father, Person grandfather, List JavaDoc childs)
317         {
318             this.name = name;
319             this.father = father;
320             this.grandfather = grandfather;
321             this.childs = childs;
322         }
323
324         public Integer JavaDoc getId()
325         {
326             return id;
327         }
328
329         public void setId(Integer JavaDoc id)
330         {
331             this.id = id;
332         }
333
334         public String JavaDoc getName()
335         {
336             return name;
337         }
338
339         public void setName(String JavaDoc name)
340         {
341             this.name = name;
342         }
343
344         public Person getFather()
345         {
346             return father;
347         }
348
349         public void setFather(Person father)
350         {
351             this.father = father;
352         }
353
354         public Person getGrandfather()
355         {
356             return grandfather;
357         }
358
359         public void setGrandfather(Person grandfather)
360         {
361             this.grandfather = grandfather;
362         }
363
364         public List JavaDoc getChilds()
365         {
366             return childs;
367         }
368
369         public void setChilds(List JavaDoc childs)
370         {
371             this.childs = childs;
372         }
373
374         public void addChild(Person child)
375         {
376             if(childs == null)
377             {
378                 childs = new ArrayList JavaDoc();
379             }
380             childs.add(child);
381         }
382
383         public List JavaDoc getGrandchilds()
384         {
385             return grandchilds;
386         }
387
388         public void setGrandchilds(List JavaDoc grandchilds)
389         {
390             this.grandchilds = grandchilds;
391         }
392
393         public void addGranschild(Person child)
394         {
395             if(grandchilds == null)
396             {
397                 grandchilds = new ArrayList JavaDoc();
398             }
399             grandchilds.add(child);
400         }
401
402         public Integer JavaDoc getFkChild()
403         {
404             return fkChild;
405         }
406
407         public void setFkChild(Integer JavaDoc fkChild)
408         {
409             this.fkChild = fkChild;
410         }
411
412         public Integer JavaDoc getFkGrandchild()
413         {
414             return fkGrandchild;
415         }
416
417         public void setFkGrandchild(Integer JavaDoc fkGrandchild)
418         {
419             this.fkGrandchild = fkGrandchild;
420         }
421     }
422 }
423
Popular Tags