KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > legacy > Book


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.test.legacy;
22
23 import com.db4o.*;
24 import com.db4o.db4ounit.common.querying.MultiFieldIndexQueryTestCase.*;
25 import com.db4o.query.*;
26 import com.db4o.test.*;
27
28
29 public class Book {
30     
31     public Person[] authors;
32     public String JavaDoc title;
33
34     public Book(){}
35
36     public Book(String JavaDoc title, Person[] authors){
37         this.title = title;
38         this.authors = authors;
39     }
40
41     public void store(){
42         Person aaron = new Person("Aaron", "OneOK");
43         Person bill = new Person("Bill", "TwoOK");
44         Person chris = new Person("Chris", "ThreeOK");
45         Person dave = new Person("Dave", "FourOK");
46         Person neil = new Person("Neil", "Notwanted");
47         Person nat = new Person("Nat", "Neverwanted");
48         Test.store(new Book("Persistence possibilities", new Person[] { aaron, bill,chris}));
49         Test.store(new Book("Persistence using S.O.D.A.", new Person[]{aaron}));
50         Test.store(new Book("Persistence using JDO", new Person[]{bill, dave}));
51         Test.store(new Book("Don't want to find Phil", new Person[]{aaron, bill, neil}));
52         Test.store(new Book("Persistence by Jeff", new Person[]{nat}));
53     }
54
55     public void test(){
56         Query qBooks = Test.query();
57         qBooks.constrain(Book.class);
58         qBooks.descend("title").constrain("Persistence").like();
59         Query qAuthors = qBooks.descend("authors");
60         Query qFirstName = qAuthors.descend("firstName");
61         Query qLastName = qAuthors.descend("lastName");
62         Constraint cAaron =
63             qFirstName.constrain("Aaron").and(
64             qLastName.constrain("OneOK")
65             );
66         Constraint cBill =
67             qFirstName.constrain("Bill").and(
68             qLastName.constrain("TwoOK")
69             );
70         cAaron.or(cBill);
71         ObjectSet results = qAuthors.execute();
72         Test.ensure(results.size() == 4);
73         while(results.hasNext()){
74             Person person = (Person)results.next();
75             Test.ensure(person.lastName.endsWith("OK"));
76         }
77     }
78
79
80     public String JavaDoc toString(){
81         String JavaDoc ret = title;
82         if(authors != null){
83             for (int i = 0; i < authors.length; i++) {
84                 ret += "\n " + authors[i].toString();
85             }
86         }
87         return ret;
88     }
89 }
90
Popular Tags