KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > fieldindex > FieldIndexTestCase


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.fieldindex;
22
23 import com.db4o.*;
24 import com.db4o.config.*;
25 import com.db4o.db4ounit.common.btree.*;
26 import com.db4o.db4ounit.common.foundation.IntArrays4;
27 import com.db4o.ext.StoredField;
28 import com.db4o.foundation.Visitor4;
29 import com.db4o.inside.btree.*;
30 import com.db4o.query.Query;
31 import com.db4o.reflect.ReflectClass;
32
33 import db4ounit.Assert;
34
35
36 public class FieldIndexTestCase extends FieldIndexTestCaseBase {
37     
38     private static final int[] FOOS = new int[]{3,7,9,4};
39     
40     public static void main(String JavaDoc[] arguments) {
41         new FieldIndexTestCase().runSolo();
42     }
43     
44     protected void configure(Configuration config) {
45         super.configure(config);
46     }
47     
48     protected void store() {
49         storeItems(FOOS);
50     }
51     
52     public void testTraverseValues(){
53         StoredField field = yapField();
54         ExpectingVisitor expectingVisitor = new ExpectingVisitor(IntArrays4.toObjectArray(FOOS));
55         field.traverseValues(expectingVisitor);
56         expectingVisitor.assertExpectations();
57     }
58     
59     public void testAllThere() throws Exception JavaDoc{
60         for (int i = 0; i < FOOS.length; i++) {
61             Query q = createQuery(FOOS[i]);
62             ObjectSet objectSet = q.execute();
63             Assert.areEqual(1, objectSet.size());
64             FieldIndexItem fii = (FieldIndexItem) objectSet.next();
65             Assert.areEqual(FOOS[i], fii.foo);
66         }
67     }
68
69     public void testAccessingBTree() throws Exception JavaDoc{
70         BTree bTree = yapField().getIndex(trans());
71         Assert.isNotNull(bTree);
72         expectKeysSearch(bTree, FOOS);
73     }
74
75     private void expectKeysSearch(BTree btree, int[] values) {
76         int lastValue = Integer.MIN_VALUE;
77         for (int i = 0; i < values.length; i++) {
78             if(values[i] != lastValue){
79                 final ExpectingVisitor expectingVisitor = BTreeAssert.createExpectingVisitor(values[i], IntArrays4.occurences(values, values[i]));
80                 BTreeRange range = fieldIndexKeySearch(trans(), btree, new Integer JavaDoc(values[i]));
81                 BTreeAssert.traverseKeys(range, new Visitor4() {
82                     public void visit(Object JavaDoc obj) {
83                         FieldIndexKey fik = (FieldIndexKey)obj;
84                         expectingVisitor.visit(fik.value());
85                     }
86                 });
87                 expectingVisitor.assertExpectations();
88                 lastValue = values[i];
89             }
90         }
91     }
92     
93     private FieldIndexKey fieldIndexKey(int integerPart, Object JavaDoc composite){
94         return new FieldIndexKey(integerPart, composite);
95     }
96     
97     private BTreeRange fieldIndexKeySearch(Transaction trans, BTree btree, Object JavaDoc key) {
98         // SearchTarget should not make a difference, HIGHEST is faster
99
BTreeNodeSearchResult start = btree.searchLeaf(trans, fieldIndexKey(0, key), SearchTarget.LOWEST);
100         BTreeNodeSearchResult end = btree.searchLeaf(trans, fieldIndexKey(Integer.MAX_VALUE, key), SearchTarget.LOWEST);
101         return start.createIncludingRange(end);
102     }
103     
104     private YapField yapField() {
105         ReflectClass claxx = stream().reflector().forObject(new FieldIndexItem());
106         YapClass yc = stream().getYapClass(claxx);
107         YapField yf = yc.getYapField("foo");
108         return yf;
109     }
110     
111
112 }
113
Popular Tags