1 25 26 package org.objectweb.jonas.jtests.clients.entity; 27 28 import java.util.Enumeration ; 29 import java.util.Hashtable ; 30 31 import javax.naming.NamingException ; 32 import javax.rmi.PortableRemoteObject ; 33 34 import junit.framework.Test; 35 import junit.framework.TestSuite; 36 37 import org.objectweb.jonas.jtests.beans.relation.mou.AHomeRemote; 38 import org.objectweb.jonas.jtests.beans.relation.mou.ARemote; 39 import org.objectweb.jonas.jtests.beans.relation.mou.BHomeRemote; 40 import org.objectweb.jonas.jtests.beans.relation.mou.BRemote; 41 42 49 public class F_Relation_mouEC2 extends A_Cmp2Util { 50 51 52 private static String BEAN_HOME_A = "relation_mou_AHome"; 53 protected static AHomeRemote ahome = null; 54 private static String BEAN_HOME_B = "relation_mou_BHome"; 55 protected static BHomeRemote bhome = null; 56 57 static Hashtable tbRelationA2B = new Hashtable (); 58 59 static { 60 tbRelationA2B.put("a20", "b2"); 61 tbRelationA2B.put("a21", "b2"); 62 tbRelationA2B.put("a22", "b2"); 63 tbRelationA2B.put("a0", "b0"); 64 tbRelationA2B.put("a1", "b0"); 65 tbRelationA2B.put("a2", "b0"); 66 tbRelationA2B.put("a3", ""); 67 tbRelationA2B.put("a4", "b4"); 68 tbRelationA2B.put("a5", "b4"); 69 } 70 71 public F_Relation_mouEC2(String name) { 72 super(name); 73 } 74 75 protected static boolean isInit = false; 76 77 protected void setUp() { 78 super.setUp(); 79 boolean ok = false; 80 int nbtry = 0; 81 while (!ok && nbtry < 3) { 82 if (!isInit) { 83 useBeans("mou", false); 85 try { 87 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A), 88 AHomeRemote.class); 89 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B), 90 BHomeRemote.class); 91 } catch (NamingException e) { 92 fail("Cannot get bean home: " + e.getMessage()); 93 } 94 try { 96 ahome.findByPrimaryKey("a0"); 97 } catch (Exception e) { 98 try { 100 utx.begin(); 101 ARemote a0 = ahome.create("a0"); 102 ARemote a1 = ahome.create("a1"); 103 ARemote a2 = ahome.create("a2"); 104 ARemote a3 = ahome.create("a3"); 105 ARemote a4 = ahome.create("a4"); 106 ARemote a5 = ahome.create("a5"); 107 ARemote a20 = ahome.create("a20"); 108 ARemote a21 = ahome.create("a21"); 109 ARemote a22 = ahome.create("a22"); 110 bhome.create("b0"); 111 bhome.create("b1"); 112 bhome.create("b2"); 113 bhome.create("b4"); 114 a0.assignB((String ) tbRelationA2B.get("a0")); 116 a1.assignB((String ) tbRelationA2B.get("a1")); 117 a2.assignB((String ) tbRelationA2B.get("a2")); 118 a4.assignB((String ) tbRelationA2B.get("a4")); 119 a5.assignB((String ) tbRelationA2B.get("a5")); 120 a20.assignB((String ) tbRelationA2B.get("a20")); 121 a21.assignB((String ) tbRelationA2B.get("a21")); 122 a22.assignB((String ) tbRelationA2B.get("a22")); 123 } catch (Exception e2) { 124 fail("InitialState creation problem: " + e2); 125 } finally { 126 try { 127 utx.commit(); 128 } catch (Exception e3) { 129 } 130 } 131 } 132 isInit = true; 133 } 134 nbtry++; 137 try { 138 if (initStateOK()) { 139 ok = true; 140 } 141 } catch (Exception e) { 142 } 143 if (!ok) { 144 isInit = false; 145 unloadBeans("mou"); 146 } 147 } 148 } 149 150 151 155 boolean initStateOK() throws Exception { 156 boolean isOk = true; 157 msgerror = new StringBuffer (); 158 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 159 String aname = (String ) (ea.nextElement()); 160 ARemote a = ahome.findByPrimaryKey(aname); 161 String BActual = a.retrieveB(); 162 if (BActual == null) BActual = ""; 164 String BExpected = (String ) (tbRelationA2B.get(aname)); 165 if (!BActual.equals(BExpected)) { 166 isOk = false; 167 msgerror = msgerror.append("Wrong relation for " + aname 168 + " (expected:" + BExpected 169 + ", found:" + BActual + ")"); 170 } 171 } 172 return isOk; 173 } 174 175 176 177 180 public void _testBasicGetEmpty(int tx) throws Exception { 181 String c = null; 182 if ((tx == TX_CALL) || (tx == TX_RB)) { 183 utx.begin(); 184 } 185 ARemote a = ahome.findByPrimaryKey("a3"); 186 if (tx == TX_CONT) { 187 c = a.retrieveBInNewTx(); 188 } else { 189 c = a.retrieveB(); 190 } 191 if (tx == TX_CALL) { 192 utx.commit(); 193 } else if (tx == TX_RB) { 194 utx.rollback(); 195 } 196 checkIsInitialState(); 197 } 198 199 public void testBasicGetEmptyTxNo() throws Exception { 200 _testBasicGetEmpty(TX_NO); 201 } 202 203 public void testBasicGetEmptyTxCall() throws Exception { 204 _testBasicGetEmpty(TX_CALL); 205 } 206 207 public void testBasicGetEmptyTxCont() throws Exception { 208 _testBasicGetEmpty(TX_CONT); 209 } 210 211 public void testBasicGetEmptyTxRb() throws Exception { 212 _testBasicGetEmpty(TX_RB); 213 } 214 215 218 public void _testBasicSetEmpty(int tx) throws Exception { 219 String c = null; 220 if ((tx == TX_CALL) || (tx == TX_RB)) { 221 utx.begin(); 222 } 223 ARemote a = ahome.findByPrimaryKey("a3"); 224 if (tx == TX_CONT) { 225 a.assignBInNewTx("b1"); 226 } else { 227 a.assignB("b1"); 228 } 229 if (tx == TX_CALL) { 230 utx.commit(); 231 } else if (tx == TX_RB) { 232 utx.rollback(); 233 } 234 String idB = a.retrieveB(); 236 if (tx != TX_RB) { 237 assertEquals("Wrong new relation a3->b1 : ", "b1", idB); 238 } else { 239 assertNull("Wrong new relation a3->b1 : ", idB); 240 } 241 if (tx != TX_RB) { 243 a.assignB(null); 244 } 245 checkIsInitialState(); 246 } 247 248 public void testBasicSetEmptyTxNo() throws Exception { 249 _testBasicSetEmpty(TX_NO); 250 } 251 252 public void testBasicSetEmptyTxCall() throws Exception { 253 _testBasicSetEmpty(TX_CALL); 254 } 255 256 public void testBasicSetEmptyTxCont() throws Exception { 257 _testBasicSetEmpty(TX_CONT); 258 } 259 260 public void testBasicSetEmptyTxRb() throws Exception { 261 _testBasicSetEmpty(TX_RB); 262 } 263 264 267 public void _testBasicGet(int tx) throws Exception { 268 String b = null; 269 if ((tx == TX_CALL) || (tx == TX_RB)) { 270 utx.begin(); 271 } 272 ARemote a1 = ahome.findByPrimaryKey("a1"); 273 274 if (tx == TX_CONT) { 275 b = a1.retrieveBInNewTx(); 276 } else { 277 b = a1.retrieveB(); 278 } 279 if (tx == TX_CALL) { 280 utx.commit(); 281 } else if (tx == TX_RB) { 282 utx.rollback(); 283 } 284 if (tx != TX_RB) { 286 assertEquals("Wrong relation a1->b0 : ", "b0", b); 287 } 288 checkIsInitialState(); 289 } 290 291 public void testBasicGetTxNo() throws Exception { 292 _testBasicGet(TX_NO); 293 } 294 295 public void testBasicGetTxCall() throws Exception { 296 _testBasicGet(TX_CALL); 297 } 298 299 public void testBasicGetTxCont() throws Exception { 300 _testBasicGet(TX_CONT); 301 } 302 303 public void testBasicGetTxRb() throws Exception { 304 _testBasicGet(TX_RB); 305 } 306 307 310 public void _testBasicSetNull(int tx) throws Exception { 311 if ((tx == TX_CALL) || (tx == TX_RB)) { 312 utx.begin(); 313 } 314 ARemote a1 = ahome.findByPrimaryKey("a1"); 315 316 if (tx == TX_CONT) { 317 a1.assignBInNewTx(null); 318 } else { 319 a1.assignB(null); 320 } 321 if (tx == TX_CALL) { 322 utx.commit(); 323 } else if (tx == TX_RB) { 324 utx.rollback(); 325 } 326 if (tx != TX_RB) { 328 String b = a1.retrieveB(); 329 assertNull("Wrong relation a1 : ", b); 330 a1.assignB("b0"); 332 } 333 checkIsInitialState(); 334 } 335 336 public void testBasicSetNullTxNo() throws Exception { 337 _testBasicSetNull(TX_NO); 338 } 339 340 public void testBasicSetNullTxCall() throws Exception { 341 _testBasicSetNull(TX_CALL); 342 } 343 344 public void testBasicSetNullTxCont() throws Exception { 345 _testBasicSetNull(TX_CONT); 346 } 347 348 public void testBasicSetNullTxRb() throws Exception { 349 _testBasicSetNull(TX_RB); 350 } 351 352 355 public void _testBasicSet(int tx) throws Exception { 356 String c = null; 357 if ((tx == TX_CALL) || (tx == TX_RB)) { 358 utx.begin(); 359 } 360 ARemote a1 = ahome.findByPrimaryKey("a1"); 361 ARemote a21 = ahome.findByPrimaryKey("a21"); 362 if (tx == TX_CONT) { 363 a1.assignBInNewTx(a21.retrieveB()); 364 } else { 365 a1.assignB(a21.retrieveB()); 366 } 367 if (tx == TX_CALL) { 368 utx.commit(); 369 } else if (tx == TX_RB) { 370 utx.rollback(); 371 } 372 if (tx != TX_RB) { 373 String idB1 = a1.retrieveB(); 375 ARemote a0 = ahome.findByPrimaryKey("a0"); 376 ARemote a2 = ahome.findByPrimaryKey("a2"); 377 ARemote a3 = ahome.findByPrimaryKey("a3"); 378 ARemote a20 = ahome.findByPrimaryKey("a20"); 379 ARemote a22 = ahome.findByPrimaryKey("a22"); 380 String idB0 = a0.retrieveB(); 381 String idB2 = a2.retrieveB(); 382 String idB3 = a3.retrieveB(); 383 String idB20 = a20.retrieveB(); 384 String idB21 = a21.retrieveB(); 385 String idB22 = a22.retrieveB(); 386 assertEquals("Wrong new relation a1->b2 : ", "b2", idB1); 387 assertEquals("Wrong new relation a0->b0 : ", "b0", idB0); 388 assertEquals("Wrong new relation a2->b0 : ", "b0", idB2); 389 assertEquals("Wrong new relation a20->b2 : ", "b2", idB20); 390 assertEquals("Wrong new relation a21->b2 : ", "b2", idB21); 391 assertEquals("Wrong new relation a22->b2 : ", "b2", idB22); 392 a1.assignB("b0"); 394 } 395 checkIsInitialState(); 396 } 397 398 public void testBasicSetTxNo() throws Exception { 399 _testBasicSet(TX_NO); 400 } 401 402 public void testBasicSetTxCall() throws Exception { 403 _testBasicSet(TX_CALL); 404 } 405 406 public void testBasicSetTxCont() throws Exception { 407 _testBasicSet(TX_CONT); 408 } 409 410 public void testBasicSetTxRb() throws Exception { 411 _testBasicSet(TX_RB); 412 } 413 414 417 public void _testCohRemoveA(int tx) throws Exception { 418 if (tx == TX_CONT) { 419 fail("Transaction cannot be initiate by the container for this test"); 422 } 423 if ((tx == TX_CALL) || (tx == TX_RB)) { 424 utx.begin(); 425 } 426 ahome.remove("a0"); 427 if (tx == TX_CALL) { 428 utx.commit(); 429 } else if (tx == TX_RB) { 430 utx.rollback(); 431 } 432 if (tx != TX_RB) { 434 ARemote a0 = ahome.create("a0"); 435 a0.assignBInNewTx("b0"); 436 } 437 checkIsInitialState(); 438 } 439 440 public void testCohRemoveATxNo() throws Exception { 441 _testCohRemoveA(TX_NO); 442 } 443 444 public void testCohRemoveATxCall() throws Exception { 445 _testCohRemoveA(TX_CALL); 446 } 447 448 public void testCohRemoveATxRb() throws Exception { 449 _testCohRemoveA(TX_RB); 450 } 451 452 455 public void _testCohRemoveB(int tx) throws Exception { 456 if (tx == TX_CONT) { 457 fail("Transaction cannot be initiate by the container for this test"); 460 } 461 if ((tx == TX_CALL) || (tx == TX_RB)) { 462 utx.begin(); 463 } 464 bhome.remove("b0"); 465 if (tx == TX_CALL) { 466 utx.commit(); 467 } else if (tx == TX_RB) { 468 utx.rollback(); 469 } 470 if (tx != TX_RB) { 472 ARemote a0 = ahome.findByPrimaryKey("a0"); 473 ARemote a1 = ahome.findByPrimaryKey("a1"); 474 ARemote a2 = ahome.findByPrimaryKey("a2"); 475 String idrela0 = a0.retrieveB(); 476 String idrela1 = a1.retrieveB(); 477 String idrela2 = a2.retrieveB(); 478 479 assertNull("Wrong not null relation a0, found: " + idrela0, idrela0); 480 assertNull("Wrong not null relation a1, found: " + idrela1, idrela1); 481 assertNull("Wrong not null relation a2, found: " + idrela2, idrela2); 482 bhome.create("b0"); 484 a0.assignBInNewTx("b0"); 485 a1.assignBInNewTx("b0"); 486 a2.assignBInNewTx("b0"); 487 } 488 checkIsInitialState(); 489 } 490 491 public void testCohRemoveBTxNo() throws Exception { 492 _testCohRemoveB(TX_NO); 493 } 494 495 public void testCohRemoveBTxCall() throws Exception { 496 _testCohRemoveB(TX_CALL); 497 } 498 499 public void testCohRemoveBTxRb() throws Exception { 500 _testCohRemoveB(TX_RB); 501 } 502 503 508 public void _testCohBeanRemoveB(int tx) throws Exception { 509 if (tx == TX_CONT) { 510 fail("Transaction cannot be initiate by the container for this test"); 513 } 514 if ((tx == TX_CALL) || (tx == TX_RB)) { 515 utx.begin(); 516 } 517 BRemote b = bhome.findByPrimaryKey("b0"); 518 b.remove(); 519 if (tx == TX_CALL) { 520 utx.commit(); 521 } else if (tx == TX_RB) { 522 utx.rollback(); 523 } 524 if (tx != TX_RB) { 526 ARemote a0 = ahome.findByPrimaryKey("a0"); 527 ARemote a1 = ahome.findByPrimaryKey("a1"); 528 ARemote a2 = ahome.findByPrimaryKey("a2"); 529 String idrela0 = a0.retrieveB(); 530 String idrela1 = a1.retrieveB(); 531 String idrela2 = a2.retrieveB(); 532 533 assertNull("Wrong not null relation a0, found: " + idrela0, idrela0); 534 assertNull("Wrong not null relation a1, found: " + idrela1, idrela1); 535 assertNull("Wrong not null relation a2, found: " + idrela2, idrela2); 536 bhome.create("b0"); 538 a0.assignBInNewTx("b0"); 539 a1.assignBInNewTx("b0"); 540 a2.assignBInNewTx("b0"); 541 } 542 checkIsInitialState(); 543 } 544 545 public void testCohBeanRemoveBTxNo() throws Exception { 546 _testCohBeanRemoveB(TX_NO); 547 } 548 549 public void testCohBeanRemoveBTxCall() throws Exception { 550 _testCohBeanRemoveB(TX_CALL); 551 } 552 553 public void testCohBeanRemoveBTxRb() throws Exception { 554 _testCohBeanRemoveB(TX_RB); 555 } 556 557 public void testCohRemove2() throws Exception { 558 _testCohRemoveB(TX_NO); 559 _testCohRemoveB(TX_CALL); 560 _testCohRemoveB(TX_NO); 561 _testCohRemoveB(TX_RB); 562 } 563 564 568 public void testInstanceReuse1() throws Exception { 569 570 ARemote a0 = ahome.findByPrimaryKey("a0"); 571 ARemote a1 = ahome.findByPrimaryKey("a1"); 572 ARemote a2 = ahome.findByPrimaryKey("a2"); 573 ARemote a21 = ahome.findByPrimaryKey("a21"); 574 575 utx.begin(); 577 a1.assignB("b1"); 578 utx.rollback(); 579 580 utx.begin(); 582 String bs = a21.retrieveB(); 583 a1.assignB(bs); 584 utx.rollback(); 585 a1.retrieveB(); 586 a21.retrieveB(); 587 588 bhome.remove("b0"); 590 bhome.create("b0"); 591 a0.assignBInNewTx("b0"); 592 593 utx.begin(); 595 bhome.remove("b0"); 596 utx.commit(); 597 BRemote b0 = bhome.create("b0"); 598 a0.assignBInNewTx("b0"); 599 600 utx.begin(); 602 bhome.remove("b0"); 603 utx.rollback(); 604 605 b0.remove(); 607 String idrela0 = a0.retrieveB(); 608 assertNull("Wrong not null relation a0, found: " + idrela0, idrela0); 609 bhome.create("b0"); 611 a0.assignBInNewTx("b0"); 612 a1.assignBInNewTx("b0"); 613 a2.assignBInNewTx("b0"); 614 checkIsInitialState(); 615 } 616 617 622 public void testInstanceReuse2() throws Exception { 623 ARemote a4 = ahome.findByPrimaryKey("a4"); 624 ARemote a5 = ahome.findByPrimaryKey("a5"); 625 626 utx.begin(); 627 a5.assignB("b2"); 628 utx.rollback(); 629 630 bhome.remove("b4"); 631 BRemote b4 = bhome.create("b4"); 632 a4.assignB("b4"); 633 634 utx.begin(); 635 bhome.remove("b4"); 636 utx.rollback(); 637 String s = a4.retrieveB(); 638 assertEquals("Broken relation", "b4", s); 639 640 b4.remove(); 642 s = a4.retrieveB(); 643 assertNull("Wrong not null relation a4, found: " + s, s); 644 bhome.create("b4"); 646 a4.assignB("b4"); 647 a5.assignB("b4"); 648 checkIsInitialState(); 649 } 650 651 657 public void testInstanceReuse3() throws Exception { 658 String id; 659 utx.begin(); 661 bhome.remove("b0"); 662 utx.rollback(); 663 bhome.remove("b0"); 665 ARemote a0 = ahome.findByPrimaryKey("a0"); 667 id = a0.retrieveB(); 668 assertNull("Wrong not null relation a0, found: " + id, id); 669 ARemote a1 = ahome.findByPrimaryKey("a1"); 670 id = a1.retrieveB(); 671 assertNull("Wrong not null relation a1, found: " + id, id); 672 ARemote a2 = ahome.findByPrimaryKey("a2"); 673 id = a2.retrieveB(); 674 assertNull("Wrong not null relation a2, found: " + id, id); 675 bhome.create("b0"); 677 a0.assignBInNewTx("b0"); 678 a1.assignBInNewTx("b0"); 679 a2.assignBInNewTx("b0"); 680 checkIsInitialState(); 681 } 682 683 public static Test suite() { 684 return new TestSuite(F_Relation_mouEC2.class); 685 } 686 687 public static void main(String args[]) { 688 String testtorun = null; 689 for (int argn = 0; argn < args.length; argn++) { 691 String s_arg = args[argn]; 692 Integer i_arg; 693 if (s_arg.equals("-n")) { 694 testtorun = args[++argn]; 695 } 696 } 697 if (testtorun == null) { 698 junit.textui.TestRunner.run(suite()); 699 } else { 700 junit.textui.TestRunner.run(new F_Relation_mouEC2(testtorun)); 701 } 702 } 703 704 } 705 | Popular Tags |