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.querying;
022:
023: import com.db4o.*;
024: import com.db4o.config.*;
025: import com.db4o.foundation.*;
026:
027: import db4ounit.*;
028: import db4ounit.extensions.*;
029:
030: public class CascadeToArray extends AbstractDb4oTestCase {
031:
032: public static class Atom {
033:
034: public Atom child;
035:
036: public String name;
037:
038: public Atom() {
039: }
040:
041: public Atom(Atom child) {
042: this .child = child;
043: }
044:
045: public Atom(String name) {
046: this .name = name;
047: }
048:
049: public Atom(Atom child, String name) {
050: this (child);
051: this .name = name;
052: }
053: }
054:
055: public Object[] objects;
056:
057: protected void configure(Configuration conf) {
058: conf.objectClass(this ).cascadeOnUpdate(true);
059: conf.objectClass(this ).cascadeOnDelete(true);
060: }
061:
062: protected void store() {
063: CascadeToArray cta = new CascadeToArray();
064: cta.objects = new Object[] { new Atom("stored1"),
065: new Atom(new Atom("storedChild1"), "stored2") };
066: db().set(cta);
067: }
068:
069: public void test() throws Exception {
070: foreach(getClass(), new Visitor4() {
071: public void visit(Object obj) {
072: CascadeToArray cta = (CascadeToArray) obj;
073: for (int i = 0; i < cta.objects.length; i++) {
074: Atom atom = (Atom) cta.objects[i];
075: atom.name = "updated";
076: if (atom.child != null) {
077: // This one should NOT cascade
078: atom.child.name = "updated";
079: }
080: }
081: db().set(cta);
082: }
083: });
084:
085: reopen();
086:
087: foreach(getClass(), new Visitor4() {
088: public void visit(Object obj) {
089: CascadeToArray cta = (CascadeToArray) obj;
090: for (int i = 0; i < cta.objects.length; i++) {
091: Atom atom = (Atom) cta.objects[i];
092: Assert.areEqual("updated", atom.name);
093: if (atom.child != null) {
094: Assert.areNotEqual("updated", atom.child.name);
095: }
096: }
097: }
098: });
099:
100: // Cascade-On-Delete Test: We only want one Atom to remain.
101: db().commit();
102: reopen();
103:
104: ObjectSet os = newQuery(getClass()).execute();
105: while (os.hasNext())
106: db().delete(os.next());
107:
108: Assert.areEqual(1, countOccurences(Atom.class));
109: }
110: }
|