001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.querying;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.query.*;
026:
027: import db4ounit.*;
028: import db4ounit.extensions.*;
029:
030: public class CascadeDeleteDeleted extends AbstractDb4oTestCase {
031:
032: public static class CddMember {
033: public String name;
034: }
035:
036: public String name;
037:
038: public Object untypedMember;
039:
040: public CddMember typedMember;
041:
042: public CascadeDeleteDeleted() {
043: }
044:
045: public CascadeDeleteDeleted(String name) {
046: this .name = name;
047: }
048:
049: protected void configure(Configuration config) {
050: config.objectClass(this ).cascadeOnDelete(true);
051: }
052:
053: protected void store() {
054: membersFirst("membersFirst commit");
055: membersFirst("membersFirst");
056:
057: twoRef("twoRef");
058: twoRef("twoRef commit");
059: twoRef("twoRef delete");
060: twoRef("twoRef delete commit");
061: }
062:
063: private void membersFirst(String name) {
064: CascadeDeleteDeleted cdd = new CascadeDeleteDeleted(name);
065: cdd.untypedMember = new CddMember();
066: cdd.typedMember = new CddMember();
067: db().set(cdd);
068: }
069:
070: private void twoRef(String name) {
071: CascadeDeleteDeleted cdd = new CascadeDeleteDeleted(name);
072: cdd.untypedMember = new CddMember();
073: cdd.typedMember = new CddMember();
074: CascadeDeleteDeleted cdd2 = new CascadeDeleteDeleted(name);
075: cdd2.untypedMember = cdd.untypedMember;
076: cdd2.typedMember = cdd.typedMember;
077: db().set(cdd);
078: db().set(cdd2);
079:
080: }
081:
082: public void test() {
083: tMembersFirst("membersFirst commit");
084: tMembersFirst("membersFirst");
085: tTwoRef("twoRef");
086: tTwoRef("twoRef commit");
087: tTwoRef("twoRef delete");
088: tTwoRef("twoRef delete commit");
089: Assert.areEqual(0, countOccurences(CddMember.class));
090: }
091:
092: private void tMembersFirst(String name) {
093: boolean commit = name.indexOf("commit") > 1;
094:
095: Query q = newQuery(this .getClass());
096: q.descend("name").constrain(name);
097: ObjectSet objectSet = q.execute();
098: CascadeDeleteDeleted cdd = (CascadeDeleteDeleted) objectSet
099: .next();
100: db().delete(cdd.untypedMember);
101: db().delete(cdd.typedMember);
102: if (commit) {
103: db().commit();
104: }
105: db().delete(cdd);
106: if (!commit) {
107: db().commit();
108: }
109: }
110:
111: private void tTwoRef(String name) {
112: boolean commit = name.indexOf("commit") > 1;
113: boolean delete = name.indexOf("delete") > 1;
114: Query q = newQuery(this .getClass());
115: q.descend("name").constrain(name);
116: ObjectSet objectSet = q.execute();
117: CascadeDeleteDeleted cdd = (CascadeDeleteDeleted) objectSet
118: .next();
119: CascadeDeleteDeleted cdd2 = (CascadeDeleteDeleted) objectSet
120: .next();
121: if (delete) {
122: db().delete(cdd.untypedMember);
123: db().delete(cdd.typedMember);
124: }
125: db().delete(cdd);
126: if (commit) {
127: db().commit();
128: }
129: db().delete(cdd2);
130: if (!commit) {
131: db().commit();
132: }
133: }
134: }
|