Skip to content

link-intersystems/dbunit-extensions

Repository files navigation

Java CI with Maven Coverage Status Maven Central Open Issues

DBUnit compatibility

The dbunit compatibility tests, test this library against dbunit versions

Maven Central to Maven Central

DBUnit Compatibility Test

A module that provides support for data set migration.

void migrate() throws DataSetException, IOException {
DataSetMigration dataSetMigration = new DataSetMigration();
dataSetMigration.setDataSetProducer(sourceDataSet);
dataSetMigration.setFlatXmlConsumer("target/migrated-source-data-set.xml");
DatabaseContainerSupport containerSupport = DatabaseContainerSupport.getDatabaseContainerSupport("postgres:latest");
dataSetMigration.setMigrationDataSetTransformerFactory(new TestcontainersMigrationDataSetPipeFactory(containerSupport));
FlywayMigrationConfig migrationConfig = createConfig();
dataSetMigration.setDatabaseMigrationSupport(new FlywayDatabaseMigrationSupport(migrationConfig));
dataSetMigration.exec();
}

The stream module provides a lot of utility classes for dbunit stream handling.

It also contains the stream.commands package that provides a high level api for common dbunit tasks.

Connection sourceConnection = ...; // java.sql.Connection
DatabaseConnection databaseConnection = new DatabaseConnection(sourceConnection);
DatabaseDataSet databaseDataSet = new DatabaseDataSet(databaseConnection, false);

DataSetCommand migrateCommand = new DataSetCommand(databaseDataSet);
migrateCommand.setTables("actor", "film_actor", "film");
migrateCommand.setTableOrder(new DatabaseTableOrder(databaseConnection));
migrateCommand.setResultDecorator(ds -> new ConsistentDatabaseDataSet(databaseConnection, ds));

migrateCommand.setCsvConsumer("target/export/csv");

migrateCommand.exec();

For details take a look at lis-dbunit-dataset

TableBrowser

The TableBrowser can be used to extract a dataset based on a kind of extract description. This description can be build using a domain-specific language. E.g.

void browse(Connection sakilaConnection) throws DatabaseUnitException {
BrowseTable filmActor = new BrowseTable("film_actor");
filmActor.with("film_id").eq(1);
BrowseTable actor = filmActor.browse("actor").natural();
actor.with("first_name").like("P%");
BrowseTable film = filmActor.browse("film").natural();
film.browse("language").on("language_id").references("language_id");
TableBrowser tableBrowser = new TableBrowser(new DatabaseConnection(sakilaConnection));
IDataSet dataSet = tableBrowser.browse(filmActor);
}

ConsistentDataSetLoader

The ConsistentDataSetLoader is another option to load consistent datasets. You can pass it an SQL-Select statement.

void consistentLoad(Connection sakilaConnection) throws DatabaseUnitException {
ConsistentDataSetLoader dataSetLoader = new ConsistentDataSetLoader(new DatabaseConnection(sakilaConnection));
IDataSet dataSet = dataSetLoader.load("SELECT * from film_actor where film_actor.film_id = ?", 1);
String[] tableNames = dataSet.getTableNames();
assertArrayEquals(new String[]{"film_actor", "film", "actor", "language"}, tableNames);
ITable filmActorTable = dataSet.getTable("film_actor");
assertEquals(1, filmActorTable.getRowCount(), "film_actor entity count");
TableUtil filmActorUtil = new TableUtil(filmActorTable);
assertNotNull(filmActorUtil.getRowById(1, 1));
ITable actorTable = dataSet.getTable("actor");
assertEquals(1, actorTable.getRowCount(), "actor entity count");
TableUtil actorUtil = new TableUtil(actorTable);
assertNotNull(actorUtil.getRowById(1));
ITable filmTable = dataSet.getTable("film");
assertEquals(1, filmTable.getRowCount(), "film entity count");
TableUtil filmUtil = new TableUtil(filmTable);
assertNotNull(filmUtil.getRowById(1));
ITable languageTable = dataSet.getTable("language");
assertEquals(1, languageTable.getRowCount(), "language entity count");
TableUtil languageUtil = new TableUtil(languageTable);
assertNotNull(languageUtil.getRowById(1));
}

lis-dbunit-table

Provides extensions for dealing with dbunit tables.

  • MergedTable: Merged two or more tables by the primary key definition so that the result table's rows are distinct.

  • TableList: A list of tables that can be packed to make the tables unique using MergedTables.

  • CellRowFilter: Filters rows based on a column predicate.

  • ColumnList: Provides filter and query methods for a list of columns.

  • ColumnPredicates: Convenience class that provides reusable predicates that can be applied to columns.

  • TableUtil: Convenience methods for table related queries.

      ITable table = ...;
      TableUtil tableUtil = new TableUtil(table);
    
      Row row = tableUtil.getRowById(321);
    
      for (Object cellValua : row) {
          System.out.println(cellValue)
      }
    

lis-dbunit-beans

Provides Java Beans support for IDataSets which allows you to define a data set from a collection of beans. Since Java Beans are based on Java classes you can use refactoring tools to reflect database schema changes.

List<BeanList<?>> beanLists = new ArrayList<>();

BeanList<EmployeeBean> employeeBeans = new BeanList<>(EmployeeBean.class, asList(EmployeeBean.king(), EmployeeBean.blake()));
beanLists.add(employeeBeans);

BeanList<DepartmentBean> departmentBeans = new BeanList<>(DepartmentBean.class, asList(DepartmentBean.accounting(), DepartmentBean.sales(), DepartmentBean.research()));
beanLists.add(departmentBeans);

beanDataSet = new BeanDataSet(beanLists);

The ITableMetaData for bean based data sets is determined using a BeanTableMetaDataProvider. The DefaultBeanTableMetaDataProvider can be customized by setting a PropertyConversion. The PropertyConversion is responsible for converting bean properties to database types and vice versa. The DefaultPropertyConversion uses the DefaultDataTypeRegistry and the DefaultPropertyTypeRegistry to convert property values.

lis-dbunit-sql

Provides support for generating sql insert scripts from a IDataSet.

// Instantiate a SqlDialect 
// Either from 
// - com.link-intersystems.commons:lis-commons-sql 
// or 
// - com.link-intersystems.commons:lis-commons-sql-hibernate
SqlDialect sqlDialect = new DefaultSqlDialect();

// Create a SqlStatementWriter that generates the sql script.
Writer writer = ....; // from java.io
SqlStatementWriter sqlStatementWriter = new SqlStatementWriter(sqlDialect, writer);

// Configure the output format if needed
SqlFormatSettings sqlFormatSettings = new SqlFormatSettings();
sqlStatementWriter.setSqlFormatSettings(sqlFormatSettings);

IDataSet dataSet = ...; // A dbunit data set to export.
DataSetProducerAdapter dataSetProducerAdapter = new DataSetProducerAdapter(dataSet);
dataSetProducerAdapter.setConsumer(sqlStatementWriter);
dataSetProducerAdapter.produce();

About

Extensions for the dbunit testing library.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages