FileDocCategorySizeDatePackage
AsynchronousTestCase.javaAPI DocJBoss 4.2.112331Fri Jul 13 20:53:36 BST 2007org.jboss.ejb3.test.asynchronous.unit

AsynchronousTestCase

public class AsynchronousTestCase extends org.jboss.test.JBossTestCase
author
Kabir Khan
version
$Revision: 57207 $

Fields Summary
private static final Logger
log
static boolean
deployed
static int
test
Constructors Summary
public AsynchronousTestCase(String name)


     
   
      super(name);
   
Methods Summary
private java.lang.ObjectgetReturnOrException(org.jboss.aspects.asynch.AsynchProvider provider)

      try
      {
         Future future = provider.getFuture();

         waitForFuture(future);
         return future.get();
      }
      catch(InvocationTargetException e)
      {
         return e.getCause();
      }
   
public static junit.framework.Testsuite()

      return getDeploySetup(AsynchronousTestCase.class, "asynchronous-test.sar, asynchronous-test.jar");
   
public voidtestAsynchSecurity()


      SecuredStatelessRemote tester =
            (SecuredStatelessRemote) getInitialContext().lookup("SecuredStatelessBean/remote");
      SecuredStatelessRemote asynchTester = (SecuredStatelessRemote)((JBossProxy)tester).getAsynchronousProxy();
      AsynchProvider ap = (AsynchProvider)asynchTester;

      SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail"));
      SecurityAssociation.setCredential("password".toCharArray());

      asynchTester.method(61);
      Object ret = getReturnOrException(ap);
      assertTrue("SecurityException not thrown: " + ret, ret instanceof EJBAccessException);

      asynchTester.uncheckedMethod(62);
      ret = getReturnOrException(ap);
      assertEquals("Wrong value", 62, ret);

      asynchTester.excludedMethod(63);
      ret = getReturnOrException(ap);
      assertTrue("SecurityException not thrown: " + ret, ret instanceof EJBAccessException);

      SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
      SecurityAssociation.setCredential("password".toCharArray());

      asynchTester.method(64);
      ret = getReturnOrException(ap);
      assertEquals("Wrong return for authorised method", 64, ret);

      SecurityAssociation.setPrincipal(new SimplePrincipal("nosuchuser"));
      SecurityAssociation.setCredential("password".toCharArray());

      asynchTester.method(65);
      ret = getReturnOrException(ap);
      assertTrue("SecurityException not thrown: " + ret, ret instanceof EJBAccessException);
   
public voidtestLocalAsynchTransaction()

	  MBeanServerConnection server = getServer();
      ObjectName testerName = new ObjectName("jboss.ejb3:service=Tester,test=asynchronous");
      Object[] params = {};
      String[] sig = {};
      server.invoke(testerName, "testLocalAsynchTransaction", params, sig);

   
public voidtestRemoteAsynchTransaction()

      TxSessionRemote tester = (TxSessionRemote) getInitialContext().lookup("TxSessionBean/remote");
      TxSessionRemote asynchTester = (TxSessionRemote)((JBossProxy)tester).getAsynchronousProxy();
      AsynchProvider ap = (AsynchProvider) asynchTester;
      UserTransaction tx = (UserTransaction)getInitialContext().lookup("UserTransaction");

      //Add some entries in different threads and commit
      tx.begin();
      tester.createFruit("apple", false);
      tester.createFruit("pear", false);
      tester.createFruit("tomato", false);

      asynchTester.createVeg("Potato", false);
      waitForProvider(ap);
      asynchTester.createVeg("Turnip", false);
      waitForProvider(ap);
      asynchTester.createVeg("Carrot", false);
      waitForProvider(ap);
      tx.commit();

      tx.begin();
      Collection entries = tester.getEntries();
      tx.commit();
      assertEquals("Wrong number of entries", 6, entries.size());

      //Cleanup synchronously
      tx.begin();
      tester.cleanAll();
      tx.commit();

      tx.begin();
      entries = tester.getEntries();
      tx.commit();
      assertEquals("Wrong number of entries", 0, entries.size());

      //Add some entries in different threads and rollback
      tx.begin();
      tester.createFruit("apple", false);
      tester.createFruit("pear", false);
      tester.createFruit("tomato", false);

      asynchTester.createVeg("Potato", false);
      waitForProvider(ap);
      asynchTester.createVeg("Turnip", false);
      waitForProvider(ap);
      asynchTester.createVeg("Carrot", false);
      waitForProvider(ap);
      tx.rollback();

      tx.begin();
      entries = tester.getEntries();
      tx.commit();

      assertEquals("Wrong number of entries", 0, entries.size());

      //Add some entries in different threads and rollback from within Tx
      tx.begin();
      tester.createFruit("apple", false);
      tester.createFruit("pear", false);
      tester.createFruit("tomato", true);

      asynchTester.createVeg("Potato", false);
      waitForProvider(ap);
      asynchTester.createVeg("Turnip", false);
      waitForProvider(ap);
      asynchTester.createVeg("Carrot", true);
      waitForProvider(ap);

      boolean rollbackException = false;
      try
      {
         tx.commit();
      }
      catch(RollbackException e)
      {
         rollbackException = true;
      }

      assertTrue("RollbackException not picked up", rollbackException);

      tx.begin();
      entries = tester.getEntries();
      tx.commit();
      assertEquals("Wrong number of entries", 0, entries.size());
   
