All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xj4.dbunit.DBUnitLifecycle Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2008 Peach Jean Solutions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xj4.dbunit;

import org.xj4.lifecycle.AbstractLifecycle;
import org.xj4.lifecycle.Lifecycle;
import org.xj4.XJ4TestClass;

import java.lang.reflect.Field;
import java.util.List;

import org.dbunit.IDatabaseTester;

/**
 * Performs setup and teardown on dbunit database tester objects.
 *
 * @author Jared Bunting
 */
public class DBUnitLifecycle extends AbstractLifecycle {
  public boolean supportsClassLevel() {
    return true;
  }

  public boolean supportsInstanceLevel() {
    return true;
  }

  public void start(XJ4TestClass testClass, Field field, Object target) {
    try {
      getDatabaseTester(field, target).onSetup();
    } catch (Exception e) {
      throw new IllegalStateException("Couldn't start database tester " + field.getName());
    }
  }

  public void stop(XJ4TestClass testClass, Field field, Object target) {
    try {
      getDatabaseTester(field, target).onTearDown();
    } catch (Exception e) {
      throw new IllegalStateException("Couldn't stop the database tester " + field.getName());
    }
  }

  private IDatabaseTester getDatabaseTester(Field field, Object target) {
    IDatabaseTester iDatabaseTester;
    try {
      iDatabaseTester = (IDatabaseTester) field.get(target);
    } catch (IllegalAccessException e) {
      throw new IllegalArgumentException("Problem accessing the field.", e);
    }
    return iDatabaseTester;
  }

  public Class requiredType() {
    return IDatabaseTester.class;
  }

  public boolean acceptsNull() {
    return false;
  }

  public void validate(List errors, Field field) {
    // we have nothing to validate
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy