1 22 package org.jboss.test.testbean.test; 23 24 import java.rmi.*; 25 26 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 import javax.ejb.DuplicateKeyException ; 30 import javax.ejb.Handle ; 31 import javax.ejb.EJBMetaData ; 32 import javax.ejb.EJBHome ; 33 import javax.ejb.HomeHandle ; 34 35 import java.util.Date ; 36 import java.util.Properties ; 37 import java.util.Collection ; 38 import java.util.Iterator ; 39 import java.util.Enumeration ; 40 import java.util.Arrays ; 41 42 import junit.framework.Test; 43 import junit.framework.TestCase; 44 import junit.framework.TestSuite; 45 46 import org.jboss.test.testbean.interfaces.StatelessSessionHome; 47 import org.jboss.test.testbean.interfaces.StatelessSession; 48 import org.jboss.test.testbean.interfaces.StatefulSessionHome; 49 import org.jboss.test.testbean.interfaces.StatefulSession; 50 import org.jboss.test.testbean.interfaces.EntityBMPHome; 51 import org.jboss.test.testbean.interfaces.EntityBMP; 52 import org.jboss.test.testbean.interfaces.EnterpriseEntityHome; 53 import org.jboss.test.testbean.interfaces.EnterpriseEntity; 54 import org.jboss.test.testbean.interfaces.EntityPKHome; 55 import org.jboss.test.testbean.interfaces.EntityPK; 56 import org.jboss.test.testbean.interfaces.BusinessMethodException; 57 import org.jboss.test.testbean.interfaces.AComplexPK; 58 import org.jboss.test.testbean.interfaces.TxSessionHome; 59 import org.jboss.test.testbean.interfaces.TxSession; 60 import org.jboss.test.testbean.interfaces.BMTStatefulHome; 61 import org.jboss.test.testbean.interfaces.BMTStateful; 62 import org.jboss.test.testbean.interfaces.BMTStatelessHome; 63 import org.jboss.test.testbean.interfaces.BMTStateless; 64 import org.jboss.test.testbean2.interfaces.AllTypes; 65 import org.jboss.test.testbean2.interfaces.AllTypesHome; 66 import org.jboss.test.testbean2.interfaces.MyObject; 67 68 import org.jboss.test.bmp.interfaces.BMPHelperSession; 69 import org.jboss.test.bmp.interfaces.BMPHelperSessionHome; 70 import org.jboss.test.bmp.interfaces.SimpleBMP; 71 import org.jboss.test.bmp.interfaces.SimpleBMPHome; 72 73 import org.jboss.test.JBossTestCase; 74 import org.jboss.invocation.MarshalledValue; 75 76 83 public class BeanUnitTestCase 84 extends JBossTestCase 85 { 86 static boolean deployed = false; 87 static int test = 0; 88 static Date startDate = new Date (); 89 90 protected final String namingFactory = 91 System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 92 93 protected final String providerURL = 94 System.getProperty(Context.PROVIDER_URL); 95 96 public BeanUnitTestCase(String name) { 97 super(name); 98 } 99 100 101 public void testRealBMP() 102 throws Exception 103 { 104 105 getLog().debug(""); 106 getLog().debug("Test Real BMP (load/passivation/..."); 107 getLog().debug("==================================="); 108 getLog().debug(""); 109 110 BMPHelperSessionHome sessionHome = (BMPHelperSessionHome)new InitialContext ().lookup ("bmp.BMPHelperSession"); 111 BMPHelperSession session = sessionHome.create (); 112 113 getLog().debug ("looking up table:"); 114 if (!session.existsSimpleBeanTable ()) 115 { 116 getLog().debug ("table does not exist."); 117 getLog().debug ("create it..."); 118 session.createSimpleBeanTable(); 119 getLog().debug ("done."); 120 } 121 122 SimpleBMPHome home = (SimpleBMPHome)new InitialContext ().lookup ("bmp.SimpleBMP"); 123 124 getLog().debug(++test+"- "+"create bean1: 1, Daniel"); 125 SimpleBMP b1 = home.create (1, "Daniel"); 126 getLog().debug ("getName (): "+b1.getName ()); 127 128 getLog().debug(++test+"- "+"create bean2: 2, Robert"); 129 b1 = home.create (2, "Robert"); 130 getLog().debug ("getName (): "+b1.getName ()); 131 132 try 133 { 134 getLog().debug(++test+"- trying to create one with same primkey: 1, Patrick"); 135 b1 = home.create (1, "Patrick"); 136 fail("Was able to create duplicate SimpleBMP"); 137 } 138 catch (Exception _e) 139 { 140 getLog().debug (_e.toString ()); 141 } 142 143 getLog().debug(++test+"- create some more dummys:"); 144 for (int i = 0; i < 50; ++i) 145 home.create (i + 3, ("Dummy "+i)); 146 147 getLog().debug(++test+"- trying to find Robert again"); 148 b1 = home.findByPrimaryKey (new Integer (2)); 149 getLog().debug ("getName (): "+b1.getName ()); 150 151 try 152 { 153 getLog().debug(++test+"- trying to find an not existing bean"); 154 b1 = home.findByPrimaryKey (new Integer (0)); 155 assertTrue("findByPrimaryKey(0) should fail", b1 == null); 156 } 157 catch (Exception _e) 158 { 159 getLog().debug (_e.toString ()); 160 } 161 162 getLog().debug(++test+"- rename Daniel to Maria: 1, Daniel"); 163 b1 = home.findByPrimaryKey (new Integer (1)); 164 getLog().debug ("name old: " + b1.getName ()); 165 b1.setName ("Maria"); 166 assertTrue("getName == Maria", "Maria".equals(b1.getName ())); 167 168 getLog().debug(++test+"- find all beans:"); 169 Iterator it = home.findAll ().iterator (); 170 while (it.hasNext ()) 171 { 172 getLog().debug ("found:"+((SimpleBMP)it.next ()).getName ()); 173 } 174 175 getLog().debug(++test+"- Now trying from within the Session bean (to be able to rollback):"); 176 getLog().debug (session.doTest ()); 177 178 getLog().debug(++test+"- get name after rollback "); 179 getLog().debug (session.doTestAfterRollback ()); 180 181 getLog().debug(++test+"- removing all beans"); 182 it = home.findAll ().iterator (); 183 while (it.hasNext ()) 184 ((SimpleBMP)it.next ()).remove (); 185 186 getLog().debug ("drop table..."); 187 session.dropSimpleBeanTable(); 188 getLog().debug ("done."); 189 } 190 191 192 public void testStatelessBean() 193 throws Exception 194 { 195 196 getLog().debug(++test+"- "+"Trying the context..."); 197 198 Context ctx = new InitialContext (); 199 getLog().debug("OK"); 200 201 getLog().debug(""); 203 getLog().debug("Test Stateless Bean"); 204 getLog().debug("==================="); 205 getLog().debug(""); 206 getLog().debug(++test+"- "+"Looking up the home nextgen.StatelessSession..."); 207 StatelessSessionHome statelessSessionHome = 208 (StatelessSessionHome) ctx.lookup("nextgen.StatelessSession"); 209 if (statelessSessionHome!= null ) getLog().debug("ok"); 210 getLog().debug(++test+"- "+"Calling create on StatelessSessionHome..."); 211 StatelessSession statelessSession = 212 statelessSessionHome.create(); 213 assertTrue("statelessSessionHome.create() != null", statelessSession != null); 214 getLog().debug("ok"); 215 216 getLog().debug(++test+"- "+"Calling getEJBHome() on StatelessSession..."); 217 assertTrue("statelessSession.getEJBHome() != null", statelessSession.getEJBHome() != null); 218 getLog().debug("ok"); 219 220 getLog().debug(++test+"- "+"Calling Business Method A on StatelessSession... "); 221 statelessSession.callBusinessMethodA(); 222 getLog().debug("ok"); 223 getLog().debug(++test+"- "+"Calling Business Method B on StatelessSession... "); 224 getLog().debug(statelessSession.callBusinessMethodB()); 225 getLog().debug(++test+"- "+"Calling Business Method B(String) on StatelessSession... "); 226 getLog().debug(statelessSession.callBusinessMethodB("of wisdom")); 227 getLog().debug(++test+"- "+"Calling Business Method C on StatelessSession... "); 228 getLog().debug(statelessSession.callBusinessMethodC()); 229 getLog().debug(++test+"- "+"Calling Business Method D on StatelessSession... "); 230 try 231 { 232 statelessSession.callBusinessMethodD(); 233 fail("callBusinessMethodD, no exception was thrown"); 234 } 235 catch (BusinessMethodException e) 236 { 237 getLog().debug("Caught BusinessMethodException OK"); 238 } 239 getLog().debug(++test+"- "+"Calling Business Method E (getEJBObject) on StatelessSession... "); 240 getLog().debug(statelessSession.callBusinessMethodE()); 241 242 getLog().debug(++test+"- "+"Calling testClassLoading on StatelessSession... "); 243 statelessSession.testClassLoading(); 244 getLog().debug("OK"); 245 246 getLog().debug("***Testing the various local Object class calls"); 247 getLog().debug(++test+"- "+"toString ... " + statelessSession.toString()); 248 249 getLog().debug(++test+"- "+"hashCode ... " + statelessSession.hashCode()); 250 251 getLog().debug(++test+"- "+"equals (same object) ... " + statelessSession.equals(statelessSession)); 252 253 getLog().debug(++test+"- "+"equals (another object) (true under same home)... " + statelessSession.equals(statelessSessionHome.create())); 254 255 getLog().debug("***Testing the various local EJBObject class calls"); 256 257 getLog().debug(++test+"- "+"Get Handle ... "); 258 Handle statelessHandle = statelessSession.getHandle(); 259 assertTrue("statelessHandle != null", statelessHandle != null); 260 getLog().debug("OK"); 261 getLog().debug(++test+"- "+"Serialize handle and deserialize.."); 262 MarshalledObject mo = new MarshalledObject(statelessHandle); 263 Handle handle2 = (Handle ) mo.get(); 264 StatelessSession statelessSession2 = (StatelessSession) handle2.getEJBObject(); 265 assertTrue("statelessSession2 != null", statelessSession2 != null); 266 getLog().debug("OK"); 267 getLog().debug(++test+"- "+"Calling businessMethodB on it..."); 268 getLog().debug(statelessSession2.callBusinessMethodB()); 269 getLog().debug(++test+"- "+"They should be identical..."+statelessSession.isIdentical(statelessSession2)); 270 getLog().debug("***Testing the various local EJBHome class calls"); 271 getLog().debug(++test+"- "+"Getting the metaData..."); 272 EJBMetaData statelessMetaData = statelessSessionHome.getEJBMetaData(); 273 assertTrue("statelessMetaData != null", statelessMetaData != null); 274 getLog().debug("OK"); 275 getLog().debug(++test+"- "+"Is stateless Session? "+statelessMetaData.isStatelessSession()); 276 getLog().debug(++test+"- "+"The remote class is "+statelessMetaData.getRemoteInterfaceClass()); 277 278 getLog().debug(""); 279 getLog().debug(++test+"- "+"Calling StatelessSession.remove()..."); 280 statelessSession.remove(); 281 getLog().debug("ok"); 282 } 283 284 public void testStatefulBean() 285 throws Exception 286 { 287 Context ctx = new InitialContext (); 288 289 getLog().debug(""); 290 getLog().debug("Test Stateful Bean"); 291 getLog().debug("=================="); 292 getLog().debug(""); 293 getLog().debug(++test+"- "+"Looking up the home nextgen.StatefulSession..."); 294 StatefulSessionHome statefulSessionHome = 295 (StatefulSessionHome) ctx.lookup("nextgen.StatefulSession"); 296 assertTrue("statefulSessionHome!= null", statefulSessionHome!= null); 297 getLog().debug("ok"); 298 getLog().debug(++test+"- "+"Calling create on StatefulSessionHome with name Marc..."); 299 StatefulSession statefulSession = 300 statefulSessionHome.create("Marc"); 301 assertTrue("statefulSession != null", statefulSession != null); 302 getLog().debug("ok"); 303 getLog().debug(++test+"- "+"Calling getEJBHome() on StatefulSession..."); 304 assertTrue("statefulSession.getEJBHome() != null", statefulSession.getEJBHome() != null); 305 getLog().debug("ok"); 306 getLog().debug(++test+"- "+"Calling Business Method A on StatefulSession... "); 307 getLog().debug(statefulSession.callBusinessMethodA()); 308 getLog().debug(++test+"- "+"Calling Business Method A (state) on StatefulSession... "); 309 getLog().debug(statefulSession.callBusinessMethodA()); 310 getLog().debug(++test+"- "+"Calling Business Method B (EJBObject) on StatefulSession... "); 311 getLog().debug(statefulSession.callBusinessMethodB()); 312 313 getLog().debug(++test+"- "+"Calling Business Method B(String) on StatefulSession... "); 314 getLog().debug(statefulSession.callBusinessMethodB("of wisdom")); 315 316 317 getLog().debug("***Testing the various local Object class calls"); 318 getLog().debug(++test+"- "+"toString ... "); 319 getLog().debug(statefulSession.toString()); 320 getLog().debug(++test+"- "+"hashCode ... " + 321 statefulSession.hashCode()); 322 323 getLog().debug(++test+"- "+"equals (same object) ... " + 324 statefulSession.equals(statefulSession)); 325 326 getLog().debug(++test+"- "+"equals (another object) (false under same home)... " + 327 statefulSession.equals(statefulSessionHome.create("marc4"))); 328 329 getLog().debug("***Testing the various local EJBObject class calls"); 330 331 getLog().debug(++test+"- "+"Get Handle ... "); 332 Handle statefulHandle = statefulSession.getHandle(); 333 assertTrue("statefulHandle != null", statefulHandle != null); 334 getLog().debug("OK"); 335 getLog().debug(++test+"- "+"Serialize handle and deserialize...."); 336 MarshalledObject mo2 = new MarshalledObject(statefulHandle); 337 Handle statefulHandle2 = (Handle ) mo2.get(); 338 StatefulSession statefulSession2 = (StatefulSession) statefulHandle2.getEJBObject(); 339 assertTrue("statefulSession2 != null", statefulSession2 != null); 340 getLog().debug("OK"); 341 getLog().debug(++test+"- "+"Calling businessMethodB on it..." + 342 statefulSession2.callBusinessMethodB()); 343 344 getLog().debug(++test+"- "+"They should be identical..." + 345 statefulSession.isIdentical(statefulSession2)); 346 347 getLog().debug(++test+"- "+"Calling StatefulSession.remove()..."); 348 statefulSession.remove(); 349 getLog().debug("ok"); 350 getLog().debug(++test+"- "+"Calling StatefulHome.remove(Handle) (this should fail)..."); 351 try 352 { 353 statefulSessionHome.remove(statefulSession2.getHandle()); 354 fail("statefulSessionHome.remove did not fail"); 355 } 356 catch (Exception e) 357 { 358 getLog().debug("not found OK"); 359 } 360 getLog().debug(++test+"- "+"Creating a 3rd bean and calling it..."); 361 StatefulSession ss3 = statefulSessionHome.create("marc3"); 362 getLog().debug(ss3.callBusinessMethodA()); 363 getLog().debug(++test+"- "+"Calling StatefulSession.remove(Handle) on a third bean..."); 364 Handle statefulHandle3 = ss3.getHandle(); 365 statefulSessionHome.remove(statefulHandle3); 366 getLog().debug("OK"); 367 getLog().debug(++test+"- "+"I should not be able to remove it directly..."); 368 try { 369 ss3.remove(); 370 fail("ss3.remove() did not fail"); 371 } catch (Exception e) { 372 getLog().debug("OK"); 373 } 374 375 getLog().debug(++test+"- "+"Creating a 4th bean using create<METHOD> and calling it..."); 376 StatefulSession ss4 = statefulSessionHome.createMETHOD("marc4", "address"); 377 getLog().debug(ss4.callBusinessMethodA()); 378 getLog().debug(++test+"- "+"Calling StatefulSession.remove(Handle) on a fourth bean..."); 379 Handle statefulHandle4 = ss4.getHandle(); 380 statefulSessionHome.remove(statefulHandle4); 381 } 382 383 public void testEntityBeanCMP() 384 throws Exception 385 { 386 Context ctx = new InitialContext (); 387 388 getLog().debug("testEntityBeanCMP"); 389 getLog().debug(++test+"- "+"Looking up the home nextgen.EnterpriseEntity...ok"); 390 391 EnterpriseEntityHome enterpriseEntityHome = (EnterpriseEntityHome) ctx.lookup("nextgen.EnterpriseEntity"); 392 getLog().debug(++test+"- "+"Calling find on EnterpriseEntityHome with name Marc..."); 393 EnterpriseEntity enterpriseEntity = null; 394 try 395 { 396 enterpriseEntity = enterpriseEntityHome.findByPrimaryKey("Marc"); 397 } 398 catch (Exception e) 399 { 400 getLog().debug("findByPrimaryKey(Marc) failed", e); 401 } 402 if (enterpriseEntity == null) 403 { 404 getLog().debug("not found OK"); 405 getLog().debug(++test+"- "+"Calling create on EnterpriseEntityHome with name Marc..."); 406 enterpriseEntity = enterpriseEntityHome.create("Marc"); 407 } 408 409 if (enterpriseEntity != null) 410 getLog().debug("ok, enterpriseEntity"+enterpriseEntity+", hashCode="+enterpriseEntity.hashCode()); 411 412 getLog().debug(++test+"- "+"Calling for duplicate create and DuplicateKeyException..."); 413 try 414 { 415 Object e = enterpriseEntityHome.create("Marc"); 416 getLog().debug("I Really should not make it here, e="+e+", hashCode="+e.hashCode()); 417 throw new Exception ("DuplicateKey not seen"); 418 } 419 catch (DuplicateKeyException dke) 420 { 421 getLog().debug("DuplicateKeyException ok"); 422 } 423 424 getLog().debug(++test+"- "+"Calling getEJBHome() on EntityCMP..."); 425 assertTrue("enterpriseEntity.getEJBHome() != null", enterpriseEntity.getEJBHome() != null); 426 getLog().debug("ok"); 427 428 getLog().debug(++test+"- "+"Getting a new reference with findByPK..."); 429 EnterpriseEntity enterpriseEntity2 = null; 430 try { 431 enterpriseEntity2 = enterpriseEntityHome.findByPrimaryKey("Marc"); 432 } 433 catch (Exception re) { 434 getLog().debug("Exception: ", re); 435 } 436 assertTrue("enterpriseEntity2 != null", enterpriseEntity2 != null); 437 getLog().debug("ok"); 438 getLog().debug(++test+"- "+"Calling Business Method A on enterpriseEntity... "); 439 getLog().debug(enterpriseEntity.callBusinessMethodA()); 440 441 getLog().debug(++test+"- "+"Calling Business Method A (again to ejbLoad if TypeC) on enterpriseEntity... "); 442 getLog().debug(enterpriseEntity.callBusinessMethodA()); 443 444 getLog().debug(++test+"- "+"Calling Business Method B (EJBObject from entity) on enterpriseEntity..."); 445 getLog().debug(enterpriseEntity.callBusinessMethodB()); 446 447 getLog().debug(++test+"- "+"Calling Business Method B(String) on EnterpriseEntity... "); 448 getLog().debug(enterpriseEntity.callBusinessMethodB("of wisdom")); 449 450 getLog().debug(++test+"- "+"Calling getOtherField (non pk) on enterpriseEntity..."); 451 getLog().debug("value: "+enterpriseEntity.getOtherField()); 452 453 getLog().debug(++test+"- "+"Calling setOtherField(4) on enterpriseEntity..."); 454 enterpriseEntity.setOtherField(4); 455 getLog().debug("OK"); 456 457 getLog().debug(++test+"- "+"Calling getOtherField() on enterpriseEntity (should be 4)..."); 458 int value = enterpriseEntity.getOtherField(); 459 assertTrue("enterpriseEntity.getOtherField() == 4", value == 4); 460 getLog().debug("value is "+value+", OK"); 461 462 getLog().debug("***Testing the various local Object class calls"); 463 getLog().debug(++test+"- "+"toString ... " + enterpriseEntity); 464 465 getLog().debug(++test+"- "+"hashCode ... " + enterpriseEntity.hashCode()); 466 467 getLog().debug(++test+"- "+"equals (same object) ... " + 468 enterpriseEntity.equals(enterpriseEntity)); 469 470 getLog().debug(++test+"- "+"equals (another object) (true for this case)... " + 471 enterpriseEntity.equals(enterpriseEntity2)); 472 473 getLog().debug("***Testing the various local EJBObject class calls"); 474 getLog().debug(++test+"- "+"Get Primary Key ... " + 475 enterpriseEntity.getPrimaryKey()); 476 477 getLog().debug(++test+"- "+"Get Handle ... "); 478 Handle entityHandle = enterpriseEntity.getHandle(); 479 assertTrue("entityHandle != null", entityHandle != null); 480 getLog().debug("OK"); 481 getLog().debug(++test+"- "+"Serialize handle and deserialize...."); 482 MarshalledObject mo3 = new MarshalledObject(entityHandle); 483 Handle entityHandle3 = (Handle ) mo3.get(); 484 EnterpriseEntity enterpriseEntity3 = (EnterpriseEntity) entityHandle3.getEJBObject(); 485 if (enterpriseEntity3 != null) getLog().debug("OK"); 486 getLog().debug(++test+"- "+"Calling businessMethodA on it..."); 487 getLog().debug(enterpriseEntity3.callBusinessMethodB()); 488 getLog().debug(++test+"- "+"They should be identical..."+enterpriseEntity.isIdentical(enterpriseEntity3)); 489 getLog().debug(++test+"- "+"Calling entityHome.remove(Handle)..."); 490 enterpriseEntityHome.remove(enterpriseEntity3.getHandle()); 491 getLog().debug("OK"); 492 493 getLog().debug(++test+"- "+"Calling enterpriseEntity.remove() (should fail)..."); 494 try { 495 enterpriseEntity.remove(); 496 fail("enterpriseEntity.remove() did not fail"); 497 } 498 catch (Exception e) { 499 getLog().debug("OK"); 500 } 501 502 getLog().debug(++test+"- "+"Calling EnterpriseEntity.create() for marc6..."); 503 EnterpriseEntity marc6 = enterpriseEntityHome.create("marc6"); 504 getLog().debug("ok"); 505 506 getLog().debug(++test+"- "+"Calling method createEntity on enterpriseEntity... "); 507 EnterpriseEntity marc2 = marc6.createEntity("marc2"); 508 getLog().debug("OK"); 509 510 511 getLog().debug(++test+"- "+"removing by PK on home (marc2)..."); 512 enterpriseEntityHome.remove(marc2.getPrimaryKey()); 513 getLog().debug("ok"); 514 515 getLog().debug(++test+"- "+"Calling enterpriseEntity.remove() (marc6)..."); 516 marc6.remove(); 517 getLog().debug("ok"); 518 519 getLog().debug(++test+"- "+"Calling EnterpriseEntity.create<METHOD>() for marc7..."); 520 EnterpriseEntity marc7 = enterpriseEntityHome.createMETHOD("marc7"); 521 getLog().debug("ok"); 522 523 getLog().debug(++test+"- "+"Calling enterpriseEntity.remove() (marc7)..."); 524 marc7.remove(); 525 getLog().debug("ok"); 526 527 528 529 getLog().debug(""); 530 getLog().debug(""); 531 getLog().debug(""); 532 } 533 534 public void testEntityBeanBMP() 535 throws Exception 536 { 537 Context ctx = new InitialContext (); 538 539 getLog().debug(""); 540 getLog().debug(""); 541 getLog().debug("Test Entity Bean BMP"); 542 getLog().debug("===================="); 543 getLog().debug(""); 544 545 546 getLog().debug(++test+"- "+"Looking up home for nextgen.EntityBMP..."); 547 EntityBMPHome bmpHome = (EntityBMPHome) ctx.lookup("nextgen.EntityBMP"); 548 assertTrue("bmpHome != null", bmpHome != null); 549 getLog().debug("ok"); 550 551 553 getLog().debug(++test+"- "+"Calling create on the home..."); 554 EntityBMP bmpBean = bmpHome.create("Marc"); 555 assertTrue("bmpBean != null", bmpBean != null); 556 getLog().debug("ok"); 557 558 560 getLog().debug(++test+"- "+"Calling getEJBHome() on EntityBMP..."); 561 assertTrue("bmpBean.getEJBHome() != null", bmpBean.getEJBHome() != null); 562 getLog().debug("ok"); 563 564 getLog().debug(++test+"- "+"Calling business methodA on BMP bean..."); 565 getLog().debug(bmpBean.callBusinessMethodA()); 566 getLog().debug(++test+"- "+"Calling business methodB (B2B) on BMP bean and it says "); 567 getLog().debug(bmpBean.callBusinessMethodB()); 568 569 570 getLog().debug(++test+"- "+"Calling Business Method B(String) on BMP... "); 571 getLog().debug(bmpBean.callBusinessMethodB("of wisdom")); 572 573 getLog().debug(++test+"- "+"calling remove() on BMP..."); 574 bmpBean.remove(); 575 576 getLog().debug(++test+"- "+"calling findCollectionKeys...."); 577 Collection pks = bmpHome.findCollectionKeys(3); 578 Iterator pkIterator = pks.iterator(); 579 while (pkIterator.hasNext()) { 580 EntityBMP currentBean = (EntityBMP)pkIterator.next(); 581 getLog().debug((String )currentBean.getPrimaryKey()); 582 } 583 getLog().debug("ok"); 584 585 getLog().debug(++test+"- "+"calling findEnumeratedKeys...."); 586 Enumeration pksEnumeration = bmpHome.findEnumeratedKeys(3); 587 while (pksEnumeration.hasMoreElements()) { 588 EntityBMP currentBean = (EntityBMP)pksEnumeration.nextElement(); 589 getLog().debug((String )currentBean.getPrimaryKey()); 590 } 591 getLog().debug("ok"); 592 593 getLog().debug(++test+"- "+"Calling create<METHOD> on the home..."); 594 bmpBean = bmpHome.createMETHOD("Marc2"); 595 assertTrue("bmpBean != null", bmpBean != null); 596 getLog().debug("ok"); 597 598 getLog().debug(++test+"- "+"calling remove() on BMP..."); 599 bmpBean.remove(); 600 } 601 602 public void testEntityBeanPK() 603 throws Exception 604 { 605 Context ctx = new InitialContext (); 606 607 getLog().debug(""); 608 getLog().debug(""); 609 getLog().debug("Test Entity Bean PK"); 610 getLog().debug("===================="); 611 getLog().debug(""); 612 613 getLog().debug(++test+"- "+"Looking up home for nextgen.EntityPK..."); 614 EntityPKHome pkHome = (EntityPKHome) ctx.lookup("nextgen.EntityPK"); 615 assertTrue("pkHome != null", pkHome != null); 616 getLog().debug("ok"); 617 618 getLog().debug(++test+"- "+"Calling find on the home..."); 619 EntityPK pkBean = null; 620 621 try { 623 624 pkBean = pkHome.findByPrimaryKey(new AComplexPK(true, 10, 100, 1000.0, "Marc")); 625 } catch (Exception e) { 626 627 getLog().debug("not found"); 628 getLog().debug(++test+"- "+"Did not find the instance will create it..."); 629 pkBean = pkHome.create(true, 10,100, 1000.0, "Marc"); 630 } 631 632 633 assertTrue("pkBean != null", pkBean != null); 634 getLog().debug("ok"); 635 636 getLog().debug(++test+"- "+"Retrieving other field..."); 637 assertTrue("pkBean.getOtherField() == 0", pkBean.getOtherField() == 0); 638 getLog().debug(++test+"- "+"Setting it to 4..."); 639 pkBean.setOtherField(4); 640 getLog().debug("ok"); 641 642 getLog().debug(++test+"- "+"Findind it again ... ") ; 643 644 pkBean = pkHome.findByPrimaryKey(new AComplexPK(true, 10, 100,1000.0, "Marc")); 646 647 assertTrue("pkBean != null", pkBean != null); 648 getLog().debug("ok"); 649 650 getLog().debug(++test+"- "+"Retrieving other field again, should be 4..."); 652 int newValue = pkBean.getOtherField(); 653 assertTrue("pkBean.getOtherField() == 4", newValue == 4); 654 getLog().debug("4, ok"); 655 656 getLog().debug(++test+"- gettting a new reference ... ") ; 659 EntityPK pkBean2 = pkHome.findByPrimaryKey(new AComplexPK(true, 10, 100,1000.0, "Marc")); 660 assertTrue("findByPrimaryKey AComplexPK", pkBean2 != null); 661 getLog().debug("ok"); 662 getLog().debug(++test+"- Retrieving other field again, should be 4..."); 663 int newValue2 = pkBean2.getOtherField(); 664 assertTrue("Retrieving other field again, should be 4...", newValue2 == 4); 665 666 getLog().debug(++test+"- "+"Removing the bean..."); 668 pkBean.remove(); 669 try 670 { 671 getLog().debug(++test+"- "+"I should not find it..."); 672 pkBean = pkHome.findByPrimaryKey(new AComplexPK(true, 10, 100, 1000.0, "Marc")); 673 assertTrue("findByPrimaryKey should fail", pkBean == null); 674 } 675 catch (Exception e) 676 { 677 getLog().debug("not found, OK"); 678 } 679 680 getLog().debug(++test+"- "+"Call create<METHOD> on EntityPKHome..."); 681 pkBean = pkHome.createMETHOD(true, 10,100, 1000.0, "Marc"); 682 getLog().debug(++test+"- "+"Removing the bean..."); 683 pkBean.remove(); 684 } 685 686 public void testTxSession() 687 throws Exception 688 { 689 Context ctx = new InitialContext (); 690 691 getLog().debug(""); 692 getLog().debug(""); 693 getLog().debug("Test TxSession"); 694 getLog().debug("=============="); 695 getLog().debug(""); 696 697 getLog().debug(++test+"- "+"Looking up home for nextgen.TxSession..."); 698 TxSessionHome txHome = (TxSessionHome) ctx.lookup("nextgen.TxSession"); 699 if (txHome != null )getLog().debug("ok"); 700 701 getLog().debug(++test+"- "+"Calling create on the home..."); 702 TxSession txBean = null; 703 704 try { 706 707 txBean = txHome.create(); 708 } catch (Exception e) { 709 710 getLog().debug("Exception: ", e); 711 } 712 713 714 if (txBean!= null) getLog().debug("ok"); 715 716 getLog().debug(++test+"- "+"calling supports... "); 717 getLog().debug(txBean.txSupports()); 718 719 getLog().debug(++test+"- "+"calling required... "); 720 getLog().debug(txBean.txRequired()); 721 722 getLog().debug(++test+"- "+"calling requiresNew... "); 723 getLog().debug(txBean.txRequiresNew()); 724 725 getLog().debug(++test+"- "+"calling not supported... "); 726 getLog().debug(txBean.txNotSupported()); 727 728 getLog().debug(++test+"- "+"calling mandatory (should get an exception)..."); 729 try { 730 getLog().debug(txBean.txMandatory()); 731 } catch (Exception e) { 732 getLog().debug("got Exception, ok"); 733 } 734 735 getLog().debug(++test+"- "+"calling requiredToSupports... "); 736 getLog().debug(txBean.requiredToSupports()); 737 738 getLog().debug(++test+"- "+"calling requiredToNotSupported... "); 739 getLog().debug(txBean.requiredToNotSupported()); 740 741 getLog().debug(++test+"- "+"calling requiredToRequiresNew... "); 742 getLog().debug(txBean.requiredToRequiresNew()); 743 744 getLog().debug("ok"); 745 } 746 747 public void testAllTypesBean() 748 throws Exception 749 { 750 751 try { 752 Context ctx = new InitialContext (); 753 754 getLog().debug(""); 755 getLog().debug(""); 756 getLog().debug("Test AllTypesBean"); 757 getLog().debug("================="); 758 getLog().debug(""); 759 getLog().debug(++test+"- "+"Looking up the home AllTypes..."); 760 AllTypesHome allTypesHome = (AllTypesHome) ctx.lookup("AllTypes"); 761 if (allTypesHome!= null ) getLog().debug("ok"); 762 763 getLog().debug(++test+"- "+"Getting the home handle..."); 764 HomeHandle homeHandle = allTypesHome.getHomeHandle(); 765 getLog().debug("OK"); 766 767 getLog().debug(++test+"- "+"Getting the home back from the handle..."); 768 EJBHome aHome = homeHandle.getEJBHome(); 769 getLog().debug("OK"); 770 771 getLog().debug(++test+"- "+"Getting metadata from home..."); 772 EJBMetaData aMetaData = aHome.getEJBMetaData(); 773 getLog().debug("OK"); 774 775 getLog().debug(++test+"- "+"Getting home from metadata..."); 776 aHome = aMetaData.getEJBHome(); 777 getLog().debug("OK"); 778 779 getLog().debug(++test+"- "+"Calling findByPrimaryKey on AllTypesHome with name seb..."); 780 781 AllTypes allTypes = null; 782 try { 783 allTypes = allTypesHome.findByPrimaryKey("seb"); 784 } 785 catch (Exception e) {getLog().debug(e.getMessage());} 786 if (allTypes == null) { 787 788 getLog().debug("not found OK"); 789 getLog().debug(++test+"- "+"Calling create on AllTypesHome with name seb..."); 790 allTypes = allTypesHome.create("seb"); 791 } 792 793 if (allTypes != null) getLog().debug("ok"); 794 795 getLog().debug(++test+"- "+"Calling business method A an AllTypes (B2B with external ejb-ref)..."); 796 getLog().debug("OK, result is" + allTypes.callBusinessMethodA()); 797 798 getLog().debug("Getting all the fields"); 799 getLog().debug(++test+"- "+"boolean " + allTypes.getBoolean() + " Ok"); 800 getLog().debug(++test+"- "+"byte " + allTypes.getByte() + " Ok"); 801 getLog().debug(++test+"- "+"short " + allTypes.getShort() + " Ok"); 802 getLog().debug(++test+"- "+"int " + allTypes.getInt() + " Ok"); 803 getLog().debug(++test+"- "+"long " + allTypes.getLong() + " Ok"); 804 getLog().debug(++test+"- "+"float " + allTypes.getFloat() + " Ok"); 805 getLog().debug(++test+"- "+"double " + allTypes.getDouble() + " Ok"); 806 getLog().debug("No char test yet, bug in jdk"); 807 getLog().debug(++test+"- "+"String " + allTypes.getString() + " Ok"); 808 getLog().debug(++test+"- "+"Date " + allTypes.getDate() + " Ok"); 809 getLog().debug(++test+"- "+"Timestamp " + allTypes.getTimestamp() + " Ok"); 810 811 getLog().debug(++test+"- "+"MyObject "); 812 MyObject obj = allTypes.getObject(); 813 getLog().debug("OK"); 814 815 getLog().debug(++test+"- "+"getting handle of stateful..."); 816 Handle sfHandle = allTypes.getStateful(); 817 getLog().debug("OK"); 818 819 getLog().debug(++test+"- "+"getting the bean back from the handle..."); 820 StatefulSession sfBean = (StatefulSession)sfHandle.getEJBObject(); 821 getLog().debug("OK"); 822 823 getLog().debug(++test+"- "+"comparing serialized handles..."); 824 assertTrue(Arrays.equals(new MarshalledValue(sfHandle).toByteArray(), new MarshalledValue(sfBean.getHandle()).toByteArray())); 825 getLog().debug("OK"); 826 827 getLog().debug(++test+"- "+"calling business method A on stateful: "); 828 getLog().debug("OK, result is " + sfBean.callBusinessMethodA()); 829 830 getLog().debug(++test+"- "+"adding the stateful bean as an object in AllTypes.."); 831 allTypes.addObjectToList(sfBean); 832 getLog().debug("OK"); 833 834 getLog().debug(++test+"- "+"getting handle of stateless..."); 835 Handle slHandle = allTypes.getStateless(); 836 getLog().debug("OK"); 837 838 getLog().debug(++test+"- "+"getting the bean back from the handle..."); 839 StatelessSession slBean = (StatelessSession)slHandle.getEJBObject(); 840 getLog().debug("OK"); 841 842 getLog().debug(++test+"- "+"comparing serialized handles..."); 843 assertTrue(Arrays.equals(new MarshalledValue(slHandle).toByteArray(), new MarshalledValue(slBean.getHandle()).toByteArray())); 844 getLog().debug("OK"); 845 846 getLog().debug(++test+"- "+"calling business method B on stateless: "); 847 getLog().debug("OK, result is " + slBean.callBusinessMethodB()); 848 849 getLog().debug(++test+"- "+"adding the stateless bean as an object in AllTypes.."); 850 allTypes.addObjectToList(slBean); 851 getLog().debug("OK"); 852 853 getLog().debug(++test+"- "+"getting handle of entity..."); 854 Handle eeHandle = allTypes.getEntity(); 855 getLog().debug("OK"); 856 857 getLog().debug(++test+"- "+"getting the bean back from the handle..."); 858 EnterpriseEntity eeBean = (EnterpriseEntity)eeHandle.getEJBObject(); 859 getLog().debug("OK"); 860 861 getLog().debug(++test+"- "+"comparing serialized handles..."); 862 assertTrue(Arrays.equals(new MarshalledValue(eeHandle).toByteArray(), new MarshalledValue(eeBean.getHandle()).toByteArray())); 863 getLog().debug("OK"); 864 865 getLog().debug(++test+"- "+"calling business method A on stateless: "); 866 getLog().debug("OK, result is" + eeBean.callBusinessMethodA()); 867 868 getLog().debug(++test+"- "+"adding the entity bean as an object in AllTypes.."); 869 allTypes.addObjectToList(eeBean); 870 getLog().debug("OK"); 871 872 getLog().debug(++test+"- "+"Getting the list of objects back (should contain the 3 beans)..."); 873 Collection coll = allTypes.getObjectList(); 874 assertEquals(coll.size(), 3); 875 getLog().debug("OK"); 876 getLog().debug(++test+"- "+"stateful bean "); 877 assertTrue(coll.contains(sfBean)); 878 getLog().debug("OK"); 879 getLog().debug(++test+"- "+"stateless bean "); 880 assertTrue(coll.contains(slBean)); 881 getLog().debug("OK"); 882 getLog().debug(++test+"- "+"entity bean "); 883 assertTrue(coll.contains(eeBean)); 884 getLog().debug("OK"); 885 886 getLog().debug("Testing automatically generated finders"); 887 888 getLog().debug(++test+"- "+"findAll().."); 889 coll = allTypesHome.findAll(); 890 assertTrue(coll.contains(allTypes)); 891 getLog().debug("OK"); 892 893 getLog().debug(++test+"- "+"findByPrimaryKey()..."); 894 AllTypes result = allTypesHome.findByPrimaryKey("seb"); 895 assertTrue(result.equals(allTypes)); 896 getLog().debug("OK"); 897 898 getLog().debug(++test+"- "+"findByABoolean().."); 899 coll = allTypesHome.findByABoolean(allTypes.getBoolean()); 900 assertTrue(coll.contains(allTypes)); 901 getLog().debug("OK"); 902 903 getLog().debug(++test+"- "+"findByAByte().."); 904 coll = allTypesHome.findByAByte(allTypes.getByte()); 905 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 906 907 getLog().debug(++test+"- "+"findByAShort().."); 908 coll = allTypesHome.findByAShort(allTypes.getShort()); 909 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 910 911 getLog().debug(++test+"- "+"findByAnInt().."); 912 coll = allTypesHome.findByAnInt(allTypes.getInt()); 913 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 914 915 getLog().debug(++test+"- "+"findByALong().."); 916 coll = allTypesHome.findByALong(allTypes.getLong()); 917 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 918 919 getLog().debug(++test+"- "+"findByAFloat().."); 920 coll = allTypesHome.findByAFloat(allTypes.getFloat()); 921 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 922 923 getLog().debug(++test+"- "+"findByADouble().."); 924 coll = allTypesHome.findByADouble(allTypes.getDouble()); 925 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 926 927 getLog().debug("No Char test yet, bug in jdk"); 928 929 getLog().debug(++test+"- "+"findByAString().."); 930 coll = allTypesHome.findByAString(allTypes.getString()); 931 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 932 933 getLog().debug(++test+"- "+"findByADate().."); 934 coll = allTypesHome.findByADate(allTypes.getDate()); 935 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 936 937 getLog().debug(++test+"- "+"findByATimestamp().."); 938 coll = allTypesHome.findByATimestamp(allTypes.getTimestamp()); 939 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 940 941 getLog().debug(++test+"- "+"findByAnObject().."); 942 coll = allTypesHome.findByAnObject(allTypes.getObject()); 943 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 944 945 getLog().debug(++test+"- "+"findByStatefulSession().."); 946 coll = allTypesHome.findByStatefulSession((StatefulSession)allTypes.getStateful().getEJBObject()); 947 getLog().debug("size="+coll.size()); 948 for (Iterator i = coll.iterator(); i.hasNext();) { 949 Object o = i.next(); 950 getLog().debug("o="+o); 951 } 952 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 953 954 getLog().debug(++test+"- "+"findByStatelessSession().."); 955 coll = allTypesHome.findByStatelessSession((StatelessSession)allTypes.getStateless().getEJBObject()); 956 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 957 958 getLog().debug(++test+"- "+"findByEnterpriseEntity().."); 959 coll = allTypesHome.findByEnterpriseEntity((EnterpriseEntity)allTypes.getEntity().getEJBObject()); 960 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 961 962 getLog().debug("Testing finders defined in jaws.xml"); 963 964 getLog().debug(++test+"- "+"findByMinInt().."); 965 coll = allTypesHome.findByMinInt(0); 966 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 967 968 getLog().debug(++test+"- "+"findByIntAndDouble().."); 969 coll = allTypesHome.findByIntAndDouble(allTypes.getInt(), allTypes.getDouble()); 970 assertTrue(coll.contains(allTypes));getLog().debug("OK"); 971 972 } 973 catch (Exception e) {getLog().debug("Exception: ", e); throw e;} 974 } 975 976 public void testBeanManagedTransactionDemarcation() 977 throws Exception 978 { 979 Context ctx = new InitialContext (); 980 try{ 981 getLog().debug(""); 982 getLog().debug(""); 983 getLog().debug("Test Bean Managed Transaction Demarcation"); 984 getLog().debug("========================================="); 985 getLog().debug(""); 986 ; 987 988 getLog().debug(++test+"- "+"Looking up the home BMTStateful..."); 989 BMTStatefulHome bmtSFHome = (BMTStatefulHome) ctx.lookup("BMTStateful"); 990 991 if (bmtSFHome != null ) getLog().debug("ok"); 992 993 getLog().debug(++test+"- "+"Calling create on BMTStatefulHome..."); 994 995 BMTStateful bmtSF = bmtSFHome.create(); 996 997 getLog().debug(++test+"- "+"Calling create(anything) on BMTStatefulHome..."); 998 999 bmtSFHome.create("coca"); 1000 getLog().debug("OK"); 1001 getLog().debug(++test+"- "+"Can the bean access its UserTransaction"); 1002 getLog().debug(bmtSF.txExists()); 1003 getLog().debug(++test+"- "+"Testing commit on UserTransaction"); 1004 getLog().debug(bmtSF.txCommit()); 1005 getLog().debug(++test+"- "+"Testing rollback on UserTransaction"); 1006 getLog().debug(bmtSF.txRollback()); 1007 getLog().debug(++test+"- "+"Beginning a transaction..."); 1008 getLog().debug(bmtSF.txBegin()); 1009 getLog().debug(++test+"- "+"Committing the transaction in another call..."); 1010 getLog().debug(bmtSF.txEnd()); 1011 getLog().debug(++test+"- "+"Creating a table for real db w/ tx test..."); 1012 bmtSF.createTable(); 1013 getLog().debug("OK, field value is:"); 1014 getLog().debug(bmtSF.getDbField()); 1015 getLog().debug(++test+"- "+"Updating the field in a transaction, commit..."); 1016 bmtSF.dbCommit(); 1017 getLog().debug("OK, field value is:"); 1018 getLog().debug(bmtSF.getDbField()); 1019 getLog().debug(++test+"- "+"Updating the field in a transaction, rollback..."); 1020 bmtSF.dbRollback(); 1021 getLog().debug("OK, field value is:"); 1022 getLog().debug(bmtSF.getDbField()); 1023 getLog().debug(++test+"- "+"Now dropping the table..."); 1024 bmtSF.dropTable(); 1025 getLog().debug("OK"); 1026 getLog().debug(++test+"- "+"Looking up the home BMTStateful..."); 1027 BMTStatelessHome bmtSLHome = (BMTStatelessHome) ctx.lookup("BMTStateless"); 1028 if (bmtSLHome != null ) getLog().debug("ok"); 1029 getLog().debug(++test+"- "+"Calling create on BMTStatelessHome..."); 1030 BMTStateless bmtSL = bmtSLHome.create(); 1031 getLog().debug("OK"); 1032 getLog().debug(++test+"- "+"Can the bean access its UserTransaction"); 1033 getLog().debug(bmtSL.txExists()); 1034 getLog().debug(++test+"- "+"Testing commit on UserTransaction"); 1035 getLog().debug(bmtSL.txCommit()); 1036 getLog().debug(++test+"- "+"Testing rollback on UserTransaction"); 1037 getLog().debug(bmtSL.txRollback()); 1038 getLog().debug(++test+"- "+"Beginning a transaction (container should throw an exception)..."); 1039 try { 1040 getLog().debug(bmtSL.txBegin()); 1041 fail("bmtSL.txBegin() did not fail"); 1042 } catch (RemoteException e) { 1043 getLog().debug(" ... OK, exception message: "+ e.getMessage()); 1044 } 1045 1046 getLog().debug(""); 1048 getLog().debug(""); 1049 getLog().debug(""); 1050 getLog().debug("Test OK, "+test+" tests run, congratulations"); 1051 1052 Date finishDate = new Date (); 1053 getLog().debug("Tests took "+(finishDate.getTime()-startDate.getTime())+" milliseconds"); 1054 } 1055 catch (Exception e) { 1056 1057 getLog().debug("exception: ", e); throw e;} 1058 1059 } 1060 1061 1062 public static Test suite() throws Exception 1063 { 1064 Test t1 = getDeploySetup(BeanUnitTestCase.class, "bmp.jar"); 1065 Test t2 = getDeploySetup(t1, "testbean.jar"); 1066 Test t3 = getDeploySetup(t2, "testbean2.jar"); 1067 return t3; 1068 } 1069 1070} 1071 | Popular Tags |