Java Code Examples for org.dbunit.dataset.IDataSet#getTable()

The following examples show how to use org.dbunit.dataset.IDataSet#getTable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KylinTestBase.java    From kylin with Apache License 2.0 6 votes vote down vote up
protected void verifyResultContent(String queryFolder) throws Exception {
    logger.info("---------- verify result content in folder: " + queryFolder);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        String sql = getTextFromFile(sqlFile);

        File expectResultFile = new File(sqlFile.getParent(), sqlFile.getName() + ".expected.xml");
        IDataSet expect = new FlatXmlDataSetBuilder().build(expectResultFile);
        // Get expected table named "expect". FIXME Only support default table name
        ITable expectTable = expect.getTable("expect");

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql, false);

        // compare the result
        assertTableEquals(expectTable, kylinTable);
    }
}
 
Example 2
Source File: OldSchoolDbUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenDataSet_whenUpdateWithNoProduced_thenItemHasNewName() throws Exception {
    try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
        .getResourceAsStream("dbunit/items_exp_rename_no_produced.xml")) {
        // given
        ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
        expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[] { "produced" });

        // when
        connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");

        // then
        IDataSet databaseDataSet = tester.getConnection().createDataSet();
        ITable actualTable = databaseDataSet.getTable("ITEMS");
        actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
        assertEquals(expectedTable, actualTable);
    }
}
 
Example 3
Source File: OldSchoolDbUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenDataSet_whenUpdate_thenItemHasNewName() throws Exception {
    try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
        .getResourceAsStream("dbunit/items_exp_rename.xml")) {
        // given
        ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");

        // when
        connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");

        // then
        IDataSet databaseDataSet = tester.getConnection().createDataSet();
        ITable actualTable = databaseDataSet.getTable("ITEMS");
        assertEquals(expectedTable, actualTable);
    }
}
 
Example 4
Source File: OldSchoolDbUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenDataSet_whenProductIgnoredAndDelete_thenItemIsRemoved() throws Exception {
    try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
        .getResourceAsStream("dbunit/items_exp_delete_no_produced.xml")) {
        // given
        ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");

        // when
        connection.createStatement().executeUpdate("delete from ITEMS where id = 2");

        // then
        IDataSet databaseDataSet = tester.getConnection().createDataSet();
        ITable actualTable = databaseDataSet.getTable("ITEMS");
        actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
        assertEquals(expectedTable, actualTable);
    }
}
 
Example 5
Source File: OldSchoolDbUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenDataSet_whenDelete_thenItemIsRemoved() throws Exception {
    try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
        .getResourceAsStream("dbunit/items_exp_delete.xml")) {
        // given
        ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");

        // when
        connection.createStatement().executeUpdate("delete from ITEMS where id = 2");

        // then
        IDataSet databaseDataSet = tester.getConnection().createDataSet();
        ITable actualTable = databaseDataSet.getTable("ITEMS");
        assertEquals(expectedTable, actualTable);
    }
}
 
Example 6
Source File: DataSourceDBUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenDataSet_whenInsertUnexpectedData_thenFail() throws Exception {
    try (InputStream is = getClass().getClassLoader()
        .getResourceAsStream("dbunit/expected-multiple-failures.xml")) {

        // given
        IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
        ITable expectedTable = expectedDataSet.getTable("ITEMS");
        Connection conn = getDataSource().getConnection();
        DiffCollectingFailureHandler collectingHandler = new DiffCollectingFailureHandler();

        // when
        conn.createStatement().executeUpdate("INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')");
        ITable actualData = getConnection().createDataSet().getTable("ITEMS");

        // then
        Assertion.assertEquals(expectedTable, actualData, collectingHandler);
        if (!collectingHandler.getDiffList().isEmpty()) {
            String message = (String) collectingHandler.getDiffList().stream()
                .map(d -> formatDifference((Difference) d)).collect(joining("\n"));
            logger.error(() -> message);
        }
    }
}
 
Example 7
Source File: DataSourceDBUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenDataSet_whenDelete_thenItemIsDeleted() throws Exception {
    try (InputStream is = DataSourceDBUnitTest.class.getClassLoader()
        .getResourceAsStream("dbunit/items_exp_delete.xml")) {
        // given
        ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS");

        // when
        connection.createStatement().executeUpdate("delete from ITEMS where id = 2");

        // then
        IDataSet databaseDataSet = getConnection().createDataSet();
        ITable actualTable = databaseDataSet.getTable("ITEMS");
        Assertion.assertEquals(expectedTable, actualTable);
    }
}
 
Example 8
Source File: DataSourceDBUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenDataSet_whenInsert_thenTableHasNewClient() throws Exception {
    try (InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-user.xml")) {
        // given
        IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
        ITable expectedTable = expectedDataSet.getTable("CLIENTS");
        Connection conn = getDataSource().getConnection();

        // when
        conn.createStatement()
            .executeUpdate("INSERT INTO CLIENTS (first_name, last_name) VALUES ('John', 'Jansen')");
        ITable actualData = getConnection()
            .createQueryTable("result_name", "SELECT * FROM CLIENTS WHERE last_name='Jansen'");

        // then
        assertEqualsIgnoreCols(expectedTable, actualData, new String[] { "id" });
    }
}
 
