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.test.legacy.soda.classes.typedhierarchy;
022:
023: import com.db4o.query.*;
024: import com.db4o.test.legacy.soda.*;
025:
026: /** RTH: Roundtrip Typed Hierarchy */
027: public class STRTH1 implements STClass {
028:
029: public static transient SodaTest st;
030:
031: STRTH2 h2;
032: String foo1;
033:
034: public STRTH1() {
035: }
036:
037: public STRTH1(STRTH2 a2) {
038: h2 = a2;
039: }
040:
041: public STRTH1(String str) {
042: foo1 = str;
043: }
044:
045: public STRTH1(STRTH2 a2, String str) {
046: h2 = a2;
047: foo1 = str;
048: }
049:
050: public Object[] store() {
051:
052: STRTH1[] objects = { new STRTH1(), new STRTH1("str1"),
053: new STRTH1(new STRTH2()),
054: new STRTH1(new STRTH2("str2")),
055: new STRTH1(new STRTH2(new STRTH3("str3"))),
056: new STRTH1(new STRTH2(new STRTH3("str3"), "str2")) };
057: for (int i = 0; i < objects.length; i++) {
058: objects[i].adjustParents();
059:
060: }
061: return objects;
062: }
063:
064: /** this is the special part of this test: circular references */
065: void adjustParents() {
066: if (h2 != null) {
067: h2.parent = this ;
068: if (h2.h3 != null) {
069: h2.h3.parent = h2;
070: h2.h3.grandParent = this ;
071: }
072: }
073: }
074:
075: public void testStrNull() {
076: Query q = st.query();
077: q.constrain(new STRTH1());
078: q.descend("foo1").constrain(null);
079: Object[] r = store();
080: st.expect(q, new Object[] { r[0], r[2], r[3], r[4], r[5] });
081: }
082:
083: public void testBothNull() {
084: Query q = st.query();
085: q.constrain(new STRTH1());
086: q.descend("foo1").constrain(null);
087: q.descend("h2").constrain(null);
088: st.expectOne(q, store()[0]);
089: }
090:
091: public void testDescendantNotNull() {
092: Query q = st.query();
093: Object[] r = store();
094: q.constrain(new STRTH1());
095: q.descend("h2").constrain(null).not();
096: st.expect(q, new Object[] { r[2], r[3], r[4], r[5] });
097: }
098:
099: public void testDescendantDescendantNotNull() {
100: Query q = st.query();
101: Object[] r = store();
102: q.constrain(new STRTH1());
103: q.descend("h2").descend("h3").constrain(null).not();
104: st.expect(q, new Object[] { r[4], r[5] });
105: }
106:
107: public void testDescendantExists() {
108: Query q = st.query();
109: Object[] r = store();
110: q.constrain(r[2]);
111: st.expect(q, new Object[] { r[2], r[3], r[4], r[5] });
112: }
113:
114: public void testDescendantValue() {
115: Query q = st.query();
116: Object[] r = store();
117: q.constrain(r[3]);
118: st.expect(q, new Object[] { r[3], r[5] });
119: }
120:
121: public void testDescendantDescendantExists() {
122: Query q = st.query();
123: Object[] r = store();
124: q.constrain(new STRTH1(new STRTH2(new STRTH3())));
125: st.expect(q, new Object[] { r[4], r[5] });
126: }
127:
128: public void testDescendantDescendantValue() {
129: Query q = st.query();
130: Object[] r = store();
131: q.constrain(new STRTH1(new STRTH2(new STRTH3("str3"))));
132: st.expect(q, new Object[] { r[4], r[5] });
133: }
134:
135: public void testDescendantDescendantStringPath() {
136: Query q = st.query();
137: Object[] r = store();
138: q.constrain(new STRTH1());
139: q.descend("h2").descend("h3").descend("foo3").constrain("str3");
140: st.expect(q, new Object[] { r[4], r[5] });
141: }
142:
143: public void testSequentialAddition() {
144: Query q = st.query();
145: Object[] r = store();
146: q.constrain(new STRTH1());
147: Query cur = q.descend("h2");
148: cur.constrain(new STRTH2());
149: cur.descend("foo2").constrain("str2");
150: cur = cur.descend("h3");
151: cur.constrain(new STRTH3());
152: cur.descend("foo3").constrain("str3");
153: st.expectOne(q, store()[5]);
154: }
155:
156: public void testTwoLevelOr() {
157: Query q = st.query();
158: Object[] r = store();
159: q.constrain(new STRTH1("str1"));
160: q.descend("foo1").constraints().or(
161: q.descend("h2").descend("h3").descend("foo3")
162: .constrain("str3"));
163: st.expect(q, new Object[] { r[1], r[4], r[5] });
164: }
165:
166: public void testThreeLevelOr() {
167: Query q = st.query();
168: Object[] r = store();
169: q.constrain(new STRTH1("str1"));
170: q.descend("foo1").constraints().or(
171: q.descend("h2").descend("foo2").constrain("str2")).or(
172: q.descend("h2").descend("h3").descend("foo3")
173: .constrain("str3"));
174: st.expect(q, new Object[] { r[1], r[3], r[4], r[5] });
175: }
176: }
|