KickJava   Java API By Example, From Geeks To Geeks.

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


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.query.Query;
27
28 import db4ounit.*;
29 import db4ounit.extensions.*;
30 import db4ounit.extensions.fixtures.Db4oSolo;
31
32 public class SelectiveActivationTestCase extends AbstractDb4oTestCase {
33     private boolean debug = false;
34
35     protected void configure(Configuration config) {
36         enableCascadeOnDelete(config);
37         config.activationDepth(1);
38     }
39
40     protected void store() {
41         Item c = new Item("C", null);
42         Item b = new Item("B", c);
43         Item a = new Item("A", b);
44         db().set(a);
45     }
46
47     public void testActivateFullTree() throws Exception JavaDoc {
48         reopen();
49         print("test activate full tree");
50         Assert.areEqual(3, queryItems().size());
51
52         Item a = queryItem("A");
53         Assert.isNull(a.child.child); // only A and B should be activated.
54

55         reopen(); // start fresh
56

57         // Add listener for activated event
58
eventRegistry().activated().addListener(new EventListener4() {
59             public void onEvent(Event4 e, EventArgs args) {
60                 try {
61                     print("event occured");
62                     ObjectEventArgs a = (ObjectEventArgs) args;
63                     if (a.object() instanceof Item) {
64                         Item item = ((Item) a.object());
65                         print("Activated item: " + item.id);
66                         boolean isChildActive = db().isActive(item.child);
67                         print("child is active? " + isChildActive);
68                         if (!isChildActive) {
69                             print("Activating child manually...");
70                             db().activate(item.child, 1);
71                         }
72                     } else {
73                         print("got object: " + a.object());
74                     }
75                 } catch (Throwable JavaDoc e1) {
76                     // todo: Classcast exceptions weren't breaking out? silently hidden
77
e1.printStackTrace();
78                 }
79             }
80         });
81
82         a = queryItem("A");
83         Assert.isNotNull(a.child.child); // this time, they should all be activated.
84
}
85
86     private void print(String JavaDoc s) {
87         if(debug) System.out.println(s);
88     }
89
90     private ObjectSet queryItems() {
91         return createItemQuery().execute();
92     }
93
94     private Query createItemQuery() {
95         Query q = db().query();
96         q.constrain(Item.class);
97         // todo: having this here will not fire the activation events!
98
/*q.sortBy(new QueryComparator() {
99             public int compare(Object first, Object second) {
100                 return ((Item)first).id.compareTo(((Item)second).id);
101             }
102         });*/

103         return q;
104     }
105
106     private EventRegistry eventRegistry() {
107         return EventRegistryFactory.forObjectContainer(db());
108     }
109
110     private void enableCascadeOnDelete(Configuration config) {
111         config.objectClass(Item.class).cascadeOnDelete(true);
112     }
113
114     private Item queryItem(final String JavaDoc id) {
115         Query q = createItemQuery();
116         q.descend("id").constrain(id);
117         return (Item) q.execute().next();
118     }
119
120     public static void main(String JavaDoc[] args) {
121         new TestRunner(
122                 new Db4oTestSuiteBuilder(
123                         new Db4oSolo(),
124                         SelectiveActivationTestCase.class)).run();
125     }
126 }
127
Popular Tags