Esempio n. 1
0
  /**
   * Verifies the MessageDrivenContext methods.
   *
   * @param msg message
   */
  public void onMessage(final Message msg) {
    String op = null;

    try {
      op = msg.getStringProperty(MessageProperty.OPERATION.toString());
    } catch (JMSException e) {
      logger.debug("Error getting operation type: {0}", e);
    }

    if (ctx == null) {
      logger.debug("The SessionContext reference is null.");
    } else if (isEqual(GET_CALLER_PRINCIPAL, op)) {

      // TODO: test case

    } else if (isEqual(GET_ROLLBACK_ONLY, op)) {
      try {
        ctx.getRollbackOnly();
      } catch (IllegalStateException e) {
        log(
            MDBBeanManagedContext.class,
            ON_MESSAGE,
            MDBBeanManagedContext.class,
            GET_ROLLBACK_ONLY);
      } catch (Exception e) {
        logger.debug("Unexpected error.", e);
      }

    } else if (isEqual(SET_ROLLBACK_ONLY, op)) {
      try {
        ctx.setRollbackOnly();
      } catch (IllegalStateException e) {
        log(
            MDBBeanManagedContext.class,
            ON_MESSAGE,
            MDBBeanManagedContext.class,
            SET_ROLLBACK_ONLY);
      }

    } else if (isEqual(TIMER, op)) {
      try {
        checkInstance(ctx.getTimerService());
        log(MDBBeanManagedContext.class, ON_MESSAGE, MDBBeanManagedContext.class, TIMER);
      } catch (Exception e) {
        logger.debug("Unexpected error.", e);
      }

    } else if (isEqual(USER_TRANSACTION, op)) {
      try {
        checkInstance(ctx.getTimerService());
        log(MDBBeanManagedContext.class, ON_MESSAGE, MDBBeanManagedContext.class, USER_TRANSACTION);
      } catch (Exception e) {
        logger.debug("Unexpected error.", e);
      }
    }
  }
 /**
  * Verifies if the container re-throws the application exception that extends a runtime exception
  * (with rollback = false) and does not do the rollback in this case. The test uses a client
  * transaction.
  *
  * @throws Exception if an error during the tests occurs.
  */
 public void testUsingClientTransWithAppRuntimeException() throws Exception {
   UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
   utx.begin();
   try {
     sfsbContainerTransactionApp01.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
     fail("The container did not re-throw the application exception.");
   } catch (AppRuntimeException e) {
     logger.debug("The bean threw an expected error during the execution {0}", e);
   }
   // tries to commit
   try {
     utx.commit();
   } catch (Exception e) {
     logger.debug("The bean threw an expected error during the execution {0}", e);
   }
   // verifies if the table was created in the DATABASE_1
   ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
 }
 /**
  * Verifies if the container re-throws the application exception that extends a runtime exception
  * (with rollback = true) and marks the transaction for rollback in this case.
  *
  * @throws Exception if an error during the tests occurs.
  */
 public void testNotUsingClientTransWithAppRuntimeRollbackException() throws Exception {
   try {
     sfsbContainerTransactionApp02.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
     fail("The container did not re-throw the application exception.");
   } catch (RollbackAppRuntimeException e) {
     logger.debug("The bean threw an expected error during the execution {0}", e);
   }
   // verifies if the table was created in the DATABASE_1
   ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
 }
  /**
   * Verifies if the container throws the correct exception (EJBException for RequiresNew and
   * NotSupports, and EJBTransactionRolledbackException for Require, Supports and Mandatory) when
   * the bean throws a RuntimeException, discards the bean and does the rollback if the bean uses
   * the client transaction.
   *
   * @param expectedException EJBException for RequiresNew and NotSupports, and
   *     EJBTransactionRolledbackException for Require, Supports and Mandatory
   * @param canCommit true if the bean does not use the client transaction.
   * @throws Exception if an error during the tests occurs.
   */
  public void testUsingClientTransWithRuntimeException(
      final Class expectedException, final boolean canCommit) throws Exception {
    UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
    utx.begin();
    // verifies if the exception thrown is correct
    try {
      sfsbContainerTransactionRuntime.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
    } catch (Exception e) {
      assertTrue(
          ExceptionHelper.isEquals(e, expectedException),
          "The container did not throw the correct exception, the expected exception is "
              + expectedException.getName()
              + ", but the container threw "
              + e.getClass().getName());
    }
    // tries to commit
    try {
      utx.commit();
      if (!canCommit) {
        fail("The transaction was marked as rolled back, the client cannot make the commit.");
      }
    } catch (Exception e) {
      logger.debug("The bean threw an expected error during the execution {0}", e);
    }
    // verifies if the bean was discarded
    assertTrue(
        ExceptionHandleUtil.isDiscarded(sfsbContainerTransactionRuntime),
        "There was a runtime exception and the container did not discarded the bean.");
    // verifies if the transaction was rolled back
    if (!canCommit) {
      try {
        // verifies if the table was created in the DATABASE_1
        ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
        fail("There was an error during the commit and the transaction was not rolled back.");
      } catch (SQLException e) {
        logger.debug("The bean threw an expected error during the execution {0}", e);
      }
    }

    // TODO - verifies if the container log the error.

  }
 /**
  * Verifies if the container re-throws the application exception that extends a runtime exception
  * (with rollback = true) and marks the transaction for rollback in this case. This test uses a
  * client transaction.
  *
  * @param canCommit says if the bean is able to commit the client transaction. The client is able
  *     to commit when the bean does not use the client transaction.
  * @throws Exception if an error during the tests occurs.
  */
 public void testUsingClientTransWithAppRuntimeRollbackException(final boolean canCommit)
     throws Exception {
   UserTransaction utx = ExceptionHandleUtil.getUserTransaction();
   utx.begin();
   try {
     sfsbContainerTransactionApp02.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
     fail("The container did not re-throw the application exception.");
   } catch (RollbackAppRuntimeException e) {
     logger.debug("The bean threw an expected error during the execution {0}", e);
   }
   // tries to commit
   try {
     utx.commit();
     if (!canCommit) {
       fail("The transaction was marked as rolled back, the client cannot make the commit.");
     }
   } catch (Exception e) {
     logger.debug("The bean threw an expected error during the execution {0}", e);
   }
   // verifies if the table was created in the DATABASE_1
   ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
 }
  /**
   * Verifies if the container throws the EJBException when the bean throws a RuntimeException,
   * discards the bean and does the rollback in this case.
   *
   * @throws Exception if an error during the tests occurs.
   */
  public void testNotUsingClientTransWithRuntimeException() throws Exception {
    // verifies if the container threw the correct exception
    try {
      sfsbContainerTransactionRuntime.insertCorrectFirstErrorSecond(DATABASE_1, DATABASE_2);
      fail("The container did not throw the EJBException.");
    } catch (EJBException e) {
      logger.debug("The bean threw an expected error during the execution {0}", e);
    }

    // verifies if the bean was discarded
    assertTrue(
        ExceptionHandleUtil.isDiscarded(sfsbContainerTransactionRuntime),
        "There was a runtime exception and the container did not discarded the bean.");

    // TODO - verifies if the container log the error.

    // verifies if the table was created in the DATABASE_1
    ExceptionHandleUtil.verifyTable(DATABASE_1, ItfContainerTransaction.TABLE);
  }
  /**
   * Verifies if the operations allowed for Message Driven listener methods are working properly.
   *
   * @param message msg
   */
  public void onMessage(final Message message) {
    String op = null;

    try {
      op = message.getStringProperty(MessageProperty.OPERATION.toString());
    } catch (JMSException e) {
      logger.debug("Error getting operation type: {0}", e);
    }

    if (isEqual(RESOURCE_MANAGER, op)) {
      DataSource ds = (DataSource) ctx.lookup("jdbc/ds01");
      checkResource(ds);
      log(
          MDBListenerMethodAccess.class,
          ON_MESSAGE,
          MDBListenerMethodAccess.class,
          RESOURCE_MANAGER);

    } else if (isEqual(ENTERPRISE_BEAN, op)) {
      ItfOneMethod01 bean = (ItfOneMethod01) ctx.lookup("ejb/bean01");
      bean.getBool();
      log(
          MDBListenerMethodAccess.class,
          ON_MESSAGE,
          MDBListenerMethodAccess.class,
          ENTERPRISE_BEAN);

    } else if (isEqual(ENTITY_MANAGER_FACTORY, op)) {
      EntityManagerFactory emf = (EntityManagerFactory) ctx.lookup("persistence/pu01");
      try {
        EMFactoryTester.checkInstance(emf, "tmpTable" + this.hashCode());
        log(
            MDBListenerMethodAccess.class,
            ON_MESSAGE,
            MDBListenerMethodAccess.class,
            ENTITY_MANAGER_FACTORY);
      } catch (Exception e) {
        logger.debug("Error in EntityManagerFactory use: {0}", e);
        e.printStackTrace();
      }

    } else if (isEqual(ENTITY_MANAGER, op)) {
      EntityManager em = (EntityManager) ctx.lookup("persistence/pctx01");
      try {
        EntityManagerTester.checkInstance(em, "tmpTable" + this.hashCode());
        log(
            MDBListenerMethodAccess.class,
            ON_MESSAGE,
            MDBListenerMethodAccess.class,
            ENTITY_MANAGER);
      } catch (Exception e) {
        logger.debug("Error in EntityManagerFactory use: {0}", e);
        e.printStackTrace();
      }

    } else if (isEqual(TIMER, op)) {
      timer.hashCode();
      log(MDBListenerMethodAccess.class, ON_MESSAGE, MDBListenerMethodAccess.class, TIMER);

    } else if (isEqual(USER_TRANSACTION, op)) {
      if (utx == null) {
        log(
            MDBListenerMethodAccess.class,
            ON_MESSAGE,
            MDBListenerMethodAccess.class,
            USER_TRANSACTION);
      }
    }
  }
