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 java.util.*;
024:
025: import com.db4o.ext.*;
026:
027: public class TransientClone {
028:
029: List list;
030: Hashtable ht;
031: String str;
032: int myInt;
033: Molecule[] molecules;
034:
035: public void storeOne() {
036: list = new ArrayList();
037: list.add(new Atom("listAtom"));
038: list.add(this );
039: ht = new Hashtable();
040: ht.put("htc", new Molecule("htAtom"));
041: ht.put("recurse", this );
042: str = "str";
043: myInt = 100;
044: molecules = new Molecule[3];
045: for (int i = 0; i < molecules.length; i++) {
046: molecules[i] = new Molecule("arr" + i);
047: molecules[i].child = new Atom("arr" + i);
048: molecules[i].child.child = new Atom("arrc" + i);
049: }
050: }
051:
052: public void testOne() {
053: ExtObjectContainer oc = Test.objectContainer();
054: oc.activate(this , Integer.MAX_VALUE);
055: TransientClone originalValues = peekPersisted(false);
056: cmp(this , originalValues);
057: oc.deactivate(this , Integer.MAX_VALUE);
058: TransientClone modified = peekPersisted(false);
059: cmp(originalValues, modified);
060: oc.activate(this , Integer.MAX_VALUE);
061:
062: modified.str = "changed";
063: modified.molecules[0].name = "changed";
064: str = "changed";
065: molecules[0].name = "changed";
066: oc.set(molecules[0]);
067: oc.set(this );
068:
069: TransientClone tc = peekPersisted(true);
070: cmp(originalValues, tc);
071:
072: tc = peekPersisted(false);
073: cmp(modified, tc);
074:
075: oc.commit();
076: tc = peekPersisted(true);
077: cmp(modified, tc);
078: }
079:
080: private TransientClone cmp(TransientClone to, TransientClone tc) {
081: Test.ensure(tc != to);
082: Test.ensure(tc.list != to);
083: Test.ensure(tc.list.size() == to.list.size());
084: Iterator i = tc.list.iterator();
085: Iterator j = to.list.iterator();
086: Atom tca = (Atom) i.next();
087: Atom tct = (Atom) j.next();
088: Test.ensure(tca != tct);
089: Test.ensure(tca.name.equals(tct.name));
090: Test.ensure(i.next() == tc);
091: Test.ensure(j.next() == to);
092: Test.ensure(tc.ht != to.ht);
093: Molecule tcm = (Molecule) tc.ht.get("htc");
094: Molecule tom = (Molecule) to.ht.get("htc");
095: Test.ensure(tcm != tom);
096: Test.ensure(tcm.name.equals(tom.name));
097: Test.ensure(tc.ht.get("recurse") == tc);
098: Test.ensure(to.ht.get("recurse") == to);
099: Test.ensure(tc.str.equals(to.str));
100: Test.ensure(tc.myInt == to.myInt);
101: Test.ensure(tc.molecules.length == to.molecules.length);
102: Test.ensure(tc.molecules.length == to.molecules.length);
103: tcm = tc.molecules[0];
104: tom = to.molecules[0];
105: Test.ensure(tcm != tom);
106: Test.ensure(tcm.name.equals(tom.name));
107: Test.ensure(tcm.child != tom.child);
108: Test.ensure(tcm.child.name.equals(tom.child.name));
109: return tc;
110: }
111:
112: private TransientClone peekPersisted(boolean committed) {
113: ExtObjectContainer oc = Test.objectContainer();
114: TransientClone tc = (TransientClone) oc.peekPersisted(this,
115: Integer.MAX_VALUE, committed);
116: return tc;
117: }
118: }
|