KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Field JavaDoc;
25
26 import com.db4o.ObjectSet;
27 import com.db4o.config.*;
28 import com.db4o.query.*;
29
30 import db4ounit.Assert;
31 import db4ounit.extensions.AbstractDb4oTestCase;
32
33 /**
34  * @exclude
35  */

36 public class MultiFieldIndexQueryTestCase extends AbstractDb4oTestCase {
37     
38     public static void main(String JavaDoc[] args) {
39         new MultiFieldIndexQueryTestCase().runSolo();
40     }
41     
42     public static class Book {
43         
44         public Person[] authors;
45         public String JavaDoc title;
46
47         public Book(){}
48
49         public Book(String JavaDoc title, Person[] authors){
50             this.title = title;
51             this.authors = authors;
52         }
53
54         public String JavaDoc toString(){
55             String JavaDoc ret = title;
56             if(authors != null){
57                 for (int i = 0; i < authors.length; i++) {
58                     ret += "\n " + authors[i].toString();
59                 }
60             }
61             return ret;
62         }
63     }
64     
65     public static class Person {
66         
67         public String JavaDoc firstName;
68         public String JavaDoc lastName;
69
70         public Person(){}
71
72         public Person(String JavaDoc firstName, String JavaDoc lastName){
73             this.firstName = firstName;
74             this.lastName = lastName;
75         }
76
77         public String JavaDoc toString(){
78             return "Person " + firstName + " " + lastName;
79         }
80     }
81     
82     protected void configure(Configuration config) {
83         indexAllFields(config,Book.class);
84         indexAllFields(config,Person.class);
85     }
86     
87     protected void indexAllFields(Configuration config,Class JavaDoc clazz) {
88         final Field JavaDoc[] fields = clazz.getDeclaredFields();
89         for (int i = 0; i < fields.length; i++) {
90             indexField(config,clazz, fields[i].getName());
91         }
92         final Class JavaDoc superclass = clazz.getSuperclass();
93         if (superclass != null) {
94             indexAllFields(config,superclass);
95         }
96     }
97
98     protected void store() throws Exception JavaDoc {
99         Person aaron = new Person("Aaron", "OneOK");
100         Person bill = new Person("Bill", "TwoOK");
101         Person chris = new Person("Chris", "ThreeOK");
102         Person dave = new Person("Dave", "FourOK");
103         Person neil = new Person("Neil", "Notwanted");
104         Person nat = new Person("Nat", "Neverwanted");
105         db().set(new Book("Persistence possibilities", new Person[] { aaron,
106                 bill, chris }));
107         db().set(new Book("Persistence using S.O.D.A.",
108                 new Person[] { aaron }));
109         db().set(new Book("Persistence using JDO",
110                 new Person[] { bill, dave }));
111         db().set(new Book("Don't want to find Phil", new Person[] { aaron,
112                 bill, neil }));
113         db().set(new Book("Persistence by Jeff", new Person[] { nat }));
114     }
115
116     public void test() {
117         Query qBooks = newQuery();
118         qBooks.constrain(Book.class);
119         qBooks.descend("title").constrain("Persistence").like();
120         Query qAuthors = qBooks.descend("authors");
121         Query qFirstName = qAuthors.descend("firstName");
122         Query qLastName = qAuthors.descend("lastName");
123         Constraint cAaron = qFirstName.constrain("Aaron").and(
124                 qLastName.constrain("OneOK"));
125         Constraint cBill = qFirstName.constrain("Bill").and(
126                 qLastName.constrain("TwoOK"));
127         cAaron.or(cBill);
128         ObjectSet results = qAuthors.execute();
129         Assert.areEqual(4, results.size());
130         while (results.hasNext()) {
131             Person person = (Person) results.next();
132             Assert.isTrue(person.lastName.endsWith("OK"));
133         }
134     }
135
136 }
137
Popular Tags