1 25 26 package org.objectweb.jonas.jtests.clients.jdbcra; 27 28 import java.util.Enumeration ; 29 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.rmi.PortableRemoteObject ; 33 import javax.transaction.UserTransaction ; 34 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 38 import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA1; 39 import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA1Home; 40 import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA2; 41 import org.objectweb.jonas.jtests.beans.jdbcra.JdbcRA2Home; 42 import org.objectweb.jonas.jtests.util.JTestCase; 43 44 47 48 public class F_JdbcRATest extends JTestCase { 49 50 private static UserTransaction utx = null; 51 52 static Context ctx = null; 53 54 56 protected static String BEAN_HOME1 = "JdbcRA1EBRHome"; 57 protected static JdbcRA1Home ra1home = null; 58 59 protected static String BEAN_HOME2 = "JdbcRA2EBRHome"; 60 protected static JdbcRA2Home ra2home = null; 61 62 Enumeration ra1list; 63 static JdbcRA1 ra1acc; 64 65 Enumeration ra2list; 66 static JdbcRA2 ra2acc; 67 68 public F_JdbcRATest(String name) { 69 super(name); 70 } 71 72 protected void setUp() { 73 super.setUp(); 74 75 77 try { 78 79 81 if (ctx == null) { 82 ctx = new InitialContext (); 83 } 84 85 87 if (utx == null) { 88 utx = (UserTransaction ) ctx.lookup("javax.transaction.UserTransaction"); 89 } 90 91 if (ra1home == null && ra2home == null) { 92 useBeans("jdbcra", true); 93 } 94 95 97 if (ra1home == null) { 98 ra1home = (JdbcRA1Home)PortableRemoteObject.narrow( 99 ctx.lookup(BEAN_HOME1), 100 JdbcRA1Home.class); 101 } 102 103 105 if (ra2home == null) { 106 ra2home = (JdbcRA2Home)PortableRemoteObject.narrow( 107 ctx.lookup(BEAN_HOME2), 108 JdbcRA2Home.class); 109 } 110 111 assertTrue (5 == 5); 112 113 } catch (Exception e) { 114 fail("Cannot lookup UserTransaction in setUp: " +e); 115 } 116 } 117 118 public void testRaCleanUpCommit() { 119 120 123 ra1acc = null; 124 ra2acc = null; 125 int dlaccno; 126 127 try { 128 129 utx.begin(); 130 131 133 135 ra1list = ra1home.findAllAccounts(); 136 137 while (ra1list.hasMoreElements()) { 138 ra1acc = (JdbcRA1) ra1list.nextElement(); 139 dlaccno = ra1acc.getNumber(); 140 141 if ((dlaccno >= 201 && dlaccno <= 205) || dlaccno == 209) { 142 ra1acc.remove(); 143 } 144 } 145 146 148 ra2list = ra2home.findAllAccounts(); 149 150 while (ra2list.hasMoreElements()) { 151 ra2acc = (JdbcRA2) ra2list.nextElement(); 152 dlaccno = ra2acc.getNumber(); 153 154 if ((dlaccno >= 201 && dlaccno <= 205) || dlaccno == 209) { 155 ra2acc.remove(); 156 } 157 } 158 159 utx.commit(); 160 161 assertTrue (5 == 5); 162 163 } catch (Exception e) { 164 try { 166 utx.rollback(); 167 } 168 catch (Exception f) { 169 assertTrue (5 == 5); 170 } 171 assertTrue (5 == 5); 172 } 173 } 174 175 public void testRaBatchInsert() { 176 177 179 ra1acc = null; 180 ra2acc = null; 181 182 String str1 = null; 183 String str2 = null; 184 185 try { 186 187 utx.begin(); 188 189 191 ra1acc = ra1home.create(); 192 193 195 ra2acc = ra2home.create(); 196 197 199 ra1acc = ra1home.findByNumber(203); 200 201 203 ra2acc = ra2home.findByNumber(203); 204 205 utx.commit(); 206 207 assertEquals (String.valueOf(ra1acc.getBalance()), 208 String.valueOf(ra2acc.getBalance())); 209 210 } catch (Exception e) { 211 212 try { 213 utx.rollback(); 214 fail("Cannot store accounts using batch" +e); 215 216 } 217 catch (Exception f) { 218 fail("Cannot rollback testRaBatchInsert: " +f); 219 } 220 } 221 } 222 223 public void testRaSelectAll() { 224 225 try { 226 227 229 utx.begin(); 230 231 233 ra1list = ra1home.findAllAccounts(); 234 235 while (ra1list.hasMoreElements()) { 236 ra1acc = (JdbcRA1) PortableRemoteObject.narrow(ra1list.nextElement(), JdbcRA1.class); 237 } 238 239 241 ra2list = ra2home.findAllAccounts(); 242 243 while (ra2list.hasMoreElements()) { 244 ra2acc = (JdbcRA2) PortableRemoteObject.narrow(ra2list.nextElement(), JdbcRA2.class); 245 } 246 247 utx.commit(); 248 249 assertTrue (5 == 5); 250 251 } catch (Exception e) { 252 253 try { 254 utx.rollback(); 255 fail("Cannot retrieve all accounts: " +e); 256 } 257 catch (Exception f) { 258 fail("Cannot rollback testRaSelectAll: " +f); 259 } 260 } 261 } 262 263 public void testRaSelectOne() { 264 265 267 ra1acc = null; 268 ra2acc = null; 269 270 try { 271 272 274 ra1acc = ra1home.findByNumber(202); 275 276 278 ra2acc = ra2home.findByNumber(202); 279 280 assertEquals (ra1acc.getNumber(), ra2acc.getNumber()); 281 282 } catch (Exception e) { 283 fail("Cannot find account 202 " +e); 284 } 285 } 286 287 public void testRaUpdateCommit() { 288 289 292 294 String str1 = null; 295 String str2 = null; 296 297 double value = 100; 298 299 try { 300 301 utx.begin(); 302 303 305 ra1acc = ra1home.findByNumber(202); 306 307 309 ra2acc = ra2home.findByNumber(202); 310 311 313 ra1acc.setBalance(-value); 314 315 317 ra2acc.setBalance(value); 318 319 utx.commit(); 320 321 assertEquals (String.valueOf(ra1acc.getBalance() + 100), 322 String.valueOf(ra2acc.getBalance() - 100)); 323 324 } catch (Exception e) { 325 326 try { 327 utx.rollback(); 328 fail("Cannot update balance by 100 for account 202 " +e); 329 330 } 331 catch (Exception f) { 332 fail("Cannot rollback TestRaUpdateCommit: " +f); 333 } 334 } 335 } 336 337 public void testRaUpdateRollback() { 338 339 342 344 String str1 = null; 345 String str2 = null; 346 347 double value = 20; 348 349 try { 350 351 utx.begin(); 352 353 355 ra1acc = ra1home.findByNumber(202); 356 357 359 ra2acc = ra2home.findByNumber(202); 360 361 363 ra1acc.setBalance(-value); 364 365 367 ra2acc.setBalance(value); 368 369 utx.rollback(); 370 371 assertEquals (String.valueOf(ra1acc.getBalance() + 100), 372 String.valueOf(ra2acc.getBalance() - 100)); 373 374 } catch (Exception e) { 375 try { 376 utx.rollback(); 377 fail("Cannot update balance by 20 for account 202 " +e); 378 } 379 catch (Exception f) { 380 fail("Cannot rollback testRaUpdateRollback: " +f); 381 } 382 } 383 } 384 385 public void testRaInsertCommit() { 386 387 389 ra1acc = null; 390 ra2acc = null; 391 392 try { 393 394 396 utx.begin(); 397 398 ra1acc = ra1home.create(209, "John Smith", 100); 399 400 402 ra2acc = ra2home.create(209, "John Smith", 100); 403 404 utx.commit(); 405 406 assertEquals (ra1acc.getNumber(), ra2acc.getNumber()); 407 408 } catch (Exception e) { 409 try { 410 utx.rollback(); 411 fail("Cannot create account 209: " +e); 412 } 413 catch (Exception f) { 414 fail("Cannot rollback testRaInsertCommit: " +f); 415 } 416 } 417 } 418 419 public void testRaInsertRollback() { 420 421 423 ra1acc = null; 424 ra2acc = null; 425 426 try { 427 428 430 utx.begin(); 431 432 ra1acc = ra1home.create(210, "Mike Smith", 100); 433 434 436 ra2acc = ra2home.create(210, "Mike Smith", 100); 437 438 utx.rollback(); 439 440 } catch (Exception e) { 441 try { 442 utx.rollback(); 443 fail("Cannot create account 210: " +e); 444 } 445 catch (Exception f) { 446 fail("Cannot rollback testRaInsertCommit: " +f); 447 } 448 } 449 450 try { 451 452 ra1acc = ra1home.findByNumber(210); 453 assertTrue (5 == 6); 454 455 } 456 catch (Exception g) { 457 459 try { 460 ra2acc = ra2home.findByNumber(210); 461 assertTrue (5 == 6); 462 } 463 catch (Exception h) { 464 assertTrue (5 == 5); 466 } 467 } 468 } 469 470 public void testRaDeleteRollback() { 471 472 474 ra1acc = null; 475 ra2acc = null; 476 477 try { 478 479 utx.begin(); 480 481 483 ra1acc = ra1home.findByNumber(201); 484 ra1acc.remove(); 485 486 488 ra2acc = ra2home.findByNumber(201); 489 ra2acc.remove(); 490 491 utx.rollback(); 492 493 utx.begin(); 494 495 497 ra1acc = ra1home.findByNumber(201); 498 499 501 ra2acc = ra2home.findByNumber(201); 502 503 utx.commit(); 504 505 assertEquals (ra1acc.getNumber(), ra2acc.getNumber()); 506 507 } catch (Exception e) { 508 try { 509 utx.rollback(); 510 fail("Cannot delete accounts 201-205, 209: " +e); 511 } 512 catch (Exception f) { 513 fail("Cannot rollback testRaDeleteCommit: " +f); 514 } 515 } 516 } 517 518 public void testRaDeleteCommit() { 519 520 522 ra1acc = null; 523 ra2acc = null; 524 525 try { 526 527 utx.begin(); 528 529 531 ra1acc = ra1home.findByNumber(201); 532 ra1acc.remove(); 533 ra1acc = ra1home.findByNumber(202); 534 ra1acc.remove(); 535 ra1acc = ra1home.findByNumber(203); 536 ra1acc.remove(); 537 ra1acc = ra1home.findByNumber(204); 538 ra1acc.remove(); 539 ra1acc = ra1home.findByNumber(205); 540 ra1acc.remove(); 541 ra1acc = ra1home.findByNumber(209); 542 ra1acc.remove(); 543 544 546 ra2acc = ra2home.findByNumber(201); 547 ra2acc.remove(); 548 ra2acc = ra2home.findByNumber(202); 549 ra2acc.remove(); 550 ra2acc = ra2home.findByNumber(203); 551 ra2acc.remove(); 552 ra2acc = ra2home.findByNumber(204); 553 ra2acc.remove(); 554 ra2acc = ra2home.findByNumber(205); 555 ra2acc.remove(); 556 ra2acc = ra2home.findByNumber(209); 557 ra2acc.remove(); 558 559 utx.commit(); 560 561 assertTrue (5 == 5); 562 563 } catch (Exception e) { 564 try { 565 utx.rollback(); 566 fail("Cannot delete accounts 201-205, 209: " +e); 567 } 568 catch (Exception f) { 569 fail("Cannot rollback testRaDeleteCommit: " +f); 570 } 571 } 572 } 573 574 public static Test suite() { 575 return new TestSuite(F_JdbcRATest.class); 576 } 577 578 public static void main (String args[]) { 579 580 String testtorun = null; 581 582 584 for (int argn = 0; argn < args.length; argn++) { 585 586 String s_arg = args[argn]; 587 Integer i_arg; 588 589 if (s_arg.equals("-n")) { 590 testtorun = args[++argn]; 591 } 592 } 593 594 if (testtorun == null) { 595 junit.textui.TestRunner.run(suite()); 596 } else { 597 junit.textui.TestRunner.run(new F_JdbcRATest(testtorun)); 598 } 599 } 600 } 601 | Popular Tags |