KickJava   Java API By Example, From Geeks To Geeks.

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


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 Refresh extends AllTests {
28
29     public String JavaDoc name;
30
31     public Refresh child;
32
33     public Refresh() {
34
35     }
36
37     public Refresh(String JavaDoc name, Refresh child) {
38         this.name = name;
39         this.child = child;
40     }
41
42     public void store() {
43         Refresh r3 = new Refresh("o3", null);
44         Refresh r2 = new Refresh("o2", r3);
45         Refresh r1 = new Refresh("o1", r2);
46         Test.store(r1);
47     }
48
49     public void test() {
50         ExtObjectContainer oc1 = Test.objectContainer();
51         Refresh r11 = getRoot(oc1);
52         r11.name = "cc";
53         oc1.refresh(r11, 0);
54         Test.ensure(r11.name.equals("cc"));
55         oc1.refresh(r11, 1);
56         Test.ensure(r11.name.equals("o1"));
57         r11.child.name = "cc";
58         oc1.refresh(r11, 1);
59         Test.ensure(r11.child.name.equals("cc"));
60         oc1.refresh(r11, 2);
61         Test.ensure(r11.child.name.equals("o2"));
62
63         if (Test.isClientServer()) {
64             ExtObjectContainer oc2 = null;
65             try {
66                 oc2 = Db4o.openClient(SERVER_HOSTNAME, SERVER_PORT, DB4O_USER,
67                         DB4O_PASSWORD).ext();
68             } catch (Exception JavaDoc e) {
69                 e.printStackTrace();
70                 return;
71             }
72
73             Refresh r12 = getRoot(oc2);
74             Refresh r32 = r12.child.child;
75
76             r11.child.name = "n2";
77             r11.child.child.name = "n3";
78             r11.child.child.child = new Refresh("n4", null);
79             oc1.set(r11.child.child);
80             oc1.set(r11.child);
81             oc1.set(r11);
82
83             oc2.refresh(r12, Integer.MAX_VALUE);
84             Test.ensure(r12.child.name.equals("o2"));
85
86             Test.commitSync(oc1,oc2);
87
88             oc2.refresh(r12, Integer.MAX_VALUE);
89             Test.ensure(r12.child.name.equals("n2"));
90             Test.ensure(r12.child.child.name.equals("n3"));
91             Test.ensure(r12.child.child.child.name.equals("n4"));
92
93             r11.child.child.child = null;
94             oc1.set(r11.child.child);
95             Test.commitSync(oc1,oc2);
96
97             oc2.refresh(r12, Integer.MAX_VALUE);
98             Test.ensure(r12.child.child.child == null);
99
100             r11.child.child = new Refresh("nn2", null);
101             oc1.set(r11.child);
102             Test.commitSync(oc1,oc2);
103
104             oc2.refresh(r12, Integer.MAX_VALUE);
105             Test.ensure(r12.child.child != r32);
106             Test.ensure(r12.child.child.name.equals("nn2"));
107
108             oc2.close();
109         }
110
111     }
112     
113     private void commitAndWait(ObjectContainer client1, ObjectContainer client2){
114     }
115
116     private Refresh getRoot(ObjectContainer oc) {
117         Query q = oc.query();
118         q.constrain(Refresh.class);
119         q.descend("name").constrain("o1");
120         ObjectSet objectSet = q.execute();
121         return (Refresh) objectSet.next();
122     }
123
124 }
125
Popular Tags