1 package org.apache.ojb.broker.cache; 2 3 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.ojb.broker.Identity; 24 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor; 25 import org.apache.ojb.broker.query.Criteria; 26 import org.apache.ojb.broker.query.Query; 27 import org.apache.ojb.broker.query.QueryFactory; 28 import org.apache.ojb.junit.PBTestCase; 29 30 37 public class LocalCacheTest extends PBTestCase 38 { 39 private static final int CASCADE_OBJECT = ObjectReferenceDescriptor.CASCADE_OBJECT; 42 43 public static void main(String [] args) 44 { 45 String [] arr = {LocalCacheTest.class.getName()}; 46 junit.textui.TestRunner.main(arr); 47 } 48 49 public void tearDown() throws Exception 50 { 51 super.tearDown(); 52 } 53 54 public void testCircularStore() 55 { 56 ojbChangeReferenceSetting(Person.class, "father", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 58 ojbChangeReferenceSetting(Person.class, "grandfather", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 59 ojbChangeReferenceSetting(Person.class, "childs", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 60 ojbChangeReferenceSetting(Person.class, "grandchilds", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 61 62 String postfix = "_testCircularStore_" + System.currentTimeMillis(); 63 Person junior = createComplexFamily(postfix); 64 broker.beginTransaction(); 65 broker.store(junior); 66 broker.commitTransaction(); 67 Identity oidJunior = new Identity(junior, broker); 68 Identity oidSenior = new Identity(junior.getFather(), broker); 69 broker.clearCache(); 70 71 Criteria crit = new Criteria(); 72 crit.addLike("name", "jeffChild_%" + postfix); 73 Query q = QueryFactory.newQuery(Person.class, crit); 74 75 Person newJunior = (Person) broker.getObjectByIdentity(oidJunior); 76 assertNotNull(newJunior); 77 assertNotNull(newJunior.getFather()); 78 assertNotNull(newJunior.getChilds()); 79 assertEquals(2, newJunior.getChilds().size()); 80 81 Person newSenior = (Person) broker.getObjectByIdentity(oidSenior); 82 assertNotNull(newSenior); 83 assertNotNull(newSenior.getChilds()); 84 assertEquals(1, newSenior.getChilds().size()); 85 assertNotNull(newSenior.getGrandchilds()); 86 assertEquals(2, newSenior.getGrandchilds().size()); 87 88 Collection result = broker.getCollectionByQuery(q); 89 assertEquals(2, result.size()); 90 for(Iterator iterator = result.iterator(); iterator.hasNext();) 91 { 92 Person p = (Person) iterator.next(); 93 assertNotNull(p.getFather()); 94 assertEquals("jeffJunior" + postfix, p.getFather().getName()); 95 assertNotNull(p.getGrandfather()); 96 assertEquals("jeffSenior" + postfix, p.getGrandfather().getName()); 97 } 98 } 99 100 103 public void testCircularStore_2() 104 { 105 ojbChangeReferenceSetting(Person.class, "father", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 107 ojbChangeReferenceSetting(Person.class, "grandfather", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 108 ojbChangeReferenceSetting(Person.class, "childs", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 109 ojbChangeReferenceSetting(Person.class, "grandchilds", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 110 111 String postfix = "_testCircularStore_2_" + System.currentTimeMillis(); 112 Person junior = createComplexFamily(postfix); 113 broker.beginTransaction(); 114 broker.store(junior); 115 broker.commitTransaction(); 116 Identity oidJunior = new Identity(junior, broker); 117 Identity oidSenior = new Identity(junior.getFather(), broker); 118 119 Criteria crit = new Criteria(); 120 crit.addLike("name", "jeffChild_%" + postfix); 121 Query q = QueryFactory.newQuery(Person.class, crit); 122 123 Person newJunior = (Person) broker.getObjectByIdentity(oidJunior); 124 assertNotNull(newJunior); 125 assertNotNull(newJunior.getFather()); 126 assertNotNull(newJunior.getChilds()); 127 assertEquals(2, newJunior.getChilds().size()); 128 129 Person newSenior = (Person) broker.getObjectByIdentity(oidSenior); 130 assertNotNull(newSenior); 131 assertNotNull(newSenior.getChilds()); 132 assertEquals(1, newSenior.getChilds().size()); 133 assertNotNull(newSenior.getGrandchilds()); 134 assertEquals(2, newSenior.getGrandchilds().size()); 135 136 Collection result = broker.getCollectionByQuery(q); 137 assertEquals(2, result.size()); 138 for(Iterator iterator = result.iterator(); iterator.hasNext();) 139 { 140 Person p = (Person) iterator.next(); 141 assertNotNull(p.getFather()); 142 assertEquals("jeffJunior" + postfix, p.getFather().getName()); 143 assertNotNull(p.getGrandfather()); 144 assertEquals("jeffSenior" + postfix, p.getGrandfather().getName()); 145 } 146 } 147 148 public void testCircularStoreUpdate() 149 { 150 ojbChangeReferenceSetting(Person.class, "father", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 152 ojbChangeReferenceSetting(Person.class, "grandfather", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 153 ojbChangeReferenceSetting(Person.class, "childs", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 154 ojbChangeReferenceSetting(Person.class, "grandchilds", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 155 156 String postfix = "_testCircularStore_" + System.currentTimeMillis(); 157 Person junior = createComplexFamily(postfix); 158 broker.beginTransaction(); 159 broker.store(junior); 160 broker.commitTransaction(); 161 Identity oidJunior = new Identity(junior, broker); 162 Identity oidSenior = new Identity(junior.getFather(), broker); 163 broker.clearCache(); 164 165 Criteria crit = new Criteria(); 166 crit.addLike("name", "jeffChild_%" + postfix); 167 Query q = QueryFactory.newQuery(Person.class, crit); 168 169 Person newJunior = (Person) broker.getObjectByIdentity(oidJunior); 170 assertNotNull(newJunior); 171 assertNotNull(newJunior.getFather()); 172 assertNotNull(newJunior.getChilds()); 173 assertEquals(2, newJunior.getChilds().size()); 174 175 Person newSenior = (Person) broker.getObjectByIdentity(oidSenior); 176 assertNotNull(newSenior); 177 assertNotNull(newSenior.getChilds()); 178 assertEquals(1, newSenior.getChilds().size()); 179 assertNotNull(newSenior.getGrandchilds()); 180 assertEquals(2, newSenior.getGrandchilds().size()); 181 182 Collection result = broker.getCollectionByQuery(q); 183 assertEquals(2, result.size()); 184 for(Iterator iterator = result.iterator(); iterator.hasNext();) 185 { 186 Person p = (Person) iterator.next(); 187 assertNotNull(p.getFather()); 188 assertEquals("jeffJunior" + postfix, p.getFather().getName()); 189 assertNotNull(p.getGrandfather()); 190 assertEquals("jeffSenior" + postfix, p.getGrandfather().getName()); 191 } 192 193 broker.beginTransaction(); 194 Person newChild = new Person("newGrandChild" + postfix, null, newSenior, null); 195 newSenior.addGranschild(newChild); 196 broker.store(newSenior); 197 broker.commitTransaction(); 198 broker.clearCache(); 199 200 newSenior = (Person) broker.getObjectByIdentity(oidSenior); 201 assertNotNull(newSenior); 202 assertNotNull(newSenior.getChilds()); 203 assertEquals(1, newSenior.getChilds().size()); 204 assertNotNull(newSenior.getGrandchilds()); 205 assertEquals(3, newSenior.getGrandchilds().size()); 206 } 207 208 211 public void testCircularStoreUpdate_2() 212 { 213 ojbChangeReferenceSetting(Person.class, "father", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 215 ojbChangeReferenceSetting(Person.class, "grandfather", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 216 ojbChangeReferenceSetting(Person.class, "childs", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 217 ojbChangeReferenceSetting(Person.class, "grandchilds", true, CASCADE_OBJECT, CASCADE_OBJECT, false); 218 219 String postfix = "_testCircularStore_2_" + System.currentTimeMillis(); 220 Person junior = createComplexFamily(postfix); 221 broker.beginTransaction(); 222 broker.store(junior); 223 broker.commitTransaction(); 224 Identity oidJunior = new Identity(junior, broker); 225 Identity oidSenior = new Identity(junior.getFather(), broker); 226 227 Criteria crit = new Criteria(); 228 crit.addLike("name", "jeffChild_%" + postfix); 229 Query q = QueryFactory.newQuery(Person.class, crit); 230 231 Person newJunior = (Person) broker.getObjectByIdentity(oidJunior); 232 assertNotNull(newJunior); 233 assertNotNull(newJunior.getFather()); 234 assertNotNull(newJunior.getChilds()); 235 assertEquals(2, newJunior.getChilds().size()); 236 237 Person newSenior = (Person) broker.getObjectByIdentity(oidSenior); 238 assertNotNull(newSenior); 239 assertNotNull(newSenior.getChilds()); 240 assertEquals(1, newSenior.getChilds().size()); 241 assertNotNull(newSenior.getGrandchilds()); 242 assertEquals(2, newSenior.getGrandchilds().size()); 243 244 Collection result = broker.getCollectionByQuery(q); 245 assertEquals(2, result.size()); 246 for(Iterator iterator = result.iterator(); iterator.hasNext();) 247 { 248 Person p = (Person) iterator.next(); 249 assertNotNull(p.getFather()); 250 assertEquals("jeffJunior" + postfix, p.getFather().getName()); 251 assertNotNull(p.getGrandfather()); 252 assertEquals("jeffSenior" + postfix, p.getGrandfather().getName()); 253 } 254 255 broker.beginTransaction(); 256 Person newChild = new Person("newGrandChild" + postfix, null, newSenior, null); 257 newSenior.addGranschild(newChild); 258 broker.store(newSenior); 259 broker.commitTransaction(); 260 261 newSenior = (Person) broker.getObjectByIdentity(oidSenior); 262 assertNotNull(newSenior); 263 assertNotNull(newSenior.getChilds()); 264 assertEquals(1, newSenior.getChilds().size()); 265 assertNotNull(newSenior.getGrandchilds()); 266 assertEquals(3, newSenior.getGrandchilds().size()); 267 } 268 269 272 private Person createComplexFamily(String postfix) 273 { 274 Person jeffJunior = new Person(); 275 jeffJunior.setName("jeffJunior" + postfix); 276 277 Person jeffSenior = new Person(); 278 jeffSenior.setName("jeffSenior" + postfix); 279 280 Person jeffChild_1 = new Person(); 281 jeffChild_1.setName("jeffChild_1" + postfix); 282 jeffChild_1.setFather(jeffJunior); 283 jeffChild_1.setGrandfather(jeffSenior); 284 285 Person jeffChild_2 = new Person(); 286 jeffChild_2.setName("jeffChild_2" + postfix); 287 jeffChild_2.setFather(jeffJunior); 288 jeffChild_2.setGrandfather(jeffSenior); 289 290 jeffJunior.setFather(jeffSenior); 291 jeffJunior.addChild(jeffChild_1); 292 jeffJunior.addChild(jeffChild_2); 293 294 jeffSenior.addChild(jeffJunior); 295 jeffSenior.addGranschild(jeffChild_1); 296 jeffSenior.addGranschild(jeffChild_2); 297 298 return jeffJunior; 299 } 300 301 public static class Person 302 { 303 private Integer id; 304 private String name; 305 private Person father; 306 private Person grandfather; 307 private List childs; 308 private List grandchilds; 309 private Integer fkChild; 310 private Integer fkGrandchild; 311 312 public Person() 313 { 314 } 315 316 public Person(String name, Person father, Person grandfather, List childs) 317 { 318 this.name = name; 319 this.father = father; 320 this.grandfather = grandfather; 321 this.childs = childs; 322 } 323 324 public Integer getId() 325 { 326 return id; 327 } 328 329 public void setId(Integer id) 330 { 331 this.id = id; 332 } 333 334 public String getName() 335 { 336 return name; 337 } 338 339 public void setName(String name) 340 { 341 this.name = name; 342 } 343 344 public Person getFather() 345 { 346 return father; 347 } 348 349 public void setFather(Person father) 350 { 351 this.father = father; 352 } 353 354 public Person getGrandfather() 355 { 356 return grandfather; 357 } 358 359 public void setGrandfather(Person grandfather) 360 { 361 this.grandfather = grandfather; 362 } 363 364 public List getChilds() 365 { 366 return childs; 367 } 368 369 public void setChilds(List childs) 370 { 371 this.childs = childs; 372 } 373 374 public void addChild(Person child) 375 { 376 if(childs == null) 377 { 378 childs = new ArrayList (); 379 } 380 childs.add(child); 381 } 382 383 public List getGrandchilds() 384 { 385 return grandchilds; 386 } 387 388 public void setGrandchilds(List grandchilds) 389 { 390 this.grandchilds = grandchilds; 391 } 392 393 public void addGranschild(Person child) 394 { 395 if(grandchilds == null) 396 { 397 grandchilds = new ArrayList (); 398 } 399 grandchilds.add(child); 400 } 401 402 public Integer getFkChild() 403 { 404 return fkChild; 405 } 406 407 public void setFkChild(Integer fkChild) 408 { 409 this.fkChild = fkChild; 410 } 411 412 public Integer getFkGrandchild() 413 { 414 return fkGrandchild; 415 } 416 417 public void setFkGrandchild(Integer fkGrandchild) 418 { 419 this.fkGrandchild = fkGrandchild; 420 } 421 } 422 } 423 | Popular Tags |