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