1 22 package org.jboss.ejb3.test.stateful.unit; 23 24 import java.util.Map ; 25 26 import javax.ejb.NoSuchEJBException ; 27 import javax.management.MBeanServerConnection ; 28 import javax.management.ObjectName ; 29 import javax.naming.InitialContext ; 30 31 import org.jboss.ejb3.ClientKernelAbstraction; 32 import org.jboss.ejb3.KernelAbstractionFactory; 33 import org.jboss.ejb3.test.stateful.ClusteredStateful; 34 import org.jboss.ejb3.test.stateful.ConcurrentStateful; 35 import org.jboss.ejb3.test.stateful.Entity; 36 import org.jboss.ejb3.test.stateful.EntityFacade; 37 import org.jboss.ejb3.test.stateful.ServiceRemote; 38 import org.jboss.ejb3.test.stateful.SmallCacheStateful; 39 import org.jboss.ejb3.test.stateful.Stateful; 40 import org.jboss.ejb3.test.stateful.StatefulInvoker; 41 import org.jboss.ejb3.test.stateful.StatefulLocal; 42 import org.jboss.ejb3.test.stateful.StatefulTx; 43 import org.jboss.ejb3.test.stateful.ProxyFactoryInterface; 44 import org.jboss.ejb3.test.stateful.RemoteBindingInterceptor; 45 import org.jboss.ejb3.test.stateful.State; 46 import org.jboss.ejb3.test.stateful.StatefulHome; 47 import org.jboss.ejb3.test.stateful.Stateless; 48 import org.jboss.ejb3.test.stateful.ExtendedState; 49 import org.jboss.ejb3.test.stateful.Tester; 50 import org.jboss.logging.Logger; 51 import org.jboss.test.JBossTestCase; 52 import junit.framework.Test; 53 54 import org.jboss.security.SimplePrincipal; 55 import org.jboss.security.SecurityAssociation; 56 57 63 64 public class RemoteUnitTestCase 65 extends JBossTestCase 66 { 67 private static final Logger log = Logger.getLogger(RemoteUnitTestCase.class); 68 69 static boolean deployed = false; 70 static int test = 0; 71 72 public RemoteUnitTestCase(String name) 73 { 74 75 super(name); 76 77 } 78 79 private class ConcurrentInvocation extends Thread 80 { 81 SmallCacheStateful small = null; 82 public Exception ex; 83 private int id; 84 85 public ConcurrentInvocation(int id) 86 { 87 this.id = id; 88 try 89 { 90 small = (SmallCacheStateful)getInitialContext().lookup("SmallCacheStatefulBean/remote"); 91 small.setId(id); 92 } 93 catch (Exception e) 94 { 95 } 96 } 97 98 public void run() 99 { 100 for (int i = 0; i < 5; i++) 101 { 102 try 103 { 104 assertEquals(small.doit(id),i); 105 } 106 catch (Exception e) 107 { 108 ex = e; 109 } 110 catch (Error er) 111 { 112 ex = new RuntimeException ("Failed assert: " + id, er); 113 } 114 } 115 } 116 } 117 118 public void testSmallCache() throws Exception 119 { 120 ConcurrentInvocation[] threads = new ConcurrentInvocation[5]; 121 for (int i = 0; i < 5; i++) threads[i] = new ConcurrentInvocation(i); 122 for (int i = 0; i < 5; i++) threads[i].start(); 123 for (int i = 0; i < 5; i++) threads[i].join(); 124 for (int i = 0; i < 5; i++) 125 { 126 if (threads[i].ex != null) 127 { 128 throw new Exception (threads[i].ex); 129 } 130 } 131 } 132 133 134 135 public void testStatefulSynchronization() throws Exception 136 { 137 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 138 SecurityAssociation.setCredential("password".toCharArray()); 139 140 Tester test = (Tester) getInitialContext().lookup("TesterBean/remote"); 141 test.testSessionSynchronization(); 142 143 } 144 145 public void testEJBObject() throws Exception 146 { 147 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 148 SecurityAssociation.setCredential("password".toCharArray()); 149 150 StatefulHome home = (StatefulHome)getInitialContext().lookup("StatefulHome"); 151 assertNotNull(home); 152 javax.ejb.EJBObject stateful = (javax.ejb.EJBObject )home.create(); 153 assertNotNull(stateful); 154 stateful = (javax.ejb.EJBObject )getInitialContext().lookup("Stateful"); 155 assertNotNull(stateful); 156 } 157 158 public void testStatefulTx() throws Exception 159 { 160 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 161 SecurityAssociation.setCredential("password".toCharArray()); 162 163 StatefulTx stateful = (StatefulTx)getInitialContext().lookup("StatefulTx"); 164 assertNotNull(stateful); 165 166 boolean transacted = stateful.isLocalTransacted(); 167 assertTrue(transacted); 168 transacted = stateful.isGlobalTransacted(); 169 assertFalse(transacted); 170 transacted = stateful.testNewTx(); 171 assertTrue(transacted); 172 173 try 174 { 175 stateful.testTxRollback(); 176 fail("should have caught exception"); 177 } 178 catch (javax.ejb.EJBException e) 179 { 180 } 181 } 182 183 public void testTemplateInterfaceTx() throws Exception 184 { 185 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 186 SecurityAssociation.setCredential("password".toCharArray()); 187 188 StatefulTx stateful = (StatefulTx)getInitialContext().lookup("StatefulTx"); 189 assertNotNull(stateful); 190 191 try 192 { 193 stateful.testMandatoryTx(new State("test")); 194 fail("should have caught exception"); 195 } 196 catch (javax.ejb.EJBTransactionRequiredException e) 197 { 198 } 199 } 200 201 public void testLocalSFSB() throws Exception 202 { 203 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 204 SecurityAssociation.setCredential("password".toCharArray()); 205 206 try 207 { 208 StatefulLocal stateful = (StatefulLocal)getInitialContext().lookup("StatefulBean/local"); 209 assertNotNull(stateful); 210 211 stateful.getState(); 212 fail("EJBException should be thrown"); 213 } 214 catch (Exception e) 215 { 216 if (e.getCause() == null || !(e.getCause() instanceof javax.ejb.EJBException )) 217 fail("EJBException should be thrown as cause"); 218 } 219 } 220 221 public void testNotSerialableSFSB() throws Exception 222 { 223 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 224 SecurityAssociation.setCredential("password".toCharArray()); 225 226 Stateful stateful = (Stateful)getInitialContext().lookup("Stateful"); 227 assertNotNull(stateful); 228 stateful.setState("state"); 229 String state = stateful.getState(); 230 assertEquals("state", state); 231 } 232 233 public void testSFSBInit() throws Exception 234 { 235 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 236 SecurityAssociation.setCredential("password".toCharArray()); 237 238 StatefulHome home = (StatefulHome)getInitialContext().lookup("StatefulHome"); 239 assertNotNull(home); 240 ExtendedState state = new ExtendedState("init"); 241 Stateful stateful = home.create(state); 242 assertNotNull(stateful); 243 String s = stateful.getState(); 244 assertEquals("Extended_init", s); 245 } 246 247 public void testStackTrace() throws Exception 248 { 249 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 250 SecurityAssociation.setCredential("password".toCharArray()); 251 252 Stateful stateful = (Stateful)getInitialContext().lookup("Stateful"); 253 assertNotNull(stateful); 254 255 try 256 { 257 stateful.testThrownException(); 258 this.fail("no exception caught"); 259 } 260 catch (Exception e) 261 { 262 StackTraceElement [] stackTrace = e.getStackTrace(); 263 assertTrue(stackTrace[stackTrace.length - 1].getClassName().startsWith("org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner")); 264 } 265 } 266 267 public void testExceptionCase() throws Exception 268 { 269 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 270 SecurityAssociation.setCredential("password".toCharArray()); 271 272 Stateful stateful = (Stateful)getInitialContext().lookup("Stateful"); 273 assertNotNull(stateful); 274 275 try 276 { 277 stateful.testExceptionCause(); 278 this.fail("no exception caught"); 279 } 280 catch (Exception e) 281 { 282 Throwable cause = e.getCause(); 283 assertNotNull(cause); 284 assertEquals(NullPointerException .class.getName(), cause.getClass().getName()); 285 } 286 } 287 288 public void testRemoteBindingProxyFactory() throws Exception 289 { 290 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 291 SecurityAssociation.setCredential("password".toCharArray()); 292 293 ProxyFactoryInterface stateful = (ProxyFactoryInterface)getInitialContext().lookup("Stateful"); 294 assertNotNull(stateful); 295 } 296 297 public void testRemoteBindingInterceptorStack() throws Exception 298 { 299 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 300 SecurityAssociation.setCredential("password".toCharArray()); 301 302 Stateful stateful = (Stateful)getInitialContext().lookup("Stateful"); 303 assertNotNull(stateful); 304 assertFalse(stateful.interceptorAccessed()); 305 assertTrue(RemoteBindingInterceptor.accessed); 306 } 307 308 public void testUninstantiatedPassivation() throws Exception 309 { 310 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 311 SecurityAssociation.setCredential("password".toCharArray()); 312 313 Stateful stateful = (Stateful)getInitialContext().lookup("Stateful"); 314 assertNotNull(stateful); 315 stateful.lookupStateful(); 316 Thread.sleep(10 * 1000); 317 stateful.testStateful(); 318 319 stateful.lookupStateful(); 320 stateful.testStateful(); 321 Thread.sleep(10 * 1000); 322 stateful.testStateful(); 323 } 324 325 public void testExtendedPersistencePassivation() throws Exception 326 { 327 EntityFacade stateful = (EntityFacade)getInitialContext().lookup("EntityFacadeBean/remote"); 328 Entity entity = null; 329 entity = stateful.createEntity("Kalin"); 330 331 Thread.sleep(10 * 1000); 333 334 MBeanServerConnection server = getServer(); 335 ObjectName objectName = new ObjectName ("jboss.jca:service=CachedConnectionManager"); 336 int inUseConnections = (Integer )server.getAttribute(objectName, "InUseConnections"); 337 System.out.println("inUseConnections \n" + inUseConnections); 338 assertEquals(0, inUseConnections); 339 340 entity = stateful.loadEntity(entity.getId()); 342 343 inUseConnections = (Integer )server.getAttribute(objectName, "InUseConnections"); 344 System.out.println("inUseConnections \n" + inUseConnections); 345 if (inUseConnections != 0) 346 printStats(server); 347 assertEquals(0, inUseConnections); 348 349 entity = stateful.createEntity("Kalin" + entity.getId()); 350 351 inUseConnections = (Integer )server.getAttribute(objectName, "InUseConnections"); 352 System.out.println("inUseConnections \n" + inUseConnections); 353 354 if (inUseConnections != 0) 355 printStats(server); 356 assertEquals(0, inUseConnections); 357 } 358 359 protected void printStats(MBeanServerConnection server) throws Exception 360 { 361 ObjectName objectName = new ObjectName ("jboss.jca:service=CachedConnectionManager"); 362 Object [] params = {}; 363 String [] sig = {}; 364 Map result = (Map )server.invoke(objectName, "listInUseConnections", params, sig); 365 System.out.println(result); 366 367 objectName = new ObjectName ("jboss:service=TransactionManager"); 368 long activeTransactions = (Long )server.getAttribute(objectName, "TransactionCount"); 369 System.out.println("activeTransactions \n" + activeTransactions); 370 371 objectName = new ObjectName ("jboss.jca:service=ManagedConnectionPool,name=DefaultDS"); 372 long inUseConnectionCount = (Long )server.getAttribute(objectName, "InUseConnectionCount"); 373 System.out.println("inUseConnectionCount \n" + inUseConnectionCount); 374 375 long maxConnectionsInUseCount = (Long )server.getAttribute(objectName, "MaxConnectionsInUseCount"); 376 System.out.println("maxConnectionsInUseCount \n" + maxConnectionsInUseCount); 377 } 378 379 public void testPassivation() throws Exception 380 { 381 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 382 SecurityAssociation.setCredential("password".toCharArray()); 383 384 System.out.println("testPassivation"); 385 Stateless stateless = (Stateless)getInitialContext().lookup("Stateless"); 386 stateless.testInjection(); 387 388 ServiceRemote service = (ServiceRemote) getInitialContext().lookup("ServiceBean/remote"); 389 service.testInjection(); 390 391 Stateful stateful = (Stateful)getInitialContext().lookup("Stateful"); 392 assertNotNull(stateful); 393 stateful.setState("state"); 394 assertEquals("state", stateful.getState()); 395 stateful.testSerializedState("state"); 396 assertEquals(null, stateful.getInterceptorState()); 397 stateful.setInterceptorState("hello world"); 398 assertFalse(stateful.testSessionContext()); 399 Thread.sleep(10 * 1000); 400 assertTrue(stateful.wasPassivated()); 401 402 assertEquals("state", stateful.getState()); 403 assertEquals("hello world", stateful.getInterceptorState()); 404 405 Stateful another = (Stateful)getInitialContext().lookup("Stateful"); 406 assertEquals(null, another.getInterceptorState()); 407 another.setInterceptorState("foo"); 408 assertEquals("foo", another.getInterceptorState()); 409 assertEquals("hello world", stateful.getInterceptorState()); 410 411 assertFalse(stateful.testSessionContext()); 412 413 stateful.testResources(); 414 415 stateless.testInjection(); 416 417 service.testInjection(); 418 } 419 420 public void testClusteredPassivation() throws Exception 421 { 422 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 423 SecurityAssociation.setCredential("password".toCharArray()); 424 425 System.out.println("testPassivation"); 426 Stateless stateless = (Stateless)getInitialContext().lookup("Stateless"); 427 stateless.testInjection(); 428 429 ServiceRemote service = (ServiceRemote) getInitialContext().lookup("ServiceBean/remote"); 430 service.testInjection(); 431 432 ClusteredStateful stateful = (ClusteredStateful)getInitialContext().lookup("ClusteredStateful"); 433 assertNotNull(stateful); 434 System.out.println("!!!! clusteredStateful " + stateful); 435 stateful.setState("state"); 436 assertEquals("state", stateful.getState()); 437 stateful.testSerializedState("state"); 438 assertEquals(null, stateful.getInterceptorState()); 439 stateful.setInterceptorState("hello world"); 440 assertFalse(stateful.testSessionContext()); 441 Thread.sleep(10 * 1000); 442 assertTrue(stateful.wasPassivated()); 443 444 assertEquals("state", stateful.getState()); 445 assertEquals("hello world", stateful.getInterceptorState()); 446 447 Stateful another = (Stateful)getInitialContext().lookup("Stateful"); 448 assertEquals(null, another.getInterceptorState()); 449 another.setInterceptorState("foo"); 450 assertEquals("foo", another.getInterceptorState()); 451 assertEquals("hello world", stateful.getInterceptorState()); 452 453 assertFalse(stateful.testSessionContext()); 454 455 stateful.testResources(); 456 457 stateless.testInjection(); 458 459 service.testInjection(); 460 } 461 462 463 public void testRemove() throws Exception 464 { 465 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 466 SecurityAssociation.setCredential("password".toCharArray()); 467 468 System.out.println("testPassivation"); 469 Stateful stateful = (Stateful)getInitialContext().lookup("Stateful"); 470 assertNotNull(stateful); 471 473 stateful.removeBean(); 474 475 try 476 { 477 stateful.getState(); 478 fail("Bean should have been removed"); 479 } catch (NoSuchEJBException e) 480 { 481 482 } 483 } 484 485 public void testRemoveWithRollback() throws Exception 486 { 487 Tester test = (Tester) getInitialContext().lookup("TesterBean/remote"); 488 test.testRollback1(); 489 test.testRollback2(); 490 } 491 492 public void testConcurrentAccess() throws Exception 493 { 494 ConcurrentStateful stateful = (ConcurrentStateful) new InitialContext ().lookup("ConcurrentStateful"); 495 stateful.getState(); 496 497 StatefulInvoker[] invokers = new StatefulInvoker[2]; 498 for (int i = 0; i < 2 ; ++i) 499 { 500 invokers[i] = new StatefulInvoker(stateful); 501 } 502 503 for (StatefulInvoker invoker: invokers) 504 { 505 invoker.start(); 506 } 507 508 Thread.sleep(10000); 509 510 for (StatefulInvoker invoker: invokers) 511 { 512 if (invoker.getException() != null) 513 throw invoker.getException(); 514 } 515 516 stateful = (ConcurrentStateful) new InitialContext ().lookup("Stateful"); 517 518 invokers = new StatefulInvoker[2]; 519 for (int i = 0; i < 2 ; ++i) 520 { 521 invokers[i] = new StatefulInvoker(stateful); 522 } 523 524 for (StatefulInvoker invoker: invokers) 525 { 526 invoker.start(); 527 } 528 529 Thread.sleep(10000); 530 531 boolean wasConcurrentException = false; 532 for (StatefulInvoker invoker: invokers) 533 { 534 if (invoker.getException() != null) 535 wasConcurrentException = true; 536 } 537 538 assertTrue(wasConcurrentException); 539 } 540 541 public void testOverrideConcurrentAccess() throws Exception 542 { 543 ConcurrentStateful stateful = (ConcurrentStateful) new InitialContext ().lookup("OverrideConcurrentStateful"); 544 stateful.getState(); 545 546 StatefulInvoker[] invokers = new StatefulInvoker[2]; 547 for (int i = 0; i < 2 ; ++i) 548 { 549 invokers[i] = new StatefulInvoker(stateful); 550 } 551 552 for (StatefulInvoker invoker: invokers) 553 { 554 invoker.start(); 555 } 556 557 Thread.sleep(5000); 558 559 boolean wasConcurrentException = false; 560 for (StatefulInvoker invoker: invokers) 561 { 562 if (invoker.getException() != null) 563 wasConcurrentException = true; 564 } 565 566 assertTrue(wasConcurrentException); 567 } 568 569 public void testJmxName() throws Exception 570 { 571 ObjectName deployment = new ObjectName ("test.ejb3:name=Bill,service=EJB3"); 572 573 ClientKernelAbstraction kernel = KernelAbstractionFactory.getClientInstance(); 574 kernel.invoke(deployment, "stop", new Object [0], new String [0]); 575 kernel.invoke(deployment, "start", new Object [0], new String [0]); 576 } 577 578 public void testDestroyException() throws Exception 579 { 580 EntityFacade stateful = (EntityFacade)getInitialContext().lookup("EntityFacadeBean/remote"); 581 assertNotNull(stateful); 582 stateful.createEntity("Kalin"); 583 584 try 585 { 586 stateful.remove(); 587 fail("should catch RuntimeException"); 588 } 589 catch (RuntimeException e) 590 { 591 } 592 593 stateful.createEntity("Cabernet"); 594 595 try 596 { 597 stateful.removeWithTx(); 598 fail("should catch RuntimeException"); 599 } 600 catch (RuntimeException e) 601 { 602 } 603 604 stateful.createEntity("Bailey"); 605 606 } 607 608 public static Test suite() throws Exception 609 { 610 return getDeploySetup(RemoteUnitTestCase.class, "stateful-test.jar"); 611 } 612 613 } 614 | Popular Tags |