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.events.*;
024:
025: import db4ounit.Assert;
026: import db4ounit.extensions.AbstractDb4oTestCase;
027: import db4ounit.extensions.fixtures.*;
028:
029: public class GlobalLifecycleEventsTestCase extends AbstractDb4oTestCase
030: implements OptOutCS {
031:
032: public static void main(String[] arguments) {
033: new GlobalLifecycleEventsTestCase().runClientServer();
034: }
035:
036: public static final class Item {
037:
038: public int id;
039:
040: public Item(int id_) {
041: id = id_;
042: }
043:
044: public boolean equals(Object obj) {
045: if (!(obj instanceof Item))
046: return false;
047: return ((Item) obj).id == id;
048: }
049:
050: public String toString() {
051: return "Item(" + id + ")";
052: }
053: }
054:
055: private EventRecorder _recorder;
056:
057: protected void db4oSetupBeforeStore() throws Exception {
058: _recorder = new EventRecorder(fileSession().lock());
059: }
060:
061: public void testActivating() throws Exception {
062: // FIXME: It fails in c/s mode. The objects are activated twice during
063: // readPrefetch.
064: // The first place is in YapObject#readPrefetch:
065: // a_reader.setInstantiationDepth(_class.configOrAncestorConfig() ==
066: // null ? 1 : 0);
067: // The second place is in ClientQueryResultIterator#current():
068: // return _client.activate(prefetchedCurrent());
069: // Therefore, activating event listener is notified twice. The test is
070: // good, and the implementation code should be FIXED.
071: storeAndReopen();
072: assertActivationEvent(eventRegistry().activating());
073: }
074:
075: public void testCancelDeactivating() {
076: listenToEvent(eventRegistry().deactivating());
077:
078: _recorder.cancel(true);
079:
080: Item item = storeItem();
081: db().deactivate(item, 1);
082:
083: assertSingleObjectEventArgs(eventRegistry().deactivating(),
084: item);
085:
086: Assert.areEqual(1, item.id);
087: }
088:
089: public void testDeleting() throws Exception {
090: assertDeletionEvent(eventRegistryForDelete().deleting());
091: }
092:
093: public void testDeleted() throws Exception {
094: assertDeletionEvent(eventRegistryForDelete().deleted());
095: }
096:
097: private void assertDeletionEvent(Event4 event4) throws Exception {
098: assertDeletionEvent(event4, false);
099: }
100:
101: private void assertDeletionEvent(Event4 event, boolean cancel)
102: throws Exception {
103: listenToEvent(event);
104:
105: Item item = storeItem();
106: if (cancel) {
107: _recorder.cancel(true);
108: }
109:
110: Item expectedItem = isClientServer() ? queryServerItem(item)
111: : item;
112: db().delete(item);
113: sync();
114:
115: assertSingleObjectEventArgs(event, expectedItem);
116:
117: if (cancel) {
118: Assert.areSame(item, db().get(Item.class).next());
119: } else {
120: Assert.areEqual(0, db().get(Item.class).size());
121: }
122: }
123:
124: public void testCancelDeleting() throws Exception {
125: assertDeletionEvent(eventRegistryForDelete().deleting(), true);
126: }
127:
128: public void testCancelCreating() {
129: listenToEvent(eventRegistry().creating());
130:
131: _recorder.cancel(true);
132:
133: Item item = storeItem();
134:
135: assertSingleObjectEventArgs(eventRegistry().creating(), item);
136:
137: Assert.areEqual(0, db().get(Item.class).size());
138: }
139:
140: public void testCancelUpdating() throws Exception {
141: listenToEvent(eventRegistry().updating());
142:
143: _recorder.cancel(true);
144:
145: Item item = storeItem();
146: item.id = 42;
147: db().set(item);
148:
149: assertSingleObjectEventArgs(eventRegistry().updating(), item);
150:
151: reopen();
152:
153: item = (Item) db().get(Item.class).next();
154: Assert.areEqual(1, item.id);
155: }
156:
157: public void testCreating() {
158: assertCreationEvent(eventRegistry().creating());
159: }
160:
161: public void testDeactivating() throws Exception {
162: assertDeactivationEvent(eventRegistry().deactivating());
163: }
164:
165: public void testUpdating() {
166: assertUpdateEvent(eventRegistry().updating());
167: }
168:
169: public void testActivated() throws Exception {
170: storeAndReopen();
171: assertActivationEvent(eventRegistry().activated());
172: }
173:
174: public void testDeactivated() throws Exception {
175: assertDeactivationEvent(eventRegistry().deactivated());
176: }
177:
178: public void testCreated() {
179: assertCreationEvent(eventRegistry().created());
180: }
181:
182: public void testUpdated() {
183: assertUpdateEvent(eventRegistry().updated());
184: }
185:
186: private void assertActivationEvent(Event4 event) throws Exception {
187: listenToEvent(event);
188:
189: Item item = (Item) db().get(Item.class).next();
190:
191: assertSingleObjectEventArgs(event, item);
192: }
193:
194: private void assertCreationEvent(Event4 event) {
195: listenToEvent(event);
196:
197: Item item = storeItem();
198:
199: assertSingleObjectEventArgs(event, item);
200:
201: Assert.areSame(item, db().get(Item.class).next());
202: }
203:
204: private void assertDeactivationEvent(Event4 event) throws Exception {
205: listenToEvent(event);
206:
207: Item item = storeItem();
208: db().deactivate(item, 1);
209:
210: assertSingleObjectEventArgs(event, item);
211:
212: Assert.areEqual(0, item.id);
213: }
214:
215: private Item queryServerItem(Item item) {
216: return (Item) fileSession().get(item).next();
217: }
218:
219: private void assertSingleObjectEventArgs(Event4 expectedEvent,
220: Item expectedItem) {
221: Assert.areEqual(1, _recorder.size());
222: EventRecord record = _recorder.get(0);
223: Assert.areSame(expectedEvent, record.e);
224:
225: final Object actual = ((ObjectEventArgs) record.args).object();
226: Assert.areSame(expectedItem, actual);
227: }
228:
229: private void assertUpdateEvent(Event4 event) {
230: listenToEvent(event);
231:
232: Item item = storeItem();
233: item.id = 42;
234: db().set(item);
235:
236: assertSingleObjectEventArgs(event, item);
237:
238: Assert.areSame(item, db().get(Item.class).next());
239: }
240:
241: private EventRegistry eventRegistryForDelete() {
242: return serverEventRegistry();
243: }
244:
245: private void listenToEvent(Event4 event) {
246: event.addListener(_recorder);
247: }
248:
249: private void storeAndReopen() throws Exception {
250: storeItem();
251: reopen();
252: }
253:
254: private Item storeItem() {
255: Item item = new Item(1);
256: db().set(item);
257: db().commit();
258: sync();
259: return item;
260: }
261:
262: private void sync() {
263: if (!isClientServer())
264: return;
265: final String name = "___";
266: db().setSemaphore(name, 0);
267: db().releaseSemaphore(name);
268: }
269:
270: }
|