Package org.dbunit

Class DefaultPrepAndExpectedTestCase

java.lang.Object
org.dbunit.DatabaseTestCase
org.dbunit.DBTestCase
org.dbunit.DefaultPrepAndExpectedTestCase
All Implemented Interfaces:
PrepAndExpectedTestCase, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.InvocationInterceptor, org.junit.jupiter.api.extension.TestInstantiationAwareExtension

public class DefaultPrepAndExpectedTestCase extends DBTestCase implements PrepAndExpectedTestCase
Test case base class supporting prep data and expected data. Prep data is the data needed for the test to run. Expected data is the data needed to compare if the test ran successfully.

configureTest(), setupData(), verifyData(), and cleanupData() share one IDatabaseConnection for a test's lifecycle, acquired lazily on first use and closed once by cleanupData(), instead of each acquiring (and often closing) its own. Calling any of those methods without an eventual cleanupData() call - e.g. testing them individually rather than through preTest()/postTest() - leaves that connection open.

If databaseTester is configured with a CachingConnectionProvider shared across test methods, set setCloseConnectionAfterTest(boolean) to false so cleanupData() does not close a connection other tests still expect to reuse; the provider's owner is then responsible for closing it once, itself, when the whole run finishes.

The verifyData() method hands assertion failures to DefaultFailureHandler by default, which throws on the first mismatch found. Set setFailureHandler(FailureHandler) to, for example, a DiffCollectingFailureHandler to collect every Difference instead; that is not the default since most tests want to keep failing fast.

