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.query.Query;
027:
028: import db4ounit.*;
029: import db4ounit.extensions.*;
030: import db4ounit.extensions.fixtures.Db4oSolo;
031:
032: public class SelectiveActivationTestCase extends AbstractDb4oTestCase {
033: private boolean debug = false;
034:
035: protected void configure(Configuration config) {
036: enableCascadeOnDelete(config);
037: config.activationDepth(1);
038: }
039:
040: protected void store() {
041: Item c = new Item("C", null);
042: Item b = new Item("B", c);
043: Item a = new Item("A", b);
044: db().set(a);
045: }
046:
047: public void testActivateFullTree() throws Exception {
048: reopen();
049: print("test activate full tree");
050: Assert.areEqual(3, queryItems().size());
051:
052: Item a = queryItem("A");
053: Assert.isNull(a.child.child); // only A and B should be activated.
054:
055: reopen(); // start fresh
056:
057: // Add listener for activated event
058: eventRegistry().activated().addListener(new EventListener4() {
059: public void onEvent(Event4 e, EventArgs args) {
060: try {
061: print("event occured");
062: ObjectEventArgs a = (ObjectEventArgs) args;
063: if (a.object() instanceof Item) {
064: Item item = ((Item) a.object());
065: print("Activated item: " + item.id);
066: boolean isChildActive = db().isActive(
067: item.child);
068: print("child is active? " + isChildActive);
069: if (!isChildActive) {
070: print("Activating child manually...");
071: db().activate(item.child, 1);
072: }
073: } else {
074: print("got object: " + a.object());
075: }
076: } catch (Throwable e1) {
077: // todo: Classcast exceptions weren't breaking out? silently hidden
078: e1.printStackTrace();
079: }
080: }
081: });
082:
083: a = queryItem("A");
084: Assert.isNotNull(a.child.child); // this time, they should all be activated.
085: }
086:
087: private void print(String s) {
088: if (debug)
089: System.out.println(s);
090: }
091:
092: private ObjectSet queryItems() {
093: return createItemQuery().execute();
094: }
095:
096: private Query createItemQuery() {
097: Query q = db().query();
098: q.constrain(Item.class);
099: // todo: having this here will not fire the activation events!
100: /*q.sortBy(new QueryComparator() {
101: public int compare(Object first, Object second) {
102: return ((Item)first).id.compareTo(((Item)second).id);
103: }
104: });*/
105: return q;
106: }
107:
108: private void enableCascadeOnDelete(Configuration config) {
109: config.objectClass(Item.class).cascadeOnDelete(true);
110: }
111:
112: private Item queryItem(final String id) {
113: Query q = createItemQuery();
114: q.descend("id").constrain(id);
115: return (Item) q.execute().next();
116: }
117:
118: public static void main(String[] args) {
119: new TestRunner(new Db4oTestSuiteBuilder(new Db4oSolo(),
120: SelectiveActivationTestCase.class)).run();
121: }
122: }
|