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 CommittedCallbacksTestCase extends AbstractDb4oTestCase {
037:
038: /**
039: * @param args
040: */
041: public static void main(String[] args) {
042: new CommittedCallbacksTestCase().runClientServer();
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: }
067:
068: protected void store() throws Exception {
069: for (int i = 0; i < 3; ++i) {
070: store(new Item(i));
071: }
072: }
073:
074: protected void db4oSetupAfterStore() throws Exception {
075: _eventRecorder = new EventRecorder(db().lock());
076: committed().addListener(_eventRecorder);
077: }
078:
079: protected void db4oTearDownBeforeClean() throws Exception {
080: committed().removeListener(_eventRecorder);
081: }
082:
083: public void testLocalTransactionIsAvailableToEventListener() {
084: if (isClientServer()) {
085: return;
086: }
087:
088: final Transaction transaction = stream().transaction();
089: final ObjectByRef objectByRef = new ObjectByRef();
090: eventRegistry().committed().addListener(new EventListener4() {
091: public void onEvent(Event4 e, EventArgs args) {
092: objectByRef.value = ((CommitEventArgs) args)
093: .transaction();
094: }
095: });
096: db().commit();
097: Assert.areSame(transaction, objectByRef.value);
098: }
099:
100: public void testCommittedAdded() {
101: Item item4 = new Item(4);
102: Item item5 = new Item(5);
103: db().set(item4);
104: db().set(item5);
105:
106: ObjectInfo info4 = getInfo(4);
107: ObjectInfo info5 = getInfo(5);
108:
109: assertNoEvents();
110:
111: db().commit();
112:
113: assertCommittedEvent(new ObjectInfo[] { info4, info5 }, NONE,
114: NONE);
115: }
116:
117: public void testCommittedAddedDeleted() {
118: Item item4 = new Item(4);
119: Item item1 = getItem(1);
120: Item item2 = getItem(2);
121:
122: ObjectInfo info1 = getInfo(1);
123: ObjectInfo info2 = getInfo(2);
124:
125: db().set(item4);
126: db().delete(item1);
127: db().delete(item2);
128:
129: ObjectInfo info4 = getInfo(4);
130:
131: assertNoEvents();
132:
133: db().commit();
134: assertCommittedEvent(new ObjectInfo[] { info4 },
135: new ObjectInfo[] { info1, info2 }, NONE);
136: }
137:
138: public void testCommittedAddedUpdatedDeleted() {
139: Item item1 = getItem(1);
140: Item item2 = getItem(2);
141:
142: ObjectInfo info1 = getInfo(1);
143: ObjectInfo info2 = getInfo(2);
144:
145: Item item4 = new Item(4);
146: db().set(item4);
147: db().set(item2);
148: db().delete(item1);
149:
150: ObjectInfo info4 = getInfo(4);
151:
152: assertNoEvents();
153:
154: db().commit();
155: assertCommittedEvent(new ObjectInfo[] { info4 },
156: new ObjectInfo[] { info1 }, new ObjectInfo[] { info2 });
157: }
158:
159: public void testCommittedDeleted() {
160: Item item1 = getItem(1);
161: ObjectInfo info1 = getInfo(1);
162:
163: assertNoEvents();
164:
165: db().delete(item1);
166:
167: db().commit();
168:
169: assertCommittedEvent(NONE, new ObjectInfo[] { info1 }, NONE);
170: }
171:
172: public void testObjectSetTwiceShouldStillAppearAsAdded() {
173: final Item item4 = new Item(4);
174: db().set(item4);
175: db().set(item4);
176:
177: ObjectInfo info4 = getInfo(4);
178:
179: db().commit();
180: assertCommittedEvent(new ObjectInfo[] { info4 }, NONE, NONE);
181: }
182:
183: private Item getItem(int id) {
184: final Query query = newQuery(Item.class);
185: query.descend("id").constrain(new Integer(id));
186: return (Item) query.execute().next();
187: }
188:
189: private ObjectInfo getInfo(int itemId) {
190: Item item = getItem(itemId);
191: int internalId = (int) db().getID(item);
192: return new LazyObjectReference(trans(), internalId);
193: }
194:
195: private void assertCommittedEvent(final ObjectInfo[] expectedAdded,
196: final ObjectInfo[] expectedDeleted,
197: final ObjectInfo[] expectedUpdated) {
198:
199: EventAssert.assertCommitEvent(_eventRecorder, committed(),
200: expectedAdded, expectedDeleted, expectedUpdated);
201: }
202:
203: private void assertNoEvents() {
204: EventAssert.assertNoEvents(_eventRecorder);
205: }
206:
207: private Event4 committed() {
208: return eventRegistry().committed();
209: }
210: }
|