private <T extends Annotation> Collection<T> getAnnotations(
     DbUnitTestContext testContext, Class<T> annotationType) {
   List<T> annotations = new ArrayList<T>();
   addAnnotationToList(
       annotations, AnnotationUtils.findAnnotation(testContext.getTestClass(), annotationType));
   addAnnotationToList(
       annotations, AnnotationUtils.findAnnotation(testContext.getTestMethod(), annotationType));
   return annotations;
 }
 private IDataSet loadDataset(DbUnitTestContext testContext, String dataSetLocation)
     throws Exception {
   DataSetLoader dataSetLoader = testContext.getDataSetLoader();
   if (StringUtils.hasLength(dataSetLocation)) {
     IDataSet dataSet = dataSetLoader.loadDataSet(testContext.getTestClass(), dataSetLocation);
     Assert.notNull(
         dataSet,
         "Unable to load dataset from \""
             + dataSetLocation
             + "\" using "
             + dataSetLoader.getClass());
     return dataSet;
   }
   return null;
 }
 private void setupOrTeardown(
     DbUnitTestContext testContext, boolean isSetup, Collection<AnnotationAttributes> annotations)
     throws Exception {
   IDatabaseConnection connection = testContext.getConnection();
   DatabaseOperation lastOperation = null;
   for (AnnotationAttributes annotation : annotations) {
     for (String dataSetLocation : annotation.getValue()) {
       DatabaseOperation operation = annotation.getType();
       org.dbunit.operation.DatabaseOperation dbUnitDatabaseOperation =
           getDbUnitDatabaseOperation(testContext, operation, lastOperation);
       IDataSet dataSet = loadDataset(testContext, dataSetLocation);
       if (dataSet != null) {
         if (logger.isDebugEnabled()) {
           logger.debug(
               "Executing "
                   + (isSetup ? "Setup" : "Teardown")
                   + " of @DatabaseTest using "
                   + operation
                   + " on "
                   + dataSetLocation);
         }
         dbUnitDatabaseOperation.execute(connection, dataSet);
         lastOperation = operation;
       }
     }
   }
 }
  @AroundInvoke
  public Object execute(InvocationContext invocationContext) throws Exception {

    // Execute following logic only if @Test method
    Test testAnnotation = invocationContext.getMethod().getAnnotation(Test.class);
    if (testAnnotation == null) {
      return invocationContext.proceed();
    }

    dbUnitSetup();
    DbUnitTestContext testContext = new DbUnitTestContext();

    Object returnValue = null;
    Class<?> testClass = ProxyUtils.getUnproxiedClass(invocationContext.getTarget().getClass());
    testContext.setTestClass(testClass);
    testContext.setTestInstance(invocationContext.getTarget());
    testContext.setTestMethod(invocationContext.getMethod());

    DbUnitConfiguration dbUnitConfiguration = testClass.getAnnotation(DbUnitConfiguration.class);
    try {
      dbUnitRunner.prepareTesterSetup(testContext, dbUnitConfiguration, databaseTester);

      //            try {
      databaseTester.onSetup();
      //            } catch (Throwable e) { // do not propagate db setup exception or transaction
      // will not rollback
      //                e.printStackTrace();
      //            }

      returnValue = invocationContext.proceed();

      dbUnitRunner.verifyExpected(testContext);
    } finally {
      dbUnitRunner.prepareTesterTearDown(testContext, dbUnitConfiguration, databaseTester);
      databaseTester.onTearDown();
      try {
        if (connection != null) {
          connection.close();
        }
      } catch (Exception e) {
        connection = null;
      }
    }
    return returnValue;
  }
 /**
  * Called after a test method is executed to perform any database teardown and to check expected
  * results.
  *
  * @param testContext The test context
  * @throws Exception
  */
 public void afterTestMethod(DbUnitTestContext testContext) throws Exception {
   try {
     verifyExpected(testContext, getAnnotations(testContext, ExpectedDatabase.class));
     Collection<DatabaseTearDown> annotations =
         getAnnotations(testContext, DatabaseTearDown.class);
     try {
       setupOrTeardown(testContext, false, AnnotationAttributes.get(annotations));
     } catch (RuntimeException e) {
       if (testContext.getTestException() == null) {
         throw e;
       }
       if (logger.isWarnEnabled()) {
         logger.warn("Unable to throw database cleanup exception due to existing test error", e);
       }
     }
   } finally {
     testContext.getConnection().close();
   }
 }
 private org.dbunit.operation.DatabaseOperation getDbUnitDatabaseOperation(
     DbUnitTestContext testContext, DatabaseOperation operation, DatabaseOperation lastOperation) {
   if ((operation == DatabaseOperation.CLEAN_INSERT)
       && (lastOperation == DatabaseOperation.CLEAN_INSERT)) {
     operation = DatabaseOperation.INSERT;
   }
   org.dbunit.operation.DatabaseOperation databaseOperation =
       testContext.getDatbaseOperationLookup().get(operation);
   Assert.state(
       databaseOperation != null, "The databse operation " + operation + " is not supported");
   return databaseOperation;
 }
 private void verifyExpected(
     DbUnitTestContext testContext, Collection<ExpectedDatabase> annotations) throws Exception {
   if (testContext.getTestException() != null) {
     if (logger.isDebugEnabled()) {
       logger.debug(
           "Skipping @DatabaseTest expectation due to test exception "
               + testContext.getTestException().getClass());
     }
     return;
   }
   IDatabaseConnection connection = testContext.getConnection();
   IDataSet actualDataSet = connection.createDataSet();
   for (ExpectedDatabase annotation : annotations) {
     IDataSet expectedDataSet = loadDataset(testContext, annotation.value());
     if (expectedDataSet != null) {
       if (logger.isDebugEnabled()) {
         logger.debug("Veriftying @DatabaseTest expectation using " + annotation.value());
       }
       DatabaseAssertion assertion = annotation.assertionMode().getDatabaseAssertion();
       assertion.assertEquals(expectedDataSet, actualDataSet);
     }
   }
 }