KickJava   Java API By Example, From Geeks To Geeks.

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


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.TreeInt;
24 import com.db4o.foundation.Iterator4;
25 import com.db4o.inside.fieldindex.*;
26 import com.db4o.query.*;
27
28 import db4ounit.Assert;
29
30 public class IndexedNodeTestCase extends FieldIndexProcessorTestCaseBase {
31     
32     public static void main(String JavaDoc[] args) {
33         new IndexedNodeTestCase().runSolo();
34     }
35     
36     protected void store() {
37         storeItems(new int[] { 3, 4, 7, 9 });
38         storeComplexItems(
39                         new int[] { 3, 4, 7, 9 },
40                         new int[] { 2, 2, 8, 8 });
41     }
42     
43     public void testTwoLevelDescendOr() {
44         Query query = createComplexItemQuery();
45         Constraint c1 = query.descend("child").descend("foo").constrain(new Integer JavaDoc(4)).smaller();
46         Constraint c2 = query.descend("child").descend("foo").constrain(new Integer JavaDoc(4)).greater();
47         c1.or(c2);
48         
49         assertSingleOrNode(query);
50     }
51     
52     public void testMultipleOrs() {
53         Query query = createComplexItemQuery();
54         Constraint c1 = query.descend("foo").constrain(new Integer JavaDoc(4)).smaller();
55         for (int i = 0; i < 5; i++) {
56             Constraint c2 = query.descend("foo").constrain(new Integer JavaDoc(4)).greater();
57             c1 = c1.or(c2);
58         }
59         assertSingleOrNode(query);
60     }
61     
62     public void testDoubleDescendingOnIndexedNodes() {
63         final Query query = createComplexItemQuery();
64         query.descend("child").descend("foo").constrain(new Integer JavaDoc(3));
65         query.descend("bar").constrain(new Integer JavaDoc(2));
66         
67         final IndexedNode index = selectBestIndex(query);
68         assertComplexItemIndex("foo", index);
69         
70         Assert.isFalse(index.isResolved());
71         
72         IndexedNode result = index.resolve();
73         Assert.isNotNull(result);
74         assertComplexItemIndex("child", result);
75         
76         Assert.isTrue(result.isResolved());
77         Assert.isNull(result.resolve());
78         
79         assertComplexItems(new int[] { 4 }, result.toTreeInt());
80     }
81     
82     public void testTripleDescendingOnQuery() {
83         final Query query = createComplexItemQuery();
84         query.descend("child").descend("child").descend("foo").constrain(new Integer JavaDoc(3));
85         
86         final IndexedNode index = selectBestIndex(query);
87         assertComplexItemIndex("foo", index);
88         
89         Assert.isFalse(index.isResolved());
90         
91         IndexedNode result = index.resolve();
92         Assert.isNotNull(result);
93         assertComplexItemIndex("child", result);
94         
95         Assert.isFalse(result.isResolved());
96         result = result.resolve();
97         Assert.isNotNull(result);
98         assertComplexItemIndex("child", result);
99         
100         assertComplexItems(new int[] { 7 } , result.toTreeInt());
101     }
102     
103     private void assertComplexItems(final int[] expectedFoos, final TreeInt found) {
104         Assert.isNotNull(found);
105         assertTreeInt(
106                 mapToObjectIds(createComplexItemQuery(), expectedFoos),
107                 found);
108     }
109     
110     private void assertSingleOrNode(Query query) {
111         Iterator4 nodes = createProcessor(query).collectIndexedNodes();
112         Assert.isTrue(nodes.moveNext());
113         
114         OrIndexedLeaf node = (OrIndexedLeaf)nodes.current();
115         Assert.isNotNull(node);
116         
117         Assert.isFalse(nodes.moveNext());
118     }
119 }
120
Popular Tags