1 21 package com.db4o.db4ounit.jre11.events; 22 23 import com.db4o.events.*; 24 25 import db4ounit.Assert; 26 import db4ounit.extensions.AbstractDb4oTestCase; 27 28 public class GlobalLifecycleEventsTestCase extends AbstractDb4oTestCase { 29 30 public static void main(String [] arguments) { 31 new GlobalLifecycleEventsTestCase().runClientServer(); 32 } 33 34 public static final class Item { 35 36 public int id; 37 38 public Item(int id_) { 39 id = id_; 40 } 41 42 public boolean equals(Object obj) { 43 if (!(obj instanceof Item)) return false; 44 return ((Item)obj).id == id; 45 } 46 47 public String toString() { 48 return "Item(" + id + ")"; 49 } 50 } 51 52 private EventRecorder _recorder; 53 54 protected void db4oSetupBeforeStore() throws Exception { 55 _recorder = new EventRecorder(); 56 } 57 58 public void testActivating() throws Exception { 59 storeAndReopen(); 60 assertActivationEvent(eventRegistry().activating()); 61 } 62 63 public void testCancelDeactivating() { 64 listenToEvent(eventRegistry().deactivating()); 65 66 _recorder.cancel(true); 67 68 Item item = storeItem(); 69 db().deactivate(item, 1); 70 71 assertSingleObjectEventArgs(eventRegistry().deactivating(), item); 72 73 Assert.areEqual(1, item.id); 74 } 75 76 public void testDeleting() throws Exception { 77 assertDeletionEvent(eventRegistryForDelete().deleting()); 78 } 79 80 public void testDeleted() throws Exception { 81 assertDeletionEvent(eventRegistryForDelete().deleted()); 82 } 83 84 private void assertDeletionEvent(Event4 event4) throws Exception { 85 assertDeletionEvent(event4, false); 86 } 87 88 private void assertDeletionEvent(Event4 event, boolean cancel) throws Exception { 89 listenToEvent(event); 90 91 Item item = storeItem(); 92 if (cancel) { 93 _recorder.cancel(true); 94 } 95 96 Item expectedItem = isClientServer() ? queryServerItem(item) : item; 97 db().delete(item); 98 sync(); 99 100 assertSingleObjectEventArgs(event, expectedItem); 101 102 if (cancel) { 103 Assert.areSame(item, db().get(Item.class).next()); 104 } else { 105 Assert.areEqual(0, db().get(Item.class).size()); 106 } 107 } 108 109 public void testCancelDeleting() throws Exception { 110 assertDeletionEvent(eventRegistryForDelete().deleting(), true); 111 } 112 113 public void testCancelCreating() { 114 listenToEvent(eventRegistry().creating()); 115 116 _recorder.cancel(true); 117 118 Item item = storeItem(); 119 120 assertSingleObjectEventArgs(eventRegistry().creating(), item); 121 122 Assert.areEqual(0, db().get(Item.class).size()); 123 } 124 125 public void testCancelUpdating() throws Exception { 126 listenToEvent(eventRegistry().updating()); 127 128 _recorder.cancel(true); 129 130 Item item = storeItem(); 131 item.id = 42; 132 db().set(item); 133 134 assertSingleObjectEventArgs(eventRegistry().updating(), item); 135 136 reopen(); 137 138 item = (Item)db().get(Item.class).next(); 139 Assert.areEqual(1, item.id); 140 } 141 142 public void testCreating() { 143 assertCreationEvent(eventRegistry().creating()); 144 } 145 146 public void testDeactivating() throws Exception { 147 assertDeactivationEvent(eventRegistry().deactivating()); 148 } 149 150 public void testUpdating() { 151 assertUpdateEvent(eventRegistry().updating()); 152 } 153 154 public void testActivated() throws Exception { 155 storeAndReopen(); 156 assertActivationEvent(eventRegistry().activated()); 157 } 158 159 public void testDeactivated() throws Exception { 160 assertDeactivationEvent(eventRegistry().deactivated()); 161 } 162 163 public void testCreated() { 164 assertCreationEvent(eventRegistry().created()); 165 } 166 167 public void testUpdated() { 168 assertUpdateEvent(eventRegistry().updated()); 169 } 170 171 private void assertActivationEvent(Event4 event) throws Exception { 172 listenToEvent(event); 173 174 Item item = (Item)db().get(Item.class).next(); 175 176 assertSingleObjectEventArgs(event, item); 177 } 178 179 private void assertCreationEvent(Event4 event) { 180 listenToEvent(event); 181 182 Item item = storeItem(); 183 184 assertSingleObjectEventArgs(event, item); 185 186 Assert.areSame(item, db().get(Item.class).next()); 187 } 188 189 private void assertDeactivationEvent(Event4 event) throws Exception { 190 listenToEvent(event); 191 192 Item item = storeItem(); 193 db().deactivate(item, 1); 194 195 assertSingleObjectEventArgs(event, item); 196 197 Assert.areEqual(0, item.id); 198 } 199 200 private Item queryServerItem(Item item) { 201 return (Item)fileSession().get(item).next(); 202 } 203 204 private void assertSingleObjectEventArgs(Event4 expectedEvent, Item expectedItem) { 205 Assert.areEqual(1, _recorder.size()); 206 EventRecord record = _recorder.get(0); 207 Assert.areSame(expectedEvent, record.e); 208 209 final Object actual = ((ObjectEventArgs)record.args).object(); 210 Assert.areSame(expectedItem, actual); 211 } 212 213 private void assertUpdateEvent(Event4 event) { 214 listenToEvent(event); 215 216 Item item = storeItem(); 217 item.id = 42; 218 db().set(item); 219 220 assertSingleObjectEventArgs(event, item); 221 222 Assert.areSame(item, db().get(Item.class).next()); 223 } 224 225 private EventRegistry eventRegistryForDelete() { 226 return EventRegistryFactory.forObjectContainer(fileSession()); 227 } 228 229 private EventRegistry eventRegistry() { 230 return EventRegistryFactory.forObjectContainer(db()); 231 } 232 233 private void listenToEvent(Event4 event) { 234 event.addListener(_recorder); 235 } 236 237 private void storeAndReopen() throws Exception { 238 storeItem(); 239 reopen(); 240 } 241 242 private Item storeItem() { 243 Item item = new Item(1); 244 db().set(item); 245 db().commit(); 246 sync(); 247 return item; 248 } 249 250 private void sync() { 251 if (!isClientServer()) return; 252 final String name = "___"; 253 db().setSemaphore(name, 0); 254 db().releaseSemaphore(name); 255 } 256 257 } 258 | Popular Tags |