KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > jre11 > events > SelectiveCascadingDeleteTestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.jre11.events;
22
23 import com.db4o.*;
24 import com.db4o.config.*;
25 import com.db4o.events.*;
26 import com.db4o.foundation.*;
27 import com.db4o.inside.btree.*;
28 import com.db4o.inside.classindex.*;
29 import com.db4o.query.*;
30
31 import db4ounit.*;
32 import db4ounit.extensions.*;
33
34
35 public class SelectiveCascadingDeleteTestCase extends AbstractDb4oTestCase {
36     
37     protected void configure(Configuration config) {
38         enableCascadeOnDelete(config);
39     }
40     
41     protected void store() {
42         Item c = new Item("C", null);
43         Item b = new Item("B", c);
44         Item a = new Item("A", b);
45         db().set(a);
46     }
47     
48     public void testPreventMiddleObjectDeletion() throws Exception JavaDoc {
49         
50         Assert.areEqual(3, queryItems().size());
51         
52         eventRegistry().deleting().addListener(new EventListener4() {
53             public void onEvent(Event4 e, EventArgs args) {
54                 CancellableObjectEventArgs a = (CancellableObjectEventArgs)args;
55                 Item item = ((Item)a.object());
56                 fileSession().activate(item, 1);
57                 if (item.id.equals("B")) {
58                     // cancel deletion of this item
59
a.cancel();
60                     
61                     // restart from the child
62
fileSession().delete(item.child);
63                     
64                     // and disconnect it
65
item.child = null;
66                     fileSession().set(item);
67                 }
68             }
69         });
70
71         Item a = queryItem("A");
72         Assert.isNotNull(a);
73
74         assertIndexCount(3);
75
76         db().delete(a);
77         
78         db().commit();
79
80         reopen();
81         
82         a = queryItem("A");
83         Assert.isNull(a);
84         assertIndexCount(1);
85         
86         ObjectSet found = queryItems();
87         Assert.areEqual(1, found.size());
88         final Item remainingItem = ((Item)found.next());
89         Assert.areEqual("B", remainingItem.id);
90         Assert.isNull(remainingItem.child);
91     }
92
93     private ObjectSet queryItems() {
94         return createItemQuery().execute();
95     }
96
97     private Query createItemQuery() {
98         Query q = db().query();
99         q.constrain(Item.class);
100         return q;
101     }
102
103     private EventRegistry eventRegistry() {
104         return EventRegistryFactory.forObjectContainer(fileSession());
105     }
106
107     private void enableCascadeOnDelete(Configuration config) {
108         config.objectClass(Item.class).cascadeOnDelete(true);
109     }
110
111     private Item queryItem(final String JavaDoc id) {
112         Query q = createItemQuery();
113         q.descend("id").constrain(id);
114         ObjectSet result = q.execute();
115         return result.hasNext() ? (Item)result.next() : null;
116     }
117     
118     private void assertIndexCount(int expectedCount) {
119         final int[] sum={0};
120         Visitor4 visitor = new Visitor4() {
121             public void visit(Object JavaDoc obj) {
122                 sum[0]++;
123             }
124         };
125         btree().traverseKeys(fileSession().getSystemTransaction(),visitor);
126         Assert.areEqual(expectedCount,sum[0]);
127     }
128
129     private BTree btree() {
130         return BTreeClassIndexStrategy.btree(yapClass());
131     }
132
133     private YapClass yapClass() {
134         YapClass clazz = fileSession().getYapClass(reflector().forClass(Item.class));
135         return clazz;
136     }
137
138     public static void main(String JavaDoc[] args) {
139         new SelectiveCascadingDeleteTestCase().runClientServer();
140     }
141 }
142
Popular Tags