KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > querying > QueryResultTestCase


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.querying;
22
23
24 import com.db4o.*;
25 import com.db4o.config.*;
26 import com.db4o.db4ounit.common.btree.*;
27 import com.db4o.db4ounit.common.foundation.*;
28 import com.db4o.foundation.*;
29 import com.db4o.inside.query.*;
30 import com.db4o.query.*;
31
32 import db4ounit.extensions.*;
33 import db4ounit.extensions.fixtures.*;
34
35
36 public abstract class QueryResultTestCase extends AbstractDb4oTestCase implements OptOutCS, OptOutDefragSolo {
37     
38     private static final int[] VALUES = new int[] { 1 , 5, 6 , 7, 9};
39     
40     private final int [] itemIds = new int[VALUES.length];
41     
42     private int idForGetAll;
43     
44     
45     protected void configure(Configuration config) {
46         indexField(config, Item.class, "foo");
47     }
48     
49     public void testClassQuery(){
50         assertIDs(classOnlyQuery(), itemIds);
51     }
52     
53     public void testGetAll(){
54         AbstractQueryResult queryResult = newQueryResult();
55         queryResult.loadFromClassIndexes(stream().classCollection().iterator());
56         int[] ids = IntArrays4.concat(itemIds, new int[] {idForGetAll});
57         assertIDs(queryResult, ids, true);
58     }
59     
60     public void testIndexedFieldQuery(){
61         Query query = newItemQuery();
62         query.descend("foo").constrain(new Integer JavaDoc(6)).smaller();
63         QueryResult queryResult = executeQuery(query);
64         assertIDs(queryResult, new int[] {itemIds[0], itemIds[1] });
65     }
66     
67     public void testNonIndexedFieldQuery(){
68         Query query = newItemQuery();
69         query.descend("bar").constrain(new Integer JavaDoc(6)).smaller();
70         QueryResult queryResult = executeQuery(query);
71         assertIDs(queryResult, new int[] {itemIds[0], itemIds[1] });
72     }
73     
74     private QueryResult classOnlyQuery() {
75         AbstractQueryResult queryResult = newQueryResult();
76         queryResult.loadFromClassIndex(yapClass());
77         return queryResult;
78     }
79
80     private YapClass yapClass() {
81         return stream().getYapClass(reflector().forClass(Item.class));
82     }
83
84     private QueryResult executeQuery(Query query) {
85         AbstractQueryResult queryResult = newQueryResult();
86         queryResult.loadFromQuery((QQuery)query);
87         return queryResult;
88     }
89     
90     private void assertIDs(QueryResult queryResult, int[] expectedIDs){
91         assertIDs(queryResult, expectedIDs, false);
92     }
93     
94     private void assertIDs(QueryResult queryResult, int[] expectedIDs, boolean ignoreUnexpected){
95         ExpectingVisitor expectingVisitor = new ExpectingVisitor(IntArrays4.toObjectArray(expectedIDs), false, ignoreUnexpected);
96         IntIterator4 i = queryResult.iterateIDs();
97         while(i.moveNext()){
98             expectingVisitor.visit(new Integer JavaDoc(i.currentInt()));
99         }
100         expectingVisitor.assertExpectations();
101     }
102     
103     protected Query newItemQuery() {
104         return newQuery(Item.class);
105     }
106
107     protected void store() throws Exception JavaDoc {
108         storeItems(VALUES);
109         ItemForGetAll ifga = new ItemForGetAll();
110         store(ifga);
111         idForGetAll = (int)db().getID(ifga);
112     }
113     
114     protected void storeItems(final int[] foos) {
115         for (int i = 0; i < foos.length; i++) {
116             Item item = new Item(foos[i]);
117             store(item);
118             itemIds[i] = (int)db().getID(item);
119         }
120     }
121     
122     public static class Item {
123         
124         public int foo;
125         
126         public int bar;
127         
128         public Item() {
129             
130         }
131         
132         public Item(int foo_) {
133             foo = foo_;
134             bar = foo;
135         }
136         
137     }
138     
139     public static class ItemForGetAll {
140         
141     }
142     
143     protected abstract AbstractQueryResult newQueryResult();
144     
145 }
146
Popular Tags