KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > legacy > CreateIndex


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test.legacy;
22
23 import com.db4o.*;
24 import com.db4o.inside.marshall.*;
25 import com.db4o.query.*;
26 import com.db4o.test.*;
27
28 public class CreateIndex {
29
30     public String JavaDoc i_name;
31     
32     public int i_int;
33     
34     public Integer JavaDoc i_intWrapper;
35
36     public CreateIndex() {
37     }
38
39     public CreateIndex(String JavaDoc name) {
40         this.i_name = name;
41     }
42
43     public CreateIndex(int a_int) {
44         i_int = a_int;
45         i_intWrapper = new Integer JavaDoc(a_int);
46     }
47
48     public void configure() {
49         Db4o.configure().objectClass(this).objectField("i_name").indexed(true);
50         Db4o.configure().objectClass(this).objectField("i_int").indexed(true);
51         Db4o.configure().objectClass(this).objectField("i_intWrapper").indexed(true);
52     }
53
54     public void store() {
55         Test.deleteAllInstances(this);
56
57         Test.store(new CreateIndex("a"));
58         Test.store(new CreateIndex("c"));
59         Test.store(new CreateIndex("b"));
60         Test.store(new CreateIndex("f"));
61         Test.store(new CreateIndex("e"));
62
63         Test.store(new CreateIndex(1));
64         Test.store(new CreateIndex(5));
65         Test.store(new CreateIndex(7));
66         Test.store(new CreateIndex(3));
67         Test.store(new CreateIndex(2));
68         Test.store(new CreateIndex(3));
69
70         tQueryB();
71         tQueryInts(5);
72         tQueryIntWrapper();
73     }
74
75     public void test() {
76         
77         tQueryNull(6);
78         
79         Test.store(new CreateIndex("d"));
80         tQueryB();
81         tUpdateB();
82         Test.store(new CreateIndex("z"));
83         Test.store(new CreateIndex("y"));
84         Test.reOpen();
85         tQueryB();
86
87         tQueryInts(8);
88     }
89     
90     private void tQueryIntWrapper(){
91         
92         Query q = Test.query();
93         q.constrain(CreateIndex.class);
94         q.descend("i_intWrapper").constrain(new Integer JavaDoc(4)).greater().equal();
95         tExpectInts(q, new int[] { 5, 7 });
96
97         q = Test.query();
98         q.constrain(CreateIndex.class);
99         q.descend("i_intWrapper").constrain(new Integer JavaDoc(4)).smaller();
100         tExpectInts(q, new int[] { 1, 2, 3, 3 });
101         
102     }
103
104     private void tQueryInts(int expectedZeroSize) {
105         
106         Query q = Test.query();
107         q.constrain(CreateIndex.class);
108         q.descend("i_int").constrain(new Integer JavaDoc(0));
109         int zeroSize = q.execute().size();
110         Test.ensure(zeroSize == expectedZeroSize);
111         
112         q = Test.query();
113         q.constrain(CreateIndex.class);
114         q.descend("i_int").constrain(new Integer JavaDoc(4)).greater().equal();
115         tExpectInts(q, new int[] { 5, 7 });
116          
117         q = Test.query();
118         q.constrain(CreateIndex.class);
119         q.descend("i_int").constrain(new Integer JavaDoc(4)).greater();
120         tExpectInts(q, new int[] { 5, 7 });
121
122         q = Test.query();
123         q.constrain(CreateIndex.class);
124         q.descend("i_int").constrain(new Integer JavaDoc(3)).greater();
125         tExpectInts(q, new int[] { 5, 7 });
126
127         q = Test.query();
128         q.constrain(CreateIndex.class);
129         q.descend("i_int").constrain(new Integer JavaDoc(3)).greater().equal();
130         tExpectInts(q, new int[] { 3, 3, 5, 7 });
131
132         q = Test.query();
133         q.constrain(CreateIndex.class);
134         q.descend("i_int").constrain(new Integer JavaDoc(2)).greater().equal();
135         tExpectInts(q, new int[] { 2, 3, 3, 5, 7 });
136         q = Test.query();
137
138         q = Test.query();
139         q.constrain(CreateIndex.class);
140         q.descend("i_int").constrain(new Integer JavaDoc(2)).greater();
141         tExpectInts(q, new int[] { 3, 3, 5, 7 });
142
143         q = Test.query();
144         q.constrain(CreateIndex.class);
145         q.descend("i_int").constrain(new Integer JavaDoc(1)).greater().equal();
146         tExpectInts(q, new int[] { 1, 2, 3, 3, 5, 7 });
147
148         q = Test.query();
149         q.constrain(CreateIndex.class);
150         q.descend("i_int").constrain(new Integer JavaDoc(1)).greater();
151         tExpectInts(q, new int[] { 2, 3, 3, 5, 7 });
152
153         q = Test.query();
154         q.constrain(CreateIndex.class);
155         q.descend("i_int").constrain(new Integer JavaDoc(4)).smaller();
156         tExpectInts(q, new int[] { 1, 2, 3, 3 }, expectedZeroSize);
157
158         q = Test.query();
159         q.constrain(CreateIndex.class);
160         q.descend("i_int").constrain(new Integer JavaDoc(4)).smaller().equal();
161         tExpectInts(q, new int[] { 1, 2, 3, 3 }, expectedZeroSize);
162
163         q = Test.query();
164         q.constrain(CreateIndex.class);
165         q.descend("i_int").constrain(new Integer JavaDoc(3)).smaller();
166         tExpectInts(q, new int[] { 1, 2 }, expectedZeroSize);
167
168         q = Test.query();
169         q.constrain(CreateIndex.class);
170         q.descend("i_int").constrain(new Integer JavaDoc(3)).smaller().equal();
171         tExpectInts(q, new int[] { 1, 2, 3, 3 }, expectedZeroSize);
172
173         q = Test.query();
174         q.constrain(CreateIndex.class);
175         q.descend("i_int").constrain(new Integer JavaDoc(2)).smaller().equal();
176         tExpectInts(q, new int[] { 1, 2 }, expectedZeroSize);
177         q = Test.query();
178
179         q = Test.query();
180         q.constrain(CreateIndex.class);
181         q.descend("i_int").constrain(new Integer JavaDoc(2)).smaller();
182         tExpectInts(q, new int[] { 1 }, expectedZeroSize);
183
184         q = Test.query();
185         q.constrain(CreateIndex.class);
186         q.descend("i_int").constrain(new Integer JavaDoc(1)).smaller().equal();
187         tExpectInts(q, new int[] { 1 }, expectedZeroSize);
188
189         q = Test.query();
190         q.constrain(CreateIndex.class);
191         q.descend("i_int").constrain(new Integer JavaDoc(1)).smaller();
192         tExpectInts(q, new int[] {
193         }, expectedZeroSize);
194
195     }
196
197     private void tExpectInts(Query q, int[] ints, int zeroSize) {
198         ObjectSet res = q.execute();
199         Test.ensure(res.size() == (ints.length + zeroSize));
200         while (res.hasNext()) {
201             CreateIndex ci = (CreateIndex)res.next();
202             for (int i = 0; i < ints.length; i++) {
203                 if (ints[i] == ci.i_int) {
204                     ints[i] = 0;
205                     break;
206                 }
207             }
208         }
209         for (int i = 0; i < ints.length; i++) {
210             Test.ensure(ints[i] == 0);
211         }
212     }
213
214     private void tExpectInts(Query q, int[] ints) {
215         tExpectInts(q, ints, 0);
216     }
217
218     private void tQueryB() {
219         ObjectSet res = query("b");
220         Test.ensure(res.size() == 1);
221         CreateIndex ci = (CreateIndex)res.next();
222         Test.ensure(ci.i_name.equals("b"));
223     }
224     
225     private void tQueryNull(int expect) {
226         ObjectSet res = query(null);
227         Test.ensure(res.size() == expect);
228         while(res.hasNext()){
229             CreateIndex ci = (CreateIndex)res.next();
230             Test.ensure(ci.i_name == null);
231         }
232     }
233
234     private void tUpdateB() {
235         ObjectSet res = query("b");
236         CreateIndex ci = (CreateIndex)res.next();
237         ci.i_name = "j";
238         Test.objectContainer().set(ci);
239         res = query("b");
240         Test.ensure(res.size() == 0);
241         res = query("j");
242         Test.ensure(res.size() == 1);
243         ci.i_name = "b";
244         Test.objectContainer().set(ci);
245         tQueryB();
246     }
247
248     private ObjectSet query(String JavaDoc n) {
249         Query q = Test.query();
250         q.constrain(CreateIndex.class);
251         q.descend("i_name").constrain(n);
252         return q.execute();
253     }
254
255 }
256
Popular Tags