| 1 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 { 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); 55 reopen(); 57 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 e1) { 76 e1.printStackTrace(); 78 } 79 } 80 }); 81 82 a = queryItem("A"); 83 Assert.isNotNull(a.child.child); } 85 86 private void print(String 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 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 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 [] args) { 121 new TestRunner( 122 new Db4oTestSuiteBuilder( 123 new Db4oSolo(), 124 SelectiveActivationTestCase.class)).run(); 125 } 126 } 127 | Popular Tags |