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.jre11.events;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.events.*;
026: import com.db4o.foundation.*;
027: import com.db4o.internal.*;
028: import com.db4o.internal.btree.*;
029: import com.db4o.internal.classindex.*;
030: import com.db4o.query.*;
031:
032: import db4ounit.*;
033: import db4ounit.extensions.*;
034:
035: public class SelectiveCascadingDeleteTestCase extends
036: AbstractDb4oTestCase {
037:
038: protected void configure(Configuration config) {
039: enableCascadeOnDelete(config);
040: }
041:
042: protected void store() {
043: Item c = new Item("C", null);
044: Item b = new Item("B", c);
045: Item a = new Item("A", b);
046: db().set(a);
047: }
048:
049: public void testPreventMiddleObjectDeletion() throws Exception {
050:
051: Assert.areEqual(3, queryItems().size());
052:
053: serverEventRegistry().deleting().addListener(
054: new EventListener4() {
055: public void onEvent(Event4 e, EventArgs args) {
056: CancellableObjectEventArgs a = (CancellableObjectEventArgs) args;
057:
058: // TODO: Not really nice. Internal abstract class Transaction
059: // is exposed. We should have an outside Transaction interface.
060: Transaction trans = (Transaction) a
061: .transaction();
062:
063: ObjectContainer container = trans
064: .objectContainer();
065:
066: Item item = ((Item) a.object());
067: container.activate(item, 1);
068: if (item.id.equals("B")) {
069: // cancel deletion of this item
070: a.cancel();
071:
072: // restart from the child
073: container.delete(item.child);
074:
075: // and disconnect it
076: item.child = null;
077: container.set(item);
078: }
079: }
080: });
081:
082: Item a = queryItem("A");
083: Assert.isNotNull(a);
084:
085: assertIndexCount(3);
086:
087: db().delete(a);
088:
089: db().commit();
090:
091: reopen();
092:
093: a = queryItem("A");
094: Assert.isNull(a);
095: assertIndexCount(1);
096:
097: ObjectSet found = queryItems();
098: Assert.areEqual(1, found.size());
099: final Item remainingItem = ((Item) found.next());
100: Assert.areEqual("B", remainingItem.id);
101: Assert.isNull(remainingItem.child);
102: }
103:
104: private ObjectSet queryItems() {
105: return createItemQuery().execute();
106: }
107:
108: private Query createItemQuery() {
109: Query q = db().query();
110: q.constrain(Item.class);
111: return q;
112: }
113:
114: private void enableCascadeOnDelete(Configuration config) {
115: config.objectClass(Item.class).cascadeOnDelete(true);
116: }
117:
118: private Item queryItem(final String id) {
119: Query q = createItemQuery();
120: q.descend("id").constrain(id);
121: ObjectSet result = q.execute();
122: return result.hasNext() ? (Item) result.next() : null;
123: }
124:
125: private void assertIndexCount(int expectedCount) {
126: final int[] sum = { 0 };
127: Visitor4 visitor = new Visitor4() {
128: public void visit(Object obj) {
129: sum[0]++;
130: }
131: };
132: btree()
133: .traverseKeys(fileSession().systemTransaction(),
134: visitor);
135: Assert.areEqual(expectedCount, sum[0]);
136: }
137:
138: private BTree btree() {
139: return BTreeClassIndexStrategy.btree(yapClass());
140: }
141:
142: private ClassMetadata yapClass() {
143: ClassMetadata clazz = fileSession()
144: .classMetadataForReflectClass(
145: reflector().forClass(Item.class));
146: return clazz;
147: }
148:
149: public static void main(String[] args) {
150: new SelectiveCascadingDeleteTestCase().runClientServer();
151: }
152: }
|