1 25 26 package org.objectweb.jonas.jtests.clients.entity; 27 28 import javax.ejb.ObjectNotFoundException ; 29 import javax.naming.NamingException ; 30 import javax.rmi.PortableRemoteObject ; 31 32 import junit.framework.Test; 33 import junit.framework.TestSuite; 34 35 import org.objectweb.jonas.jtests.beans.relation.oou.AHomeRemote; 36 import org.objectweb.jonas.jtests.beans.relation.oou.ARemote; 37 import org.objectweb.jonas.jtests.beans.relation.oou.BHomeRemote; 38 import org.objectweb.jonas.jtests.beans.relation.oou.BRemote; 39 40 43 public class F_Relation_oouEC2 extends A_Cmp2Util { 44 45 private static String BEAN_HOME_A = "relation_oou_AHome"; 46 private static String BEAN_HOME_B = "relation_oou_BHome"; 47 protected static AHomeRemote ahome = null; 48 protected static BHomeRemote bhome = null; 49 50 public F_Relation_oouEC2(String name) { 51 super(name); 52 } 53 54 protected static boolean isInit = false; 55 56 protected void setUp() { 57 super.setUp(); 58 boolean ok = false; 59 int nbtry = 0; 60 while (!ok && nbtry < 3) { 61 if (!isInit) { 62 useBeans("oou", false); 64 try { 66 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A), 67 AHomeRemote.class); 68 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B), 69 BHomeRemote.class); 70 } catch (NamingException e) { 71 fail("Cannot get bean home: " + e.getMessage()); 72 } 73 try { 75 ahome.findByPrimaryKey("a2"); 76 } catch (Exception e) { 77 try { 79 utx.begin(); 80 ARemote a1 = ahome.create("a1"); 81 ARemote a2 = ahome.create("a2"); 82 ahome.create("a3"); 83 bhome.create("b1"); 84 bhome.create("b2"); 85 bhome.create("b3"); 86 a1.assignB("b1"); 87 a2.assignB("b2"); 88 } catch (Exception i) { 89 fail("InitialState creation problem: "+i); 90 } finally { 91 try { 92 utx.commit(); 93 } catch (Exception ii) { 94 } 95 } 96 } 97 isInit = true; 98 } 99 nbtry++; 102 try { 103 if (initStateOK()) { 104 ok = true; 105 } 106 } catch (Exception e) { 107 } 108 if (!ok) { 109 isInit = false; 110 unloadBeans("oou"); 111 } 112 } 113 } 114 115 116 120 boolean initStateOK() throws Exception { 121 boolean isOk = true; 122 123 msgerror = new StringBuffer (); 124 125 ARemote a1 = ahome.findByPrimaryKey("a1"); 126 BRemote b1 = bhome.findByPrimaryKey("b1"); 127 ARemote a2 = ahome.findByPrimaryKey("a2"); 128 BRemote b2 = bhome.findByPrimaryKey("b2"); 129 ARemote a3 = ahome.findByPrimaryKey("a3"); 130 BRemote b3 = bhome.findByPrimaryKey("b3"); 131 String idB1 = a1.retrieveB(); 132 String idB2 = a2.retrieveB(); 133 String idB3 = a3.retrieveB(); 134 135 if (idB1!=null && !idB1.equals("b1")){ 136 isOk = false; 137 msgerror.append("Wrong relation for a1->b1" 138 +"(expected: A1.retrieveB()='b1'" 139 + ", found:" + idB1 + ")"); 140 } 141 else if (idB2!=null && !idB2.equals("b2")){ 142 isOk = false; 143 msgerror.append("Wrong relation for a2->b2" 144 +"(expected: A2.retrieveB()='b2'" 145 + ", found:" + idB2 + ")"); 146 } 147 else if (idB3!=null){ 148 isOk = false; 149 msgerror.append("Wrong relation for b3->a3" 150 +"(expected: B3.retrieveA()=null" 151 + ", found:" + idB3 + ")"); 152 } 153 return isOk; 154 } 155 156 157 160 public void _testBasicGetEmpty(int tx) throws Exception { 161 String idB = null; 162 if ((tx == TX_CALL) || (tx == TX_RB)) { 163 utx.begin(); 164 } 165 ARemote a = ahome.findByPrimaryKey("a3"); 166 if (tx == TX_CONT) { 167 idB = a.retrieveBInNewTx(); 168 } else { 169 idB = a.retrieveB(); 170 } 171 if (tx == TX_CALL) { 172 utx.commit(); 173 } else if (tx == TX_RB) { 174 utx.rollback(); 175 } 176 177 checkIsInitialState(); 178 } 179 public void testBasicGetEmptyTxNo() throws Exception { 180 _testBasicGetEmpty(TX_NO); 181 } 182 public void testBasicGetEmptyTxCall() throws Exception { 183 _testBasicGetEmpty(TX_CALL); 184 } 185 public void testBasicGetEmptyTxCont() throws Exception { 186 _testBasicGetEmpty(TX_CONT); 187 } 188 189 192 public void _testBasicSetEmpty(int tx) throws Exception { 193 if ((tx == TX_CALL) || (tx == TX_RB)) { 194 utx.begin(); 195 } 196 ARemote a = ahome.findByPrimaryKey("a3"); 197 if (tx == TX_CONT) { 198 a.assignBInNewTx("b3"); 199 } else { 200 a.assignB("b3"); 201 } 202 if (tx == TX_CALL) { 203 utx.commit(); 204 } else if (tx == TX_RB) { 205 utx.rollback(); 206 } 207 String idB = a.retrieveB(); 209 if (tx != TX_RB) { 210 assertEquals("Wrong assign null for relation a3->b3 : ","b3", idB); 211 } else { 212 assertNull("Wrong assign null for relation a3->b3 : ", idB); 213 } 214 if (tx != TX_RB) { 216 a.assignB(null); 217 } 218 checkIsInitialState(); 219 } 220 221 public void testBasicSetEmptyTxNo() throws Exception { 222 _testBasicSetEmpty(TX_NO); 223 } 224 public void testBasicSetEmptyTxCall() throws Exception { 225 _testBasicSetEmpty(TX_CALL); 226 } 227 public void testBasicSetEmptyTxCont() throws Exception { 228 _testBasicSetEmpty(TX_CONT); 229 } 230 public void testBasicSetEmptyTxRb() throws Exception { 231 _testBasicSetEmpty(TX_RB); 232 } 233 234 237 public void _testBasicSetEmptyNull(int tx) throws Exception { 238 if ((tx == TX_CALL) || (tx == TX_RB)) { 239 utx.begin(); 240 } 241 ARemote a = ahome.findByPrimaryKey("a1"); 242 if (tx == TX_CONT) { 243 a.assignBInNewTx(null); 244 } else { 245 a.assignB(null); 246 } 247 if (tx == TX_CALL) { 248 utx.commit(); 249 } else if (tx == TX_RB) { 250 utx.rollback(); 251 } 252 String idB = a.retrieveB(); 254 if (tx == TX_RB) { 255 assertEquals("Wrong assign null for relation a1->b1: ","b1", idB); 256 } else { 257 assertNull("Wrong assign null for relation a1->b1: " + idB, idB); 258 } 259 if (tx != TX_RB) { 261 a.assignB("b1"); 262 } 263 264 checkIsInitialState(); 265 266 } 267 268 public void testBasicSetEmptyNullTxNo() throws Exception { 269 _testBasicSetEmptyNull(TX_NO); 270 } 271 public void testBasicSetEmptyNullTxCall() throws Exception { 272 _testBasicSetEmptyNull(TX_CALL); 273 } 274 public void testBasicSetEmptyNullTxCont() throws Exception { 275 _testBasicSetEmptyNull(TX_CONT); 276 } 277 public void testBasicSetEmptyNullTxRb() throws Exception { 278 _testBasicSetEmptyNull(TX_RB); 279 } 280 281 285 public void _testCohSetOne(int tx) throws Exception { 286 287 if ((tx == TX_CALL) || (tx == TX_RB)) { 288 utx.begin(); 289 } 290 ARemote a1 = ahome.findByPrimaryKey("a1"); 291 ARemote a2 = ahome.findByPrimaryKey("a2"); 292 if (tx == TX_CONT) { 294 a1.assignBInNewTx("b2"); 295 } else { 296 a1.assignB("b2"); 297 } 298 if (tx == TX_CALL) { 299 utx.commit(); 300 } else if (tx == TX_RB) { 301 utx.rollback(); 302 } 303 if (tx != TX_RB) { 304 assertNull("Bad coherence of relation null : expected for a2.retreiveB() found :" + a2.retrieveB(), a2.retrieveB()); 306 assertEquals("Bad coherence of relation b2 : expected for a1.retreiveB() found :" + a1.retrieveB(), "b2", a1.retrieveB()); 307 308 a2.assignB("b2"); 310 a1.assignB("b1"); 311 } 312 checkIsInitialState(); 313 } 314 315 public void testCohSetOneTxNo() throws Exception { 316 _testCohSetOne(TX_NO); 317 } 318 public void testCohSetOneTxCall() throws Exception { 319 _testCohSetOne(TX_CALL); 320 } 321 322 public void testCohSetOneTxCont() throws Exception { 323 _testCohSetOne(TX_CONT); 324 } 325 326 public void testCohSetOneTxRb() throws Exception { 327 _testCohSetOne(TX_RB); 328 } 329 330 334 public void _testCohWithoutRelation(int tx) throws Exception { 335 336 if ((tx == TX_CALL) || (tx == TX_RB)) { 337 utx.begin(); 338 } 339 ARemote a3 = ahome.findByPrimaryKey("a3"); 340 if (tx == TX_CONT) { 342 a3.assignBInNewTx("b3"); 343 } else { 344 a3.assignB("b3"); 345 } 346 if (tx == TX_CALL) { 347 utx.commit(); 348 } else if (tx == TX_RB) { 349 utx.rollback(); 350 } 351 if (tx != TX_RB) { 352 assertEquals("Bad coherence of relation : b3 expected for a3.retreiveB() found :"+a3.retrieveB(),"b3", a3.retrieveB()); 354 355 a3.assignB(null); 357 } 358 checkIsInitialState(); 359 } 360 361 public void testBasicCohWithoutRelation() throws Exception { 362 _testCohWithoutRelation(TX_NO); 363 } 364 public void testBasicCohWithoutRelationTxCall() throws Exception { 365 _testCohWithoutRelation(TX_CALL); 366 } 367 368 372 public void _testCohAlreadyAssign(int tx) throws Exception { 373 374 if ((tx == TX_CALL) || (tx == TX_RB)) { 375 utx.begin(); 376 } 377 ARemote a1 = ahome.findByPrimaryKey("a1"); 378 if (tx == TX_CONT) { 380 a1.assignBInNewTx("b3"); 381 } else { 382 a1.assignB("b3"); 383 } 384 if (tx == TX_CALL) { 385 utx.commit(); 386 } else if (tx == TX_RB) { 387 utx.rollback(); 388 } 389 if (tx != TX_RB) { 390 assertEquals("Bad coherence of relation : b3 expected for a1.retreiveB() found :"+a1.retrieveB(),"b3", a1.retrieveB()); 392 393 a1.assignB("b1"); 395 } 396 checkIsInitialState(); 397 } 398 399 public void testCohAlreadyAssign() throws Exception { 400 _testCohAlreadyAssign(TX_NO); 401 } 402 public void testCohAlreadyAssignTxCall() throws Exception { 403 _testCohAlreadyAssign(TX_CALL); 404 } 405 406 410 public void _testCohSetNull(int tx) throws Exception { 411 412 if ((tx == TX_CALL) || (tx == TX_RB)) { 413 utx.begin(); 414 } 415 ARemote a1 = ahome.findByPrimaryKey("a1"); 416 if (tx == TX_CONT) { 418 a1.assignBInNewTx(null); 419 } else { 420 a1.assignB(null); 421 } 422 if (tx == TX_CALL) { 423 utx.commit(); 424 } else if (tx == TX_RB) { 425 utx.rollback(); 426 } 427 if (tx != TX_RB) { 428 assertNull("Bad coherence of relation : null expected for a1.retreiveB() found :"+a1.retrieveB(), a1.retrieveB()); 430 431 a1.assignB("b1"); 433 } 434 checkIsInitialState(); 435 } 436 437 public void testCohSetNull() throws Exception { 438 _testCohSetNull(TX_NO); 439 } 440 public void testCohSetNullTxCall() throws Exception { 441 _testCohSetNull(TX_CALL); 442 } 443 444 445 449 public void _testCohRemove(int tx) throws Exception { 450 if (tx == TX_CONT) { 451 fail("Transaction cannot be initiate by the container for this test"); 454 } 455 if ((tx == TX_CALL) || (tx == TX_RB)) { 456 utx.begin(); 457 } 458 ARemote a1 = ahome.findByPrimaryKey("a1"); 459 try { 460 bhome.remove("b1"); 462 } finally { 463 if (tx == TX_CALL) { 464 utx.commit(); 465 } else if (tx == TX_RB) { 466 utx.rollback(); 467 } 468 } 469 if (tx != TX_RB) { 470 assertNull("Bad coherence of relation : null expected for a1.retreiveB() found :"+a1.retrieveB(), a1.retrieveB()); 472 try { 473 BRemote b1 = bhome.findByPrimaryKey("b1"); 474 fail("BA is not removed"); 475 } catch (ObjectNotFoundException e) { 476 } 477 478 bhome.create("b1"); 480 a1.assignB("b1"); 481 } 482 checkIsInitialState(); 483 } 484 485 public void testCohRemoveNoTx() throws Exception { 486 _testCohRemove(TX_NO); 487 } 488 public void testCohRemoveTxCall() throws Exception { 489 _testCohRemove(TX_CALL); 490 } 491 public void testCohRemoveTxRb() throws Exception { 492 _testCohRemove(TX_RB); 493 } 494 495 public void testRemoveCreate() throws Exception { 496 ARemote a1 = ahome.findByPrimaryKey("a1"); 497 bhome.remove("b1"); 498 BRemote b1 = null; 499 try { 500 b1 = bhome.findByPrimaryKey("b1"); 501 fail("BA is not removed"); 502 } catch (ObjectNotFoundException e) { 503 } 504 505 bhome.create("b1"); 507 508 utx.begin(); 509 try { 510 bhome.remove("b1"); 512 } finally { 513 utx.commit(); 514 } 515 try { 516 b1 = bhome.findByPrimaryKey("b1"); 517 fail("BA is not removed"); 518 } catch (ObjectNotFoundException e) { 519 } 520 521 b1 = bhome.create("b1"); 523 a1.assignB("b1"); 524 525 BRemote b2 = bhome.findByPrimaryKey("b2"); 528 try { 529 String idB1 = a1.retrieveB(); 530 } catch (Exception e) { 531 fail("a1.retrieveB():" + e); 532 } 533 534 } 535 536 537 543 public void _testCohBeanRemove(int tx) throws Exception { 544 if (tx == TX_CONT) { 545 fail("Transaction cannot be initiate by the container for this test"); 548 } 549 550 if ((tx == TX_CALL) || (tx == TX_RB)) { 551 utx.begin(); 552 } 553 ARemote a1 = ahome.findByPrimaryKey("a1"); 554 BRemote b1 = bhome.findByPrimaryKey("b1"); 555 try { 556 b1.remove(); 558 } finally { 559 if (tx == TX_CALL) { 560 utx.commit(); 561 } else if (tx == TX_RB) { 562 utx.rollback(); 563 } 564 } 565 if (tx != TX_RB) { 566 assertNull("Bad coherence of relation : null expected for a1.retreiveB() found :"+a1.retrieveB(), a1.retrieveB()); 568 boolean not_found=false; 569 try { 570 b1 = bhome.findByPrimaryKey("b1"); 571 } catch (ObjectNotFoundException e) { 572 not_found=true; 573 } 574 assertTrue("BA is not removed",not_found); 575 bhome.create("b1"); 577 a1.assignB("b1"); 578 } 579 checkIsInitialState(); 580 } 581 582 public void testCohBeanRemoveNoTx() throws Exception { 583 _testCohBeanRemove(TX_NO); 584 } 585 public void testCohBeanRemoveTxCall() throws Exception { 586 _testCohBeanRemove(TX_CALL); 587 } 588 public void testCohBeanRemoveTxRb() throws Exception { 589 _testCohBeanRemove(TX_RB); 590 } 591 592 public static Test suite() { 593 return new TestSuite(F_Relation_oouEC2.class); 594 } 595 596 public static void main (String args[]) { 597 String testtorun = null; 598 for (int argn = 0; argn < args.length; argn++) { 600 String s_arg = args[argn]; 601 Integer i_arg; 602 if (s_arg.equals("-n")) { 603 testtorun = args[++argn]; 604 } 605 } 606 if (testtorun == null) { 607 junit.textui.TestRunner.run(suite()); 608 } else { 609 junit.textui.TestRunner.run(new F_Relation_oouEC2(testtorun)); 610 } 611 } 612 } 613 614 | Popular Tags |