001: package org.apache.ojb.broker.cache;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.apache.ojb.broker.Identity;
024: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
025: import org.apache.ojb.broker.query.Criteria;
026: import org.apache.ojb.broker.query.Query;
027: import org.apache.ojb.broker.query.QueryFactory;
028: import org.apache.ojb.junit.PBTestCase;
029:
030: /**
031: * Tests both caching level (LocalCache + real OjbjectCache) using circular
032: * relationship tests.
033: *
034: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
035: * @version $Id: LocalCacheTest.java,v 1.2.2.2 2005/12/21 22:31:24 tomdz Exp $
036: */
037: public class LocalCacheTest extends PBTestCase {
038: // private static final int CASCADE_NONE = ObjectReferenceDescriptor.CASCADE_NONE;
039: // private static final int CASCADE_LINK = ObjectReferenceDescriptor.CASCADE_LINK;
040: private static final int CASCADE_OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT;
041:
042: public static void main(String[] args) {
043: String[] arr = { LocalCacheTest.class.getName() };
044: junit.textui.TestRunner.main(arr);
045: }
046:
047: public void tearDown() throws Exception {
048: super .tearDown();
049: }
050:
051: public void testCircularStore() {
052: // prepare test
053: ojbChangeReferenceSetting(Person.class, "father", true,
054: CASCADE_OBJECT, CASCADE_OBJECT, false);
055: ojbChangeReferenceSetting(Person.class, "grandfather", true,
056: CASCADE_OBJECT, CASCADE_OBJECT, false);
057: ojbChangeReferenceSetting(Person.class, "childs", true,
058: CASCADE_OBJECT, CASCADE_OBJECT, false);
059: ojbChangeReferenceSetting(Person.class, "grandchilds", true,
060: CASCADE_OBJECT, CASCADE_OBJECT, false);
061:
062: String postfix = "_testCircularStore_"
063: + System.currentTimeMillis();
064: Person junior = createComplexFamily(postfix);
065: broker.beginTransaction();
066: broker.store(junior);
067: broker.commitTransaction();
068: Identity oidJunior = new Identity(junior, broker);
069: Identity oidSenior = new Identity(junior.getFather(), broker);
070: broker.clearCache();
071:
072: Criteria crit = new Criteria();
073: crit.addLike("name", "jeffChild_%" + postfix);
074: Query q = QueryFactory.newQuery(Person.class, crit);
075:
076: Person newJunior = (Person) broker
077: .getObjectByIdentity(oidJunior);
078: assertNotNull(newJunior);
079: assertNotNull(newJunior.getFather());
080: assertNotNull(newJunior.getChilds());
081: assertEquals(2, newJunior.getChilds().size());
082:
083: Person newSenior = (Person) broker
084: .getObjectByIdentity(oidSenior);
085: assertNotNull(newSenior);
086: assertNotNull(newSenior.getChilds());
087: assertEquals(1, newSenior.getChilds().size());
088: assertNotNull(newSenior.getGrandchilds());
089: assertEquals(2, newSenior.getGrandchilds().size());
090:
091: Collection result = broker.getCollectionByQuery(q);
092: assertEquals(2, result.size());
093: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
094: Person p = (Person) iterator.next();
095: assertNotNull(p.getFather());
096: assertEquals("jeffJunior" + postfix, p.getFather()
097: .getName());
098: assertNotNull(p.getGrandfather());
099: assertEquals("jeffSenior" + postfix, p.getGrandfather()
100: .getName());
101: }
102: }
103:
104: /**
105: * same as above but without clearing the cache
106: */
107: public void testCircularStore_2() {
108: // prepare test
109: ojbChangeReferenceSetting(Person.class, "father", true,
110: CASCADE_OBJECT, CASCADE_OBJECT, false);
111: ojbChangeReferenceSetting(Person.class, "grandfather", true,
112: CASCADE_OBJECT, CASCADE_OBJECT, false);
113: ojbChangeReferenceSetting(Person.class, "childs", true,
114: CASCADE_OBJECT, CASCADE_OBJECT, false);
115: ojbChangeReferenceSetting(Person.class, "grandchilds", true,
116: CASCADE_OBJECT, CASCADE_OBJECT, false);
117:
118: String postfix = "_testCircularStore_2_"
119: + System.currentTimeMillis();
120: Person junior = createComplexFamily(postfix);
121: broker.beginTransaction();
122: broker.store(junior);
123: broker.commitTransaction();
124: Identity oidJunior = new Identity(junior, broker);
125: Identity oidSenior = new Identity(junior.getFather(), broker);
126:
127: Criteria crit = new Criteria();
128: crit.addLike("name", "jeffChild_%" + postfix);
129: Query q = QueryFactory.newQuery(Person.class, crit);
130:
131: Person newJunior = (Person) broker
132: .getObjectByIdentity(oidJunior);
133: assertNotNull(newJunior);
134: assertNotNull(newJunior.getFather());
135: assertNotNull(newJunior.getChilds());
136: assertEquals(2, newJunior.getChilds().size());
137:
138: Person newSenior = (Person) broker
139: .getObjectByIdentity(oidSenior);
140: assertNotNull(newSenior);
141: assertNotNull(newSenior.getChilds());
142: assertEquals(1, newSenior.getChilds().size());
143: assertNotNull(newSenior.getGrandchilds());
144: assertEquals(2, newSenior.getGrandchilds().size());
145:
146: Collection result = broker.getCollectionByQuery(q);
147: assertEquals(2, result.size());
148: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
149: Person p = (Person) iterator.next();
150: assertNotNull(p.getFather());
151: assertEquals("jeffJunior" + postfix, p.getFather()
152: .getName());
153: assertNotNull(p.getGrandfather());
154: assertEquals("jeffSenior" + postfix, p.getGrandfather()
155: .getName());
156: }
157: }
158:
159: public void testCircularStoreUpdate() {
160: // prepare test
161: ojbChangeReferenceSetting(Person.class, "father", true,
162: CASCADE_OBJECT, CASCADE_OBJECT, false);
163: ojbChangeReferenceSetting(Person.class, "grandfather", true,
164: CASCADE_OBJECT, CASCADE_OBJECT, false);
165: ojbChangeReferenceSetting(Person.class, "childs", true,
166: CASCADE_OBJECT, CASCADE_OBJECT, false);
167: ojbChangeReferenceSetting(Person.class, "grandchilds", true,
168: CASCADE_OBJECT, CASCADE_OBJECT, false);
169:
170: String postfix = "_testCircularStore_"
171: + System.currentTimeMillis();
172: Person junior = createComplexFamily(postfix);
173: broker.beginTransaction();
174: broker.store(junior);
175: broker.commitTransaction();
176: Identity oidJunior = new Identity(junior, broker);
177: Identity oidSenior = new Identity(junior.getFather(), broker);
178: broker.clearCache();
179:
180: Criteria crit = new Criteria();
181: crit.addLike("name", "jeffChild_%" + postfix);
182: Query q = QueryFactory.newQuery(Person.class, crit);
183:
184: Person newJunior = (Person) broker
185: .getObjectByIdentity(oidJunior);
186: assertNotNull(newJunior);
187: assertNotNull(newJunior.getFather());
188: assertNotNull(newJunior.getChilds());
189: assertEquals(2, newJunior.getChilds().size());
190:
191: Person newSenior = (Person) broker
192: .getObjectByIdentity(oidSenior);
193: assertNotNull(newSenior);
194: assertNotNull(newSenior.getChilds());
195: assertEquals(1, newSenior.getChilds().size());
196: assertNotNull(newSenior.getGrandchilds());
197: assertEquals(2, newSenior.getGrandchilds().size());
198:
199: Collection result = broker.getCollectionByQuery(q);
200: assertEquals(2, result.size());
201: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
202: Person p = (Person) iterator.next();
203: assertNotNull(p.getFather());
204: assertEquals("jeffJunior" + postfix, p.getFather()
205: .getName());
206: assertNotNull(p.getGrandfather());
207: assertEquals("jeffSenior" + postfix, p.getGrandfather()
208: .getName());
209: }
210:
211: broker.beginTransaction();
212: Person newChild = new Person("newGrandChild" + postfix, null,
213: newSenior, null);
214: newSenior.addGranschild(newChild);
215: broker.store(newSenior);
216: broker.commitTransaction();
217: broker.clearCache();
218:
219: newSenior = (Person) broker.getObjectByIdentity(oidSenior);
220: assertNotNull(newSenior);
221: assertNotNull(newSenior.getChilds());
222: assertEquals(1, newSenior.getChilds().size());
223: assertNotNull(newSenior.getGrandchilds());
224: assertEquals(3, newSenior.getGrandchilds().size());
225: }
226:
227: /**
228: * same as above, but without clearing the cache
229: */
230: public void testCircularStoreUpdate_2() {
231: // prepare test
232: ojbChangeReferenceSetting(Person.class, "father", true,
233: CASCADE_OBJECT, CASCADE_OBJECT, false);
234: ojbChangeReferenceSetting(Person.class, "grandfather", true,
235: CASCADE_OBJECT, CASCADE_OBJECT, false);
236: ojbChangeReferenceSetting(Person.class, "childs", true,
237: CASCADE_OBJECT, CASCADE_OBJECT, false);
238: ojbChangeReferenceSetting(Person.class, "grandchilds", true,
239: CASCADE_OBJECT, CASCADE_OBJECT, false);
240:
241: String postfix = "_testCircularStore_2_"
242: + System.currentTimeMillis();
243: Person junior = createComplexFamily(postfix);
244: broker.beginTransaction();
245: broker.store(junior);
246: broker.commitTransaction();
247: Identity oidJunior = new Identity(junior, broker);
248: Identity oidSenior = new Identity(junior.getFather(), broker);
249:
250: Criteria crit = new Criteria();
251: crit.addLike("name", "jeffChild_%" + postfix);
252: Query q = QueryFactory.newQuery(Person.class, crit);
253:
254: Person newJunior = (Person) broker
255: .getObjectByIdentity(oidJunior);
256: assertNotNull(newJunior);
257: assertNotNull(newJunior.getFather());
258: assertNotNull(newJunior.getChilds());
259: assertEquals(2, newJunior.getChilds().size());
260:
261: Person newSenior = (Person) broker
262: .getObjectByIdentity(oidSenior);
263: assertNotNull(newSenior);
264: assertNotNull(newSenior.getChilds());
265: assertEquals(1, newSenior.getChilds().size());
266: assertNotNull(newSenior.getGrandchilds());
267: assertEquals(2, newSenior.getGrandchilds().size());
268:
269: Collection result = broker.getCollectionByQuery(q);
270: assertEquals(2, result.size());
271: for (Iterator iterator = result.iterator(); iterator.hasNext();) {
272: Person p = (Person) iterator.next();
273: assertNotNull(p.getFather());
274: assertEquals("jeffJunior" + postfix, p.getFather()
275: .getName());
276: assertNotNull(p.getGrandfather());
277: assertEquals("jeffSenior" + postfix, p.getGrandfather()
278: .getName());
279: }
280:
281: broker.beginTransaction();
282: Person newChild = new Person("newGrandChild" + postfix, null,
283: newSenior, null);
284: newSenior.addGranschild(newChild);
285: broker.store(newSenior);
286: broker.commitTransaction();
287:
288: newSenior = (Person) broker.getObjectByIdentity(oidSenior);
289: assertNotNull(newSenior);
290: assertNotNull(newSenior.getChilds());
291: assertEquals(1, newSenior.getChilds().size());
292: assertNotNull(newSenior.getGrandchilds());
293: assertEquals(3, newSenior.getGrandchilds().size());
294: }
295:
296: /**
297: * Creates an circular object hierarchy
298: */
299: private Person createComplexFamily(String postfix) {
300: Person jeffJunior = new Person();
301: jeffJunior.setName("jeffJunior" + postfix);
302:
303: Person jeffSenior = new Person();
304: jeffSenior.setName("jeffSenior" + postfix);
305:
306: Person jeffChild_1 = new Person();
307: jeffChild_1.setName("jeffChild_1" + postfix);
308: jeffChild_1.setFather(jeffJunior);
309: jeffChild_1.setGrandfather(jeffSenior);
310:
311: Person jeffChild_2 = new Person();
312: jeffChild_2.setName("jeffChild_2" + postfix);
313: jeffChild_2.setFather(jeffJunior);
314: jeffChild_2.setGrandfather(jeffSenior);
315:
316: jeffJunior.setFather(jeffSenior);
317: jeffJunior.addChild(jeffChild_1);
318: jeffJunior.addChild(jeffChild_2);
319:
320: jeffSenior.addChild(jeffJunior);
321: jeffSenior.addGranschild(jeffChild_1);
322: jeffSenior.addGranschild(jeffChild_2);
323:
324: return jeffJunior;
325: }
326:
327: public static class Person {
328: private Integer id;
329: private String name;
330: private Person father;
331: private Person grandfather;
332: private List childs;
333: private List grandchilds;
334: private Integer fkChild;
335: private Integer fkGrandchild;
336:
337: public Person() {
338: }
339:
340: public Person(String name, Person father, Person grandfather,
341: List childs) {
342: this .name = name;
343: this .father = father;
344: this .grandfather = grandfather;
345: this .childs = childs;
346: }
347:
348: public Integer getId() {
349: return id;
350: }
351:
352: public void setId(Integer id) {
353: this .id = id;
354: }
355:
356: public String getName() {
357: return name;
358: }
359:
360: public void setName(String name) {
361: this .name = name;
362: }
363:
364: public Person getFather() {
365: return father;
366: }
367:
368: public void setFather(Person father) {
369: this .father = father;
370: }
371:
372: public Person getGrandfather() {
373: return grandfather;
374: }
375:
376: public void setGrandfather(Person grandfather) {
377: this .grandfather = grandfather;
378: }
379:
380: public List getChilds() {
381: return childs;
382: }
383:
384: public void setChilds(List childs) {
385: this .childs = childs;
386: }
387:
388: public void addChild(Person child) {
389: if (childs == null) {
390: childs = new ArrayList();
391: }
392: childs.add(child);
393: }
394:
395: public List getGrandchilds() {
396: return grandchilds;
397: }
398:
399: public void setGrandchilds(List grandchilds) {
400: this .grandchilds = grandchilds;
401: }
402:
403: public void addGranschild(Person child) {
404: if (grandchilds == null) {
405: grandchilds = new ArrayList();
406: }
407: grandchilds.add(child);
408: }
409:
410: public Integer getFkChild() {
411: return fkChild;
412: }
413:
414: public void setFkChild(Integer fkChild) {
415: this .fkChild = fkChild;
416: }
417:
418: public Integer getFkGrandchild() {
419: return fkGrandchild;
420: }
421:
422: public void setFkGrandchild(Integer fkGrandchild) {
423: this.fkGrandchild = fkGrandchild;
424: }
425: }
426: }
|