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.test.nativequery.cats;
022:
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import com.db4o.ObjectContainer;
027: import com.db4o.config.Configuration;
028: import com.db4o.ext.ExtObjectContainer;
029: import com.db4o.query.Predicate;
030:
031: import db4ounit.Assert;
032: import db4ounit.extensions.AbstractDb4oTestCase;
033:
034: public class NQCatConsistencyTestCase extends AbstractDb4oTestCase {
035:
036: protected void configure(Configuration config) {
037: config.optimizeNativeQueries(false);
038: }
039:
040: public void store() {
041: storeCats();
042: }
043:
044: public void test() {
045:
046: ExtObjectContainer oc = db();
047: oc.configure().optimizeNativeQueries(true);
048: runTests();
049: oc.configure().optimizeNativeQueries(false);
050: runTests();
051:
052: }
053:
054: public void runTests() {
055:
056: expect(new Predicate() {
057: public boolean match(Cat cat) {
058: return cat._age == 7;
059: }
060: }, null);
061:
062: expect(new Predicate() {
063: public boolean match(Cat cat) {
064: return cat._age == 1;
065: }
066: }, new String[] { "Occam", "Vahiné" });
067:
068: expect(new Predicate() {
069: public boolean match(Cat cat) {
070: return cat._father._age == 1;
071: }
072: }, new String[] { "Achat", "Acrobat" });
073:
074: expect(new Predicate() {
075: public boolean match(Cat cat) {
076: return cat._father._father._firstName.equals("Edwin");
077: }
078: }, new String[] { "Achat", "Acrobat" });
079:
080: // The following will fail with nqopt
081: // because SODA swallows nulls
082:
083: // expect(new Predicate(){
084: // public boolean match(Cat cat){
085: // return cat._father._father._firstName.equals("Edwin")
086: // || cat._father._firstName.equals("Edwin");
087: // }
088: // }, new String[]{"Achat", "Acrobat"});
089:
090: // will be run unoptimized (arithmetics on candidate member)
091: expect(new Predicate() {
092: public boolean match(Cat cat) {
093: return cat._age + 1 == 2;
094: }
095: }, new String[] { "Occam", "Vahiné" });
096:
097: expect(new Predicate() {
098: public boolean match(Cat cat) {
099: return cat.getFirstName().equals("Occam")
100: && cat.getAge() == 1;
101: }
102: }, new String[] { "Occam" });
103:
104: // will be run unoptimized (non-getter method call: getFullName)
105: expect(new Predicate() {
106: public boolean match(Cat cat) {
107: return cat.getFullName().equals("Achat Leo Lenis");
108: }
109: }, new String[] { "Achat" });
110:
111: // will be run unoptimized (non-getter method call: getFullName)
112: expect(new Predicate() {
113: public boolean match(Cat cat) {
114: return cat.getFullName() == null;
115: }
116: }, new String[] { "Trulla" });
117:
118: // will be run optimized
119: expect(new Predicate() {
120: public boolean match(Cat cat) {
121: return cat._firstName.startsWith("A");
122: }
123:
124: }, new String[] { "Achat", "Acrobat" });
125:
126: }
127:
128: public void storeCats() {
129:
130: Cat winni = new Cat();
131: winni._sex = Animal.MALE;
132: winni._firstName = "Edwin";
133: winni._lastName = "Sanddrops";
134: winni._age = 12;
135:
136: Cat bachi = new Cat();
137: bachi._sex = Animal.FEMALE;
138: bachi._firstName = "Frau Bachmann";
139: bachi._lastName = "von der Bärenhöhle";
140: bachi._age = 10;
141:
142: Cat occam = new Cat();
143: occam._sex = Animal.MALE;
144: occam._firstName = "Occam";
145: occam._lastName = "von der Bärenhöhle";
146: occam._age = 1;
147: occam._father = winni;
148: occam._mother = bachi;
149:
150: Cat zora = new Cat();
151: zora._sex = Animal.FEMALE;
152: zora._firstName = "Vahiné";
153: zora._lastName = "des Fauves et Or";
154: zora._age = 1;
155:
156: Cat achat = new Cat();
157: achat._sex = Animal.FEMALE;
158: achat._firstName = "Achat";
159: achat._lastName = "Leo Lenis";
160: achat._father = occam;
161: achat._mother = zora;
162:
163: Cat acrobat = new Cat();
164: acrobat._sex = Animal.FEMALE;
165: acrobat._firstName = "Acrobat";
166: acrobat._lastName = "Leo Lenis";
167: acrobat._father = occam;
168: acrobat._mother = zora;
169:
170: ObjectContainer db = db();
171:
172: db.set(achat);
173: db.set(acrobat);
174:
175: Cat trulla = new Cat();
176: trulla._firstName = "Trulla";
177: db.set(trulla);
178:
179: }
180:
181: private void expect(Predicate predicate, String[] names) {
182:
183: if (names == null) {
184: names = new String[] {};
185: }
186:
187: List list = db().query(predicate);
188:
189: Iterator i = list.iterator();
190: while (i.hasNext()) {
191: Cat cat = (Cat) i.next();
192: boolean good = false;
193: for (int j = 0; j < names.length; j++) {
194: if (names[j] != null) {
195: if (cat._firstName.equals(names[j])) {
196: names[j] = null;
197: good = true;
198: break;
199: }
200: }
201: }
202: Assert.isTrue(good);
203: }
204: for (int j = 0; j < names.length; j++) {
205: Assert.isNull(names[j]);
206: }
207: }
208:
209: }
|