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.test;
022:
023: import com.db4o.*;
024: import com.db4o.ext.*;
025: import com.db4o.query.*;
026:
027: public class TwoClients extends AllTestsConfAll {
028:
029: public void test() {
030: if (Test.clientServer) {
031: Test.deleteAllInstances(new Atom());
032: Test.commit();
033:
034: ExtObjectContainer client1 = Test.objectContainer();
035: ExtObjectContainer client2 = Test.openClient();
036: Atom a_1_1 = new Atom("One");
037: Atom a_1_2 = new Atom("Two");
038: Atom a_1_3 = new Atom("Three");
039: client1.set(a_1_1);
040: client1.set(a_1_2);
041: client1.set(a_1_3);
042: ensureAtomCount(client2, null, 0);
043: Test.commitSync(client1, client2);
044: ensureAtomCount(client2, null, 3);
045: Atom a_2_1 = (Atom) client2.get(new Atom("One")).next();
046: a_1_1.child = new Atom("OneChild");
047: client1.set(a_1_1);
048: ensureAtomCount(client2, null, 3);
049: Test.commitSync(client1, client2);
050: ensureAtomCount(client2, null, 4);
051: client2.deactivate(a_2_1, Integer.MAX_VALUE);
052: client2.activate(a_2_1, Integer.MAX_VALUE);
053: Test.ensure(a_2_1.child.name.equals("OneChild"));
054: a_2_1.name = "Zulu";
055: client2.set(a_2_1);
056:
057: Atom a_1_4 = new Atom("Zorro");
058: client1.set(a_1_4);
059: Atom a_1_5 = new Atom("Zzerk");
060: client1.set(a_1_5);
061:
062: ensureAtomCount(client1, "Zulu", 0);
063:
064: Test.commitSync(client2, client1);
065:
066: ensureAtomCount(client1, "Zulu", 1);
067:
068: Query q = client1.query();
069: q.constrain(Atom.class);
070: q.descend("name").constrain("Zulu");
071: ObjectSet os = q.execute();
072: Atom q_1_1 = (Atom) os.next();
073:
074: Test.ensure(a_1_1 == q_1_1);
075: a_1_1.name = "Bozo";
076: client1.set(a_1_1);
077: a_1_1.child.name = "BozoChild";
078: client1.set(a_1_1.child);
079: a_1_4.name = "Bozo";
080: client1.set(a_1_4);
081: a_1_5.name = "Cue";
082: client1.set(a_1_5);
083:
084: client2.refresh(a_2_1, Integer.MAX_VALUE);
085: Test.ensure(a_2_1.name.equals("Zulu"));
086: Test.ensure(a_2_1.child.name.equals("OneChild"));
087: ensureAtomCount(client2, "Bozo", 0);
088:
089: Test.commitSync(client1, client2);
090:
091: client2.refresh(a_2_1, Integer.MAX_VALUE);
092: Test.ensure(a_2_1.name.equals("Bozo"));
093: Test.ensure(a_2_1.child.name.equals("BozoChild"));
094: ensureAtomCount(client2, "Bozo", 2);
095: ensureAtomCount(client2, "Cue", 1);
096: ensureAtomCount(client2, "BozoChild", 1);
097:
098: client2.close();
099: }
100: }
101:
102: private void ensureAtomCount(ObjectContainer con, String name,
103: int count) {
104:
105: // try five times
106: // commit timing might cause delay to see result
107: for (int i = 0; i < 5; i++) {
108: Query q = con.query();
109: q.constrain(Atom.class);
110: if (name != null) {
111: q.descend("name").constrain(name);
112: }
113: if (q.execute().size() == count) {
114: Test.assertionCount++;
115: return;
116: }
117: }
118: Test.error();
119: }
120:
121: }
|