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.querying;
022:
023: import com.db4o.config.*;
024: import com.db4o.db4ounit.common.btree.*;
025: import com.db4o.db4ounit.common.foundation.*;
026: import com.db4o.foundation.*;
027: import com.db4o.internal.*;
028: import com.db4o.internal.query.processor.*;
029: import com.db4o.internal.query.result.*;
030: import com.db4o.query.*;
031:
032: import db4ounit.extensions.*;
033: import db4ounit.extensions.fixtures.*;
034:
035: public abstract class QueryResultTestCase extends AbstractDb4oTestCase
036: implements OptOutCS, OptOutDefragSolo {
037:
038: private static final int[] VALUES = new int[] { 1, 5, 6, 7, 9 };
039:
040: private final int[] itemIds = new int[VALUES.length];
041:
042: private int idForGetAll;
043:
044: protected void configure(Configuration config) {
045: indexField(config, Item.class, "foo");
046: }
047:
048: public void testClassQuery() {
049: assertIDs(classOnlyQuery(), itemIds);
050: }
051:
052: public void testGetAll() {
053: AbstractQueryResult queryResult = newQueryResult();
054: queryResult.loadFromClassIndexes(stream().classCollection()
055: .iterator());
056: int[] ids = IntArrays4.concat(itemIds,
057: new int[] { idForGetAll });
058: assertIDs(queryResult, ids, true);
059: }
060:
061: public void testIndexedFieldQuery() {
062: Query query = newItemQuery();
063: query.descend("foo").constrain(new Integer(6)).smaller();
064: QueryResult queryResult = executeQuery(query);
065: assertIDs(queryResult, new int[] { itemIds[0], itemIds[1] });
066: }
067:
068: public void testNonIndexedFieldQuery() {
069: Query query = newItemQuery();
070: query.descend("bar").constrain(new Integer(6)).smaller();
071: QueryResult queryResult = executeQuery(query);
072: assertIDs(queryResult, new int[] { itemIds[0], itemIds[1] });
073: }
074:
075: private QueryResult classOnlyQuery() {
076: AbstractQueryResult queryResult = newQueryResult();
077: queryResult.loadFromClassIndex(yapClass());
078: return queryResult;
079: }
080:
081: private ClassMetadata yapClass() {
082: return stream().classMetadataForReflectClass(
083: reflector().forClass(Item.class));
084: }
085:
086: private QueryResult executeQuery(Query query) {
087: AbstractQueryResult queryResult = newQueryResult();
088: queryResult.loadFromQuery((QQuery) query);
089: return queryResult;
090: }
091:
092: private void assertIDs(QueryResult queryResult, int[] expectedIDs) {
093: assertIDs(queryResult, expectedIDs, false);
094: }
095:
096: private void assertIDs(QueryResult queryResult, int[] expectedIDs,
097: boolean ignoreUnexpected) {
098: ExpectingVisitor expectingVisitor = new ExpectingVisitor(
099: IntArrays4.toObjectArray(expectedIDs), false,
100: ignoreUnexpected);
101: IntIterator4 i = queryResult.iterateIDs();
102: while (i.moveNext()) {
103: expectingVisitor.visit(new Integer(i.currentInt()));
104: }
105: expectingVisitor.assertExpectations();
106: }
107:
108: protected Query newItemQuery() {
109: return newQuery(Item.class);
110: }
111:
112: protected void store() throws Exception {
113: storeItems(VALUES);
114: ItemForGetAll ifga = new ItemForGetAll();
115: store(ifga);
116: idForGetAll = (int) db().getID(ifga);
117: }
118:
119: protected void storeItems(final int[] foos) {
120: for (int i = 0; i < foos.length; i++) {
121: Item item = new Item(foos[i]);
122: store(item);
123: itemIds[i] = (int) db().getID(item);
124: }
125: }
126:
127: public static class Item {
128:
129: public int foo;
130:
131: public int bar;
132:
133: public Item() {
134:
135: }
136:
137: public Item(int foo_) {
138: foo = foo_;
139: bar = foo;
140: }
141:
142: }
143:
144: public static class ItemForGetAll {
145:
146: }
147:
148: protected abstract AbstractQueryResult newQueryResult();
149:
150: }
|