KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > querying > CascadeToArray


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.db4ounit.common.querying;
22
23 import com.db4o.*;
24 import com.db4o.config.*;
25 import com.db4o.foundation.*;
26
27 import db4ounit.*;
28 import db4ounit.extensions.*;
29
30 public class CascadeToArray extends AbstractDb4oTestCase {
31
32     public static class Atom {
33
34         public Atom child;
35
36         public String JavaDoc name;
37
38         public Atom() {
39         }
40
41         public Atom(Atom child) {
42             this.child = child;
43         }
44
45         public Atom(String JavaDoc name) {
46             this.name = name;
47         }
48
49         public Atom(Atom child, String JavaDoc name) {
50             this(child);
51             this.name = name;
52         }
53     }
54
55     public Object JavaDoc[] objects;
56
57     protected void configure(Configuration conf) {
58         conf.objectClass(this).cascadeOnUpdate(true);
59         conf.objectClass(this).cascadeOnDelete(true);
60     }
61
62     protected void store() {
63         CascadeToArray cta = new CascadeToArray();
64         cta.objects = new Object JavaDoc[] { new Atom("stored1"),
65                 new Atom(new Atom("storedChild1"), "stored2") };
66         db().set(cta);
67     }
68
69     public void test() throws Exception JavaDoc {
70         foreach(getClass(), new Visitor4() {
71             public void visit(Object JavaDoc obj) {
72                 CascadeToArray cta = (CascadeToArray) obj;
73                 for (int i = 0; i < cta.objects.length; i++) {
74                     Atom atom = (Atom) cta.objects[i];
75                     atom.name = "updated";
76                     if (atom.child != null) {
77                         // This one should NOT cascade
78
atom.child.name = "updated";
79                     }
80                 }
81                 db().set(cta);
82             }
83         });
84
85         reopen();
86
87         foreach(getClass(), new Visitor4() {
88             public void visit(Object JavaDoc obj) {
89                 CascadeToArray cta = (CascadeToArray) obj;
90                 for (int i = 0; i < cta.objects.length; i++) {
91                     Atom atom = (Atom) cta.objects[i];
92                     Assert.areEqual("updated", atom.name);
93                     if (atom.child != null) {
94                         Assert.areNotEqual("updated", atom.child.name);
95                     }
96                 }
97             }
98         });
99
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 }
111
Popular Tags