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.db4ounit.common.ext;
022:
023: import com.db4o.ObjectContainer;
024: import com.db4o.ObjectSet;
025: import com.db4o.ext.ExtObjectContainer;
026: import com.db4o.query.Query;
027:
028: import db4ounit.Assert;
029: import db4ounit.extensions.Db4oClientServerTestCase;
030:
031: public class RefreshTestCase extends Db4oClientServerTestCase {
032:
033: public static void main(String[] args) {
034: new RefreshTestCase().runClientServer();
035: }
036:
037: public String name;
038:
039: public RefreshTestCase child;
040:
041: public RefreshTestCase() {
042:
043: }
044:
045: public RefreshTestCase(String name, RefreshTestCase child) {
046: this .name = name;
047: this .child = child;
048: }
049:
050: protected void store() {
051: RefreshTestCase r3 = new RefreshTestCase("o3", null);
052: RefreshTestCase r2 = new RefreshTestCase("o2", r3);
053: RefreshTestCase r1 = new RefreshTestCase("o1", r2);
054: store(r1);
055: }
056:
057: public void test() {
058:
059: ExtObjectContainer oc1 = openNewClient();
060: ExtObjectContainer oc2 = openNewClient();
061:
062: // FIXME: Setting cascadeOnUpdate on an open ObjectContainer only works
063: // by accident, if the class has not been used before.
064: // Moving the configuration method here gets MTOC to pass, but
065: // configuration methods should really tell the users direclty
066: // what works and what doesn't.
067: oc2.configure().objectClass(RefreshTestCase.class)
068: .cascadeOnUpdate(true);
069:
070: try {
071: RefreshTestCase r1 = getRoot(oc1);
072: r1.name = "cc";
073: oc1.refresh(r1, 0);
074: Assert.areEqual("cc", r1.name);
075: oc1.refresh(r1, 1);
076: Assert.areEqual("o1", r1.name);
077: r1.child.name = "cc";
078: oc1.refresh(r1, 1);
079: Assert.areEqual("cc", r1.child.name);
080: oc1.refresh(r1, 2);
081: Assert.areEqual("o2", r1.child.name);
082:
083: RefreshTestCase r2 = getRoot(oc2);
084: r2.name = "o21";
085: r2.child.name = "o22";
086: r2.child.child.name = "o23";
087: oc2.set(r2);
088: oc2.commit();
089:
090: // the next line is failing
091: oc1.refresh(r1, 3);
092: // but the following works
093: // r1 = getByName(oc1, "o21");
094: Assert.areEqual("o21", r1.name);
095: Assert.areEqual("o22", r1.child.name);
096: Assert.areEqual("o23", r1.child.child.name);
097:
098: } finally {
099: oc1.close();
100: oc2.close();
101: }
102: }
103:
104: private RefreshTestCase getRoot(ObjectContainer oc) {
105: return getByName(oc, "o1");
106: }
107:
108: private RefreshTestCase getByName(ObjectContainer oc,
109: final String name) {
110: Query q = oc.query();
111: q.constrain(RefreshTestCase.class);
112: q.descend("name").constrain(name);
113: ObjectSet objectSet = q.execute();
114: return (RefreshTestCase) objectSet.next();
115: }
116:
117: }
|