KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > CreateIndexInherited


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;
22
23 import com.db4o.*;
24 import com.db4o.query.*;
25
26
27 /**
28  *
29  */

30 public class CreateIndexInherited {
31
32     public int i_int;
33     
34     public CreateIndexInherited(){
35     }
36     
37     public CreateIndexInherited(int a_int){
38         i_int = a_int;
39     }
40     
41     public void store() {
42         Test.deleteAllInstances(this);
43
44         Test.store(new CreateIndexFor("a"));
45         Test.store(new CreateIndexFor("c"));
46         Test.store(new CreateIndexFor("b"));
47         Test.store(new CreateIndexFor("f"));
48         Test.store(new CreateIndexFor("e"));
49
50         Test.store(new CreateIndexFor(1));
51         Test.store(new CreateIndexFor(5));
52         Test.store(new CreateIndexFor(7));
53         Test.store(new CreateIndexFor(3));
54         Test.store(new CreateIndexFor(2));
55         Test.store(new CreateIndexFor(3));
56         
57         
58         Db4o.configure().objectClass(this).objectField("i_int").indexed(true);
59         Db4o.configure().objectClass(CreateIndexFor.class).objectField("i_name").indexed(true);
60         
61         Test.reOpen();
62
63         tQueryB();
64         tQueryInts(5);
65     }
66
67     public void test() {
68         Test.store(new CreateIndexFor("d"));
69         tQueryB();
70         tUpdateB();
71         Test.store(new CreateIndexFor("z"));
72         Test.store(new CreateIndexFor("y"));
73         Test.reOpen();
74         tQueryB();
75
76         tQueryInts(8);
77     }
78
79     private void tQueryInts(int expectedZeroSize) {
80         
81         Query q = Test.query();
82         q.constrain(CreateIndexFor.class);
83         q.descend("i_int").constrain(new Integer JavaDoc(0));
84         int zeroSize = q.execute().size();
85         Test.ensure(zeroSize == expectedZeroSize);
86         
87         q = Test.query();
88         q.constrain(CreateIndexFor.class);
89         q.descend("i_int").constrain(new Integer JavaDoc(4)).greater().equal();
90         tExpectInts(q, new int[] { 5, 7 });
91          
92         q = Test.query();
93         q.constrain(CreateIndexFor.class);
94         q.descend("i_int").constrain(new Integer JavaDoc(4)).greater();
95         tExpectInts(q, new int[] { 5, 7 });
96
97         q = Test.query();
98         q.constrain(CreateIndexFor.class);
99         q.descend("i_int").constrain(new Integer JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(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 JavaDoc(1)).smaller();
167         tExpectInts(q, new int[] {
168         }, zeroSize);
169
170     }
171
172     private void tExpectInts(Query q, int[] ints, int zeroSize) {
173         ObjectSet res = q.execute();
174         Test.ensure(res.size() == (ints.length + zeroSize));
175         while (res.hasNext()) {
176             CreateIndexFor ci = (CreateIndexFor)res.next();
177             for (int i = 0; i < ints.length; i++) {
178                 if (ints[i] == ci.i_int) {
179                     ints[i] = 0;
180                     break;
181                 }
182             }
183         }
184         for (int i = 0; i < ints.length; i++) {
185             Test.ensure(ints[i] == 0);
186         }
187     }
188
189     private void tExpectInts(Query q, int[] ints) {
190         tExpectInts(q, ints, 0);
191     }
192
193     private void tQueryB() {
194         ObjectSet res = query("b");
195         Test.ensure(res.size() == 1);
196         CreateIndexFor ci = (CreateIndexFor)res.next();
197         Test.ensure(ci.i_name.equals("b"));
198     }
199
200     private void tUpdateB() {
201         ObjectSet res = query("b");
202         CreateIndexFor ci = (CreateIndexFor)res.next();
203         ci.i_name = "j";
204         Test.objectContainer().set(ci);
205         res = query("b");
206         Test.ensure(res.size() == 0);
207         res = query("j");
208         Test.ensure(res.size() == 1);
209         ci.i_name = "b";
210         Test.objectContainer().set(ci);
211         tQueryB();
212     }
213
214     private ObjectSet query(String JavaDoc n) {
215         Query q = Test.query();
216         q.constrain(CreateIndexFor.class);
217         q.descend("i_name").constrain(n);
218         return q.execute();
219     }
220
221
222
223     
224     public static class CreateIndexFor extends CreateIndexInherited{
225         
226         public String JavaDoc i_name;
227
228         public CreateIndexFor() {
229         }
230
231         public CreateIndexFor(String JavaDoc name) {
232             this.i_name = name;
233         }
234
235         public CreateIndexFor(int a_int) {
236             super(a_int);
237         }
238
239     }
240
241
242 }
243
Popular Tags