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.concurrency;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.ext.*;
026: import com.db4o.query.*;
027:
028: import db4ounit.*;
029: import db4ounit.extensions.*;
030:
031: public class CascadeDeleteDeletedTestCase extends
032: Db4oClientServerTestCase {
033:
034: public static class Item {
035: public Item(String name) {
036: this .name = name;
037: }
038:
039: public String name;
040:
041: public Object untypedMember;
042:
043: public CddMember typedMember;
044: }
045:
046: public static void main(String[] args) {
047: new CascadeDeleteDeletedTestCase().runConcurrency();
048: }
049:
050: protected void db4oSetupBeforeStore() throws Exception {
051: configureThreadCount(10);
052: }
053:
054: protected void configure(Configuration config) {
055: config.objectClass(Item.class).cascadeOnDelete(true);
056: }
057:
058: protected void store() {
059: ExtObjectContainer oc = db();
060: membersFirst(oc, "membersFirst commit");
061: membersFirst(oc, "membersFirst");
062: twoRef(oc, "twoRef");
063: twoRef(oc, "twoRef commit");
064: twoRef(oc, "twoRef delete");
065: twoRef(oc, "twoRef delete commit");
066: }
067:
068: private void membersFirst(ExtObjectContainer oc, String name) {
069: Item item = new Item(name);
070: item.untypedMember = new CddMember();
071: item.typedMember = new CddMember();
072: oc.set(item);
073: }
074:
075: private void twoRef(ExtObjectContainer oc, String name) {
076: Item item1 = new Item(name);
077: item1.untypedMember = new CddMember();
078: item1.typedMember = new CddMember();
079: Item item2 = new Item(name);
080: item2.untypedMember = item1.untypedMember;
081: item2.typedMember = item1.typedMember;
082: oc.set(item1);
083: oc.set(item2);
084: }
085:
086: public void conc(ExtObjectContainer oc, int seq) {
087: if (seq == 0) {
088: tMembersFirst(oc, "membersFirst commit");
089: } else if (seq == 1) {
090: tMembersFirst(oc, "membersFirst");
091: } else if (seq == 2) {
092: tTwoRef(oc, "twoRef");
093: } else if (seq == 3) {
094: tTwoRef(oc, "twoRef commit");
095: } else if (seq == 4) {
096: tTwoRef(oc, "twoRef delete");
097: } else if (seq == 5) {
098: tTwoRef(oc, "twoRef delete commit");
099: }
100: }
101:
102: public void check(ExtObjectContainer oc) {
103: Assert.areEqual(0, countOccurences(oc, CddMember.class));
104: }
105:
106: private void tMembersFirst(ExtObjectContainer oc, String name) {
107: boolean commit = name.indexOf("commit") > 1;
108: Query q = oc.query();
109: q.constrain(Item.class);
110: q.descend("name").constrain(name);
111: ObjectSet objectSet = q.execute();
112: Item cdd = (Item) objectSet.next();
113: oc.delete(cdd.untypedMember);
114: oc.delete(cdd.typedMember);
115: if (commit) {
116: oc.commit();
117: }
118: oc.delete(cdd);
119: if (!commit) {
120: oc.commit();
121: }
122: }
123:
124: private void tTwoRef(ExtObjectContainer oc, String name) {
125: boolean commit = name.indexOf("commit") > 1;
126: boolean delete = name.indexOf("delete") > 1;
127:
128: Query q = oc.query();
129: q.constrain(Item.class);
130: q.descend("name").constrain(name);
131: ObjectSet objectSet = q.execute();
132: Item item1 = (Item) objectSet.next();
133: Item item2 = (Item) objectSet.next();
134: if (delete) {
135: oc.delete(item1.untypedMember);
136: oc.delete(item1.typedMember);
137: }
138: oc.delete(item1);
139: if (commit) {
140: oc.commit();
141: }
142: oc.delete(item2);
143: if (!commit) {
144: oc.commit();
145: }
146: }
147:
148: public static class CddMember {
149: public String name;
150: }
151:
152: }
|