1 package org.apache.ojb.broker; 2 3 import junit.framework.Assert; 4 import junit.framework.TestCase; 5 import org.apache.ojb.broker.query.Criteria; 6 import org.apache.ojb.broker.query.QueryByCriteria; 7 import org.apache.ojb.broker.metadata.MetadataManager; 8 import org.apache.ojb.broker.metadata.ConnectionRepository; 9 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; 10 import org.apache.commons.lang.builder.ToStringBuilder; 11 import org.apache.commons.lang.builder.ToStringStyle; 12 13 import java.sql.Connection ; 14 import java.util.Collection ; 15 16 21 public class MultipleDBTest extends TestCase 22 { 23 private static final String REFERENCE_NAME = "Test reference"; 24 25 public MultipleDBTest(String s) 26 { 27 super(s); 28 } 29 30 protected void setUp() throws Exception 31 { 32 super.setUp(); 33 MetadataManager mm = MetadataManager.getInstance(); 34 JdbcConnectionDescriptor jcd = mm.connectionRepository().getDescriptor(TestHelper.FAR_AWAY_KEY); 35 if(jcd == null) 36 { 37 ConnectionRepository cr = mm.readConnectionRepository(TestHelper.FAR_AWAY_CONNECTION_REPOSITORY); 38 mm.connectionRepository().addDescriptor(cr.getDescriptor(TestHelper.FAR_AWAY_KEY)); 39 } 40 } 41 42 protected void tearDown() throws Exception 43 { 44 MetadataManager mm = MetadataManager.getInstance(); 45 JdbcConnectionDescriptor jcd = mm.connectionRepository().getDescriptor(TestHelper.FAR_AWAY_KEY); 46 mm.connectionRepository().removeDescriptor(jcd); 47 super.tearDown(); 48 } 49 50 public static void main(String [] args) 51 { 52 String [] arr = {MultipleDBTest.class.getName()}; 53 junit.textui.TestRunner.main(arr); 54 } 55 56 61 public void testMaterializeFromDifferentDB() 62 { 63 String name = "testMaterializeFromDifferentDB" + System.currentTimeMillis(); 64 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 65 PersistenceBroker brokerFarAway = PersistenceBrokerFactory.createPersistenceBroker(TestHelper.FAR_AWAY_KEY); 66 67 MultipleObject obj = new MultipleObject(); 68 obj.setName(name); 69 broker.beginTransaction(); 70 broker.store(obj); 71 broker.commitTransaction(); 72 73 Identity oid = new Identity(obj, broker); 74 75 MultipleObjectRef ref_1 = new MultipleObjectRef(); 76 MultipleObjectRef ref_2 = new MultipleObjectRef(); 77 ref_1.setName(name); 78 ref_1.setRefId(obj.getId()); 79 ref_2.setName(name); 80 ref_2.setRefId(obj.getId()); 81 82 brokerFarAway.beginTransaction(); 83 brokerFarAway.store(ref_1); 84 brokerFarAway.store(ref_2); 85 brokerFarAway.commitTransaction(); 86 87 broker.clearCache(); 88 brokerFarAway.clearCache(); 89 90 MultipleObject newObj = (MultipleObject)broker.getObjectByIdentity(oid); 91 brokerFarAway.retrieveAllReferences(newObj); 92 93 assertNotNull(newObj.getReferences()); 94 assertEquals(2, newObj.getReferences().size()); 95 } 97 98 102 public void testLookupByPBKey() 103 { 104 PBKey pb_1a = new PBKey(TestHelper.DEF_JCD_ALIAS); 105 PBKey pb_1b = new PBKey(TestHelper.DEF_JCD_ALIAS, null, null); 106 PBKey pb_1c = new PBKey(TestHelper.DEF_JCD_ALIAS, TestHelper.DEF_USER, TestHelper.DEF_PASSWORD); 107 108 PBKey pb_2a = new PBKey(TestHelper.FAR_AWAY_JCD_ALIAS); 109 PBKey pb_2b = new PBKey(TestHelper.FAR_AWAY_JCD_ALIAS, null, null); 110 111 PersistenceBroker b1a = PersistenceBrokerFactory.createPersistenceBroker(pb_1a); 112 PersistenceBroker b1b = PersistenceBrokerFactory.createPersistenceBroker(pb_1b); 113 PersistenceBroker b1c = PersistenceBrokerFactory.createPersistenceBroker(pb_1c); 114 PersistenceBroker b2a = PersistenceBrokerFactory.createPersistenceBroker(pb_2a); 115 PersistenceBroker b2b = PersistenceBrokerFactory.createPersistenceBroker(pb_2b); 116 117 assertNotNull(b1a); 118 assertNotNull(b1b); 119 assertNotNull(b1c); 120 assertNotNull(b2a); 121 assertNotNull(b2b); 122 123 if(b1a != null) b1a.close(); 124 if(b1b != null) b1b.close(); 125 if(b1c != null) b1c.close(); 126 if(b2a != null) b2a.close(); 127 if(b2b != null) b2b.close(); 128 } 129 130 public void testPBLookupConnection() throws Exception 131 { 132 PBKey key = new PBKey(TestHelper.FAR_AWAY_JCD_ALIAS); 133 PersistenceBroker broker = PersistenceBrokerFactory.createPersistenceBroker(key); 134 Connection con = broker.serviceConnectionManager().getConnection(); 136 con.isClosed(); 137 assertNotNull(broker); 138 assertEquals(key.getAlias(), broker.getPBKey().getAlias()); 139 broker.close(); 140 141 key = new PBKey(TestHelper.DEF_JCD_ALIAS); 142 broker = PersistenceBrokerFactory.createPersistenceBroker(key); 143 con = broker.serviceConnectionManager().getConnection(); 145 con.isClosed(); 146 assertNotNull(broker); 147 assertEquals(key.getAlias(), broker.getPBKey().getAlias()); 148 broker.close(); 149 } 150 151 public void testPBCreation() throws Exception 152 { 153 PersistenceBroker defPB = null; 154 PersistenceBroker secPB = null; 155 156 try 157 { 158 defPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 159 PBKey secKey = TestHelper.FAR_AWAY_KEY; 160 secPB = PersistenceBrokerFactory.createPersistenceBroker(secKey); 161 162 Assert.assertNotNull("Cannot lookup default PB", defPB); 163 Assert.assertNotNull("Cannot lookup PB for PBKey: " + secKey, secPB); 164 Assert.assertEquals("Different repository files for second db", 165 secPB.getPBKey().getAlias(), secKey.getAlias()); 166 } 167 finally 168 { 169 if (defPB != null) defPB.close(); 170 if (secPB != null) secPB.close(); 171 } 172 173 } 174 175 178 public void testInsertDeleteNoAutoSequence() throws Exception 179 { 180 Article article = createArticleWithId(Integer.MAX_VALUE - 1001); 181 PBKey secKey = TestHelper.FAR_AWAY_KEY; 182 183 FarAwayClass fa = createFarAwayObjectWithId(Integer.MAX_VALUE - 1002); 184 PersistenceBroker secPB = PersistenceBrokerFactory.createPersistenceBroker(secKey); 185 secPB.beginTransaction(); 186 secPB.store(fa); 187 secPB.commitTransaction(); 188 secPB.close(); 189 190 PersistenceBroker defPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 191 defPB.beginTransaction(); 192 defPB.store(article); 193 defPB.commitTransaction(); 194 defPB.close(); 195 196 197 198 secPB = PersistenceBrokerFactory.createPersistenceBroker(secKey); 199 secPB.clearCache(); 200 Object [] pks = {new Integer (fa.getId())}; 201 Identity oid = new Identity(FarAwayClass.class, FarAwayClass.class, pks); 202 FarAwayClass fa2 = (FarAwayClass) secPB.getObjectByIdentity(oid); 203 Assert.assertNotNull("Lookup for article in second DB failed", fa2); 204 Assert.assertEquals(fa.toString(), fa2.toString()); 205 secPB.close(); 206 207 defPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 208 defPB.clearCache(); 209 Identity oid2 = defPB.serviceIdentity().buildIdentity(Article.class, article.getArticleId()); 210 Article article2 = (Article) defPB.getObjectByIdentity(oid2); 211 Assert.assertNotNull("Lookup for article in default DB failed", article2); 212 defPB.close(); 213 214 secPB = PersistenceBrokerFactory.createPersistenceBroker(secKey); 215 secPB.beginTransaction(); 216 secPB.delete(fa); 217 secPB.commitTransaction(); 218 secPB.close(); 219 220 defPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 221 defPB.beginTransaction(); 222 defPB.delete(article); 223 defPB.commitTransaction(); 224 defPB.close(); 225 } 226 227 230 public void testInsertDeleteAutoSequenceClearCache() throws Exception 231 { 232 Article article = createArticle(); 233 PBKey secKey = TestHelper.FAR_AWAY_KEY; 234 235 FarAwayClass fa = createFarAwayObject(); 236 PersistenceBroker farAwayPB = PersistenceBrokerFactory.createPersistenceBroker(secKey); 237 farAwayPB.clearCache(); 238 farAwayPB.beginTransaction(); 239 farAwayPB.store(fa); 240 farAwayPB.commitTransaction(); 241 farAwayPB.close(); 242 243 PersistenceBroker defaultPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 244 defaultPB.clearCache(); 245 defaultPB.beginTransaction(); 246 defaultPB.store(article); 247 defaultPB.commitTransaction(); 248 defaultPB.close(); 249 250 farAwayPB = PersistenceBrokerFactory.createPersistenceBroker(secKey); 251 farAwayPB.clearCache(); 252 Object [] pks = {new Integer (fa.getId())}; 253 Identity oid = new Identity(FarAwayClass.class, FarAwayClass.class, pks); 254 FarAwayClass fa2 = (FarAwayClass) farAwayPB.getObjectByIdentity(oid); 255 Assert.assertNotNull("Lookup for article in second DB failed", fa2); 256 Assert.assertEquals(fa.toString(), fa2.toString()); 257 farAwayPB.close(); 258 259 defaultPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 260 defaultPB.clearCache(); 261 Identity oid2 = defaultPB.serviceIdentity().buildIdentity(Article.class, article.getArticleId()); 262 Article article2 = (Article) defaultPB.getObjectByIdentity(oid2); 263 Assert.assertNotNull("Lookup for article in default DB failed", article2); 264 defaultPB.close(); 265 266 farAwayPB = PersistenceBrokerFactory.createPersistenceBroker(secKey); 267 farAwayPB.clearCache(); 268 farAwayPB.beginTransaction(); 269 farAwayPB.delete(fa); 270 farAwayPB.commitTransaction(); 271 farAwayPB.close(); 272 273 defaultPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 274 defaultPB.clearCache(); 275 defaultPB.beginTransaction(); 276 defaultPB.delete(article); 277 defaultPB.commitTransaction(); 278 defaultPB.close(); 279 } 280 281 284 public void testInsertDeleteAutoSequence() throws Exception 285 { 286 Article article = createArticle(); 287 PBKey secKey = TestHelper.FAR_AWAY_KEY; 288 289 PersistenceBroker defaultPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 290 defaultPB.beginTransaction(); 291 defaultPB.store(article); 292 defaultPB.commitTransaction(); 293 defaultPB.close(); 294 295 FarAwayClass fa = createFarAwayObject(); 296 PersistenceBroker farAwayPB = PersistenceBrokerFactory.createPersistenceBroker(secKey); 297 farAwayPB.beginTransaction(); 298 farAwayPB.store(fa); 299 farAwayPB.commitTransaction(); 300 farAwayPB.close(); 301 302 farAwayPB = PersistenceBrokerFactory.createPersistenceBroker(secKey); 303 Object [] pks = {new Integer (fa.getId())}; 304 Identity oid = new Identity(FarAwayClass.class, FarAwayClass.class, pks); 305 FarAwayClass fa2 = (FarAwayClass) farAwayPB.getObjectByIdentity(oid); 306 Assert.assertNotNull("Lookup for article in second DB failed", fa2); 307 Assert.assertEquals(fa.toString(), fa2.toString()); 308 farAwayPB.close(); 309 310 defaultPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 311 Identity oid2 = defaultPB.serviceIdentity().buildIdentity(Article.class, article.getArticleId()); 312 Article article2 = (Article) defaultPB.getObjectByIdentity(oid2); 313 Assert.assertNotNull("Lookup for article in default DB failed", article2); 314 defaultPB.close(); 315 316 farAwayPB = PersistenceBrokerFactory.createPersistenceBroker(secKey); 317 farAwayPB.beginTransaction(); 318 farAwayPB.delete(fa); 319 farAwayPB.commitTransaction(); 320 farAwayPB.close(); 321 322 defaultPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 323 defaultPB.beginTransaction(); 324 defaultPB.delete(article); 325 defaultPB.commitTransaction(); 326 defaultPB.close(); 327 } 328 329 332 public void testWithReference() throws Exception 333 { 334 PBKey secKey = TestHelper.FAR_AWAY_KEY; 335 FarAwayClass f1 = doReferenceMatchingStore(secKey); 336 FarAwayClass f2 = doReferenceMatchingStore(secKey); 337 338 PersistenceBroker broker = null; 339 try 340 { 341 broker = PersistenceBrokerFactory.createPersistenceBroker(secKey); 342 broker.clearCache(); 343 Identity oid = new Identity(f1,broker); 344 FarAwayClass fac = (FarAwayClass) broker.getObjectByIdentity(oid); 345 FarAwayReferenceIF ref = fac.getReference(); 346 assertNotNull(ref); 347 assertEquals(REFERENCE_NAME, ref.getName()); 348 } 349 finally 350 { 351 if(broker != null) broker.close(); 352 } 353 354 doReferenceMatchingDelete(secKey, f1); 355 doReferenceMatchingDelete(secKey, f2); 356 } 357 358 private FarAwayClass doReferenceMatchingStore(PBKey key) throws Exception 359 { 360 FarAwayClass fa = createFarAwayObject(); 361 FarAwayReferenceIF ref = new FarAwayReference(); 362 ref.setName(REFERENCE_NAME); 363 364 PersistenceBroker farAwayPB = PersistenceBrokerFactory.createPersistenceBroker(key); 365 farAwayPB.beginTransaction(); 366 farAwayPB.store(ref); 367 fa.setReference(ref); 368 farAwayPB.store(fa); 369 farAwayPB.commitTransaction(); 370 farAwayPB.close(); 371 372 farAwayPB = PersistenceBrokerFactory.createPersistenceBroker(key); 373 Criteria criteria = new Criteria(); 374 criteria.addEqualTo("id", new Integer (fa.getId())); 375 FarAwayClass result = (FarAwayClass)farAwayPB.getObjectByQuery( 376 new QueryByCriteria(FarAwayClass.class, criteria)); 377 farAwayPB.close(); 378 379 int refId = result.getReference().getId(); 380 assertEquals(ref.getId(), refId); 381 return result; 382 } 383 384 private void doReferenceMatchingDelete(PBKey key, FarAwayClass farAwayClass) throws Exception 385 { 386 Integer refId = farAwayClass.getReferenceId(); 387 Integer mainId = new Integer (farAwayClass.getId()); 388 PersistenceBroker broker = PersistenceBrokerFactory.createPersistenceBroker(key); 389 try 390 { 391 Criteria criteria = new Criteria(); 392 criteria.addEqualTo("id", mainId); 393 FarAwayClass result = (FarAwayClass) broker.getObjectByQuery( 394 new QueryByCriteria(FarAwayClass.class, criteria)); 395 assertNotNull("Object not found", result); 396 397 Criteria criteriaRef = new Criteria(); 398 criteriaRef.addEqualTo("id", refId); 399 FarAwayReferenceIF resultRef = (FarAwayReferenceIF) broker.getObjectByQuery( 400 new QueryByCriteria(FarAwayReference.class, criteriaRef)); 401 assertNotNull("Object not found", result); 402 403 broker.beginTransaction(); 404 broker.delete(farAwayClass); 405 broker.commitTransaction(); 406 407 criteria = new Criteria(); 408 criteria.addEqualTo("id", mainId); 409 result = (FarAwayClass) broker.getObjectByQuery( 410 new QueryByCriteria(FarAwayClass.class, criteria)); 411 assertNull("Object was not deleted", result); 412 413 criteriaRef = new Criteria(); 414 criteriaRef.addEqualTo("id", refId); 415 resultRef = (FarAwayReferenceIF) broker.getObjectByQuery( 416 new QueryByCriteria(FarAwayReference.class, criteriaRef)); 417 assertNull("Reference object was not deleted", resultRef); 418 } 419 finally 420 { 421 if(broker != null) broker.close(); 422 } 423 } 424 425 426 431 private Article createArticleWithId(int id) 432 { 433 Article ret = createArticle(); 434 ret.setArticleId(new Integer (id)); 435 return ret; 436 } 437 438 private FarAwayClass createFarAwayObjectWithId(int id) 439 { 440 FarAwayClass fa = createFarAwayObject(); 441 fa.setId(id); 442 return fa; 443 } 444 445 private FarAwayClass createFarAwayObject() 446 { 447 FarAwayClass fa = new FarAwayClass(); 448 fa.setName("away from " + counter); 449 fa.setDescription("so far away from " + counter); 450 return fa; 451 } 452 453 private static int counter; 454 455 459 private Article createArticle() 460 { 461 Article a = new Article(); 462 a.setArticleName("New Performance Article " + (++counter)); 463 a.setMinimumStock(100); 464 a.setOrderedUnits(17); 465 a.setPrice(0.45); 466 a.setStock(234); 467 a.setSupplierId(4); 468 a.setUnit("bottle"); 469 return a; 470 } 471 472 public static class MultipleObject 476 { 477 Integer id; 478 String name; 479 Collection references; 480 481 public MultipleObject() 482 { 483 } 484 485 public Integer getId() 486 { 487 return id; 488 } 489 490 public void setId(Integer id) 491 { 492 this.id = id; 493 } 494 495 public String getName() 496 { 497 return name; 498 } 499 500 public void setName(String name) 501 { 502 this.name = name; 503 } 504 505 public Collection getReferences() 506 { 507 return references; 508 } 509 510 public void setReferences(Collection references) 511 { 512 this.references = references; 513 } 514 515 public String toString() 516 { 517 return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 518 } 519 } 520 521 public static class MultipleObjectRef 522 { 523 Integer id; 524 Integer refId; 525 String name; 526 527 public MultipleObjectRef() 528 { 529 } 530 531 public Integer getId() 532 { 533 return id; 534 } 535 536 public void setId(Integer id) 537 { 538 this.id = id; 539 } 540 541 public Integer getRefId() 542 { 543 return refId; 544 } 545 546 public void setRefId(Integer refId) 547 { 548 this.refId = refId; 549 } 550 551 public String getName() 552 { 553 return name; 554 } 555 556 public void setName(String name) 557 { 558 this.name = name; 559 } 560 561 public String toString() 562 { 563 return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE); 564 } 565 } 566 } 567 | Popular Tags |