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.*;
024: import com.db4o.config.*;
025: import com.db4o.query.*;
026:
027: import db4ounit.*;
028: import db4ounit.extensions.*;
029:
030: public class IndexedQueriesTestCase extends AbstractDb4oTestCase {
031:
032: public static void main(String[] arguments) {
033: new IndexedQueriesTestCase().runSolo();
034: }
035:
036: public static class IndexedQueriesItem {
037:
038: public String _name;
039:
040: public int _int;
041:
042: public Integer _integer;
043:
044: public IndexedQueriesItem() {
045: }
046:
047: public IndexedQueriesItem(String name) {
048: _name = name;
049: }
050:
051: public IndexedQueriesItem(int int_) {
052: _int = int_;
053: _integer = new Integer(int_);
054: }
055:
056: }
057:
058: protected void configure(Configuration config) {
059: indexField(config, "_name");
060: indexField(config, "_int");
061: indexField(config, "_integer");
062: }
063:
064: private void indexField(Configuration config, String fieldName) {
065: indexField(config, IndexedQueriesItem.class, fieldName);
066: }
067:
068: protected void store() {
069: String[] strings = new String[] { "a", "c", "b", "f", "e" };
070: for (int i = 0; i < strings.length; i++) {
071: db().set(new IndexedQueriesItem(strings[i]));
072: }
073:
074: int[] ints = new int[] { 1, 5, 7, 3, 2, 3 };
075: for (int i = 0; i < ints.length; i++) {
076: db().set(new IndexedQueriesItem(ints[i]));
077: }
078: }
079:
080: public void testIntQuery() {
081: assertInts(5);
082: }
083:
084: /**
085: * @sharpen.ignore testing Integer queries only makes sense for java
086: */
087: public void testIntegerQuery() {
088: assertIntegers();
089: }
090:
091: public void testStringQuery() throws Exception {
092:
093: assertNullNameCount(6);
094:
095: db().set(new IndexedQueriesItem("d"));
096: assertQuery(1, "b");
097:
098: updateB();
099:
100: db().set(new IndexedQueriesItem("z"));
101: db().set(new IndexedQueriesItem("y"));
102:
103: reopen();
104: assertQuery(1, "b");
105:
106: assertInts(8);
107: }
108:
109: private void assertIntegers() {
110: Query q = newQuery();
111: q.descend("_integer").constrain(new Integer(4)).greater()
112: .equal();
113: assertIntsFound(new int[] { 5, 7 }, q);
114:
115: q = newQuery();
116: q.descend("_integer").constrain(new Integer(4)).smaller();
117: assertIntsFound(new int[] { 1, 2, 3, 3 }, q);
118:
119: }
120:
121: private void assertInts(int expectedZeroSize) {
122:
123: Query q = newQuery();
124: q.descend("_int").constrain(new Integer(0));
125: int zeroSize = q.execute().size();
126: Assert.areEqual(expectedZeroSize, zeroSize);
127:
128: q = newQuery();
129: q.descend("_int").constrain(new Integer(4)).greater().equal();
130: assertIntsFound(new int[] { 5, 7 }, q);
131:
132: q = newQuery();
133: q.descend("_int").constrain(new Integer(4)).greater();
134: assertIntsFound(new int[] { 5, 7 }, q);
135:
136: q = newQuery();
137: q.descend("_int").constrain(new Integer(3)).greater();
138: assertIntsFound(new int[] { 5, 7 }, q);
139:
140: q = newQuery();
141: q.descend("_int").constrain(new Integer(3)).greater().equal();
142: assertIntsFound(new int[] { 3, 3, 5, 7 }, q);
143:
144: q = newQuery();
145: q.descend("_int").constrain(new Integer(2)).greater().equal();
146: assertIntsFound(new int[] { 2, 3, 3, 5, 7 }, q);
147:
148: q = newQuery();
149: q.descend("_int").constrain(new Integer(2)).greater();
150: assertIntsFound(new int[] { 3, 3, 5, 7 }, q);
151:
152: q = newQuery();
153: q.descend("_int").constrain(new Integer(1)).greater().equal();
154: assertIntsFound(new int[] { 1, 2, 3, 3, 5, 7 }, q);
155:
156: q = newQuery();
157: q.descend("_int").constrain(new Integer(1)).greater();
158: assertIntsFound(new int[] { 2, 3, 3, 5, 7 }, q);
159:
160: q = newQuery();
161: q.descend("_int").constrain(new Integer(4)).smaller();
162: assertIntsFound(new int[] { 1, 2, 3, 3 }, expectedZeroSize, q);
163:
164: q = newQuery();
165: q.descend("_int").constrain(new Integer(4)).smaller().equal();
166: assertIntsFound(new int[] { 1, 2, 3, 3 }, expectedZeroSize, q);
167:
168: q = newQuery();
169: q.descend("_int").constrain(new Integer(3)).smaller();
170: assertIntsFound(new int[] { 1, 2 }, expectedZeroSize, q);
171:
172: q = newQuery();
173: q.descend("_int").constrain(new Integer(3)).smaller().equal();
174: assertIntsFound(new int[] { 1, 2, 3, 3 }, expectedZeroSize, q);
175:
176: q = newQuery();
177: q.descend("_int").constrain(new Integer(2)).smaller().equal();
178: assertIntsFound(new int[] { 1, 2 }, expectedZeroSize, q);
179:
180: q = newQuery();
181: q.descend("_int").constrain(new Integer(2)).smaller();
182: assertIntsFound(new int[] { 1 }, expectedZeroSize, q);
183:
184: q = newQuery();
185: q.descend("_int").constrain(new Integer(1)).smaller().equal();
186: assertIntsFound(new int[] { 1 }, expectedZeroSize, q);
187:
188: q = newQuery();
189: q.descend("_int").constrain(new Integer(1)).smaller();
190: assertIntsFound(new int[] {}, expectedZeroSize, q);
191:
192: }
193:
194: private void assertIntsFound(int[] ints, int zeroSize, Query q) {
195: ObjectSet res = q.execute();
196: Assert.areEqual((ints.length + zeroSize), res.size());
197: while (res.hasNext()) {
198: IndexedQueriesItem ci = (IndexedQueriesItem) res.next();
199: for (int i = 0; i < ints.length; i++) {
200: if (ints[i] == ci._int) {
201: ints[i] = 0;
202: break;
203: }
204: }
205: }
206: for (int i = 0; i < ints.length; i++) {
207: Assert.areEqual(0, ints[i]);
208: }
209: }
210:
211: private void assertIntsFound(int[] ints, Query q) {
212: assertIntsFound(ints, 0, q);
213: }
214:
215: private void assertQuery(int count, String string) {
216: ObjectSet res = queryForName(string);
217: Assert.areEqual(count, res.size());
218:
219: IndexedQueriesItem item = (IndexedQueriesItem) res.next();
220: Assert.areEqual("b", item._name);
221: }
222:
223: private void assertNullNameCount(int count) {
224: ObjectSet res = queryForName(null);
225: Assert.areEqual(count, res.size());
226: while (res.hasNext()) {
227: IndexedQueriesItem ci = (IndexedQueriesItem) res.next();
228: Assert.isNull(ci._name);
229: }
230: }
231:
232: private void updateB() {
233: ObjectSet res = queryForName("b");
234: IndexedQueriesItem ci = (IndexedQueriesItem) res.next();
235: ci._name = "j";
236: db().set(ci);
237: res = queryForName("b");
238: Assert.areEqual(0, res.size());
239: res = queryForName("j");
240: Assert.areEqual(1, res.size());
241: ci._name = "b";
242: db().set(ci);
243: assertQuery(1, "b");
244: }
245:
246: private ObjectSet queryForName(String n) {
247: Query q = newQuery();
248: q.descend("_name").constrain(n);
249: return q.execute();
250: }
251:
252: protected Query newQuery() {
253: Query q = super .newQuery();
254: q.constrain(IndexedQueriesItem.class);
255: return q;
256: }
257:
258: }
|