Example 9
Source File: DataSetLoaderProviderTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testCsvLoaderLoadUsingProperResource() throws Exception {
    // WHEN
    final DataSetLoader<IDataSet> loader = LOADER_PROVIDER.csvLoader();

    // THEN
    assertThat(loader, notNullValue());

    // WHEN
    final IDataSet dataSet = loader.load(getFile("test-data"));

    // THEN
    assertThat(dataSet, notNullValue());

    final List<String> tableNames = Arrays.asList(dataSet.getTableNames());
    assertThat(tableNames.size(), equalTo(2));
    assertThat(tableNames, hasItems("CSV_TABLE_1", "CSV_TABLE_2"));

    final ITable table1 = dataSet.getTable("CSV_TABLE_1");
    assertThat(table1.getRowCount(), equalTo(3));

    final ITable table2 = dataSet.getTable("CSV_TABLE_2");
    assertThat(table2.getRowCount(), equalTo(1));
}
 
Example 10
Source File: DataSetLoaderProviderTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore("Somehow the given zip file (xlsx) is invalid, even Excel opens it without issues")
public void testXlsLoaderLoadUsingProperResource() throws Exception {
    // WHEN
    final DataSetLoader<IDataSet> loader = LOADER_PROVIDER.xlsLoader();

    // THEN
    assertThat(loader, notNullValue());

    // WHEN
    final IDataSet dataSet = loader.load(getFile("test-data.xls"));

    // THEN
    assertThat(dataSet, notNullValue());

    final List<String> tableNames = Arrays.asList(dataSet.getTableNames());
    assertThat(tableNames.size(), equalTo(2));
    assertThat(tableNames, hasItems("XLS_TABLE_1", "XLS_TABLE_2"));

    final ITable table1 = dataSet.getTable("XLS_TABLE_1");
    assertThat(table1.getRowCount(), equalTo(3));

    final ITable table2 = dataSet.getTable("XLS_TABLE_2");
    assertThat(table2.getRowCount(), equalTo(1));
}
 
Example 11
Source File: DataSetLoaderProviderTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore("Somehow the given zip file (xlsx) is invalid, even Excel opens it without issues")
public void testXlsxLoaderLoadUsingProperResource() throws Exception {
    // WHEN
    final DataSetLoader<IDataSet> loader = LOADER_PROVIDER.xlsLoader();

    // THEN
    assertThat(loader, notNullValue());

    // WHEN
    final IDataSet dataSet = loader.load(getFile("test-data.xlsx"));

    // THEN
    assertThat(dataSet, notNullValue());

    final List<String> tableNames = Arrays.asList(dataSet.getTableNames());
    assertThat(tableNames.size(), equalTo(2));
    assertThat(tableNames, hasItems("XLS_TABLE_1", "XLS_TABLE_2"));

    final ITable table1 = dataSet.getTable("XLS_TABLE_1");
    assertThat(table1.getRowCount(), equalTo(3));

    final ITable table2 = dataSet.getTable("XLS_TABLE_2");
    assertThat(table2.getRowCount(), equalTo(1));
}
 
Example 12
Source File: DataSetLoaderProviderTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testXmlLoaderLoadUsingProperResource() throws Exception {
    // WHEN
    final DataSetLoader<IDataSet> loader = LOADER_PROVIDER.xmlLoader();

    // THEN
    assertThat(loader, notNullValue());

    // WHEN
    final IDataSet dataSet = loader.load(getFile("test-data.xml"));

    // THEN
    assertThat(dataSet, notNullValue());

    final List<String> tableNames = Arrays.asList(dataSet.getTableNames());
    assertThat(tableNames.size(), equalTo(2));
    assertThat(tableNames, hasItems("XML_TABLE_1", "XML_TABLE_2"));

    final ITable table1 = dataSet.getTable("XML_TABLE_1");
    assertThat(table1.getRowCount(), equalTo(3));

    final ITable table2 = dataSet.getTable("XML_TABLE_2");
    assertThat(table2.getRowCount(), equalTo(1));
}
 
Example 13
Source File: DataSetLoaderProviderTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testYamlLoaderLoadUsingProperResource() throws Exception {
    // WHEN
    final DataSetLoader<IDataSet> loader = LOADER_PROVIDER.yamlLoader();

    // THEN
    assertThat(loader, notNullValue());

    // WHEN
    final IDataSet dataSet = loader.load(getFile("test-data.yaml"));

    // THEN
    assertThat(dataSet, notNullValue());

    final List<String> tableNames = Arrays.asList(dataSet.getTableNames());
    assertThat(tableNames.size(), equalTo(2));
    assertThat(tableNames, hasItems("YAML_TABLE_1", "YAML_TABLE_2"));

    final ITable table1 = dataSet.getTable("YAML_TABLE_1");
    assertThat(table1.getRowCount(), equalTo(3));

    final ITable table2 = dataSet.getTable("YAML_TABLE_2");
    assertThat(table2.getRowCount(), equalTo(1));
}
 
