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.common.internal;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.ext.*;
026: import com.db4o.foundation.*;
027: import com.db4o.foundation.io.*;
028: import com.db4o.internal.*;
029: import com.db4o.query.*;
030: import com.db4o.reflect.*;
031:
032: import db4ounit.*;
033:
034: public class EmbeddedClientObjectContainerTestCase implements
035: TestLifeCycle {
036:
037: private static final String FIELD_NAME = "_name";
038:
039: private static final String FILENAME = "mtoc.db4o";
040:
041: private LocalObjectContainer _server;
042:
043: protected EmbeddedClientObjectContainer _client1;
044:
045: protected EmbeddedClientObjectContainer _client2;
046:
047: private static final String ORIGINAL_NAME = "original";
048:
049: private static final String CHANGED_NAME = "changed";
050:
051: public static class ItemHolder {
052:
053: public Item _item;
054:
055: public ItemHolder(Item item) {
056: _item = item;
057: }
058:
059: }
060:
061: public static class Item {
062:
063: public String _name;
064:
065: public Item() {
066:
067: }
068:
069: public Item(String name) {
070: _name = name;
071: }
072: }
073:
074: public void testSetAndCommitIsolation() {
075: Item item = new Item("one");
076: _client1.set(item);
077: assertItemCount(_client2, 0);
078: _client1.commit();
079: assertItemCount(_client2, 1);
080: }
081:
082: public void testActivate() {
083: Item storedItem = storeItemToClient1AndCommit();
084: long id = _client1.getID(storedItem);
085:
086: Item retrievedItem = (Item) _client2.getByID(id);
087: Assert.isNull(retrievedItem._name);
088: Assert.isFalse(_client2.isActive(retrievedItem));
089:
090: _client2.activate(retrievedItem, 1);
091: Assert.areEqual(ORIGINAL_NAME, retrievedItem._name);
092: Assert.isTrue(_client2.isActive(retrievedItem));
093: }
094:
095: public void testBackup() {
096: Assert.expect(NotSupportedException.class, new CodeBlock() {
097: public void run() throws Throwable {
098: _client1.backup("");
099: }
100: });
101: }
102:
103: public void testBindIsolation() {
104: Item storedItem = storeItemToClient1AndCommit();
105: long id = _client1.getID(storedItem);
106:
107: Item retrievedItem = retrieveItemFromClient2();
108:
109: Item boundItem = new Item(CHANGED_NAME);
110: _client1.bind(boundItem, id);
111: Assert.areSame(boundItem, _client1.getByID(id));
112: Assert.areSame(retrievedItem, _client2.getByID(id));
113: }
114:
115: public void testClose() {
116: final BooleanByRef closed = new BooleanByRef();
117:
118: // FIXME: Sharpen doesn't understand the null parameter (the third one), we had to add a cast
119: // to get sharpen to run through.
120:
121: Transaction trans = new LocalTransaction(_server, _server
122: .systemTransaction(),
123: (TransactionalReferenceSystem) null) {
124: public void close(boolean rollbackOnClose) {
125: super .close(rollbackOnClose);
126: closed.value = true;
127: }
128: };
129: EmbeddedClientObjectContainer client = new EmbeddedClientObjectContainer(
130: _server, trans);
131:
132: // FIXME: close needs to unregister reference system
133: // also for crashed clients
134: client.close();
135:
136: Assert.isTrue(closed.value);
137: }
138:
139: public void testCommitOnClose() {
140: Item storedItem = storeItemToClient1AndCommit();
141: storedItem._name = CHANGED_NAME;
142: _client1.set(storedItem);
143: _client1.close();
144: Item retrievedItem = retrieveItemFromClient2();
145: Assert.areEqual(CHANGED_NAME, retrievedItem._name);
146: }
147:
148: public void testConfigure() {
149: Assert.isNotNull(_client1.configure());
150: }
151:
152: public void testDeactivate() {
153: Item item = storeItemToClient1AndCommit();
154: ItemHolder holder = new ItemHolder(item);
155: _client1.set(holder);
156: _client1.commit();
157: _client1.deactivate(holder, 1);
158: Assert.isNull(holder._item);
159: }
160:
161: public void testDelete() {
162: Item item = storeItemToClient1AndCommit();
163: Assert.isTrue(_client1.isStored(item));
164: _client1.delete(item);
165: Assert.isFalse(_client1.isStored(item));
166: }
167:
168: public void testDescendIsolation() {
169: Item storedItem = storeItemToClient1AndCommit();
170: storedItem._name = CHANGED_NAME;
171: _client1.set(storedItem);
172:
173: int id = (int) _client1.getID(storedItem);
174: Object retrievedItem = _client2.getByID(id);
175: Assert.isNotNull(retrievedItem);
176:
177: Object descendValue = _client2.descend(retrievedItem,
178: new String[] { FIELD_NAME });
179: Assert.areEqual(ORIGINAL_NAME, descendValue);
180:
181: _client1.commit();
182:
183: descendValue = _client2.descend(retrievedItem,
184: new String[] { FIELD_NAME });
185: Assert.areEqual(CHANGED_NAME, descendValue);
186: }
187:
188: public void testExt() {
189: Assert.isInstanceOf(ExtObjectContainer.class, _client1.ext());
190: }
191:
192: public void testGet() {
193: Item storedItem = storeItemToClient1AndCommit();
194: Object retrievedItem = _client1.get(new Item()).next();
195: Assert.areSame(storedItem, retrievedItem);
196: }
197:
198: public void testGetID() {
199: Item storedItem = storeItemToClient1AndCommit();
200: long id = _client1.getID(storedItem);
201: Assert.isGreater(1, id);
202: }
203:
204: public void testGetByID() {
205: Item storedItem = storeItemToClient1AndCommit();
206: long id = _client1.getID(storedItem);
207: Assert.areSame(storedItem, _client1.getByID(id));
208: }
209:
210: public void testGetObjectInfo() {
211: Item storedItem = storeItemToClient1AndCommit();
212: ObjectInfo objectInfo = _client1.getObjectInfo(storedItem);
213: Assert.isNotNull(objectInfo);
214: }
215:
216: public void testGetByUUID() {
217: Item storedItem = storeItemToClient1AndCommit();
218: ObjectInfo objectInfo = _client1.getObjectInfo(storedItem);
219:
220: Object retrievedItem = _client1.getByUUID(objectInfo.getUUID());
221: Assert.areSame(storedItem, retrievedItem);
222:
223: retrievedItem = _client2.getByUUID(objectInfo.getUUID());
224: Assert.areNotSame(storedItem, retrievedItem);
225: }
226:
227: public void testIdenity() {
228: Db4oDatabase identity1 = _client1.identity();
229: Assert.isNotNull(identity1);
230: Db4oDatabase identity2 = _client2.identity();
231: Assert.isNotNull(identity2);
232:
233: // TODO: Db4oDatabase is shared between embedded clients.
234: // This should work, since there is an automatic bind
235: // replacement. Replication test cases will tell.
236: Assert.areSame(identity1, identity2);
237: }
238:
239: public void testIsCached() {
240: Item storedItem = storeItemToClient1AndCommit();
241: long id = _client1.getID(storedItem);
242:
243: Assert.isFalse(_client2.isCached(id));
244:
245: Item retrievedItem = (Item) _client2.getByID(id);
246: Assert.isNotNull(retrievedItem);
247: Assert.isTrue(_client2.isCached(id));
248: }
249:
250: public void testIsClosed() {
251: _client1.close();
252: Assert.isTrue(_client1.isClosed());
253: }
254:
255: public void testIsStored() {
256: Item storedItem = storeItemToClient1AndCommit();
257: Assert.isTrue(_client1.isStored(storedItem));
258: Assert.isFalse(_client2.isStored(storedItem));
259: }
260:
261: public void testKnownClasses() {
262: ReflectClass[] knownClasses = _client1.knownClasses();
263: ReflectClass itemClass = _client1.reflector().forClass(
264: Item.class);
265: ArrayAssert.contains(knownClasses,
266: new ReflectClass[] { itemClass });
267: }
268:
269: public void testLock() {
270: Assert.areSame(_server.lock(), _client1.lock());
271: }
272:
273: public void testPeekPersisted() {
274: Item storedItem = storeItemToClient1AndCommit();
275: storedItem._name = CHANGED_NAME;
276: _client1.set(storedItem);
277:
278: Item peekedItem = (Item) _client1.peekPersisted(storedItem, 2,
279: true);
280: Assert.isNotNull(peekedItem);
281: Assert.areNotSame(peekedItem, storedItem);
282: Assert.areEqual(ORIGINAL_NAME, peekedItem._name);
283:
284: peekedItem = (Item) _client1
285: .peekPersisted(storedItem, 2, false);
286: Assert.isNotNull(peekedItem);
287: Assert.areNotSame(peekedItem, storedItem);
288: Assert.areEqual(CHANGED_NAME, peekedItem._name);
289:
290: Item retrievedItem = retrieveItemFromClient2();
291: peekedItem = (Item) _client2.peekPersisted(retrievedItem, 2,
292: false);
293: Assert.isNotNull(peekedItem);
294: Assert.areNotSame(peekedItem, retrievedItem);
295: Assert.areEqual(ORIGINAL_NAME, peekedItem._name);
296: }
297:
298: public void testPurge() {
299: Item storedItem = storeItemToClient1AndCommit();
300: Assert.isTrue(_client1.isStored(storedItem));
301: _client1.purge(storedItem);
302: Assert.isFalse(_client1.isStored(storedItem));
303: }
304:
305: public void testReflector() {
306: Assert.isNotNull(_client1.reflector());
307: }
308:
309: public void testRefresh() {
310: Item storedItem = storeItemToClient1AndCommit();
311: storedItem._name = CHANGED_NAME;
312: _client1.refresh(storedItem, 2);
313: Assert.areEqual(ORIGINAL_NAME, storedItem._name);
314: }
315:
316: public void testRollback() {
317: Item storedItem = storeItemToClient1AndCommit();
318: storedItem._name = CHANGED_NAME;
319: _client1.set(storedItem);
320: _client1.rollback();
321: _client1.commit();
322:
323: Item retrievedItem = retrieveItemFromClient2();
324: Assert.areEqual(ORIGINAL_NAME, retrievedItem._name);
325: }
326:
327: public void testSetSemaphore() {
328: String semaphoreName = "sem";
329: Assert.isTrue(_client1.setSemaphore(semaphoreName, 0));
330: Assert.isFalse(_client2.setSemaphore(semaphoreName, 0));
331: _client1.releaseSemaphore(semaphoreName);
332: Assert.isTrue(_client2.setSemaphore(semaphoreName, 0));
333: _client2.close();
334: Assert.isTrue(_client1.setSemaphore(semaphoreName, 0));
335: }
336:
337: public void testSetWithDepth() {
338: Item item = storeItemToClient1AndCommit();
339: ItemHolder holder = new ItemHolder(item);
340: _client1.set(holder);
341: _client1.commit();
342: item._name = CHANGED_NAME;
343: _client1.set(holder, 3);
344: _client1.refresh(holder, 3);
345: Assert.areEqual(CHANGED_NAME, item._name);
346: }
347:
348: public void testStoredFieldIsolation() {
349: Item storedItem = storeItemToClient1AndCommit();
350: storedItem._name = CHANGED_NAME;
351: _client1.set(storedItem);
352:
353: Item retrievedItem = retrieveItemFromClient2();
354:
355: StoredClass storedClass = _client2.storedClass(Item.class);
356: StoredField storedField = storedClass.storedField(FIELD_NAME,
357: null);
358: Object retrievedName = storedField.get(retrievedItem);
359: Assert.areEqual(ORIGINAL_NAME, retrievedName);
360:
361: _client1.commit();
362:
363: retrievedName = storedField.get(retrievedItem);
364: Assert.areEqual(CHANGED_NAME, retrievedName);
365: }
366:
367: public void testStoredClasses() {
368: storeItemToClient1AndCommit();
369: StoredClass[] storedClasses = _client1.storedClasses();
370: StoredClass storedClass = _client1.storedClass(Item.class);
371: ArrayAssert.contains(storedClasses,
372: new Object[] { storedClass });
373: }
374:
375: public void testSystemInfo() {
376: SystemInfo systemInfo = _client1.systemInfo();
377: Assert.isNotNull(systemInfo);
378: Assert.isGreater(1, systemInfo.totalSize());
379: }
380:
381: public void testVersion() {
382: storeItemToClient1AndCommit();
383: Assert.isGreater(1, _client1.version());
384: }
385:
386: private void assertItemCount(EmbeddedClientObjectContainer client,
387: int count) {
388: Query query = client.query();
389: query.constrain(Item.class);
390: ObjectSet result = query.execute();
391: Assert.areEqual(count, result.size());
392: }
393:
394: protected Item storeItemToClient1AndCommit() {
395: Item storedItem = new Item(ORIGINAL_NAME);
396: _client1.set(storedItem);
397: _client1.commit();
398: return storedItem;
399: }
400:
401: private Item retrieveItemFromClient2() {
402: Query query = _client2.query();
403: query.constrain(Item.class);
404: ObjectSet objectSet = query.execute();
405: Item retrievedItem = (Item) objectSet.next();
406: return retrievedItem;
407: }
408:
409: public void setUp() throws Exception {
410: File4.delete(FILENAME);
411: Configuration config = Db4o.newConfiguration();
412: config.objectClass(Item.class).generateUUIDs(true);
413:
414: // ExtObjectServer server = Db4o.openServer(config, FILENAME, 0);
415: // EmbeddedClientObjectContainer container = server.openClient();
416:
417: _server = (LocalObjectContainer) Db4o
418: .openFile(config, FILENAME);
419: _client1 = new EmbeddedClientObjectContainer(_server);
420: _client2 = new EmbeddedClientObjectContainer(_server);
421: }
422:
423: public void tearDown() throws Exception {
424: _client1.close();
425: _client2.close();
426: _server.close();
427: }
428:
429: }
|