Since:
2.4.8
Version:
$Revision$ $Date$
Author:
Jeff Jensen jeffjensen AT users.sourceforge.net, Last changed by: $Author$
See Also:
  • "org.dbunit.DefaultPrepAndExpectedTestCaseDiIT, a composition-based (DI) usage example in the test sources"
  • "org.dbunit.DefaultPrepAndExpectedTestCaseExtIT, an inheritance-based usage example in the test sources"
  • Field Details

    • TEST_ERROR_MSG

      public static final String TEST_ERROR_MSG
      Message prefix used for wrapped test failures.
      See Also:
  • Constructor Details

    • DefaultPrepAndExpectedTestCase

      public DefaultPrepAndExpectedTestCase()
      Create new instance.
    • DefaultPrepAndExpectedTestCase

      public DefaultPrepAndExpectedTestCase(DataFileLoader dataFileLoader, IDatabaseTester databaseTester)
      Create new instance with specified dataFileLoader and databaseTester.
      Parameters:
      dataFileLoader - Load to use for loading the data files.
      databaseTester - Tester to use for database manipulation.
    • DefaultPrepAndExpectedTestCase

      public DefaultPrepAndExpectedTestCase(DataFileLoader dataFileLoader, IDatabaseTester databaseTester, boolean closeConnectionAfterTest)
      Create new instance with specified dataFileLoader and databaseTester.
      Parameters:
      dataFileLoader - Load to use for loading the data files.
      databaseTester - Tester to use for database manipulation.
      closeConnectionAfterTest - Whether or not to close the database connection after each test.
      Since:
      3.4.0
    • DefaultPrepAndExpectedTestCase

      public DefaultPrepAndExpectedTestCase(String name)
      Create new instance with specified test case name.
      Parameters:
      name - The test case name.
  • Method Details

    • newDatabaseTester

      public IDatabaseTester newDatabaseTester() throws Exception
      Creates a new IDatabaseTester. Default implementation returns a PropertiesBasedJdbcDatabaseTester. This implementation returns the databaseTester set by the test.
      Overrides:
      newDatabaseTester in class DBTestCase
      Returns:
      the newly created database tester.
      Throws:
      Exception - if the database tester cannot be created.
    • getDataSet

      public IDataSet getDataSet() throws Exception
      Returns the test dataset. Returns the prep dataset.
      Specified by:
      getDataSet in class DatabaseTestCase
      Returns:
      the test dataset.
      Throws:
      Exception - if the dataset cannot be retrieved or created.
    • configureTest

      public void configureTest(VerifyTableDefinition[] verifyTableDefinitions, String[] prepDataFiles, String[] expectedDataFiles) throws Exception
      Configure the test. Call this method before performing the test steps.

      Executes against the connection shared with setupData(), verifyData() and cleanupData() for this test's lifecycle rather than a fresh one, and leaves it open; cleanupData() closes it. See #800.

      Specified by:
      configureTest in interface PrepAndExpectedTestCase
      Parameters:
      verifyTableDefinitions - Table definitions to verify after test execution.
      prepDataFiles - The prep data files (as classpath resources) to load and insert contents into the database as seed data.
      expectedDataFiles - The expected data files (as classpath resources) to load as expected data and verify actual data matches at test end.
      Throws:
      Exception - if the test cannot be configured.
    • preTest

      public void preTest() throws Exception
      Execute pre-test steps. Call this method before performing the test steps.
      Specified by:
      preTest in interface PrepAndExpectedTestCase
      Throws:
      Exception - if the pre-test steps fail.
    • preTest

      public void preTest(VerifyTableDefinition[] tables, String[] prepDataFiles, String[] expectedDataFiles) throws Exception
      Convenience method to call configureTest() and preTest().
      Specified by:
      preTest in interface PrepAndExpectedTestCase
      Parameters:
      tables - Table definitions to verify after test execution.
      prepDataFiles - The prep data files (as classpath resources) to load and insert contents into the database as seed data.
      expectedDataFiles - The expected data files (as classpath resources) to load as expected data and verify actual data matches at test end.
      Throws:
      Exception - if the pre-test steps fail.
    • runTest

      public Object runTest(VerifyTableDefinition[] verifyTables, String[] prepDataFiles, String[] expectedDataFiles, PrepAndExpectedTestCaseSteps testSteps) throws Exception
      Run the DbUnit test.
      Specified by:
      runTest in interface PrepAndExpectedTestCase
      Parameters:
      verifyTables - Table definitions to verify after test execution.
      prepDataFiles - The prep data files (as classpath resources) to load and insert contents into the database as seed data.
      expectedDataFiles - The expected data files (as classpath resources) to load as expected data and verify actual data matches at test end.
      testSteps - The test steps to run.
      Returns:
      User defined object from running the test steps.
      Throws:
      Exception - if the test steps fail.
    • runTestSteps

      protected Object runTestSteps(PrepAndExpectedTestCaseSteps testSteps) throws Exception
      Run the provided test steps. Override as necessary for custom logic.
      Parameters:
      testSteps - The test steps to run.
      Returns:
      the user-defined object returned by the test steps.
      Throws:
      Exception - if the test steps fail.
    • postTest

      public void postTest() throws Exception
      Execute all post-test steps. Call this method after performing the test steps.
      Specified by:
      postTest in interface PrepAndExpectedTestCase
      Throws:
      Exception - if the post-test steps fail.
    • postTest

      public void postTest(boolean verifyData) throws Exception
      Execute post-test steps. Call this method after performing the test steps.
      Specified by:
      postTest in interface PrepAndExpectedTestCase
      Parameters:
      verifyData - Specify true to perform verify data steps, false to not. Useful to specify false when test has failure in progress (e.g. an exception) and verifying data would fail, masking original test failure.
      Throws:
      Exception - if the post-test steps fail.
    • cleanupData

      public void cleanupData() throws Exception
      Cleanup tables specified in prep and expected datasets, using the provided databaseTester. See IDatabaseTester.onTearDown().

      Runs the tear down operation against the connection shared with setupData() and verifyData() for this test's lifecycle, then closes it. See #800.

      Specified by:
      cleanupData in interface PrepAndExpectedTestCase
      Throws:
      Exception - if cleaning up the data fails.
    • tearDown

      protected void tearDown() throws Exception
      Legacy JUnit-3-era tear-down hook. Not invoked automatically under JUnit 5; kept for subclasses that drive the lifecycle manually. Calling it after a full runTest(org.dbunit.VerifyTableDefinition[], java.lang.String[], java.lang.String[], org.dbunit.PrepAndExpectedTestCaseSteps) or postTest() cycle cleans up a second time (parent tearDown() re-runs the tear down operation on the prep dataset with a fresh connection).
      Overrides:
      tearDown in class DatabaseTestCase
      Throws:
      Exception - if the tear-down operation fails.
    • setupData

      public void setupData() throws Exception
      Use the provided databaseTester to prep the database with the provided prep dataset. See IDatabaseTester.onSetup().

      Executes against the connection shared with verifyData() and cleanupData() for this test's lifecycle rather than a fresh one, and leaves it open; cleanupData() closes it. See #800.

      Throws:
      Exception - if preparing the data fails.
    • getSetUpOperation

      protected DatabaseOperation getSetUpOperation() throws Exception
      Description copied from class: DatabaseTestCase
      Returns the database operation executed in test setup.
      Overrides:
      getSetUpOperation in class DatabaseTestCase
      Returns:
      the database operation executed in test setup.
      Throws:
      Exception - if the operation cannot be determined.
    • getTearDownOperation

      protected DatabaseOperation getTearDownOperation() throws Exception
      Description copied from class: DatabaseTestCase
      Returns the database operation executed in test cleanup.
      Overrides:
      getTearDownOperation in class DatabaseTestCase
      Returns:
      the database operation executed in test cleanup.
      Throws:
      Exception - if the operation cannot be determined.
    • verifyData

      public void verifyData() throws Exception
      For the provided VerifyTableDefinitions, verify each table's actual results are as expected. Uses the connection from the provided databaseTester, shared with setupData() and cleanupData() for this test's lifecycle. Left open on return; cleanupData() closes it. See #800.
      Specified by:
      verifyData in interface PrepAndExpectedTestCase
      Throws:
      Exception - if verifying the data fails.
    • verifyData

      protected void verifyData(IDatabaseConnection connection, VerifyTableDefinition verifyTableDefinition) throws Exception
      Verifies a single table's actual data against its expected data.
      Parameters:
      connection - the database connection to load the actual data from.
      verifyTableDefinition - the table definition to verify.
      Throws:
      Exception - if verifying the table fails.
    • loadTableDataFromDataSet

      public ITable loadTableDataFromDataSet(String tableName) throws DataSetException
      Loads the given table's expected data from the expected dataset.
      Parameters:
      tableName - the name of the table to load.
      Returns:
      the table's expected data.
      Throws:
      DataSetException - if loading the table fails.
    • loadTableDataFromDatabase

      public ITable loadTableDataFromDatabase(String tableName, IDatabaseConnection connection) throws Exception
      Loads the given table's actual data from the database.
      Parameters:
      tableName - the name of the table to load.
      connection - the database connection to load the data from.
      Returns:
      the table's actual data.
      Throws:
      Exception - if loading the table fails.
    • verifyData

      protected void verifyData(ITable expectedTable, ITable actualTable, String[] excludeColumns, String[] includeColumns, ValueComparer defaultValueComparer, Map<String,ValueComparer> columnValueComparers) throws DatabaseUnitException
      For the specified expected and actual tables (and excluding and including the specified columns), verify the actual data is as expected.
      Parameters:
      expectedTable - The expected table to compare the actual table to.
      actualTable - The actual table to compare to the expected table.
      excludeColumns - The column names to exclude from comparison. See DefaultColumnFilter.excludeColumn(String) .
      includeColumns - The column names to only include in comparison. See DefaultColumnFilter.includeColumn(String) .
      defaultValueComparer - ValueComparer to use with column value comparisons when the column name for the table is not in the columnValueComparers Map. Can be null and will default.
      columnValueComparers - Map of ValueComparers to use for specific columns. Key is column name, value is the ValueComparer. Can be null and will default to defaultValueComparer for all columns in all tables.
      Throws:
      DatabaseUnitException - if the tables' row counts, columns, or data do not match.
      See Also:
    • verifyData

      protected void verifyData(ITable expectedTable, ITable actualTable, String[] excludeColumns, String[] includeColumns, ValueComparer defaultValueComparer, Map<String,ValueComparer> columnValueComparers, boolean sortOnFilteredColumnsOnly) throws DatabaseUnitException
      For the specified expected and actual tables (and excluding and including the specified columns), verify the actual data is as expected.
      Parameters:
      expectedTable - The expected table to compare the actual table to.
      actualTable - The actual table to compare to the expected table.
      excludeColumns - The column names to exclude from comparison. See DefaultColumnFilter.excludeColumn(String) .
      includeColumns - The column names to only include in comparison. See DefaultColumnFilter.includeColumn(String) .
      defaultValueComparer - ValueComparer to use with column value comparisons when the column name for the table is not in the columnValueComparers Map. Can be null and will default.
      columnValueComparers - Map of ValueComparers to use for specific columns. Key is column name, value is the ValueComparer. Can be null and will default to defaultValueComparer for all columns in all tables.
      sortOnFilteredColumnsOnly - True to sort the expected and actual tables by only the columns that survive excludeColumns/includeColumns, instead of by all of the actual table's native columns; see VerifyTableDefinition.isSortOnFilteredColumnsOnly().
      Throws:
      DatabaseUnitException - if the tables' row counts, columns, or data do not match.
      Since:
      3.5.0
    • compareData

      protected void compareData(ITable expectedTable, ITable actualTable, Column[] additionalColumnInfo, ValueComparer defaultValueComparer, Map<String,ValueComparer> columnValueComparers) throws DatabaseUnitException
      Compare the tables, enables easy overriding.

      Uses failureHandler when set; otherwise defers to Assertion.assertWithValueComparer(ITable, ITable, Column[], ValueComparer, Map)'s own DefaultFailureHandler, configured with additionalColumnInfo.

      Parameters:
      expectedTable - the table containing all expected results.
      actualTable - the table containing all actual results.
      additionalColumnInfo - the additional columns to include in failure messages.
      defaultValueComparer - the value comparer used when no more specific comparer is configured.
      columnValueComparers - the per-column value comparers to use.
      Throws:
      DatabaseUnitException - if the tables' row counts, columns, or data do not match.
    • makeAdditionalColumnInfo

      protected Column[] makeAdditionalColumnInfo(ITable expectedTable, String[] excludeColumns) throws DataSetException
      Don't add excluded columns to additionalColumnInfo as they are not found and generate a not found message in the fail message.
      Parameters:
      expectedTable - Not null.
      excludeColumns - Nullable.
      Returns:
      the additional column info, excluding excludeColumns.
      Throws:
      DataSetException - if the expected table's columns cannot be retrieved.
    • makeAdditionalColumnInfo

      protected Column[] makeAdditionalColumnInfo(String[] excludeColumns, Column[] allColumns)
      Don't add excluded columns to additionalColumnInfo as they are not found and generate a not found message in the fail message.
      Parameters:
      excludeColumns - Not null.
      allColumns - Not null.
      Returns:
      the additional column info, excluding excludeColumns.
    • makeCompositeDataSet

      public IDataSet makeCompositeDataSet(String[] dataFiles, String dataFilesName) throws DataSetException
      Make a IDataSet from the specified files with case sensitive table names as false.
      Parameters:
      dataFiles - Represents the array of dbUnit data files.
      dataFilesName - Concept name of the data files, e.g. prep, expected.
      Returns:
      The composite dataset.
      Throws:
      DataSetException - On dbUnit errors.
    • makeCompositeDataSet

      public IDataSet makeCompositeDataSet(String[] dataFiles, String dataFilesName, boolean isCaseSensitiveTableNames) throws DataSetException
      Make a IDataSet from the specified files.
      Parameters:
      dataFiles - Represents the array of dbUnit data files.
      dataFilesName - Concept name of the data files, e.g. prep, expected.
      isCaseSensitiveTableNames - true if case sensitive table names is on.
      Returns:
      The composite dataset.
      Throws:
      DataSetException - On dbUnit errors.
    • applyColumnFilters

      public ITable applyColumnFilters(ITable table, String[] excludeColumns, String[] includeColumns) throws DataSetException
      Apply the specified exclude and include column filters to the specified table.
      Parameters:
      table - The table to apply the filters to.
      excludeColumns - The exclude filters; use null or empty array to mean exclude none.
      includeColumns - The include filters; use null to mean include all.
      Returns:
      The filtered table.
      Throws:
      DataSetException - if applying the filters fails.
    • getPrepDataset

      public IDataSet getPrepDataset()
      Get the prep dataset, created from the prepDataFiles.
      Specified by:
      getPrepDataset in interface PrepAndExpectedTestCase
      Returns:
      The prep dataset.
    • getExpectedDataset

      public IDataSet getExpectedDataset()
      Get the expected dataset, created from the expectedDataFiles.
      Specified by:
      getExpectedDataset in interface PrepAndExpectedTestCase
      Returns:
      The expected dataset.
    • getDatabaseTester

      public IDatabaseTester getDatabaseTester()
      Get the databaseTester.
      Overrides:
      getDatabaseTester in class DatabaseTestCase
      Returns:
      The databaseTester.
      See Also:
      • databaseTester
    • setDatabaseTester

      public void setDatabaseTester(IDatabaseTester databaseTester)
      Set the databaseTester.
      Parameters:
      databaseTester - The databaseTester to set.
      See Also:
      • databaseTester
    • isCloseConnectionAfterTest

      public boolean isCloseConnectionAfterTest()
      Get whether the connection lookupFeatureValue() and cleanupData() are done with is closed.
      Returns:
      True if it is closed, false if not.
      Since:
      3.4.0
      See Also:
      • closeConnectionAfterTest
    • setCloseConnectionAfterTest

      public void setCloseConnectionAfterTest(boolean closeConnectionAfterTest)
      Set whether the connection lookupFeatureValue() and cleanupData() are done with is closed. Default is true. Set to false when databaseTester shares a CachingConnectionProvider across test methods, so this instance does not close a connection other tests still expect to reuse.
      Parameters:
      closeConnectionAfterTest - True to close it, false to leave it open.
      Since:
      3.4.0
      See Also:
      • closeConnectionAfterTest
    • getDataFileLoader

      public DataFileLoader getDataFileLoader()
      Get the dataFileLoader.
      Returns:
      The dataFileLoader.
      See Also:
      • dataFileLoader
    • setDataFileLoader

      public void setDataFileLoader(DataFileLoader dataFileLoader)
      Set the dataFileLoader.
      Parameters:
      dataFileLoader - The dataFileLoader to set.
      See Also:
      • dataFileLoader
    • setPrepDs

      public void setPrepDs(IDataSet prepDataSet)
      Set the prepDs.
      Parameters:
      prepDataSet - The prepDs to set.
      See Also:
      • prepDataSet
    • setExpectedDs

      public void setExpectedDs(IDataSet expectedDataSet)
      Set the expectedDs.
      Parameters:
      expectedDataSet - The expectedDs to set.
      See Also:
      • expectedDataSet
    • getVerifyTableDefs

      public VerifyTableDefinition[] getVerifyTableDefs()
      Get the tableDefs.
      Returns:
      The tableDefs.
      See Also:
      • verifyTableDefs
    • setVerifyTableDefs

      public void setVerifyTableDefs(VerifyTableDefinition[] verifyTableDefs)
      Set the tableDefs.
      Parameters:
      verifyTableDefs - The tableDefs to set.
      See Also:
      • verifyTableDefs
    • getExpectedDataSetAndVerifyTableDefinitionVerifier

      public ExpectedDataSetAndVerifyTableDefinitionVerifier getExpectedDataSetAndVerifyTableDefinitionVerifier()
      Returns the verifier used to check that verify table definitions and the expected dataset agree.
      Returns:
      the verifier used to check that verify table definitions and the expected dataset agree.
    • setExpectedDataSetAndVerifyTableDefinitionVerifier

      public void setExpectedDataSetAndVerifyTableDefinitionVerifier(ExpectedDataSetAndVerifyTableDefinitionVerifier expectedDataSetAndVerifyTableDefinitionVerifier)
      Sets the verifier used to check that verify table definitions and the expected dataset agree.
      Parameters:
      expectedDataSetAndVerifyTableDefinitionVerifier - the verifier to use.
    • getFailureHandler

      public FailureHandler getFailureHandler()
      Get the failureHandler.
      Returns:
      The failureHandler.
      Since:
      3.5.0
      See Also:
      • failureHandler
    • setFailureHandler

      public void setFailureHandler(FailureHandler failureHandler)
      Set the failureHandler.
      Parameters:
      failureHandler - The failureHandler to set.
      Since:
      3.5.0
      See Also:
      • failureHandler