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.test.legacy;
022:
023: import com.db4o.*;
024: import com.db4o.query.*;
025: import com.db4o.test.*;
026:
027: public class CreateIndex {
028:
029: public String i_name;
030:
031: public int i_int;
032:
033: public Integer i_intWrapper;
034:
035: public CreateIndex() {
036: }
037:
038: public CreateIndex(String name) {
039: this .i_name = name;
040: }
041:
042: public CreateIndex(int a_int) {
043: i_int = a_int;
044: i_intWrapper = new Integer(a_int);
045: }
046:
047: public void configure() {
048: Db4o.configure().objectClass(this ).objectField("i_name")
049: .indexed(true);
050: Db4o.configure().objectClass(this ).objectField("i_int")
051: .indexed(true);
052: Db4o.configure().objectClass(this ).objectField("i_intWrapper")
053: .indexed(true);
054: }
055:
056: public void store() {
057: Test.deleteAllInstances(this );
058:
059: Test.store(new CreateIndex("a"));
060: Test.store(new CreateIndex("c"));
061: Test.store(new CreateIndex("b"));
062: Test.store(new CreateIndex("f"));
063: Test.store(new CreateIndex("e"));
064:
065: Test.store(new CreateIndex(1));
066: Test.store(new CreateIndex(5));
067: Test.store(new CreateIndex(7));
068: Test.store(new CreateIndex(3));
069: Test.store(new CreateIndex(2));
070: Test.store(new CreateIndex(3));
071:
072: tQueryB();
073: tQueryInts(5);
074: tQueryIntWrapper();
075: }
076:
077: public void test() {
078:
079: tQueryNull(6);
080:
081: Test.store(new CreateIndex("d"));
082: tQueryB();
083: tUpdateB();
084: Test.store(new CreateIndex("z"));
085: Test.store(new CreateIndex("y"));
086: Test.reOpen();
087: tQueryB();
088:
089: tQueryInts(8);
090: }
091:
092: private void tQueryIntWrapper() {
093:
094: Query q = Test.query();
095: q.constrain(CreateIndex.class);
096: q.descend("i_intWrapper").constrain(new Integer(4)).greater()
097: .equal();
098: tExpectInts(q, new int[] { 5, 7 });
099:
100: q = Test.query();
101: q.constrain(CreateIndex.class);
102: q.descend("i_intWrapper").constrain(new Integer(4)).smaller();
103: tExpectInts(q, new int[] { 1, 2, 3, 3 });
104:
105: }
106:
107: private void tQueryInts(int expectedZeroSize) {
108:
109: Query q = Test.query();
110: q.constrain(CreateIndex.class);
111: q.descend("i_int").constrain(new Integer(0));
112: int zeroSize = q.execute().size();
113: Test.ensure(zeroSize == expectedZeroSize);
114:
115: q = Test.query();
116: q.constrain(CreateIndex.class);
117: q.descend("i_int").constrain(new Integer(4)).greater().equal();
118: tExpectInts(q, new int[] { 5, 7 });
119:
120: q = Test.query();
121: q.constrain(CreateIndex.class);
122: q.descend("i_int").constrain(new Integer(4)).greater();
123: tExpectInts(q, new int[] { 5, 7 });
124:
125: q = Test.query();
126: q.constrain(CreateIndex.class);
127: q.descend("i_int").constrain(new Integer(3)).greater();
128: tExpectInts(q, new int[] { 5, 7 });
129:
130: q = Test.query();
131: q.constrain(CreateIndex.class);
132: q.descend("i_int").constrain(new Integer(3)).greater().equal();
133: tExpectInts(q, new int[] { 3, 3, 5, 7 });
134:
135: q = Test.query();
136: q.constrain(CreateIndex.class);
137: q.descend("i_int").constrain(new Integer(2)).greater().equal();
138: tExpectInts(q, new int[] { 2, 3, 3, 5, 7 });
139: q = Test.query();
140:
141: q = Test.query();
142: q.constrain(CreateIndex.class);
143: q.descend("i_int").constrain(new Integer(2)).greater();
144: tExpectInts(q, new int[] { 3, 3, 5, 7 });
145:
146: q = Test.query();
147: q.constrain(CreateIndex.class);
148: q.descend("i_int").constrain(new Integer(1)).greater().equal();
149: tExpectInts(q, new int[] { 1, 2, 3, 3, 5, 7 });
150:
151: q = Test.query();
152: q.constrain(CreateIndex.class);
153: q.descend("i_int").constrain(new Integer(1)).greater();
154: tExpectInts(q, new int[] { 2, 3, 3, 5, 7 });
155:
156: q = Test.query();
157: q.constrain(CreateIndex.class);
158: q.descend("i_int").constrain(new Integer(4)).smaller();
159: tExpectInts(q, new int[] { 1, 2, 3, 3 }, expectedZeroSize);
160:
161: q = Test.query();
162: q.constrain(CreateIndex.class);
163: q.descend("i_int").constrain(new Integer(4)).smaller().equal();
164: tExpectInts(q, new int[] { 1, 2, 3, 3 }, expectedZeroSize);
165:
166: q = Test.query();
167: q.constrain(CreateIndex.class);
168: q.descend("i_int").constrain(new Integer(3)).smaller();
169: tExpectInts(q, new int[] { 1, 2 }, expectedZeroSize);
170:
171: q = Test.query();
172: q.constrain(CreateIndex.class);
173: q.descend("i_int").constrain(new Integer(3)).smaller().equal();
174: tExpectInts(q, new int[] { 1, 2, 3, 3 }, expectedZeroSize);
175:
176: q = Test.query();
177: q.constrain(CreateIndex.class);
178: q.descend("i_int").constrain(new Integer(2)).smaller().equal();
179: tExpectInts(q, new int[] { 1, 2 }, expectedZeroSize);
180: q = Test.query();
181:
182: q = Test.query();
183: q.constrain(CreateIndex.class);
184: q.descend("i_int").constrain(new Integer(2)).smaller();
185: tExpectInts(q, new int[] { 1 }, expectedZeroSize);
186:
187: q = Test.query();
188: q.constrain(CreateIndex.class);
189: q.descend("i_int").constrain(new Integer(1)).smaller().equal();
190: tExpectInts(q, new int[] { 1 }, expectedZeroSize);
191:
192: q = Test.query();
193: q.constrain(CreateIndex.class);
194: q.descend("i_int").constrain(new Integer(1)).smaller();
195: tExpectInts(q, new int[] {}, expectedZeroSize);
196:
197: }
198:
199: private void tExpectInts(Query q, int[] ints, int zeroSize) {
200: ObjectSet res = q.execute();
201: Test.ensure(res.size() == (ints.length + zeroSize));
202: while (res.hasNext()) {
203: CreateIndex ci = (CreateIndex) res.next();
204: for (int i = 0; i < ints.length; i++) {
205: if (ints[i] == ci.i_int) {
206: ints[i] = 0;
207: break;
208: }
209: }
210: }
211: for (int i = 0; i < ints.length; i++) {
212: Test.ensure(ints[i] == 0);
213: }
214: }
215:
216: private void tExpectInts(Query q, int[] ints) {
217: tExpectInts(q, ints, 0);
218: }
219:
220: private void tQueryB() {
221: ObjectSet res = query("b");
222: Test.ensure(res.size() == 1);
223: CreateIndex ci = (CreateIndex) res.next();
224: Test.ensure(ci.i_name.equals("b"));
225: }
226:
227: private void tQueryNull(int expect) {
228: ObjectSet res = query(null);
229: Test.ensure(res.size() == expect);
230: while (res.hasNext()) {
231: CreateIndex ci = (CreateIndex) res.next();
232: Test.ensure(ci.i_name == null);
233: }
234: }
235:
236: private void tUpdateB() {
237: ObjectSet res = query("b");
238: CreateIndex ci = (CreateIndex) res.next();
239: ci.i_name = "j";
240: Test.objectContainer().set(ci);
241: res = query("b");
242: Test.ensure(res.size() == 0);
243: res = query("j");
244: Test.ensure(res.size() == 1);
245: ci.i_name = "b";
246: Test.objectContainer().set(ci);
247: tQueryB();
248: }
249:
250: private ObjectSet query(String n) {
251: Query q = Test.query();
252: q.constrain(CreateIndex.class);
253: q.descend("i_name").constrain(n);
254: return q.execute();
255: }
256:
257: }
|