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