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.assorted;
022:
023: import com.db4o.config.*;
024: import com.db4o.ext.*;
025:
026: import db4ounit.extensions.*;
027:
028: public class CascadeDeleteDeletedTestCase extends
029: Db4oClientServerTestCase {
030:
031: public String name;
032:
033: public Object untypedMember;
034:
035: public CddMember typedMember;
036:
037: public static void main(String[] args) {
038: new CascadeDeleteDeletedTestCase().runClientServer();
039: }
040:
041: public CascadeDeleteDeletedTestCase() {
042: }
043:
044: public CascadeDeleteDeletedTestCase(String name) {
045: this .name = name;
046: }
047:
048: protected void configure(Configuration config) {
049: config.objectClass(this ).cascadeOnDelete(true);
050: }
051:
052: protected void store() {
053: ExtObjectContainer oc = db();
054: membersFirst(oc, "membersFirst commit");
055: membersFirst(oc, "membersFirst");
056: twoRef(oc, "twoRef");
057: twoRef(oc, "twoRef commit");
058: twoRef(oc, "twoRef delete");
059: twoRef(oc, "twoRef delete commit");
060: }
061:
062: private void membersFirst(ExtObjectContainer oc, String name) {
063: CascadeDeleteDeletedTestCase cdd = new CascadeDeleteDeletedTestCase(
064: name);
065: cdd.untypedMember = new CddMember();
066: cdd.typedMember = new CddMember();
067: oc.set(cdd);
068: }
069:
070: private void twoRef(ExtObjectContainer oc, String name) {
071: CascadeDeleteDeletedTestCase cdd = new CascadeDeleteDeletedTestCase(
072: name);
073: cdd.untypedMember = new CddMember();
074: cdd.typedMember = new CddMember();
075: CascadeDeleteDeletedTestCase cdd2 = new CascadeDeleteDeletedTestCase(
076: name);
077: cdd2.untypedMember = cdd.untypedMember;
078: cdd2.typedMember = cdd.typedMember;
079: oc.set(cdd);
080: oc.set(cdd2);
081: }
082:
083: public void _testDeleteDeleted() throws Exception {
084: int total = 10;
085: final int CDD_MEMBER_COUNT = 12;
086: ExtObjectContainer[] containers = new ExtObjectContainer[total];
087: ExtObjectContainer oc = null;
088: try {
089: for (int i = 0; i < total; i++) {
090: containers[i] = openNewClient();
091: assertOccurrences(containers[i], CddMember.class,
092: CDD_MEMBER_COUNT);
093: }
094: for (int i = 0; i < total; i++) {
095: deleteAll(containers[i], CddMember.class);
096: }
097: oc = openNewClient();
098: assertOccurrences(oc, CddMember.class, CDD_MEMBER_COUNT);
099: // ocs[0] deleted all CddMember objects, and committed the change
100: containers[0].commit();
101: containers[0].close();
102: // FIXME: following assertion fails
103: assertOccurrences(oc, CddMember.class, 0);
104: for (int i = 1; i < total; i++) {
105: containers[i].close();
106: }
107: assertOccurrences(oc, CddMember.class, 0);
108: } finally {
109: if (oc != null) {
110: oc.close();
111: }
112: for (int i = 0; i < total; i++) {
113: if (containers[i] != null) {
114: containers[i].close();
115: }
116: }
117: }
118:
119: }
120:
121: /*
122: private void tMembersFirst(ExtObjectContainer oc, String name) {
123: boolean commit = name.indexOf("commit") > 1;
124: Query q = oc.query();
125: q.constrain(CascadeDeleteDeletedTestCase.class);
126: q.descend("name").constrain(name);
127: ObjectSet objectSet = q.execute();
128: CascadeDeleteDeletedTestCase cdd = (CascadeDeleteDeletedTestCase) objectSet.next();
129: oc.delete(cdd.untypedMember);
130: oc.delete(cdd.typedMember);
131: if (commit) {
132: oc.commit();
133: }
134: oc.delete(cdd);
135: if (!commit) {
136: oc.commit();
137: }
138: }
139:
140: private void tTwoRef(ExtObjectContainer oc, String name) {
141: boolean commit = name.indexOf("commit") > 1;
142: boolean delete = name.indexOf("delete") > 1;
143:
144: Query q = oc.query();
145: q.constrain(this.getClass());
146: q.descend("name").constrain(name);
147: ObjectSet objectSet = q.execute();
148: CascadeDeleteDeletedTestCase cdd = (CascadeDeleteDeletedTestCase) objectSet.next();
149: CascadeDeleteDeletedTestCase cdd2 = (CascadeDeleteDeletedTestCase) objectSet.next();
150: if (delete) {
151: oc.delete(cdd.untypedMember);
152: oc.delete(cdd.typedMember);
153: }
154: oc.delete(cdd);
155: if (commit) {
156: oc.commit();
157: }
158: oc.delete(cdd2);
159: if (!commit) {
160: oc.commit();
161: }
162: }
163: */
164: public static class CddMember {
165: public String name;
166: }
167:
168: }
|