01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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.*;
26: import com.db4o.foundation.*;
27:
28: public class CascadeToVector {
29:
30: public Vector vec;
31:
32: public void configure() {
33: Db4o.configure().objectClass(this ).cascadeOnUpdate(true);
34: Db4o.configure().objectClass(this ).cascadeOnDelete(true);
35: }
36:
37: public void store() {
38: Test.deleteAllInstances(this );
39: Test.deleteAllInstances(new Atom());
40: CascadeToVector ctv = new CascadeToVector();
41: ctv.vec = new Vector();
42: ctv.vec.addElement(new Atom("stored1"));
43: ctv.vec
44: .addElement(new Atom(new Atom("storedChild1"),
45: "stored2"));
46: Test.store(ctv);
47: }
48:
49: public void test() {
50:
51: Test.forEach(this , new Visitor4() {
52: public void visit(Object obj) {
53: CascadeToVector ctv = (CascadeToVector) obj;
54: Enumeration i = ctv.vec.elements();
55: while (i.hasMoreElements()) {
56: Atom atom = (Atom) i.nextElement();
57: atom.name = "updated";
58: if (atom.child != null) {
59: // This one should NOT cascade
60: atom.child.name = "updated";
61: }
62: }
63: Test.store(ctv);
64: }
65: });
66: Test.reOpen();
67:
68: Test.forEach(this , new Visitor4() {
69: public void visit(Object obj) {
70: CascadeToVector ctv = (CascadeToVector) obj;
71: Enumeration i = ctv.vec.elements();
72: while (i.hasMoreElements()) {
73: Atom atom = (Atom) i.nextElement();
74: Test.ensure(atom.name.equals("updated"));
75: if (atom.child != null) {
76: Test.ensure(!atom.child.name.equals("updated"));
77: }
78: }
79: }
80: });
81:
82: // Cascade-On-Delete Test: We only want one atom to remain.
83:
84: Test.reOpen();
85: Test.deleteAllInstances(this );
86: Test.ensureOccurrences(new Atom(), 1);
87: }
88: }
|