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.*;
026: import com.db4o.internal.LazyObjectReference;
027: import com.db4o.query.Query;
028:
029: import db4ounit.extensions.Db4oClientServerTestCase;
030:
031: /**
032: * @exclude
033: */
034: public class CommittedCallbacksByAnotherClientTestCase extends
035: Db4oClientServerTestCase {
036:
037: /**
038: * @param args
039: */
040: public static void main(String[] args) {
041: new CommittedCallbacksByAnotherClientTestCase()
042: .runEmbeddedClientServer();
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: private ExtObjectContainer _anotherClient;
064:
065: protected void configure(Configuration config) {
066: indexField(config, Item.class, "id");
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(db().lock());
077: committed().addListener(_eventRecorder);
078: _anotherClient = clientServerFixture().openNewClient();
079: }
080:
081: protected void db4oTearDownBeforeClean() throws Exception {
082: committed().removeListener(_eventRecorder);
083: if (_anotherClient != null) {
084: _anotherClient.close();
085: }
086: }
087:
088: public void testCommittedAdded() {
089: Item item4 = new Item(4);
090: Item item5 = new Item(5);
091: _anotherClient.set(item4);
092: _anotherClient.set(item5);
093:
094: ObjectInfo info4 = getInfo(_anotherClient, 4);
095: ObjectInfo info5 = getInfo(_anotherClient, 5);
096:
097: assertNoEvents();
098:
099: _anotherClient.commit();
100:
101: assertCommittedEvent(new ObjectInfo[] { info4, info5 }, NONE,
102: NONE);
103: }
104:
105: public void testCommittedAddedDeleted() {
106: Item item4 = new Item(4);
107: Item item1 = getItem(_anotherClient, 1);
108: Item item2 = getItem(_anotherClient, 2);
109:
110: ObjectInfo info1 = getInfo(_anotherClient, 1);
111: ObjectInfo info2 = getInfo(_anotherClient, 2);
112:
113: _anotherClient.set(item4);
114: _anotherClient.delete(item1);
115: _anotherClient.delete(item2);
116:
117: ObjectInfo info4 = getInfo(_anotherClient, 4);
118:
119: assertNoEvents();
120:
121: _anotherClient.commit();
122: assertCommittedEvent(new ObjectInfo[] { info4 },
123: new ObjectInfo[] { info1, info2 }, NONE);
124: }
125:
126: public void testCommittedAddedUpdatedDeleted() {
127: Item item1 = getItem(_anotherClient, 1);
128: Item item2 = getItem(_anotherClient, 2);
129:
130: ObjectInfo info1 = getInfo(_anotherClient, 1);
131: ObjectInfo info2 = getInfo(_anotherClient, 2);
132:
133: Item item4 = new Item(4);
134: _anotherClient.set(item4);
135: _anotherClient.set(item2);
136: _anotherClient.delete(item1);
137:
138: ObjectInfo info4 = getInfo(_anotherClient, 4);
139:
140: assertNoEvents();
141:
142: _anotherClient.commit();
143: assertCommittedEvent(new ObjectInfo[] { info4 },
144: new ObjectInfo[] { info1 }, new ObjectInfo[] { info2 });
145: }
146:
147: public void testCommittedDeleted() {
148: Item item1 = getItem(_anotherClient, 1);
149: ObjectInfo info1 = getInfo(_anotherClient, 1);
150:
151: assertNoEvents();
152:
153: _anotherClient.delete(item1);
154:
155: _anotherClient.commit();
156:
157: assertCommittedEvent(NONE, new ObjectInfo[] { info1 }, NONE);
158: }
159:
160: public void testObjectSetTwiceShouldStillAppearAsAdded() {
161: final Item item4 = new Item(4);
162: _anotherClient.set(item4);
163: _anotherClient.set(item4);
164:
165: ObjectInfo info4 = getInfo(_anotherClient, 4);
166:
167: _anotherClient.commit();
168: assertCommittedEvent(new ObjectInfo[] { info4 }, NONE, NONE);
169: }
170:
171: private Item getItem(ExtObjectContainer oc, int id) {
172: Query query = oc.query();
173: query.constrain(Item.class);
174: query.descend("id").constrain(new Integer(id));
175: return (Item) query.execute().next();
176: }
177:
178: private ObjectInfo getInfo(ExtObjectContainer oc, int itemId) {
179: Item item = getItem(oc, itemId);
180: int internalId = (int) oc.getID(item);
181: return new LazyObjectReference(null, internalId);
182: }
183:
184: private void assertCommittedEvent(final ObjectInfo[] expectedAdded,
185: final ObjectInfo[] expectedDeleted,
186: final ObjectInfo[] expectedUpdated) {
187:
188: EventAssert.assertCommitEvent(_eventRecorder, committed(),
189: expectedAdded, expectedDeleted, expectedUpdated);
190: }
191:
192: private void assertNoEvents() {
193: EventAssert.assertNoEvents(_eventRecorder);
194: }
195:
196: private Event4 committed() {
197: return eventRegistry().committed();
198: }
199: }
|