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.concurrency;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.db4ounit.common.persistent.*;
026: import com.db4o.ext.*;
027: import com.db4o.query.*;
028:
029: import db4ounit.*;
030: import db4ounit.extensions.*;
031:
032: public class IndexedByIdentityTestCase extends Db4oClientServerTestCase {
033: public static void main(String[] args) {
034: new IndexedByIdentityTestCase().runConcurrency();
035: }
036:
037: public Atom atom;
038:
039: static final int COUNT = 10;
040:
041: protected void configure(Configuration config) {
042: config.objectClass(this ).objectField("atom").indexed(true);
043: config.objectClass(IndexedByIdentityTestCase.class)
044: .cascadeOnUpdate(true);
045:
046: }
047:
048: protected void store() {
049: for (int i = 0; i < COUNT; i++) {
050: IndexedByIdentityTestCase ibi = new IndexedByIdentityTestCase();
051: ibi.atom = new Atom("ibi" + i);
052: store(ibi);
053: }
054: }
055:
056: public void concRead(ExtObjectContainer oc) {
057: for (int i = 0; i < COUNT; i++) {
058: Query q = oc.query();
059: q.constrain(Atom.class);
060: q.descend("name").constrain("ibi" + i);
061: ObjectSet objectSet = q.execute();
062: Assert.areEqual(1, objectSet.size());
063: Atom child = (Atom) objectSet.next();
064: q = oc.query();
065: q.constrain(IndexedByIdentityTestCase.class);
066: q.descend("atom").constrain(child).identity();
067: objectSet = q.execute();
068: Assert.areEqual(1, objectSet.size());
069: IndexedByIdentityTestCase ibi = (IndexedByIdentityTestCase) objectSet
070: .next();
071: Assert.areSame(child, ibi.atom);
072: }
073:
074: }
075:
076: public void concUpdate(ExtObjectContainer oc, int seq)
077: throws Exception {
078: Query q = oc.query();
079: q.constrain(IndexedByIdentityTestCase.class);
080: ObjectSet os = q.execute();
081: Assert.areEqual(COUNT, os.size());
082: while (os.hasNext()) {
083: IndexedByIdentityTestCase idi = (IndexedByIdentityTestCase) os
084: .next();
085: idi.atom.name = "updated" + seq;
086: oc.set(idi);
087: Thread.sleep(100);
088: }
089: }
090:
091: public void checkUpdate(ExtObjectContainer oc) {
092: Query q = oc.query();
093: q.constrain(IndexedByIdentityTestCase.class);
094: ObjectSet os = q.execute();
095: Assert.areEqual(COUNT, os.size());
096: String expected = null;
097: while (os.hasNext()) {
098: IndexedByIdentityTestCase idi = (IndexedByIdentityTestCase) os
099: .next();
100: if (expected == null) {
101: expected = idi.atom.name;
102: Assert.isTrue(expected.startsWith("updated"));
103: Assert.isTrue(expected.length() > "updated".length());
104: }
105: Assert.areEqual(expected, idi.atom.name);
106: }
107: }
108:
109: }
|