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