KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > assorted > IndexCreateDropTestCase


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.db4ounit.common.assorted;
22
23 import java.util.Date JavaDoc;
24
25 import com.db4o.config.ObjectClass;
26 import com.db4o.query.Query;
27
28 import db4ounit.Assert;
29 import db4ounit.extensions.AbstractDb4oTestCase;
30
31 public class IndexCreateDropTestCase extends AbstractDb4oTestCase{
32     
33     public static class IndexCreateDropItem {
34         
35         public int _int;
36         
37         public String JavaDoc _string;
38         
39         public Date JavaDoc _date;
40
41         public IndexCreateDropItem(int int_, String JavaDoc string_, Date JavaDoc date_) {
42             _int = int_;
43             _string = string_;
44             _date = date_;
45         }
46         
47         public IndexCreateDropItem(int int_) {
48             this(int_, int_ == 0 ? null : "" + int_, int_ == 0 ? null : new Date JavaDoc(int_));
49         }
50
51     }
52
53     
54     private final int[] VALUES = new int[]{4, 7, 6, 6, 5, 4, 0, 0};
55     
56     public static void main(String JavaDoc[] arguments) {
57         new IndexCreateDropTestCase().runSolo();
58     }
59     
60     protected void store(){
61         for (int i = 0; i < VALUES.length; i++) {
62             db().set(new IndexCreateDropItem(VALUES[i]));
63         }
64     }
65     
66     public void test() throws Exception JavaDoc{
67         assertQueryResults();
68         assertQueryResults(true);
69         assertQueryResults(false);
70         assertQueryResults(true);
71     }
72     
73     private void assertQueryResults(boolean indexed) throws Exception JavaDoc{
74         indexed(indexed);
75         reopen();
76         assertQueryResults();
77     }
78     
79     private void indexed(boolean flag){
80         ObjectClass oc = fixture().config().objectClass(IndexCreateDropItem.class);
81         oc.objectField("_int").indexed(flag);
82         oc.objectField("_string").indexed(flag);
83         oc.objectField("_date").indexed(flag);
84     }
85     
86     protected Query newQuery(){
87         Query q = super.newQuery();
88         q.constrain(IndexCreateDropItem.class);
89         return q;
90     }
91     
92     private void assertQueryResults(){
93         Query q = newQuery();
94         q.descend("_int").constrain(new Integer JavaDoc(6));
95         assertQuerySize(2, q);
96         
97         q = newQuery();
98         q.descend("_int").constrain(new Integer JavaDoc(4)).greater();
99         assertQuerySize(4, q);
100         
101         q = newQuery();
102         q.descend("_int").constrain(new Integer JavaDoc(4)).greater().equal();
103         assertQuerySize(6, q);
104         
105         q = newQuery();
106         q.descend("_int").constrain(new Integer JavaDoc(7)).smaller().equal();
107         assertQuerySize(8, q);
108         
109         q = newQuery();
110         q.descend("_int").constrain(new Integer JavaDoc(7)).smaller();
111         assertQuerySize(7, q);
112         
113         q = newQuery();
114         q.descend("_string").constrain("6");
115         assertQuerySize(2, q);
116         
117         q = newQuery();
118         q.descend("_string").constrain("7");
119         assertQuerySize(1, q);
120         
121         q = newQuery();
122         q.descend("_string").constrain("4");
123         assertQuerySize(2, q);
124         
125         q = newQuery();
126         q.descend("_string").constrain(null);
127         assertQuerySize(2, q);
128         
129         q = newQuery();
130         q.descend("_date").constrain(new Date JavaDoc(4)).greater();
131         assertQuerySize(4, q);
132         
133         q = newQuery();
134         q.descend("_date").constrain(new Date JavaDoc(4)).greater().equal();
135         assertQuerySize(6, q);
136         
137         q = newQuery();
138         q.descend("_date").constrain(new Date JavaDoc(7)).smaller().equal();
139         assertQuerySize(6, q);
140         
141         q = newQuery();
142         q.descend("_date").constrain(new Date JavaDoc(7)).smaller();
143         assertQuerySize(5, q);
144         
145         q = newQuery();
146         q.descend("_date").constrain(null);
147         assertQuerySize(2, q);
148         
149     }
150
151     private void assertQuerySize(int size, Query q) {
152         Assert.areEqual(size, q.execute().size());
153     }
154
155 }
156
Popular Tags