1 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 []{"Occam", "Vahiné" }); 69 70 expect(new Predicate(){ 71 public boolean match(Cat cat){ 72 return cat._father._age == 1; 73 } 74 }, new String []{"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 []{"Achat", "Acrobat" }); 81 82 83 86 93 94 expect(new Predicate(){ 96 public boolean match(Cat cat){ 97 return cat._age + 1 == 2; 98 } 99 }, new String []{"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 []{"Occam"}); 108 109 110 expect(new Predicate(){ 112 public boolean match(Cat cat){ 113 return cat.getFullName().equals("Achat Leo Lenis"); 114 } 115 }, new String []{"Achat"}); 116 117 118 expect(new Predicate(){ 120 public boolean match(Cat cat){ 121 return cat.getFullName() == null; 122 } 123 }, new String []{"Trulla"}); 124 125 126 expect(new Predicate(){ 128 public boolean match(Cat cat){ 129 return cat._firstName.startsWith("A"); 130 } 131 132 }, new String []{"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 [] names){ 190 191 if(names == null){ 192 names = new String [] {}; 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 |