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;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.db4ounit.util.*;
028: import com.db4o.query.*;
029:
030: import db4ounit.*;
031: import db4ounit.extensions.*;
032:
033: public class CollectionIndexedJoinTestCase extends AbstractDb4oTestCase {
034:
035: private static final String COLLECTIONFIELDNAME = "_data";
036: private static final String IDFIELDNAME = "_id";
037: private static final int NUMENTRIES = 3;
038:
039: public static class DataHolder {
040: public Vector _data;
041:
042: public DataHolder(int id) {
043: _data = new Vector();
044: _data.addElement(new Data(id));
045: }
046: }
047:
048: public static class Data {
049: public int _id;
050:
051: public Data(int id) {
052: this ._id = id;
053: }
054: }
055:
056: protected void configure(Configuration config) {
057: config.objectClass(Data.class).objectField(IDFIELDNAME)
058: .indexed(true);
059: }
060:
061: protected void store() throws Exception {
062: for (int i = 0; i < NUMENTRIES; i++) {
063: store(new DataHolder(i));
064: }
065: }
066:
067: public void testIndexedOrTwo() {
068: assertIndexedOr(new int[] { 0, 1, -1 }, 2);
069: }
070:
071: private void assertIndexedOr(int[] values, int expectedResultCount) {
072: TestConfig config = new TestConfig(values.length);
073: while (config.moveNext()) {
074: assertIndexedOr(values, expectedResultCount, config
075: .rootIndex(), config.connectLeft());
076: }
077: }
078:
079: public void testIndexedOrAll() {
080: assertIndexedOr(new int[] { 0, 1, 2 }, 3);
081: }
082:
083: public void testTwoJoinLegs() {
084: Query query = newQuery(DataHolder.class).descend(
085: COLLECTIONFIELDNAME);
086: Constraint left = query.descend(IDFIELDNAME).constrain(
087: new Integer(0));
088: left.or(query.descend(IDFIELDNAME).constrain(new Integer(1)));
089: Constraint right = query.descend(IDFIELDNAME).constrain(
090: new Integer(2));
091: right.or(query.descend(IDFIELDNAME).constrain(new Integer(-1)));
092: left.or(right);
093: ObjectSet result = query.execute();
094: Assert.areEqual(3, result.size());
095: }
096:
097: public void assertIndexedOr(int[] values, int expectedResultCount,
098: int rootIdx, boolean connectLeft) {
099: Query query = newQuery(DataHolder.class).descend(
100: COLLECTIONFIELDNAME);
101: Constraint constraint = query.descend(IDFIELDNAME).constrain(
102: new Integer(values[rootIdx]));
103: for (int idx = 0; idx < values.length; idx++) {
104: if (idx != rootIdx) {
105: Constraint curConstraint = query.descend(IDFIELDNAME)
106: .constrain(new Integer(values[idx]));
107: if (connectLeft) {
108: constraint.or(curConstraint);
109: } else {
110: curConstraint.or(constraint);
111: }
112: }
113: }
114: ObjectSet result = query.execute();
115: Assert.areEqual(expectedResultCount, result.size());
116: }
117:
118: private static class TestConfig extends PermutingTestConfig {
119: public TestConfig(int numValues) {
120: super (new Object[][] {
121: new Object[] { new Integer(0),
122: new Integer(numValues - 1) },
123: new Object[] { Boolean.FALSE, Boolean.TRUE } });
124: }
125:
126: public int rootIndex() {
127: return ((Integer) current(0)).intValue();
128: }
129:
130: public boolean connectLeft() {
131: return ((Boolean) current(1)).booleanValue();
132: }
133: }
134: }
|