public voidtestSFClusteredAsynchronous()

      StatefulClusteredRemote tester =
            (StatefulClusteredRemote) getInitialContext().lookup("StatefulClusteredBean/remote");
      assertEquals("Wrong return for stateful clustered", 41, tester.method(41));

      StatefulClusteredRemote asynchTester = (StatefulClusteredRemote)((JBossProxy)tester).getAsynchronousProxy();
      assertEquals("Wrong return value for stateful clustered", 0, asynchTester.method(42));
      AsynchProvider ap = (AsynchProvider) asynchTester;
      Future future = ap.getFuture();
      int ret = (Integer) future.get();
      assertEquals("Wrong async return value for stateful clustered", ret, 42);
   
public voidtestSFLocalAsynchrounous()

	  MBeanServerConnection server = getServer();
      ObjectName testerName = new ObjectName("jboss.ejb3:service=Tester,test=asynchronous");
      Object[] params = {};
      String[] sig = {};
      server.invoke(testerName, "testSFLocalAsynchronous", params, sig);
   
public voidtestSFRemoteAsynchronous()

      StatefulRemote tester =
            (StatefulRemote) getInitialContext().lookup("StatefulBean/remote");
      assertEquals("Wrong return for stateful remote", 31, tester.method(31));

      StatefulRemote asynchTester = (StatefulRemote)((JBossProxy)tester).getAsynchronousProxy();
      assertEquals("Wrong return value for stateful remote", 0, asynchTester.method(32));
      AsynchProvider ap = (AsynchProvider) asynchTester;
      Future future = ap.getFuture();
      int ret = (Integer) future.get();
      assertEquals("Wrong async return value for stateful remote", ret, 32);
   
public voidtestSLClusteredAsynchronous()

      StatelessClusteredRemote tester =
            (StatelessClusteredRemote) getInitialContext().lookup("StatelessClusteredBean/remote");
      assertEquals("Wrong return for stateless clustered", 21, tester.method(21));

      StatelessClusteredRemote asynchTester = (StatelessClusteredRemote)((JBossProxy)tester).getAsynchronousProxy();
      assertEquals("Wrong return value for stateless clustered", 0, asynchTester.method(22));
      AsynchProvider ap = (AsynchProvider) asynchTester;
      Future future = ap.getFuture();
      int ret = (Integer) future.get();
      assertEquals("Wrong async return value for stateless clustered", ret, 22);
   
public voidtestSLLocalAsynchrounous()

	  MBeanServerConnection server = getServer();
      ObjectName testerName = new ObjectName("jboss.ejb3:service=Tester,test=asynchronous");
      Object[] params = {};
      String[] sig = {};
      server.invoke(testerName, "testSLLocalAsynchronous", params, sig);
   
public voidtestSLRemoteAsynchronous()

      StatelessRemote tester =
            (StatelessRemote) getInitialContext().lookup("StatelessBean/remote");
      assertEquals("Wrong return for stateless remote", 11, tester.method(11));

      StatelessRemote asynchTester = (StatelessRemote)Asynch.getAsynchronousProxy(tester);
      assertEquals("Wrong return value for stateless remote", 0, asynchTester.method(12));
      Future future = Asynch.getFutureResult(asynchTester);
      int ret = (Integer) future.get();
      assertEquals("Wrong async return value for stateless remote", ret, 12);
   
public voidtestServiceLocalAsynchrounous()

	  MBeanServerConnection server = getServer();
      ObjectName testerName = new ObjectName("jboss.ejb3:service=Tester,test=asynchronous");
      Object[] params = {};
      String[] sig = {};
      server.invoke(testerName, "testServiceLocalAsynchronous", params, sig);
   
public voidtestServiceRemoteAsynchronous()

      ServiceRemote tester =
            (ServiceRemote) getInitialContext().lookup("ServiceBean/remote");
      assertEquals("Wrong return for service remote", 51, tester.method(51));

      ServiceRemote asynchTester = (ServiceRemote)((JBossProxy)tester).getAsynchronousProxy();
      assertEquals("Wrong return value for service remote", 0, asynchTester.method(52));
      AsynchProvider ap = (AsynchProvider) asynchTester;
      Future future = ap.getFuture();
      int ret = (Integer) future.get();
      assertEquals("Wrong async return value for service remote", ret, 52);
   
private voidwaitForFuture(org.jboss.aspects.asynch.Future future)

      while (!future.isDone())
      {
         Thread.sleep(100);
      }
   
private voidwaitForProvider(org.jboss.aspects.asynch.AsynchProvider provider)

      Future future = provider.getFuture();
      waitForFuture(future);