/**
  * Tests that a SLSB method which is marked as asynchronous and returns a {@link
  * java.util.concurrent.Future} is invoked asynchronously and the client isn't blocked for the
  * lifetime of the method
  *
  * @throws Exception
  */
 @Test
 public void testAsyncFutureMethodOnSLSB() throws Exception {
   final StatelessEJBLocator<EchoRemote> locator =
       new StatelessEJBLocator<EchoRemote>(
           EchoRemote.class, APP_NAME, MODULE_NAME, EchoBean.class.getSimpleName(), "");
   final EchoRemote echoRemote = EJBClient.createProxy(locator);
   Assert.assertNotNull("Received a null proxy", echoRemote);
   final String message = "You are supposed to be an asynchronous method";
   final long DELAY = 5000;
   final long start = System.currentTimeMillis();
   // invoke the asynchronous method
   final Future<String> futureEcho = echoRemote.asyncEcho(message, DELAY);
   final long end = System.currentTimeMillis();
   logger.info(
       "Asynchronous invocation returned a Future: "
           + futureEcho
           + " in "
           + (end - start)
           + " milli seconds");
   // test that the invocation did not act like a synchronous invocation and instead returned
   // "immediately"
   Assert.assertFalse(
       "Asynchronous invocation behaved like a synchronous invocation", (end - start) >= DELAY);
   Assert.assertNotNull("Future is null", futureEcho);
   // Check if the result is marked as complete (it shouldn't be this soon)
   Assert.assertFalse("Future result is unexpectedly completed", futureEcho.isDone());
   // wait for the result
   final String echo = futureEcho.get();
   Assert.assertEquals("Unexpected echo message", message, echo);
 }
 /**
  * Tests that invocation on a stateful session bean fails, if a session hasn't been created
  *
  * @throws Exception
  */
 @Test
 @Ignore(
     "No longer appropriate, since a proxy can no longer be created without a session, for a SFSB. "
         + "Need to think if there's a different way to test this. Else just remove this test")
 public void testSFSBAccessFailureWithoutSession() throws Exception {
   // create a locator without a session
   final StatefulEJBLocator<Counter> locator =
       new StatefulEJBLocator<Counter>(
           Counter.class,
           APP_NAME,
           MODULE_NAME,
           CounterBean.class.getSimpleName(),
           "",
           null,
           Affinity.NONE,
           null);
   final Counter counter = EJBClient.createProxy(locator);
   Assert.assertNotNull("Received a null proxy", counter);
   // invoke the bean without creating a session
   try {
     final int initialCount = counter.getCount();
     Assert.fail(
         "Expected a EJBException for calling a stateful session bean without creating a session");
   } catch (EJBException ejbe) {
     // expected
     logger.info("Received the expected exception", ejbe);
   }
 }
  /**
   * Test a invocation on a stateless bean method which accepts and returns custom objectss
   *
   * @throws Exception
   */
  @Test
  public void testRemoteSLSBWithCustomObjects() throws Exception {
    final StatelessEJBLocator<EmployeeManager> locator =
        new StatelessEJBLocator(
            EmployeeManager.class, APP_NAME, MODULE_NAME, EmployeeBean.class.getSimpleName(), "");
    final EmployeeManager proxy = EJBClient.createProxy(locator);
    Assert.assertNotNull("Received a null proxy", proxy);
    final String[] nickNames =
        new String[] {"java-programmer", "ruby-programmer", "php-programmer"};
    final Employee employee = new Employee(1, "programmer");
    // invoke on the bean
    final AliasedEmployee employeeWithNickNames = proxy.addNickNames(employee, nickNames);

    // check the id of the returned employee
    Assert.assertEquals("Unexpected employee id", 1, employeeWithNickNames.getId());
    // check the name of the returned employee
    Assert.assertEquals("Unexpected employee name", "programmer", employeeWithNickNames.getName());
    // check the number of nicknames
    Assert.assertEquals(
        "Unexpected number of nick names",
        nickNames.length,
        employeeWithNickNames.getNickNames().size());
    // make sure the correct nick names are present
    for (int i = 0; i < nickNames.length; i++) {
      Assert.assertTrue(
          "Employee was expected to have nick name: " + nickNames[i],
          employeeWithNickNames.getNickNames().contains(nickNames[i]));
    }
  }
 /**
  * AS7-3129
  *
  * <p>Make sure that the CDI request scope is activated for remote EJB invocations
  */
 @Test
 public void testCdiRequestScopeActive() {
   final StatelessEJBLocator<EchoRemote> locator =
       new StatelessEJBLocator(
           EchoRemote.class, APP_NAME, MODULE_NAME, EchoBean.class.getSimpleName(), "");
   final EchoRemote proxy = EJBClient.createProxy(locator);
   Assert.assertTrue(proxy.testRequestScopeActive());
 }
 /**
  * Test bean returning a value object with a transient field. Will test that the transient field
  * is set to null (just like java serialization would do) instead of a non-null value (non-null
  * came ValueWrapper class initializer if this fails).
  *
  * @throws Exception
  */
 @Test
 public void testValueObjectWithTransientField() throws Exception {
   final StatelessEJBLocator<EchoRemote> locator =
       new StatelessEJBLocator(
           EchoRemote.class, APP_NAME, MODULE_NAME, EchoBean.class.getSimpleName(), "");
   final EchoRemote proxy = EJBClient.createProxy(locator);
   String shouldBeNil = proxy.getValue().getShouldBeNilAfterUnmarshalling();
   Assert.assertNull(
       "transient field should be serialized as null but was '" + shouldBeNil + "'", shouldBeNil);
 }
 /**
  * Test a simple invocation on a remote view of a Stateless session bean method
  *
  * @throws Exception
  */
 @Test
 public void testRemoteSLSBInvocation() throws Exception {
   final StatelessEJBLocator<EchoRemote> locator =
       new StatelessEJBLocator(
           EchoRemote.class, APP_NAME, MODULE_NAME, EchoBean.class.getSimpleName(), "");
   final EchoRemote proxy = EJBClient.createProxy(locator);
   Assert.assertNotNull("Received a null proxy", proxy);
   final String message = "Hello world from a really remote client";
   final String echo = proxy.echo(message);
   Assert.assertEquals("Unexpected echo message", message, echo);
 }
 /**
  * Tests that invocations on a stateful session bean work after a session is created and the
  * stateful session bean really acts as a stateful bean
  *
  * @throws Exception
  */
 @Test
 public void testSFSBInvocation() throws Exception {
   final StatefulEJBLocator<Counter> locator =
       EJBClient.createSession(
           Counter.class, APP_NAME, MODULE_NAME, CounterBean.class.getSimpleName(), "");
   final Counter counter = EJBClient.createProxy(locator);
   Assert.assertNotNull("Received a null proxy", counter);
   // invoke the bean
   final int initialCount = counter.getCount();
   logger.info("Got initial count " + initialCount);
   Assert.assertEquals("Unexpected initial count from stateful bean", 0, initialCount);
   final int NUM_TIMES = 50;
   for (int i = 1; i <= NUM_TIMES; i++) {
     final int count = counter.incrementAndGetCount();
     logger.info("Got next count " + count);
     Assert.assertEquals("Unexpected count after increment", i, count);
   }
   final int finalCount = counter.getCount();
   logger.info("Got final count " + finalCount);
   Assert.assertEquals("Unexpected final count", NUM_TIMES, finalCount);
 }
 /**
  * Tests that the invocation on a non-existent view of an (existing) EJB leads to a {@link
  * NoSuchEJBException}
  *
  * @throws Exception
  */
 @Test
 public void testNonExistentViewForEJB() throws Exception {
   final StatelessEJBLocator<NotAnEJBInterface> locator =
       new StatelessEJBLocator<NotAnEJBInterface>(
           NotAnEJBInterface.class, APP_NAME, MODULE_NAME, EchoBean.class.getSimpleName(), "");
   final NotAnEJBInterface nonExistentBean = EJBClient.createProxy(locator);
   Assert.assertNotNull("Received a null proxy", nonExistentBean);
   // invoke on the (non-existent) view of a bean
   try {
     nonExistentBean.echo("Hello world to a non-existent view of a bean");
     Assert.fail("Expected a NoSuchEJBException");
   } catch (NoSuchEJBException nsee) {
     // expected
     logger.info("Received the expected exception", nsee);
   }
 }
 /**
  * Tests that invoking a non-existent EJB leads to a {@link NoSuchEJBException}
  *
  * @throws Exception
  */
 @Test
 public void testNonExistentEJBAccess() throws Exception {
   final StatelessEJBLocator<NotAnEJBInterface> locator =
       new StatelessEJBLocator<NotAnEJBInterface>(
           NotAnEJBInterface.class, "non-existen-app-name", MODULE_NAME, "blah", "");
   final NotAnEJBInterface nonExistentBean = EJBClient.createProxy(locator);
   Assert.assertNotNull("Received a null proxy", nonExistentBean);
   // invoke on the (non-existent) bean
   try {
     nonExistentBean.echo("Hello world to a non-existent bean");
     Assert.fail("Expected a NoSuchEJBException");
   } catch (NoSuchEJBException nsee) {
     // expected
     logger.info("Received the expected exception", nsee);
   }
 }
