KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > nativequery > cats > NQCatConsistencyTestCase


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.nativequery.cats;
22
23 import java.util.*;
24
25 import com.db4o.*;
26 import com.db4o.config.*;
27 import com.db4o.ext.*;
28 import com.db4o.query.*;
29 import com.db4o.test.*;
30
31 import db4ounit.*;
32 import db4ounit.extensions.*;
33
34
35 public class NQCatConsistencyTestCase extends AbstractDb4oTestCase {
36     
37     protected void configure(Configuration config) {
38         config.optimizeNativeQueries(false);
39     }
40
41     public void store(){
42         storeCats();
43     }
44     
45     public void test(){
46         
47         ExtObjectContainer oc = db();
48         oc.configure().optimizeNativeQueries(true);
49         runTests();
50         oc.configure().optimizeNativeQueries(false);
51         runTests();
52
53     }
54     
55     
56     public void runTests(){
57         
58         expect(new Predicate(){
59             public boolean match(Cat cat){
60                 return cat._age == 7;
61             }
62         }, null);
63         
64         expect(new Predicate(){
65             public boolean match(Cat cat){
66                 return cat._age == 1;
67             }
68         }, new String JavaDoc[]{"Occam", "Vahiné" });
69         
70         expect(new Predicate(){
71             public boolean match(Cat cat){
72                 return cat._father._age == 1;
73             }
74         }, new String JavaDoc[]{"Achat", "Acrobat" });
75         
76         expect(new Predicate(){
77             public boolean match(Cat cat){
78                 return cat._father._father._firstName.equals("Edwin");
79             }
80         }, new String JavaDoc[]{"Achat", "Acrobat" });
81         
82         
83         // The following will fail with nqopt
84
// because SODA swallows nulls
85

86 // expect(new Predicate(){
87
// public boolean match(Cat cat){
88
// return cat._father._father._firstName.equals("Edwin")
89
// || cat._father._firstName.equals("Edwin");
90
// }
91
// }, new String[]{"Achat", "Acrobat"});
92

93         
94         // will be run unoptimized (arithmetics on candidate member)
95
expect(new Predicate(){
96             public boolean match(Cat cat){
97                 return cat._age + 1 == 2;
98             }
99         }, new String JavaDoc[]{"Occam", "Vahiné" });
100
101         
102         expect(new Predicate(){
103             public boolean match(Cat cat){
104                 return cat.getFirstName().equals("Occam")
105                     && cat.getAge() == 1;
106             }
107         }, new String JavaDoc[]{"Occam"});
108         
109         
110         // will be run unoptimized (non-getter method call: getFullName)
111
expect(new Predicate(){
112             public boolean match(Cat cat){
113                 return cat.getFullName().equals("Achat Leo Lenis");
114             }
115         }, new String JavaDoc[]{"Achat"});
116
117
118         // will be run unoptimized (non-getter method call: getFullName)
119
expect(new Predicate(){
120             public boolean match(Cat cat){
121                 return cat.getFullName() == null;
122             }
123         }, new String JavaDoc[]{"Trulla"});
124         
125
126         // will be run optimized
127
expect(new Predicate(){
128             public boolean match(Cat cat){
129                 return cat._firstName.startsWith("A");
130             }
131             
132         }, new String JavaDoc[]{"Achat", "Acrobat"});
133         
134     }
135     
136     public void storeCats(){
137         
138         Cat winni = new Cat();
139         winni._sex = Animal.MALE;
140         winni._firstName = "Edwin";
141         winni._lastName = "Sanddrops";
142         winni._age = 12;
143         
144         Cat bachi = new Cat();
145         bachi._sex = Animal.FEMALE;
146         bachi._firstName = "Frau Bachmann";
147         bachi._lastName = "von der Bärenhöhle";
148         bachi._age = 10;
149         
150         Cat occam = new Cat();
151         occam._sex = Animal.MALE;
152         occam._firstName = "Occam";
153         occam._lastName = "von der Bärenhöhle";
154         occam._age = 1;
155         occam._father = winni;
156         occam._mother = bachi;
157         
158         Cat zora = new Cat();
159         zora._sex = Animal.FEMALE;
160         zora._firstName = "Vahiné";
161         zora._lastName = "des Fauves et Or";
162         zora._age = 1;
163         
164         Cat achat = new Cat();
165         achat._sex = Animal.FEMALE;
166         achat._firstName = "Achat";
167         achat._lastName = "Leo Lenis";
168         achat._father = occam;
169         achat._mother = zora;
170         
171         Cat acrobat = new Cat();
172         acrobat._sex = Animal.FEMALE;
173         acrobat._firstName = "Acrobat";
174         acrobat._lastName = "Leo Lenis";
175         acrobat._father = occam;
176         acrobat._mother = zora;
177         
178         ObjectContainer db=db();
179         
180         db.set(achat);
181         db.set(acrobat);
182         
183         Cat trulla = new Cat();
184         trulla._firstName = "Trulla";
185         db.set(trulla);
186         
187     }
188     
189     private void expect(Predicate predicate, String JavaDoc[] names){
190         
191         if(names == null){
192             names = new String JavaDoc[] {};
193         }
194         
195         List list = db().query(predicate);
196         
197         Iterator i = list.iterator();
198         while(i.hasNext()){
199             Cat cat = (Cat)i.next();
200             boolean good = false;
201             for (int j = 0; j < names.length; j++) {
202                 if(names[j] != null){
203                     if(cat._firstName.equals(names[j])){
204                         names[j] = null;
205                         good = true;
206                         break;
207                     }
208                 }
209             }
210             Assert.isTrue(good);
211         }
212         for (int j = 0; j < names.length; j++) {
213             Assert.isNull(names[j]);
214         }
215     }
216     
217     
218     
219
220
221 }
222
Popular Tags