Esempio n. 8
0
 public void handle(final IEvent arg0) {
   EZBClusteredBeanEvent clusterEvent = (EZBClusteredBeanEvent) arg0;
   if (EZBClusteredBeanEvent.STARTING.equals(clusterEvent.getState())
       || EZBClusteredBeanEvent.STOPPING.equals(clusterEvent.getState())) {
     for (EZBRef reference : clusterEvent.getReferences()) {
       if (reference instanceof RemoteCallRef) {
         Factory<?, ?> factory = reference.getFactory();
         if (factory instanceof SessionFactory) {
           SessionFactory<?> fact = ((SessionFactory<?>) factory);
           Object cluster = fact.getBeanInfo().getCluster();
           Class<?> beanClass = fact.getBeanClass();
           Class<?> itfClass = null;
           if (cluster != null || beanClass.isAnnotationPresent(Cluster.class)) {
             if (EZBClusteredBeanEvent.STARTING.equals(clusterEvent.getState())) {
               String itfClassname = reference.getItfClassName();
               logger.info(
                   "The bean with the name {0} and the interface name {1} is clustered.",
                   reference.getFactory().getBeanInfo().getName(), itfClassname);
               for (Class<?> klass : beanClass.getInterfaces()) {
                 if (klass.getName().equals(itfClassname)) {
                   itfClass = klass;
                 }
               }
               if (itfClass == null) {
                 logger.error("Cannot find the interface for name {0}", itfClassname);
                 // throw new
                 // LifeCycleCallbackException("Cannot find the interface for name "
                 // + itfClassname);
               }
               Set<String> applicationExceptionNames =
                   factory.getBeanInfo().getApplicationExceptions().keySet();
               // Extract the informations on clustering
               ClusteredObjectInfo infos = null;
               try {
                 if (cluster != null) {
                   infos =
                       CMIInfoExtractor.extractClusteringInfoFromClusteredObject(
                           cluster,
                           itfClass,
                           beanClass,
                           factory instanceof StatefulSessionFactory,
                           false,
                           applicationExceptionNames);
                 } else {
                   infos =
                       CMIInfoExtractor.extractClusteringInfoFromAnnotatedPOJO(
                           reference.getJNDINamingInfos().get(0).jndiName(),
                           itfClass,
                           beanClass,
                           factory instanceof StatefulSessionFactory,
                           applicationExceptionNames);
                 }
                 CMIInfoRepository.addClusteredObjectInfo(
                     reference.getJNDINamingInfos().get(0).jndiName(), infos);
               } catch (CMIInfoExtractorException e) {
                 logger.error(
                     "Cannot extract infos for the class with name {0}", beanClass.getName(), e);
                 /*
                  * throw newLifeCycleCallbackException(
                  * "Cannot extract infos for the class with name "
                  * + beanClass.getName(), e);
                  */
               }
             } else {
               // Unbind reference.
               CMIInfoRepository.removeClusteredObjectInfo(
                   reference.getJNDINamingInfos().get(0).jndiName());
             }
           }
         }
       }
     }
   }
 }