KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > TwoClients


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test;
22
23 import com.db4o.*;
24 import com.db4o.ext.*;
25 import com.db4o.query.*;
26
27 public class TwoClients extends AllTestsConfAll{
28     
29     public void test(){
30         if(Test.clientServer){
31             Test.deleteAllInstances(new Atom());
32             Test.commit();
33
34             ExtObjectContainer client1 = Test.objectContainer();
35             ExtObjectContainer client2 = Test.openClient();
36             Atom a_1_1 = new Atom("One");
37             Atom a_1_2 = new Atom("Two");
38             Atom a_1_3 = new Atom("Three");
39             client1.set(a_1_1);
40             client1.set(a_1_2);
41             client1.set(a_1_3);
42             ensureAtomCount(client2,null, 0);
43             Test.commitSync(client1, client2);
44             ensureAtomCount(client2,null, 3);
45             Atom a_2_1 = (Atom)client2.get(new Atom("One")).next();
46             a_1_1.child = new Atom("OneChild");
47             client1.set(a_1_1);
48             ensureAtomCount(client2,null, 3);
49             Test.commitSync(client1, client2);
50             ensureAtomCount(client2,null, 4);
51             client2.deactivate(a_2_1, Integer.MAX_VALUE);
52             client2.activate(a_2_1, Integer.MAX_VALUE);
53             Test.ensure(a_2_1.child.name.equals("OneChild"));
54             a_2_1.name = "Zulu";
55             client2.set(a_2_1);
56             
57             Atom a_1_4 = new Atom("Zorro");
58             client1.set(a_1_4);
59             Atom a_1_5 = new Atom("Zzerk");
60             client1.set(a_1_5);
61             
62             ensureAtomCount(client1, "Zulu", 0);
63             
64             Test.commitSync(client2, client1);
65             
66             ensureAtomCount(client1, "Zulu", 1);
67
68             
69             Query q = client1.query();
70             q.constrain(Atom.class);
71             q.descend("name").constrain("Zulu");
72             ObjectSet os = q.execute();
73             Atom q_1_1 = (Atom)os.next();
74             
75             Test.ensure(a_1_1 == q_1_1);
76             a_1_1.name = "Bozo";
77             client1.set(a_1_1);
78             a_1_1.child.name = "BozoChild";
79             client1.set(a_1_1.child);
80             a_1_4.name = "Bozo";
81             client1.set(a_1_4);
82             a_1_5.name = "Cue";
83             client1.set(a_1_5);
84             
85             client2.refresh(a_2_1, Integer.MAX_VALUE);
86             Test.ensure(a_2_1.name.equals("Zulu"));
87             Test.ensure(a_2_1.child.name.equals("OneChild"));
88             ensureAtomCount(client2, "Bozo", 0);
89             
90             Test.commitSync(client1, client2);
91             
92             client2.refresh(a_2_1, Integer.MAX_VALUE);
93             Test.ensure(a_2_1.name.equals("Bozo"));
94             Test.ensure(a_2_1.child.name.equals("BozoChild"));
95             ensureAtomCount(client2, "Bozo", 2);
96             ensureAtomCount(client2, "Cue", 1);
97             ensureAtomCount(client2, "BozoChild", 1);
98             
99             client2.close();
100         }
101     }
102
103     private void ensureAtomCount(ObjectContainer con, String JavaDoc name, 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     
122     
123     
124     
125 }
126
Popular Tags