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.config.Configuration;
024: import com.db4o.events.*;
025: import com.db4o.ext.ObjectInfo;
026: import com.db4o.foundation.ObjectByRef;
027: import com.db4o.internal.*;
028: import com.db4o.query.Query;
029:
030: import db4ounit.Assert;
031: import db4ounit.extensions.AbstractDb4oTestCase;
032:
033: /**
034: * @exclude
035: */
036: public class CommittingCallbacksTestCase extends AbstractDb4oTestCase {
037:
038: /**
039: * @param args
040: */
041: public static void main(String[] args) {
042: new CommittingCallbacksTestCase().runSoloAndClientServer();
043: }
044:
045: private static final ObjectInfo[] NONE = new ObjectInfo[0];
046:
047: public static final class Item {
048: public int id;
049:
050: public Item() {
051: }
052:
053: public Item(int id_) {
054: id = id_;
055: }
056:
057: public String toString() {
058: return "Item(" + id + ")";
059: }
060: }
061:
062: private EventRecorder _eventRecorder;
063:
064: protected void configure(Configuration config) {
065: indexField(config, Item.class, "id");
066: config.clientServer().batchMessages(false);
067: }
068:
069: protected void store() throws Exception {
070: for (int i = 0; i < 3; ++i) {
071: store(new Item(i));
072: }
073: }
074:
075: protected void db4oSetupAfterStore() throws Exception {
076: _eventRecorder = new EventRecorder(fileSession().lock());
077: committing().addListener(_eventRecorder);
078: }
079:
080: protected void db4oTearDownBeforeClean() throws Exception {
081: committing().removeListener(_eventRecorder);
082: }
083:
084: public void testLocalTransactionIsAvailableToEventListener() {
085: if (isClientServer()) {
086: return;
087: }
088:
089: final Transaction transaction = stream().transaction();
090: final ObjectByRef objectByRef = new ObjectByRef();
091: serverEventRegistry().committing().addListener(
092: new EventListener4() {
093: public void onEvent(Event4 e, EventArgs args) {
094: objectByRef.value = ((CommitEventArgs) args)
095: .transaction();
096: }
097: });
098: db().commit();
099: Assert.areSame(transaction, objectByRef.value);
100: }
101:
102: public void testCommittingAdded() {
103: Item item4 = new Item(4);
104: Item item5 = new Item(5);
105: db().set(item4);
106: db().set(item5);
107:
108: ObjectInfo info4 = getInfo(4);
109: ObjectInfo info5 = getInfo(5);
110:
111: assertNoEvents();
112:
113: db().commit();
114:
115: assertCommittingEvent(new ObjectInfo[] { info4, info5 }, NONE,
116: NONE);
117: }
118:
119: public void testCommittingAddedDeleted() {
120: Item item4 = new Item(4);
121: Item item1 = getItem(1);
122: Item item2 = getItem(2);
123:
124: ObjectInfo info1 = getInfo(1);
125: ObjectInfo info2 = getInfo(2);
126:
127: db().set(item4);
128: db().delete(item1);
129: db().delete(item2);
130:
131: ObjectInfo info4 = getInfo(4);
132:
133: assertNoEvents();
134:
135: db().commit();
136: assertCommittingEvent(new ObjectInfo[] { info4 },
137: new ObjectInfo[] { info1, info2 }, NONE);
138: }
139:
140: public void testCommittingAddedUpdatedDeleted() {
141: Item item1 = getItem(1);
142: Item item2 = getItem(2);
143:
144: ObjectInfo info1 = getInfo(1);
145: ObjectInfo info2 = getInfo(2);
146:
147: Item item4 = new Item(4);
148: db().set(item4);
149: db().set(item2);
150: db().delete(item1);
151:
152: ObjectInfo info4 = getInfo(4);
153:
154: assertNoEvents();
155:
156: db().commit();
157: assertCommittingEvent(new ObjectInfo[] { info4 },
158: new ObjectInfo[] { info1 }, new ObjectInfo[] { info2 });
159: }
160:
161: public void testCommittingDeleted() {
162: Item item1 = getItem(1);
163: ObjectInfo info1 = getInfo(1);
164:
165: assertNoEvents();
166:
167: db().delete(item1);
168:
169: db().commit();
170:
171: assertCommittingEvent(NONE, new ObjectInfo[] { info1 }, NONE);
172: }
173:
174: public void testObjectSetTwiceShouldStillAppearAsAdded() {
175: final Item item4 = new Item(4);
176: db().set(item4);
177: db().set(item4);
178:
179: ObjectInfo info4 = getInfo(4);
180:
181: db().commit();
182: assertCommittingEvent(new ObjectInfo[] { info4 }, NONE, NONE);
183: }
184:
185: private Item getItem(int id) {
186: final Query query = newQuery(Item.class);
187: query.descend("id").constrain(new Integer(id));
188: return (Item) query.execute().next();
189: }
190:
191: private ObjectInfo getInfo(int itemId) {
192: Item item = getItem(itemId);
193: int internalId = (int) db().getID(item);
194: return new LazyObjectReference(trans(), internalId);
195: }
196:
197: private void assertCommittingEvent(
198: final ObjectInfo[] expectedAdded,
199: final ObjectInfo[] expectedDeleted,
200: final ObjectInfo[] expectedUpdated) {
201:
202: EventAssert.assertCommitEvent(_eventRecorder, committing(),
203: expectedAdded, expectedDeleted, expectedUpdated);
204: }
205:
206: private void assertNoEvents() {
207: EventAssert.assertNoEvents(_eventRecorder);
208: }
209:
210: private Event4 committing() {
211: return serverEventRegistry().committing();
212: }
213: }
|