1 9 package org.apache.ojb.odmg; 10 11 import java.sql.Timestamp ; 12 13 import org.apache.ojb.broker.Contract; 14 import org.apache.ojb.broker.Effectiveness; 15 import org.apache.ojb.broker.ManageableCollection; 16 import org.apache.ojb.broker.RelatedToContract; 17 import org.apache.ojb.broker.Version; 18 import org.apache.ojb.junit.ODMGTestCase; 19 import org.odmg.Database; 20 import org.odmg.Implementation; 21 import org.odmg.OQLQuery; 22 import org.odmg.Transaction; 23 24 public class ContractVersionEffectivenessOQLTest extends ODMGTestCase 25 { 26 27 private static Class CLASS = ContractVersionEffectivenessOQLTest.class; 28 private int COUNT = 10; 29 30 35 public static void main(String [] args) 36 { 37 String [] arr = { CLASS.getName()}; 38 junit.textui.TestRunner.main(arr); 39 } 40 41 public ContractVersionEffectivenessOQLTest(String name) 42 { 43 super(name); 44 } 45 46 private void createData(Database db, Implementation odmg) 47 throws Exception 48 { 49 Transaction tx = odmg.newTransaction(); 51 for (int i = 0; i < COUNT; i++) 52 { 53 tx.begin(); 54 Contract contract = new Contract(); 55 contract.setPk("C" + i + System.currentTimeMillis()); 56 contract.setContractValue1("contractvalue1"); 57 contract.setContractValue2(1); 58 contract.setContractValue3("contractvalue3"); 59 contract.setContractValue4(new Timestamp (System.currentTimeMillis())); 60 db.makePersistent(contract); 61 62 Version version = new Version(); 63 version.setPk("V" + i + System.currentTimeMillis()); 64 version.setVersionValue1("versionvalue1"); 65 version.setVersionValue2(1); 66 version.setVersionValue3(new Timestamp (System.currentTimeMillis())); 67 version.setContract(contract); 68 db.makePersistent(version); 69 70 Effectiveness eff = new Effectiveness(); 71 eff.setPk("E" + i + System.currentTimeMillis()); 72 eff.setEffValue1("effvalue1"); 73 eff.setEffValue2(1); 74 eff.setEffValue3(new Timestamp (System.currentTimeMillis())); 75 eff.setVersion(version); 76 79 db.makePersistent(eff); 80 tx.commit(); 81 } 82 83 } 84 85 public void testCreate() throws Exception 86 { 87 createData(database, odmg); 88 } 89 90 public void testComplexOQL() 91 throws Exception 92 { 93 96 Transaction tx = odmg.newTransaction(); 97 tx.begin(); 98 99 Contract contract = new Contract(); 100 contract.setPk("C" + System.currentTimeMillis()); 101 contract.setContractValue1("version.contract.contractValue1.testComplexOQL"); 102 contract.setContractValue2(1); 103 contract.setContractValue3("contractvalue3"); 104 contract.setContractValue4(new Timestamp (System.currentTimeMillis())); 105 database.makePersistent(contract); 106 107 RelatedToContract rtc = new RelatedToContract(); 108 rtc.setPk("R" + System.currentTimeMillis()); 109 rtc.setRelatedValue1("test"); 110 rtc.setRelatedValue2(5); 111 rtc.setRelatedValue3(new Timestamp (System.currentTimeMillis())); 112 contract.setRelatedToContract(rtc); 113 database.makePersistent(rtc); 114 115 Version version = new Version(); 116 version.setPk("V" + System.currentTimeMillis()); 117 version.setVersionValue1("versionvalue1"); 118 version.setVersionValue2(1); 119 version.setVersionValue3(new Timestamp (System.currentTimeMillis())); 120 version.setContract(contract); 121 database.makePersistent(version); 122 123 Effectiveness eff = new Effectiveness(); 124 eff.setPk("E" + System.currentTimeMillis()); 125 eff.setEffValue1("effValue1.testComplexOQL"); 126 eff.setEffValue2(20); 127 eff.setEffValue3(new Timestamp (System.currentTimeMillis())); 128 eff.setVersion(version); 129 database.makePersistent(eff); 130 131 tx.commit(); 132 135 String oql = "select s from " + org.apache.ojb.broker.Effectiveness.class.getName() + " where " + 136 " version.contract.contractValue1=$1 and effValue1 = $2 and " + 137 " (effValue3 > $3 or is_undefined(effValue3)) and " + 138 " effValue2 <= $4 and (effValue3<$5 or is_undefined(effValue3)) and " + 139 " version.contract.relatedToContract.relatedValue2=$6"; 140 141 OQLQuery query = odmg.newOQLQuery(); 142 query.create(oql); 143 query.bind("version.contract.contractValue1.testComplexOQL"); query.bind("effValue1.testComplexOQL"); query.bind(new Timestamp (System.currentTimeMillis() - 5000)); query.bind(new Integer (20)); query.bind(new Timestamp (System.currentTimeMillis() + 5000)); query.bind(new Integer (5)); 150 ManageableCollection all = (ManageableCollection) query.execute(); 151 java.util.Iterator it = all.ojbIterator(); 152 155 int i = 0; 156 while (it.hasNext()) 157 { 158 it.next(); 159 i++; 160 } 161 if (i != 1) 162 { 163 fail("Should have found just one object, instead found " + i); 164 } 165 } 166 167 public void testComplexOQL2() 168 throws Exception 169 { 170 173 Transaction tx = odmg.newTransaction(); 174 tx.begin(); 175 176 Contract contract = new Contract(); 177 contract.setPk("C" + System.currentTimeMillis()); 178 contract.setContractValue1("version.contract.contractValue1.testComplexOQL"); 179 contract.setContractValue2(1); 180 contract.setContractValue3("contractvalue3"); 181 contract.setContractValue4(new Timestamp (System.currentTimeMillis())); 182 database.makePersistent(contract); 183 184 Version version = new Version(); 185 version.setPk("V" + System.currentTimeMillis()); 186 version.setVersionValue1("versionvalue1"); 187 version.setVersionValue2(1); 188 version.setVersionValue3(new Timestamp (System.currentTimeMillis())); 189 version.setContract(contract); 190 database.makePersistent(version); 191 192 Effectiveness eff = new Effectiveness(); 193 eff.setPk("E" + System.currentTimeMillis()); 194 eff.setEffValue1("effValue1.testComplexOQL"); 195 eff.setEffValue2(20); 196 eff.setEffValue3(new Timestamp (System.currentTimeMillis())); 197 eff.setVersion(version); 198 database.makePersistent(eff); 199 200 tx.commit(); 201 204 String oql = "select s from " + org.apache.ojb.broker.Effectiveness.class.getName() + " where " + 205 " version.contract.contractValue1=$1 and effValue1 = $2 and " + 206 " (effValue3 > $3 or is_undefined(effValue3)) and " + 207 " effValue2 <= $4 and (effValue3<$5 or is_undefined(effValue3)) and " + 208 " is_undefined(version.contract.relatedToContract.pk)"; 209 210 OQLQuery query = odmg.newOQLQuery(); 211 query.create(oql); 212 query.bind("version.contract.contractValue1.testComplexOQL"); query.bind("effValue1.testComplexOQL"); query.bind(new Timestamp (System.currentTimeMillis() - 5000)); query.bind(new Integer (20)); query.bind(new Timestamp (System.currentTimeMillis() + 5000)); 218 ManageableCollection all = (ManageableCollection) query.execute(); 219 java.util.Iterator it = all.ojbIterator(); 220 223 int i = 0; 224 while (it.hasNext()) 225 { 226 it.next(); 227 i++; 228 } 229 if (i != 1) 230 { 231 fail("Should have found just one object, instead found " + i); 232 } 233 } 234 235 public void testGetWithVersionCriteria() throws Exception 236 { 237 createData(database, odmg); 238 OQLQuery query = odmg.newOQLQuery(); 239 int i = 0; 240 query.create("select effectiveness from " + Effectiveness.class.getName() + " where version.versionValue1=$1"); 241 query.bind("versionvalue1"); 242 ManageableCollection all = (ManageableCollection) query.execute(); 243 java.util.Iterator it = all.ojbIterator(); 244 Effectiveness temp = null; 245 while (it.hasNext()) 246 { 247 temp = (Effectiveness) it.next(); 248 if (!temp.getVersion().getVersionValue1().equals("versionvalue1")) 249 { 250 fail("Should find only effectiveness objects where version.versionValue1='versionvalue1', found one with value " + temp.getVersion().getVersionValue1()); 251 } 252 i++; 253 } 254 if (i < COUNT) 255 fail("Should have found at least " + COUNT + " where version.versionValue1='versionvalue1' items, only found " + i); 256 } 257 258 public void testGetEmbeddedObject() throws Exception 259 { 260 createData(database, odmg); 261 OQLQuery query = odmg.newOQLQuery(); 262 query.create("select effectiveness.version from " + Effectiveness.class.getName() + " where is_defined(effectiveness.version.versionValue1)"); 263 ManageableCollection all = (ManageableCollection) query.execute(); 264 java.util.Iterator it = all.ojbIterator(); 265 while (it.hasNext()) 266 { 267 assertTrue("Selected item is Version", (it.next() instanceof Version)); 268 } 269 270 query.create("select effectiveness.version.contract from " + Effectiveness.class.getName() + " where is_defined(effectiveness.version.versionValue1)"); 271 all = (ManageableCollection) query.execute(); 272 it = all.ojbIterator(); 273 while (it.hasNext()) 274 { 275 assertTrue("Selected item is Contract", (it.next() instanceof Contract)); 276 } 277 } 278 279 280 public void testGetWithContractCriteria() throws Exception 281 { 282 createData(database, odmg); 283 OQLQuery query = odmg.newOQLQuery(); 284 int i = 0; 285 query.create("select effectiveness from " + Effectiveness.class.getName() + " where version.contract.contractValue1=$1"); 286 query.bind("contractvalue1"); 287 ManageableCollection all = (ManageableCollection) query.execute(); 288 java.util.Iterator it = all.ojbIterator(); 289 Effectiveness temp = null; 290 while (it.hasNext()) 291 { 292 temp = (Effectiveness) it.next(); 293 if (!temp.getVersion().getContract().getContractValue1().equals("contractvalue1")) 294 { 295 fail("Should find only effectiveness objects where contract.contractValue1='contractvalue1', found one with value " + temp.getVersion().getContract().getContractValue1()); 296 } 297 i++; 298 } 299 if (i < COUNT) 300 fail("Should have found at least " + COUNT + " where version.contract.contractValue1='contractvalue1' items, only found " + i); 301 } 302 303 public void testGet() throws Exception 304 { 305 createData(database, odmg); 306 OQLQuery query = odmg.newOQLQuery(); 307 int i = 0; 308 query.create("select effectiveness from " + Effectiveness.class.getName()); 309 ManageableCollection all = (ManageableCollection) query.execute(); 310 java.util.Iterator it = all.ojbIterator(); 311 while (it.hasNext()) 312 { 313 it.next(); 314 i++; 315 } 316 if (i < COUNT) 317 fail("Should have found at least " + COUNT + " items, only found " + i); 318 } 319 320 public void testDelete() throws Exception 321 { 322 325 createData(database, odmg); 326 327 Transaction tx = odmg.newTransaction(); 329 330 OQLQuery query = odmg.newOQLQuery(); 331 ManageableCollection all = null; 332 java.util.Iterator it = null; 333 int i = 0; 334 query.create("select effectiveness from " + org.apache.ojb.broker.Effectiveness.class.getName()); 335 336 340 all = (ManageableCollection) query.execute(); 341 it = all.ojbIterator(); 343 Effectiveness eff = null; 344 Version ver = null; 345 Contract contract = null; 346 while (it.hasNext()) 347 { 348 eff = (Effectiveness) it.next(); 349 ver = eff.getVersion(); 350 contract = ver.getContract(); 351 352 tx.begin(); 353 database.deletePersistent(eff); 354 tx.commit(); 355 356 tx.begin(); 357 database.deletePersistent(ver); 358 tx.commit(); 359 360 tx.begin(); 361 database.deletePersistent(contract); 362 tx.commit(); 363 i++; 365 } 366 if (i < COUNT) 367 fail("Should have found at least " + COUNT + " items to delete, only found " + i); 368 371 query.create("select contracts from " + org.apache.ojb.broker.Contract.class.getName()); 372 ManageableCollection allContracts = (ManageableCollection) query.execute(); 373 allContracts = (ManageableCollection) query.execute(); 374 it = allContracts.ojbIterator(); 375 if (it.hasNext()) 376 { 377 fail("all contracts should have been removed, we found one."); 378 } 379 } 380 381 385 public void XtestNotYetWorkingDelete() throws Exception 386 { 387 390 createData(database, odmg); 391 392 Transaction tx = odmg.newTransaction(); 394 395 OQLQuery query = odmg.newOQLQuery(); 396 ManageableCollection all = null; 397 java.util.Iterator it = null; 398 int i = 0; 399 query.create("select effectiveness from " + org.apache.ojb.broker.Effectiveness.class.getName()); 400 401 405 all = (ManageableCollection) query.execute(); 406 it = all.ojbIterator(); 408 Effectiveness eff = null; 409 Version ver = null; 410 Contract contract = null; 411 415 tx.begin(); 416 while (it.hasNext()) 417 { 418 eff = (Effectiveness) it.next(); 419 ver = eff.getVersion(); 420 contract = ver.getContract(); 421 424 database.deletePersistent(contract); 425 i++; 426 } 427 430 tx.commit(); 431 if (i < COUNT) 432 fail("Should have found at least " + COUNT + " effectiveness to delete, only found " + i); 433 436 query.create("select contracts from " + org.apache.ojb.broker.Contract.class.getName()); 437 ManageableCollection allContracts = (ManageableCollection) query.execute(); 438 allContracts = (ManageableCollection) query.execute(); 439 it = allContracts.ojbIterator(); 440 if (it.hasNext()) 441 { 442 fail("all contracts should have been removed, we found one."); 443 } 444 } 445 446 449 public void testQuery() throws Exception 450 { 451 createData(database, odmg); 452 Transaction tx = odmg.newTransaction(); 454 tx.begin(); 455 456 OQLQuery query = odmg.newOQLQuery(); 457 String sql = "select effectiveness from " + Effectiveness.class.getName(); 458 query.create(sql); 459 460 ManageableCollection allEffectiveness = (ManageableCollection) query.execute(); 461 462 java.util.Iterator it = allEffectiveness.ojbIterator(); 464 int i = 0; 465 while (it.hasNext()) 466 { 467 Effectiveness value = (Effectiveness) it.next(); 468 471 if (value.getVersion().getContract().getPk() == null) 472 fail("Contract PK should not be null"); 473 i++; 474 } 475 if (i < COUNT) 476 fail("Should have found at least " + COUNT + " items, only found: " + i); 477 tx.commit(); 478 } 479 480 484 public void testContractReassignment() throws Exception 485 { 486 Transaction tx = odmg.newTransaction(); 487 Contract contract = new Contract(); 488 contract.setPk("contract1"); 489 contract.setContractValue1("contract1value1"); 490 contract.setContractValue2(1); 491 contract.setContractValue3("contract1value3"); 492 contract.setContractValue4(new Timestamp (System.currentTimeMillis())); 493 494 Version version = new Version(); 495 version.setPk("version1"); 496 version.setVersionValue1("version1value1"); 497 version.setVersionValue2(1); 498 version.setVersionValue3(new Timestamp (System.currentTimeMillis())); 499 version.setContract(contract); 500 501 Effectiveness eff = new Effectiveness(); 502 eff.setPk("eff1"); 503 eff.setEffValue1("eff1value1"); 504 eff.setEffValue2(1); 505 eff.setEffValue3(new Timestamp (System.currentTimeMillis())); 506 eff.setVersion(version); 507 508 Contract contract2 = new Contract(); 509 contract2.setPk("contract2"); 510 contract2.setContractValue1("contract2value1"); 511 contract2.setContractValue2(1); 512 contract2.setContractValue3("contractvalue3"); 513 contract2.setContractValue4(new Timestamp (System.currentTimeMillis())); 514 515 Version version2 = new Version(); 516 version2.setPk("version2"); 517 version2.setVersionValue1("version2value1"); 518 version2.setVersionValue2(1); 519 version2.setVersionValue3(new Timestamp (System.currentTimeMillis())); 520 version2.setContract(contract2); 521 522 Effectiveness eff2 = new Effectiveness(); 523 eff2.setPk("eff2"); 524 eff2.setEffValue1("eff2value1"); 525 eff2.setEffValue2(1); 526 eff2.setEffValue3(new Timestamp (System.currentTimeMillis())); 527 eff2.setVersion(version2); 528 529 532 tx.begin(); 533 database.makePersistent(eff2); 534 database.makePersistent(eff); 535 tx.commit(); 536 537 540 tx.begin(); 541 tx.lock(version, Transaction.WRITE); 542 tx.lock(version2, Transaction.WRITE); 543 version.setContract(contract2); 544 version2.setContract(contract); 545 tx.commit(); 546 547 550 OQLQuery query = odmg.newOQLQuery(); 551 String sql = "select version from " + org.apache.ojb.broker.Version.class.getName() + " where pk=$1"; 552 query.create(sql); 553 query.bind("version1"); 554 tx.begin(); 555 ManageableCollection results = (ManageableCollection) query.execute(); 556 java.util.Iterator it = results.ojbIterator(); 558 Version ver1 = null; 559 while (it.hasNext()) 560 { 561 ver1 = (Version) it.next(); 562 if (!ver1.getContract().getPk().equals(contract2.getPk())) 563 { 564 fail(ver1.getPk() + " should have pointed to contract2 instead it pointed to: " + ver1.getContract().getPk()); 565 } 566 } 567 tx.commit(); 568 569 OQLQuery query2 = odmg.newOQLQuery(); 570 String sql2 = "select version from " + org.apache.ojb.broker.Version.class.getName() + " where pk=$1"; 571 query2.create(sql2); 572 query2.bind("version2"); 573 tx.begin(); 574 results = (ManageableCollection) query2.execute(); 575 java.util.Iterator it2 = results.ojbIterator(); 577 Version ver2 = null; 578 while (it2.hasNext()) 579 { 580 ver2 = (Version) it2.next(); 581 if (!ver2.getContract().getPk().equals(contract.getPk())) 582 { 583 fail(ver2.getPk() + " should have pointed to contract instead it pointed to: " + ver2.getContract().getPk()); 584 } 585 } 586 tx.commit(); 587 588 591 tx.begin(); 592 database.deletePersistent(eff2); 593 database.deletePersistent(eff); 594 tx.commit(); 595 tx.begin(); 596 database.deletePersistent(version2); 597 database.deletePersistent(version); 598 tx.commit(); 599 tx.begin(); 600 database.deletePersistent(contract2); 601 database.deletePersistent(contract); 602 tx.commit(); 603 } 604 } 605 | Popular Tags |