001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.kernel;
020:
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.openjpa.persistence.kernel.common.apps.DependentFieldsPC;
026: import org.apache.openjpa.persistence.Extent;
027: import org.apache.openjpa.persistence.OpenJPAEntityManager;
028: import org.apache.openjpa.persistence.OpenJPAPersistence;
029:
030: public class TestDependentFields2 extends BaseKernelTest {
031:
032: private static final int COMMIT = 0;
033: private static final int ROLLBACK = 1;
034: private static final int PRESTORE = 2;
035:
036: private Object _root = null;
037: private Object _rel = null;
038: private Object _depRel = null;
039: private Object _deep = null;
040: private Object _coll = null;
041: private Object _depColl = null;
042: private Object _map = null;
043: private Object _depMap = null;
044: private Object _repeat = null;
045:
046: public TestDependentFields2(String casename) {
047: super (casename);
048: }
049:
050: public void setUp() throws Exception {
051: deleteAll(DependentFieldsPC.class);
052:
053: DependentFieldsPC root = new DependentFieldsPC();
054: root.setRelation(new DependentFieldsPC());
055: root.getList().add(new DependentFieldsPC());
056: root.getMap().put("key", new DependentFieldsPC());
057: root.setDependentRelation(new DependentFieldsPC());
058: root.getDependentRelation().setDependentRelation(
059: new DependentFieldsPC());
060: root.getDependentList().add(new DependentFieldsPC());
061: root.getDependentMap().put("key", new DependentFieldsPC());
062:
063: DependentFieldsPC repeat = new DependentFieldsPC();
064: root.getDependentList().add(repeat);
065: root.getDependentMap().put("repeat", repeat);
066:
067: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
068: startTx(pm);
069: pm.persist(root);
070: endTx(pm);
071:
072: _root = pm.getObjectId(root);
073: assertNotNull(_root);
074: _rel = pm.getObjectId(root.getRelation());
075: _depRel = pm.getObjectId(root.getDependentRelation());
076: _deep = pm.getObjectId(root.getDependentRelation()
077: .getDependentRelation());
078: _coll = pm.getObjectId(root.getList().iterator().next());
079: Iterator itr = root.getDependentList().iterator();
080: _depColl = pm.getObjectId(itr.next());
081: _repeat = pm.getObjectId(itr.next());
082: _map = pm.getObjectId(root.getMap().get("key"));
083: _depMap = pm.getObjectId(root.getDependentMap().get("key"));
084:
085: endEm(pm);
086: }
087:
088: public void testDependentFieldsLoaded() {
089: delete(true, COMMIT);
090: checkFields();
091: }
092:
093: public void testDependentFieldsLoadedWithRollback() {
094: delete(true, ROLLBACK);
095: checkFields();
096: }
097:
098: public void testDependentFieldsLoadedWithPreStore() {
099: delete(true, PRESTORE);
100: checkFields();
101: }
102:
103: public void testDependentFieldsUnloaded() {
104: delete(false, COMMIT);
105: checkFields();
106: }
107:
108: public void testDependentFieldsUnloadedWithRollback() {
109: delete(false, ROLLBACK);
110: checkFields();
111: }
112:
113: public void testDependentFieldsUnloadedWithPreStore() {
114: delete(false, PRESTORE);
115: checkFields();
116: }
117:
118: private void delete(boolean load, int action) {
119: OpenJPAEntityManager pm = getPM(true, true);
120: DependentFieldsPC root;
121: Object rel = null;
122: Object depRel = null;
123: Object coll = null;
124: Object depColl = null;
125: Object map = null;
126: Object depMap = null;
127: Object repeat = null;
128: Object deep = null;
129: while (true) {
130: startTx(pm);
131: root = (DependentFieldsPC) pm.find(DependentFieldsPC.class,
132: _root);
133: if (load) {
134: rel = root.getRelation();
135: assertNotNull(rel);
136: depRel = root.getDependentRelation();
137: assertNotNull(depRel);
138: deep = ((DependentFieldsPC) depRel)
139: .getDependentRelation();
140: assertNotNull(deep);
141: coll = root.getList().iterator().next();
142: assertNotNull(coll);
143: Iterator itr = root.getDependentList().iterator();
144: depColl = itr.next();
145: repeat = itr.next();
146: assertNotNull(depColl);
147: assertNotNull(repeat);
148: map = root.getMap().get("key");
149: assertNotNull(map);
150: depMap = root.getDependentMap().get("key");
151: assertNotNull(depMap);
152:
153: // pcl: test both depColl and repeat, since they might
154: // have been out of order above.
155: Object o = root.getDependentMap().get("repeat");
156: if (o != repeat)
157: fail("dependent map does not contain 'repeat'");
158: }
159: pm.remove(root);
160:
161: if (action == ROLLBACK) {
162: rollbackTx(pm);
163: action = COMMIT;
164: } else if (action == COMMIT) {
165: endTx(pm);
166: break;
167: } else {
168: pm.preFlush();
169: break;
170: }
171: }
172:
173: if (load) {
174: if (action == PRESTORE) {
175: assertFalse(pm.isRemoved(rel));
176: assertFalse(pm.isRemoved(coll));
177: assertFalse(pm.isRemoved(map));
178: assertTrue(pm.isRemoved(depRel));
179: assertTrue(pm.isRemoved(deep));
180: assertTrue(pm.isRemoved(depColl));
181: assertTrue(pm.isRemoved(depMap));
182: assertTrue(pm.isRemoved(repeat));
183: } else {
184: assertNotNull(OpenJPAPersistence.getEntityManager(rel));
185: assertNotNull(OpenJPAPersistence.getEntityManager(coll));
186: assertNotNull(OpenJPAPersistence.getEntityManager(map));
187: assertNull(OpenJPAPersistence.getEntityManager(depRel));
188: assertNull(OpenJPAPersistence.getEntityManager(deep));
189: assertNull(OpenJPAPersistence.getEntityManager(depColl));
190: assertNull(OpenJPAPersistence.getEntityManager(depMap));
191: assertNull(OpenJPAPersistence.getEntityManager(repeat));
192: }
193: }
194:
195: if (action == PRESTORE)
196: endTx(pm);
197: endEm(pm);
198: }
199:
200: private void checkFields() {
201: OpenJPAEntityManager pm = getPM(true, true);
202: assertNotNull(pm.find(DependentFieldsPC.class, _rel));
203: assertNotNull(pm.find(DependentFieldsPC.class, _coll));
204: assertNotNull(pm.find(DependentFieldsPC.class, _map));
205: assertNull(pm.find(DependentFieldsPC.class, _depRel));
206: assertNull(pm.find(DependentFieldsPC.class, _deep));
207: assertNull(pm.find(DependentFieldsPC.class, _depColl));
208: assertNull(pm.find(DependentFieldsPC.class, _depMap));
209:
210: endEm(pm);
211: }
212:
213: public void testNullDeletesDependent() {
214: nullDeletesDependent(COMMIT);
215: }
216:
217: public void testNullDeletesDependentWithRollback() {
218: nullDeletesDependent(ROLLBACK);
219: }
220:
221: public void testNullDeletesDependentWithPreStore() {
222: nullDeletesDependent(PRESTORE);
223: }
224:
225: private void nullDeletesDependent(int action) {
226: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
227: DependentFieldsPC pc;
228: DependentFieldsPC depRel;
229: while (true) {
230: startTx(pm);
231: pc = (DependentFieldsPC) pm.find(DependentFieldsPC.class,
232: _root);
233: depRel = pc.getDependentRelation();
234: assertEquals(_depRel, pm.getObjectId(depRel));
235: pc.setDependentRelation(null);
236: if (action == ROLLBACK) {
237: rollbackTx(pm);
238: action = COMMIT;
239: } else if (action == COMMIT) {
240: endTx(pm);
241: break;
242: } else {
243: pm.preFlush();
244: break;
245: }
246: }
247:
248: if (action == PRESTORE) {
249: assertTrue(pm.isRemoved(depRel));
250: endTx(pm);
251: }
252:
253: assertTrue(!pm.isPersistent(depRel));
254: assertNull(pm.find(DependentFieldsPC.class, _depRel));
255: endEm(pm);
256: }
257:
258: public void testRemoveDeletesDependent() {
259: removeDeletesDependent(COMMIT);
260: }
261:
262: public void testRemoveDeletesDependentWithRollback() {
263: removeDeletesDependent(ROLLBACK);
264: }
265:
266: public void testRemoveDeletesDependentWithPreStore() {
267: removeDeletesDependent(PRESTORE);
268: }
269:
270: private void removeDeletesDependent(int action) {
271: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
272: DependentFieldsPC pc;
273: DependentFieldsPC depColl;
274: DependentFieldsPC depMap;
275: List list;
276: Map map;
277: while (true) {
278: startTx(pm);
279: pc = (DependentFieldsPC) pm.find(DependentFieldsPC.class,
280: _root);
281: list = pc.getDependentList();
282: assertEquals("list size =! 2", 2, list.size());
283: depColl = (DependentFieldsPC) list.remove(0);
284: assertEquals("_depColl is not pm.getObjectId(depColl)",
285: _depColl, pm.getObjectId(depColl));
286:
287: map = pc.getDependentMap();
288: assertEquals("map size =! 2", 2, map.size());
289: depMap = (DependentFieldsPC) map.remove("key");
290: assertEquals("_depMap is not pm.getObjectId(depMap)",
291: _depMap, pm.getObjectId(depMap));
292:
293: if (action == ROLLBACK) {
294: rollbackTx(pm);
295: action = COMMIT;
296: } else if (action == COMMIT) {
297: endTx(pm);
298: break;
299: } else {
300: pm.preFlush();
301: break;
302: }
303: }
304:
305: if (action == PRESTORE) {
306: assertTrue(pm.isRemoved(depColl));
307: assertTrue(pm.isRemoved(depMap));
308: endTx(pm);
309: }
310:
311: // assertTrue("depcoll is persistence", !pm.isPersistent(depColl));
312: assertNull(pm.find(DependentFieldsPC.class, _depColl));
313:
314: // assertTrue("depMap is persistence", !pm.isPersistent(depMap));
315: assertNull(pm.find(DependentFieldsPC.class, _depMap));
316:
317: assertNotNull("repeat is null", pm.find(
318: DependentFieldsPC.class, _repeat));
319: endEm(pm);
320: }
321:
322: public void testMoveDependentInContainer() {
323: moveDependentInContainer(COMMIT);
324: }
325:
326: public void testMoveDependentInContainerWithRollback() {
327: moveDependentInContainer(ROLLBACK);
328: }
329:
330: public void testMoveDependentInContainerWithPreStore() {
331: moveDependentInContainer(PRESTORE);
332: }
333:
334: private void moveDependentInContainer(int action) {
335: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
336: DependentFieldsPC pc;
337: DependentFieldsPC depColl;
338: DependentFieldsPC depMap;
339: List list;
340: Map map;
341: while (true) {
342: startTx(pm);
343: pc = (DependentFieldsPC) pm.find(DependentFieldsPC.class,
344: _root);
345: list = pc.getDependentList();
346: assertEquals(2, list.size());
347: depColl = (DependentFieldsPC) list.get(0);
348: assertEquals(_depColl, pm.getObjectId(depColl));
349: list.remove(0);
350: list.add(depColl);
351:
352: map = pc.getDependentMap();
353: assertEquals(2, map.size());
354: depMap = (DependentFieldsPC) map.get("key");
355: assertEquals(_depMap, pm.getObjectId(depMap));
356: map.remove("key");
357: map.put("newkey", depMap);
358:
359: if (action == ROLLBACK) {
360: rollbackTx(pm);
361: action = COMMIT;
362: } else if (action == COMMIT) {
363: endTx(pm);
364: break;
365: } else {
366: pm.preFlush();
367: break;
368: }
369: }
370:
371: if (action == PRESTORE) {
372: assertFalse(pm.isRemoved(depColl));
373: assertFalse(pm.isRemoved(depMap));
374: endTx(pm);
375: }
376:
377: assertTrue(pm.isPersistent(depColl));
378: assertNotNull(pm.find(DependentFieldsPC.class, _depColl));
379: assertTrue(pm.isPersistent(depMap));
380: assertNotNull(pm.find(DependentFieldsPC.class, _depMap));
381: assertNotNull(pm.find(DependentFieldsPC.class, _repeat));
382: endEm(pm);
383: }
384:
385: public void testRefedDependentNotDeleted() {
386: refedDependentNotDeleted(COMMIT);
387: }
388:
389: public void testRefedDependentNotDeletedWithRollback() {
390: refedDependentNotDeleted(ROLLBACK);
391: }
392:
393: public void testRefedDependentNotDeletedWithPreStore() {
394: refedDependentNotDeleted(PRESTORE);
395: }
396:
397: private void refedDependentNotDeleted(int action) {
398: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
399: DependentFieldsPC pc;
400: DependentFieldsPC newPC = null;
401: DependentFieldsPC depRel;
402: while (true) {
403: startTx(pm);
404: pc = (DependentFieldsPC) pm.find(DependentFieldsPC.class,
405: _root);
406: if (newPC == null)
407: newPC = new DependentFieldsPC();
408: depRel = pc.getDependentRelation();
409: newPC.setDependentRelation(depRel);
410: pc.setDependentRelation(null);
411: pm.persist(newPC);
412:
413: if (action == ROLLBACK) {
414: rollbackTx(pm);
415: action = COMMIT;
416: } else if (action == COMMIT) {
417: endTx(pm);
418: break;
419: } else {
420: pm.preFlush();
421: break;
422: }
423: }
424:
425: if (action == PRESTORE) {
426: assertFalse(pm.isRemoved(depRel));
427: endTx(pm);
428: }
429:
430: assertTrue(pm.isPersistent(depRel));
431: assertNotNull(pm.find(DependentFieldsPC.class, _depRel));
432: endEm(pm);
433: }
434:
435: public void testNullSharedDependent() {
436: nullSharedDependent(COMMIT);
437: }
438:
439: public void testNullSharedDependentWithRollback() {
440: nullSharedDependent(ROLLBACK);
441: }
442:
443: public void testNullSharedDependentWithPreStore() {
444: nullSharedDependent(PRESTORE);
445: }
446:
447: private void nullSharedDependent(int action) {
448: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
449: DependentFieldsPC pc;
450: DependentFieldsPC repeat;
451: List list;
452: Map map;
453: while (true) {
454: startTx(pm);
455: pc = (DependentFieldsPC) pm.find(DependentFieldsPC.class,
456: _root);
457: list = pc.getDependentList();
458: assertEquals(2, list.size());
459: repeat = (DependentFieldsPC) list.get(1);
460: assertEquals(_repeat, pm.getObjectId(repeat));
461: list.remove(1);
462:
463: map = pc.getDependentMap();
464: assertEquals(2, map.size());
465: assertEquals(repeat, (DependentFieldsPC) map
466: .remove("repeat"));
467:
468: if (action == PRESTORE)
469: pm.preFlush();
470: else
471: pm.flush();
472: assertTrue(pm.isRemoved(repeat));
473:
474: // now after deleting on flush, assigning to another field and
475: // attempting to commit should throw an error -- can't undelete an
476: // object
477: pc.getList().add(repeat);
478:
479: if (action == ROLLBACK) {
480: rollbackTx(pm);
481: action = COMMIT;
482: } else {
483: try {
484: pm.getTransaction().commit();
485: fail("Committed with ref to deleted dependent object");
486: } catch (Exception je) {
487: rollbackTx(pm);
488: } finally {
489: }
490: break;
491: }
492: }
493:
494: endEm(pm);
495: }
496:
497: public void testClearMappedDependentOfDetached() {
498: clearDependentOfDetachedTest(true);
499: }
500:
501: public void testClearInverseKeyDependentOfDetached() {
502: clearDependentOfDetachedTest(false);
503: }
504:
505: private void clearDependentOfDetachedTest(boolean mapped) {
506: deleteAll(DependentFieldsPC.class);
507:
508: DependentFieldsPC owner = new DependentFieldsPC();
509: for (int i = 0; i < 2; i++) {
510: DependentFieldsPC child = new DependentFieldsPC();
511: if (mapped) {
512: owner.getDependentMappedList().add(child);
513: child.setOwner(owner);
514: } else
515: owner.getDependentInverseKeyList().add(child);
516: }
517:
518: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
519: startTx(pm);
520: pm.persist(owner);
521: endTx(pm);
522: Object oid = pm.getObjectId(owner);
523: assertEquals(3, ((Extent) pm.createExtent(
524: DependentFieldsPC.class, true)).list().size());
525: endEm(pm);
526:
527: pm = (OpenJPAEntityManager) currentEntityManager();
528: owner = (DependentFieldsPC) pm.find(DependentFieldsPC.class,
529: oid);
530: if (mapped)
531: assertEquals(2, owner.getDependentMappedList().size());
532: else
533: assertEquals(2, owner.getDependentInverseKeyList().size());
534: DependentFieldsPC detached = (DependentFieldsPC) pm
535: .detach(owner);
536: endEm(pm);
537:
538: if (mapped) {
539: assertEquals(2, detached.getDependentMappedList().size());
540: detached.getDependentMappedList().clear();
541: } else {
542: assertEquals(2, detached.getDependentInverseKeyList()
543: .size());
544: detached.getDependentInverseKeyList().clear();
545: }
546:
547: pm = (OpenJPAEntityManager) currentEntityManager();
548: startTx(pm);
549: owner = (DependentFieldsPC) pm.merge(detached);
550: if (mapped)
551: assertEquals(0, owner.getDependentMappedList().size());
552: else
553: assertEquals(0, owner.getDependentInverseKeyList().size());
554: endTx(pm);
555: assertEquals(1, ((Extent) pm.createExtent(
556: DependentFieldsPC.class, true)).list().size());
557: endEm(pm);
558:
559: pm = (OpenJPAEntityManager) currentEntityManager();
560: owner = (DependentFieldsPC) pm.find(DependentFieldsPC.class,
561: oid);
562: if (mapped)
563: assertEquals(0, owner.getDependentMappedList().size());
564: else
565: assertEquals(0, owner.getDependentInverseKeyList().size());
566: endEm(pm);
567: }
568: }
|