Ejemplo n.º 10
0
 public EJBHome getEJBHome() throws IllegalStateException {
   if (ejbHomeViewServiceName == null) {
     throw MESSAGES.beanHomeInterfaceIsNull(getComponentName());
   }
   final ServiceController<?> serviceController =
       currentServiceContainer().getRequiredService(ejbHomeViewServiceName);
   final ComponentView view = (ComponentView) serviceController.getValue();
   final String locatorAppName = earApplicationName == null ? "" : earApplicationName;
   return EJBClient.createProxy(
       new EJBHomeLocator<EJBHome>(
           (Class<EJBHome>) view.getViewClass(),
           locatorAppName,
           moduleName,
           getComponentName(),
           distinctName));
 }
 /**
  * Test a invocation on the remote view of a stateless bean which is configured for user
  * interceptors
  *
  * @throws Exception
  */
 @Test
 public void testRemoteSLSBWithInterceptors() throws Exception {
   final StatelessEJBLocator<EchoRemote> locator =
       new StatelessEJBLocator(
           EchoRemote.class, APP_NAME, MODULE_NAME, InterceptedEchoBean.class.getSimpleName(), "");
   final EchoRemote proxy = EJBClient.createProxy(locator);
   Assert.assertNotNull("Received a null proxy", proxy);
   final String message = "Hello world from a really remote client";
   final String echo = proxy.echo(message);
   final String expectedEcho =
       message
           + InterceptorTwo.MESSAGE_SEPARATOR
           + InterceptorOne.class.getSimpleName()
           + InterceptorOne.MESSAGE_SEPARATOR
           + InterceptorTwo.class.getSimpleName();
   Assert.assertEquals("Unexpected echo message", expectedEcho, echo);
 }
