/**
  * 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 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 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.

  }
 /**
  * Initiates all things needed to execute the tests.
  *
  * @throws Exception if an error occurs.
  */
 public void setup() throws Exception {
   // cleans the transaction active in other test
   ExceptionHandleUtil.cleanTransaction();
   // creates all beans
   createBeanApp();
   createBeanApp01();
   createBeanApp02();
   createBeanRollback();
   createBeanRuntime();
   // cleans the database
   deleteTable();
 }
 /**
  * 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);
 }
 /**
  * Deletes the tables created during test.
  *
  * @throws Exception if a lookup error occurs.
  */
 public void deleteTable() throws Exception {
   // deletes the table after each test to avoid errors.
   ExceptionHandleUtil.deleteTable(DATABASE_1, ItfContainerTransaction.TABLE);
   ExceptionHandleUtil.deleteTable(DATABASE_2, ItfContainerTransaction.TABLE);
 }