KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
24
25 import com.db4o.*;
26 import com.db4o.config.*;
27 import com.db4o.inside.marshall.*;
28 import com.db4o.query.*;
29
30 public class IndexCreateDrop {
31     
32     public int myInt;
33     public String JavaDoc myString;
34     public Date myDate;
35     
36     public void store(){
37         Test.deleteAllInstances(this);
38         store(4);
39         store(7);
40         store(6);
41         store(6);
42         store(5);
43         store(4);
44         store(0);
45         store(0);
46     }
47     
48     public void test(){
49         queries();
50         indexed(true);
51         Test.reOpenServer();
52         queries();
53         indexed(false);
54         Test.reOpenServer();
55         queries();
56         indexed(true);
57         Test.reOpenServer();
58         queries();
59     }
60     
61     private void indexed(boolean flag){
62         ObjectClass oc = Db4o.configure().objectClass(this.getClass());
63         oc.objectField("myInt").indexed(flag);
64         oc.objectField("myString").indexed(flag);
65         oc.objectField("myDate").indexed(flag);
66     }
67     
68     private void store(int val){
69         IndexCreateDrop icd = new IndexCreateDrop();
70         icd.myInt = val;
71         if(val != 0){
72             icd.myString = "" + val;
73             icd.myDate = new Date(val);
74         }
75         
76         Test.store(icd);
77     }
78     
79     private void queries(){
80         Query q = Test.query();
81         q.constrain(IndexCreateDrop.class);
82         q.descend("myInt").constrain(new Integer JavaDoc(6));
83         Test.ensure(q.execute().size() == 2);
84         
85         q = Test.query();
86         q.constrain(IndexCreateDrop.class);
87         q.descend("myInt").constrain(new Integer JavaDoc(4)).greater();
88         Test.ensure(q.execute().size() == 4);
89         
90         q = Test.query();
91         q.constrain(IndexCreateDrop.class);
92         q.descend("myInt").constrain(new Integer JavaDoc(4)).greater().equal();
93         Test.ensure(q.execute().size() == 6);
94         
95         q = Test.query();
96         q.constrain(IndexCreateDrop.class);
97         q.descend("myInt").constrain(new Integer JavaDoc(7)).smaller().equal();
98         Test.ensure(q.execute().size() == 8);
99         
100         q = Test.query();
101         q.constrain(IndexCreateDrop.class);
102         q.descend("myInt").constrain(new Integer JavaDoc(7)).smaller();
103         Test.ensure(q.execute().size() == 7);
104         
105         q = Test.query();
106         q.constrain(IndexCreateDrop.class);
107         q.descend("myString").constrain("6");
108         Test.ensure(q.execute().size() == 2);
109         
110         q = Test.query();
111         q.constrain(IndexCreateDrop.class);
112         q.descend("myString").constrain("7");
113         Test.ensure(q.execute().size() == 1);
114         
115         q = Test.query();
116         q.constrain(IndexCreateDrop.class);
117         q.descend("myString").constrain("4");
118         Test.ensure(q.execute().size() == 2);
119         
120         q = Test.query();
121         q.constrain(IndexCreateDrop.class);
122         q.descend("myString").constrain(null);
123         Test.ensure(q.execute().size() == 2);
124         
125         q = Test.query();
126         q.constrain(IndexCreateDrop.class);
127         q.descend("myDate").constrain(new Date(4)).greater();
128         Test.ensure(q.execute().size() == 4);
129         
130         q = Test.query();
131         q.constrain(IndexCreateDrop.class);
132         q.descend("myDate").constrain(new Date(4)).greater().equal();
133         Test.ensure(q.execute().size() == 6);
134         
135         q = Test.query();
136         q.constrain(IndexCreateDrop.class);
137         q.descend("myDate").constrain(new Date(7)).smaller().equal();
138         
139         Test.ensure(q.execute().size() == 6);
140         
141         q = Test.query();
142         q.constrain(IndexCreateDrop.class);
143         q.descend("myDate").constrain(new Date(7)).smaller();
144         
145         Test.ensure(q.execute().size() == 5);
146         
147         q = Test.query();
148         q.constrain(IndexCreateDrop.class);
149         q.descend("myDate").constrain(null);
150         Test.ensureEquals(2, q.execute().size());
151     }
152
153 }
154
Popular Tags