KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc[] 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 JavaDoc obj) {
43             if (!(obj instanceof Item)) return false;
44             return ((Item)obj).id == id;
45         }
46         
47         public String JavaDoc toString() {
48             return "Item(" + id + ")";
49         }
50     }
51     
52     private EventRecorder _recorder;
53     
54     protected void db4oSetupBeforeStore() throws Exception JavaDoc {
55         _recorder = new EventRecorder();
56     }
57     
58     public void testActivating() throws Exception JavaDoc {
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 JavaDoc {
77         assertDeletionEvent(eventRegistryForDelete().deleting());
78     }
79     
80     public void testDeleted() throws Exception JavaDoc {
81         assertDeletionEvent(eventRegistryForDelete().deleted());
82     }
83     
84     private void assertDeletionEvent(Event4 event4) throws Exception JavaDoc {
85         assertDeletionEvent(event4, false);
86     }
87
88     private void assertDeletionEvent(Event4 event, boolean cancel) throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
147         assertDeactivationEvent(eventRegistry().deactivating());
148     }
149     
150     public void testUpdating() {
151         assertUpdateEvent(eventRegistry().updating());
152     }
153     
154     public void testActivated() throws Exception JavaDoc {
155         storeAndReopen();
156         assertActivationEvent(eventRegistry().activated());
157     }
158
159     public void testDeactivated() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc 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 JavaDoc {
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 JavaDoc name = "___";
253         db().setSemaphore(name, 0);
254         db().releaseSemaphore(name);
255     }
256     
257 }
258
Popular Tags