Ejemplo n.º 12
0
 public EJBHome getEJBHome() throws IllegalStateException {
   if (ejbHome == null) {
     throw new IllegalStateException(
         "Bean " + getComponentName() + " does not have a Home interface");
   }
   final ServiceController<?> serviceController =
       CurrentServiceContainer.getServiceContainer().getRequiredService(ejbHome);
   final ComponentView view = (ComponentView) serviceController.getValue();
   final String locatorAppName = earApplicationName == null ? "" : earApplicationName;
   return EJBClient.createProxy(
       new EJBHomeLocator<EJBHome>(
           (Class<EJBHome>) view.getViewClass(),
           locatorAppName,
           moduleName,
           getComponentName(),
           distinctName));
 }
Ejemplo n.º 13
0
 @SuppressWarnings("unchecked")
 @Override
 public EJBObject getEJBObject(final InterceptorContext ctx) throws IllegalStateException {
   if (getEjbObjectViewServiceName() == null) {
     throw MESSAGES.beanComponentMissingEjbObject(getComponentName(), "EJBObject");
   }
   final ServiceController<?> serviceController =
       currentServiceContainer().getRequiredService(getEjbObjectViewServiceName());
   final ComponentView view = (ComponentView) serviceController.getValue();
   final String locatorAppName = getEarApplicationName() == null ? "" : getEarApplicationName();
   return EJBClient.createProxy(
       new StatefulEJBLocator<EJBObject>(
           (Class<EJBObject>) view.getViewClass(),
           locatorAppName,
           getModuleName(),
           getComponentName(),
           getDistinctName(),
           getSessionIdOf(ctx),
           this.getCache().getStrictAffinity()));
 }
  /**
   * AS7-3402
   *
   * <p>Tests that a NonSerializableException does not break the channel
   */
  @Test
  public void testNonSerializableResponse() throws InterruptedException, ExecutionException {
    final StatelessEJBLocator<NonSerialiazableResponseRemote> locator =
        new StatelessEJBLocator(
            NonSerialiazableResponseRemote.class,
            APP_NAME,
            MODULE_NAME,
            NonSerializableResponseEjb.class.getSimpleName(),
            "");
    final NonSerialiazableResponseRemote proxy = EJBClient.createProxy(locator);

    Callable<Object> task =
        new Callable<Object>() {

          @Override
          public Object call() throws Exception {
            try {
              proxy.nonSerializable();
              Assert.fail();
            } catch (Exception e) {
              logger.info("expected " + e);
            }
            Thread.sleep(1000);
            Assert.assertEquals("hello", proxy.serializable());
            return null;
          }
        };
    final ExecutorService executor = Executors.newFixedThreadPool(10);
    try {
      final List<Future> tasks = new ArrayList<Future>();
      for (int i = 0; i < 100; ++i) {
        tasks.add(executor.submit(task));
      }

      for (Future result : tasks) {
        result.get();
      }
    } finally {
      executor.shutdown();
    }
  }
 /**
  * Tests that an {@link javax.ejb.ApplicationException} thrown by a SLSB method is returned back
  * to the client correctly
  *
  * @throws Exception
  */
 @Test
 public void testApplicationExceptionOnSLSBMethod() throws Exception {
   final StatelessEJBLocator<ExceptionThrowingRemote> locator =
       new StatelessEJBLocator<ExceptionThrowingRemote>(
           ExceptionThrowingRemote.class,
           APP_NAME,
           MODULE_NAME,
           ExceptionThrowingBean.class.getSimpleName(),
           "");
   final ExceptionThrowingRemote exceptionThrowingBean = EJBClient.createProxy(locator);
   Assert.assertNotNull("Received a null proxy", exceptionThrowingBean);
   final String exceptionState = "2342348723Dsbjlfjal#";
   try {
     exceptionThrowingBean.alwaysThrowApplicationException(exceptionState);
     Assert.fail("Expected a " + StatefulApplicationException.class.getName() + " exception");
   } catch (StatefulApplicationException sae) {
     // expected
     logger.info("Received the expected exception", sae);
     Assert.assertEquals(
         "Unexpected state in the application exception", exceptionState, sae.getState());
   }
 }
 /**
  * Tests that a system exception thrown from a SLSB method is conveyed back to the client
  *
  * @throws Exception
  */
 @Test
 public void testSystemExceptionOnSLSBMethod() throws Exception {
   final StatelessEJBLocator<ExceptionThrowingRemote> locator =
       new StatelessEJBLocator<ExceptionThrowingRemote>(
           ExceptionThrowingRemote.class,
           APP_NAME,
           MODULE_NAME,
           ExceptionThrowingBean.class.getSimpleName(),
           "");
   final ExceptionThrowingRemote exceptionThrowingBean = EJBClient.createProxy(locator);
   Assert.assertNotNull("Received a null proxy", exceptionThrowingBean);
   final String exceptionState = "bafasfaj;l";
   try {
     exceptionThrowingBean.alwaysThrowSystemException(exceptionState);
     Assert.fail("Expected a " + EJBException.class.getName() + " exception");
   } catch (EJBException ejbe) {
     // expected
     logger.info("Received the expected exception", ejbe);
     final Throwable cause = ejbe.getCause();
     Assert.assertTrue("Unexpected cause in EJBException", cause instanceof RuntimeException);
     Assert.assertEquals(
         "Unexpected state in the system exception", exceptionState, cause.getMessage());
   }
 }