001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.querying;
022:
023: import java.lang.reflect.Field;
024:
025: import com.db4o.ObjectSet;
026: import com.db4o.config.*;
027: import com.db4o.query.*;
028:
029: import db4ounit.Assert;
030: import db4ounit.extensions.AbstractDb4oTestCase;
031:
032: /**
033: * @exclude
034: */
035: public class MultiFieldIndexQueryTestCase extends AbstractDb4oTestCase {
036:
037: public static void main(String[] args) {
038: new MultiFieldIndexQueryTestCase().runSolo();
039: }
040:
041: public static class Book {
042:
043: public Person[] authors;
044: public String title;
045:
046: public Book() {
047: }
048:
049: public Book(String title, Person[] authors) {
050: this .title = title;
051: this .authors = authors;
052: }
053:
054: public String toString() {
055: String ret = title;
056: if (authors != null) {
057: for (int i = 0; i < authors.length; i++) {
058: ret += "\n " + authors[i].toString();
059: }
060: }
061: return ret;
062: }
063: }
064:
065: public static class Person {
066:
067: public String firstName;
068: public String lastName;
069:
070: public Person() {
071: }
072:
073: public Person(String firstName, String lastName) {
074: this .firstName = firstName;
075: this .lastName = lastName;
076: }
077:
078: public String toString() {
079: return "Person " + firstName + " " + lastName;
080: }
081: }
082:
083: protected void configure(Configuration config) {
084: indexAllFields(config, Book.class);
085: indexAllFields(config, Person.class);
086: }
087:
088: protected void indexAllFields(Configuration config, Class clazz) {
089: final Field[] fields = clazz.getDeclaredFields();
090: for (int i = 0; i < fields.length; i++) {
091: indexField(config, clazz, fields[i].getName());
092: }
093: final Class super class = clazz.getSuperclass();
094: if (super class != null) {
095: indexAllFields(config, super class);
096: }
097: }
098:
099: protected void store() throws Exception {
100: Person aaron = new Person("Aaron", "OneOK");
101: Person bill = new Person("Bill", "TwoOK");
102: Person chris = new Person("Chris", "ThreeOK");
103: Person dave = new Person("Dave", "FourOK");
104: Person neil = new Person("Neil", "Notwanted");
105: Person nat = new Person("Nat", "Neverwanted");
106: db().set(
107: new Book("Persistence possibilities", new Person[] {
108: aaron, bill, chris }));
109: db().set(
110: new Book("Persistence using S.O.D.A.",
111: new Person[] { aaron }));
112: db().set(
113: new Book("Persistence using JDO", new Person[] { bill,
114: dave }));
115: db().set(
116: new Book("Don't want to find Phil", new Person[] {
117: aaron, bill, neil }));
118: db().set(new Book("Persistence by Jeff", new Person[] { nat }));
119: }
120:
121: public void test() {
122: Query qBooks = newQuery();
123: qBooks.constrain(Book.class);
124: qBooks.descend("title").constrain("Persistence").like();
125: Query qAuthors = qBooks.descend("authors");
126: Query qFirstName = qAuthors.descend("firstName");
127: Query qLastName = qAuthors.descend("lastName");
128: Constraint cAaron = qFirstName.constrain("Aaron").and(
129: qLastName.constrain("OneOK"));
130: Constraint cBill = qFirstName.constrain("Bill").and(
131: qLastName.constrain("TwoOK"));
132: cAaron.or(cBill);
133: ObjectSet results = qAuthors.execute();
134: Assert.areEqual(4, results.size());
135: while (results.hasNext()) {
136: Person person = (Person) results.next();
137: Assert.isTrue(person.lastName.endsWith("OK"));
138: }
139: }
140:
141: }
|