KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > legacy > soda > classes > untypedhierarchy > STRUH1


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.test.legacy.soda.classes.untypedhierarchy;
22
23 import com.db4o.query.*;
24 import com.db4o.test.legacy.soda.*;
25
26 /** RUH: RoundTrip Untyped Hierarchy */
27 public class STRUH1 implements STClass{
28     
29     public static transient SodaTest st;
30     
31     Object JavaDoc h2;
32     String JavaDoc foo1;
33     
34     public STRUH1(){
35     }
36     
37     public STRUH1(STRUH2 a2){
38         h2 = a2;
39     }
40     
41     public STRUH1(String JavaDoc str){
42         foo1 = str;
43     }
44     
45     public STRUH1(STRUH2 a2, String JavaDoc str){
46         h2 = a2;
47         foo1 = str;
48     }
49     
50     public Object JavaDoc[] store() {
51         
52         STRUH1[] objects = {
53             new STRUH1(),
54             new STRUH1("str1"),
55             new STRUH1(new STRUH2()),
56             new STRUH1(new STRUH2("str2")),
57             new STRUH1(new STRUH2(new STRUH3("str3"))),
58             new STRUH1(new STRUH2(new STRUH3("str3"), "str2"))
59         };
60         for (int i = 0; i < objects.length; i++) {
61             objects[i].adjustParents();
62             
63         }
64         return objects;
65     }
66     
67     /** this is the special part of this test: circular references */
68     void adjustParents(){
69         if(h2 != null){
70             STRUH2 th2 = (STRUH2)h2;
71             
72             th2.parent = this;
73             if(th2.h3 != null){
74                 STRUH3 th3 = (STRUH3)th2.h3;
75                 th3.parent = th2;
76                 th3.grandParent = this;
77             }
78         }
79     }
80     
81     public void testStrNull(){
82         Query q = st.query();
83         q.constrain(new STRUH1());
84         q.descend("foo1").constrain(null);
85         Object JavaDoc[] r = store();
86         st.expect(q, new Object JavaDoc[] {r[0], r[2], r[3], r[4], r[5]});
87     }
88
89     public void testBothNull(){
90         Query q = st.query();
91         q.constrain(new STRUH1());
92         q.descend("foo1").constrain(null);
93         q.descend("h2").constrain(null);
94         st.expectOne(q, store()[0]);
95     }
96
97     public void testDescendantNotNull(){
98         Query q = st.query();
99         Object JavaDoc[] r = store();
100         q.constrain(new STRUH1());
101         q.descend("h2").constrain(null).not();
102         st.expect(q, new Object JavaDoc[] {r[2], r[3], r[4], r[5]});
103     }
104     
105     public void testDescendantDescendantNotNull(){
106         Query q = st.query();
107         Object JavaDoc[] r = store();
108         q.constrain(new STRUH1());
109         q.descend("h2").descend("h3").constrain(null).not();
110         st.expect(q, new Object JavaDoc[] {r[4], r[5]});
111     }
112     
113     public void testDescendantExists(){
114         Query q = st.query();
115         Object JavaDoc[] r = store();
116         q.constrain(r[2]);
117         st.expect(q, new Object JavaDoc[] {r[2], r[3], r[4], r[5]});
118     }
119     
120     public void testDescendantValue(){
121         Query q = st.query();
122         Object JavaDoc[] r = store();
123         q.constrain(r[3]);
124         st.expect(q, new Object JavaDoc[] {r[3], r[5]});
125     }
126     
127     public void testDescendantDescendantExists(){
128         Query q = st.query();
129         Object JavaDoc[] r = store();
130         q.constrain(new STRUH1(new STRUH2(new STRUH3())));
131         st.expect(q, new Object JavaDoc[] {r[4], r[5]});
132     }
133     
134     public void testDescendantDescendantValue(){
135         Query q = st.query();
136         Object JavaDoc[] r = store();
137         q.constrain(new STRUH1(new STRUH2(new STRUH3("str3"))));
138         st.expect(q, new Object JavaDoc[] {r[4], r[5]});
139     }
140     
141     public void testDescendantDescendantStringPath(){
142         Query q = st.query();
143         Object JavaDoc[] r = store();
144         q.constrain(new STRUH1());
145         q.descend("h2").descend("h3").descend("foo3").constrain("str3");
146         st.expect(q, new Object JavaDoc[] {r[4], r[5]});
147     }
148     
149     public void testSequentialAddition(){
150         Query q = st.query();
151         Object JavaDoc[] r = store();
152         q.constrain(new STRUH1());
153         Query cur = q.descend("h2");
154         cur.constrain(new STRUH2());
155         cur.descend("foo2").constrain("str2");
156         cur = cur.descend("h3");
157         cur.constrain(new STRUH3());
158         cur.descend("foo3").constrain("str3");
159         st.expectOne(q, store()[5]);
160     }
161     
162     public void testTwoLevelOr(){
163         Query q = st.query();
164         Object JavaDoc[] r = store();
165         q.constrain(new STRUH1("str1"));
166         q.descend("foo1").constraints().or(
167             q.descend("h2").descend("h3").descend("foo3").constrain("str3")
168         );
169         st.expect(q, new Object JavaDoc[] {r[1], r[4], r[5]});
170     }
171     
172     public void testThreeLevelOr(){
173         Query q = st.query();
174         Object JavaDoc[] r = store();
175         q.constrain(new STRUH1("str1"));
176         q.descend("foo1").constraints().or(
177             q.descend("h2").descend("foo2").constrain("str2")
178         ).or(
179             q.descend("h2").descend("h3").descend("foo3").constrain("str3")
180         );
181         st.expect(q, new Object JavaDoc[] {r[1], r[3], r[4], r[5]});
182     }
183 }
184
185
Popular Tags