Example 14
Source File: DataSetLoaderProviderTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonLoaderLoadUsingProperResource() throws Exception {
    // WHEN
    final DataSetLoader<IDataSet> loader = LOADER_PROVIDER.jsonLoader();

    // THEN
    assertThat(loader, notNullValue());

    // WHEN
    final IDataSet dataSet = loader.load(getFile("test-data.json"));

    // THEN
    assertThat(dataSet, notNullValue());

    final List<String> tableNames = Arrays.asList(dataSet.getTableNames());
    assertThat(tableNames.size(), equalTo(2));
    assertThat(tableNames, hasItems("JSON_TABLE_1", "JSON_TABLE_2"));

    final ITable table1 = dataSet.getTable("JSON_TABLE_1");
    assertThat(table1.getRowCount(), equalTo(3));

    final ITable table2 = dataSet.getTable("JSON_TABLE_2");
    assertThat(table2.getRowCount(), equalTo(1));
}
 
Example 15
Source File: KylinTestBase.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
protected void verifyResultContent(String queryFolder) throws Exception {
    logger.info("---------- verify result content in folder: " + queryFolder);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        String sql = getTextFromFile(sqlFile);

        File expectResultFile = new File(sqlFile.getParent(), sqlFile.getName() + ".expected.xml");
        IDataSet expect = new FlatXmlDataSetBuilder().build(expectResultFile);
        // Get expected table named "expect". FIXME Only support default table name
        ITable expectTable = expect.getTable("expect");

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql, false);

        // compare the result
        assertTableEquals(expectTable, kylinTable);
    }
}
 
Example 16
Source File: AbstractDBUnitHibernateMemoryTest.java    From livingdoc-confluence with GNU General Public License v3.0 5 votes vote down vote up
protected void assertTableEquals(Class<?> peristentClass, String[] includedFields, IDataSet dataset) throws Exception {
    // Fetch database data after executing your code
    IDataSet databaseDataSet = getConnection().createDataSet();
    ITable actualTable = databaseDataSet.getTable(getTableName(peristentClass));
    ITable actualData =
        DefaultColumnFilter.includedColumnsTable(actualTable, getColumnNames(peristentClass, includedFields));

    // Load expected data from an XML dataset
    ITable expectedTable = dataset.getTable(getTableName(peristentClass));
    ITable expectedData =
        DefaultColumnFilter.includedColumnsTable(expectedTable, getColumnNames(peristentClass, includedFields));

    // Assert actual database table match expected table
    Assertion.assertEquals(expectedData, actualData);
}
 
Example 17
Source File: AbstractDBUnitHibernateMemoryTest.java    From livingdoc-confluence with GNU General Public License v3.0 5 votes vote down vote up
protected void assertTableEmpty(Class<?> peristentClass) throws Exception {
    // Fetch database data after executing your code
    IDataSet databaseDataSet = getConnection().createDataSet();
    ITable actualTable = databaseDataSet.getTable(getTableName(peristentClass));

    assertTrue(actualTable.getRowCount() == 0);
}
 
Example 18
Source File: DatabaseWriterTest.java    From LicenseScout with Apache License 2.0 5 votes vote down vote up
/**
 * @param expectedDataSet
 * @param databaseDataSet
 * @param tablesNamesToCheck
 * @throws DataSetException
 * @throws DatabaseUnitException
 */
private void checkDatabase(final IDataSet expectedDataSet, final IDataSet databaseDataSet,
                           final List<String> tablesNamesToCheck)
        throws DataSetException, DatabaseUnitException {
    final DefaultColumnFilter columnFilter = createColumnFilter();
    for (final String tableName : tablesNamesToCheck) {
        final ITable actualTable = databaseDataSet.getTable(tableName);
        FilteredTableMetaData metaData = new FilteredTableMetaData(actualTable.getTableMetaData(), columnFilter);
        ITable actualTableFiltered = new CompositeTable(metaData, actualTable);
        final ITable expectedTable = expectedDataSet.getTable(tableName);
        Assertion.assertEquals(expectedTable, actualTableFiltered);
    }
}
 
Example 19
Source File: DataSourceDBUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDataSetEmptySchema_whenDataSetCreated_thenTablesAreEqual() throws Exception {
    IDataSet expectedDataSet = getDataSet();
    ITable expectedTable = expectedDataSet.getTable("CLIENTS");
    IDataSet databaseDataSet = getConnection().createDataSet();
    ITable actualTable = databaseDataSet.getTable("CLIENTS");
    Assertion.assertEquals(expectedTable, actualTable);
}
 
Example 20
Source File: DataSourceDBUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDataSet_whenUpdate_thenItemHasNewName() throws Exception {
    try (InputStream is = DataSourceDBUnitTest.class.getClassLoader()
        .getResourceAsStream("dbunit/items_exp_rename.xml")) {
        // given
        ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS");

        connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");

        IDataSet databaseDataSet = getConnection().createDataSet();
        ITable actualTable = databaseDataSet.getTable("ITEMS");

        Assertion.assertEquals(expectedTable, actualTable);
    }
}