KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
24
25 import com.db4o.ext.*;
26
27
28 public class TransientClone {
29     
30     List list;
31     Hashtable ht;
32     String JavaDoc str;
33     int myInt;
34     Molecule[] molecules;
35
36     public void storeOne(){
37         list = new ArrayList();
38         list.add(new Atom("listAtom"));
39         list.add(this);
40         ht = new Hashtable();
41         ht.put("htc", new Molecule("htAtom"));
42         ht.put("recurse", this);
43         str = "str";
44         myInt = 100;
45         molecules = new Molecule[3];
46         for (int i = 0; i < molecules.length; i++) {
47             molecules[i] = new Molecule("arr" + i);
48             molecules[i].child = new Atom("arr" + i);
49             molecules[i].child.child = new Atom("arrc" + i);
50         }
51     }
52     
53     public void testOne(){
54         ExtObjectContainer oc = Test.objectContainer();
55         oc.activate(this, Integer.MAX_VALUE);
56         TransientClone originalValues = cmp(this, false);
57         oc.deactivate(this, Integer.MAX_VALUE);
58         TransientClone modified = cmp(originalValues, false);
59         oc.activate(this, Integer.MAX_VALUE);
60         modified.str = "changed";
61         modified.molecules[0].name = "changed";
62         str = "changed";
63         molecules[0].name = "changed";
64         oc.set(molecules[0]);
65         oc.set(this);
66         cmp(originalValues, true);
67         cmp(modified, false);
68         oc.commit();
69         cmp(modified, true);
70     }
71     
72     private TransientClone cmp(TransientClone to, boolean committed){
73         ExtObjectContainer oc = Test.objectContainer();
74         TransientClone tc = (TransientClone)oc.peekPersisted(this, Integer.MAX_VALUE, committed);
75         Test.ensure(tc != to);
76         Test.ensure(tc.list != to);
77         Test.ensure(tc.list.size() == to.list.size());
78         Iterator i = tc.list.iterator();
79         Iterator j = to.list.iterator();
80         Atom tca = (Atom)i.next();
81         Atom tct = (Atom)j.next();
82         Test.ensure(tca != tct);
83         Test.ensure(tca.name.equals(tct.name));
84         Test.ensure(i.next() == tc);
85         Test.ensure(j.next() == to);
86         Test.ensure(tc.ht != to.ht);
87         Molecule tcm = (Molecule)tc.ht.get("htc");
88         Molecule tom = (Molecule)to.ht.get("htc");
89         Test.ensure(tcm != tom);
90         Test.ensure(tcm.name.equals(tom.name));
91         Test.ensure(tc.ht.get("recurse") == tc);
92         Test.ensure(to.ht.get("recurse") == to);
93         Test.ensure(tc.str.equals(to.str));
94         Test.ensure(tc.myInt == to.myInt);
95         Test.ensure(tc.molecules.length == to.molecules.length);
96         Test.ensure(tc.molecules.length == to.molecules.length);
97         tcm = tc.molecules[0];
98         tom = to.molecules[0];
99         Test.ensure(tcm != tom);
100         Test.ensure(tcm.name.equals(tom.name));
101         Test.ensure(tcm.child != tom.child);
102         Test.ensure(tcm.child.name.equals(tom.child.name));
103         return tc;
104     }
105 }
106
Popular Tags