Java Code Examples for org.dbunit.database.DatabaseConnection

The following examples show how to use org.dbunit.database.DatabaseConnection. These examples are extracted from open source projects. 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 Project: sharding-jdbc-1.5.1   Source File: DBUnitUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static IDatabaseConnection getConnection(final DataBaseEnvironment dbEnv, final Connection connection) throws DatabaseUnitException {
    switch (dbEnv.getDatabaseType()) {
        case H2:
            return new H2Connection(connection, "PUBLIC");
        case MySQL:
            return new MySqlConnection(connection, null);
        case PostgreSQL:
            DatabaseConnection databaseConnection = new DatabaseConnection(connection);
            databaseConnection.getConfig().setProperty("http://www.dbunit.org/properties/datatypeFactory", new PostgresqlDataTypeFactory());
            return databaseConnection;
        case Oracle:
            return new OracleConnection(connection, "JDBC");
        case SQLServer:
            return new MsSqlConnection(connection);
        default:
            throw new UnsupportedOperationException(dbEnv.getDatabaseType().name());
    }
}
 
Example 2
Source Project: kylin-on-parquet-v2   Source File: ITKylinQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidQuery() throws Exception {

    logger.info("-------------------- Test Invalid Query --------------------");
    String queryFolder = getQueryFolderPrefix() + "src/test/resources/query/sql_invalid";
    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        logger.info("Testing Query " + queryName);
        String sql = getTextFromFile(sqlFile);
        IDatabaseConnection cubeConn = new DatabaseConnection(cubeConnection);
        try {
            cubeConn.createQueryTable(queryName, sql);
        } catch (Throwable t) {
            continue;
        } finally {
            cubeConn.close();
        }
        throw new IllegalStateException(queryName + " should be error!");
    }
}
 
Example 3
Source Project: kylin-on-parquet-v2   Source File: ITKylinQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testVersionQuery() throws Exception {
    String expectVersion = KylinVersion.getCurrentVersion().toString();
    logger.info("---------- verify expect version: " + expectVersion);

    String queryName = "QueryKylinVersion";
    String sql = "SELECT VERSION() AS version";

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

    // compare the result
    Assert.assertEquals(expectVersion, queriedVersion);
}
 
Example 4
Source Project: kylin-on-parquet-v2   Source File: ITMassInQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
protected void run(String queryFolder, String[] exclusiveQuerys, boolean needSort) throws Exception {
    logger.info("---------- test folder: " + queryFolder);
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

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

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

    }
}
 
Example 5
Source Project: kylin-on-parquet-v2   Source File: KylinTestBase.java    License: 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 6
Source Project: kylin-on-parquet-v2   Source File: KylinTestBase.java    License: Apache License 2.0 6 votes vote down vote up
protected void execAndCompColumnCount(String input, int expectedColumnCount) throws Exception {
    logger.info("---------- test column count: " + input);
    Set<String> sqlSet = ImmutableSet.of(input);

    for (String sql : sqlSet) {
        // execute Kylin
        logger.info("Query Result from Kylin - " + sql);
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, sql, sql, false);

        try {
            // compare the result
            Assert.assertEquals(expectedColumnCount, kylinTable.getTableMetaData().getColumns().length);
        } catch (Throwable t) {
            logger.info("execAndCompColumnCount failed on: " + sql);
            throw t;
        }
    }
}
 
Example 7
Source Project: database-rider   Source File: ExportDataSetIt.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@DataSet("datasets/yml/users.yml")
public void shouldExportDataSetUsingSubSelectToFilterRows() throws SQLException, DatabaseUnitException {
    DataSetExporter.getInstance().export(new DatabaseConnection(emProvider.connection()),
            new DataSetExportConfig().
                    outputFileName("target/querySubselect.yml").
                    //export only users that have tweets
                    queryList(new String[]{"select * from user u where u.id in" +
                            " (select t.user_id from tweet t)"})
    );
    File ymlDataSet = new File("target/querySubselect.yml");
    assertThat(ymlDataSet).exists();
    assertThat(contentOf(ymlDataSet)).
        isEqualTo("user:"+NEW_LINE +
                "  - ID: 1"+NEW_LINE +
                "    NAME: \"@realpestano\""+NEW_LINE+NEW_LINE);
}
 
Example 8
Source Project: database-rider   Source File: ExportNullPropertiesIt.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotExportNullColumnsInYMLDataSet() throws SQLException, DatabaseUnitException {
    DataSetExporter.getInstance().export(new DatabaseConnection(emProvider.connection()), new DataSetExportConfig().outputFileName("target/userWithNullProperty.yml"));
    File ymlDataSet = new File("target/userWithNullProperty.yml");
    assertThat(ymlDataSet).exists();
    assertThat(contentOf(ymlDataSet)).
            contains("FOLLOWER:" + NEW_LINE +
                            "" + NEW_LINE +
                            "SEQUENCE:" + NEW_LINE +
                            "  - SEQ_NAME: \"SEQ_GEN\"" + NEW_LINE +
                            "    SEQ_COUNT: 50" + NEW_LINE +
                            "" + NEW_LINE +
                            "TWEET:" + NEW_LINE +
                            "" + NEW_LINE +
                            "USER:" + NEW_LINE +
                            "  - ID: 1" + NEW_LINE + NEW_LINE
            );
}
 
Example 9
Source Project: database-rider   Source File: ExportNullPropertiesIt.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotExportNullColumnsInJSONDataSet() throws SQLException, DatabaseUnitException{
    DataSetExporter.getInstance().export(new DatabaseConnection(emProvider.connection()), new DataSetExportConfig().
            dataSetFormat(DataSetFormat.JSON).outputFileName("target/userWithNullProperty.json"));
    File jsonDataSet = new File("target/userWithNullProperty.json");
    assertThat(jsonDataSet).exists();
    assertThat(contentOf(jsonDataSet).replaceAll("\r","")).isEqualTo(("{"+NEW_LINE +
            "  \"FOLLOWER\": ["+NEW_LINE +
            "  ],"+NEW_LINE +
            "  \"SEQUENCE\": ["+NEW_LINE +
            "    {"+NEW_LINE +
            "      \"SEQ_NAME\": \"SEQ_GEN\","+NEW_LINE +
            "      \"SEQ_COUNT\": 50"+NEW_LINE +
            "    }"+NEW_LINE +
            "  ],"+NEW_LINE +
            "  \"TWEET\": ["+NEW_LINE +
            "  ],"+NEW_LINE +
            "  \"USER\": ["+NEW_LINE +
            "    {"+NEW_LINE +
            "      \"ID\": 1"+NEW_LINE +
            "    }"+NEW_LINE +
            "  ]"+NEW_LINE +
            "}").replaceAll("\r",""));

}
 
Example 10
Source Project: jpa-unit   Source File: DatabaseConnectionFactory.java    License: Apache License 2.0 6 votes vote down vote up
public static IDatabaseConnection openConnection(final BasicDataSource ds) {
    try {
        final Connection connection = ds.getConnection();

        for (final DbUnitConnectionFactory impl : SERVICE_LOADER) {
            if (impl.supportsDriver(ds.getDriverClassName())) {
                return impl.createConnection(connection, discoverSchema(connection));
            }
        }

        // fall back if no specific implementation is available
        return new DatabaseConnection(connection);
    } catch (final DatabaseUnitException | SQLException e) {
        throw new JpaUnitException(e);
    }
}
 
Example 11
Source Project: jpa-unit   Source File: DatabaseConnectionFactoryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOpenConnectionToSqliteDbHavingAllSupportedPersistenceProperties() throws Exception {
    // This test is about a fall back functionality
    // GIVEN
    final File dbFile = folder.newFile("test.db");

    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(SQLITE_DRIVER_CLASS_PROP_VALUE);
    ds.setUsername(USERNAME_PROP_VALUE);
    ds.setPassword(PASSWORD_PROP_VALUE);
    ds.setUrl(SQLITE_CONNECTION_URL_PROP_PREFIX + dbFile.getAbsolutePath());

    // WHEN
    connection = DatabaseConnectionFactory.openConnection(ds);

    // THEN
    assertThat(connection, notNullValue());
    assertThat(connection.getClass(), equalTo(DatabaseConnection.class));
}
 
Example 12
Source Project: jpa-unit   Source File: DatabaseConnectionFactoryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOpenConnectionToSqliteDbWithoutHavingUsernameAndPasswordProperties() throws Exception {
    // GIVEN
    final File dbFile = folder.newFile("test.db");

    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(SQLITE_DRIVER_CLASS_PROP_VALUE);
    ds.setUrl(SQLITE_CONNECTION_URL_PROP_PREFIX + dbFile.getAbsolutePath());

    // WHEN
    connection = DatabaseConnectionFactory.openConnection(ds);

    // THEN
    assertThat(connection, notNullValue());
    assertThat(connection.getClass(), equalTo(DatabaseConnection.class));
}
 
Example 13
Source Project: jpa-unit   Source File: CleanupStrategyProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    initialDataSet = new FlatXmlDataSetBuilder().build(new File("src/test/resources/test-data.xml"));
    connection = new DatabaseConnection(DriverManager.getConnection(CONNECTION_URL, USER_NAME, PASSWORD));

    final DatabaseOperation operation = DatabaseOperation.CLEAN_INSERT;
    operation.execute(connection, initialDataSet);

    connection.getConnection().createStatement().execute(
            "insert into XML_TABLE_1(id, version, value_1, value_2, value_3, value_4, value_5) values(10, 'Record 10 version', 'Record 10 Value 1', 'Record 10 Value 2', 'Record 10 Value 3', 'Record 10 Value 4', 'Record 10 Value 5');");
    connection.getConnection().commit();

    connection.getConnection().createStatement().execute(
            "merge into XML_TABLE_3(id, version, value_8, value_9) values(11, 'Record 11 version', 'Record 11 Value 8', 'Record 11 Value 9');");
    connection.getConnection().commit();

    assertThat(getRecordCountFromTable(connection, "XML_TABLE_1"), equalTo(4));
    assertThat(getRecordCountFromTable(connection, "XML_TABLE_2"), equalTo(1));
    assertThat(getRecordCountFromTable(connection, "XML_TABLE_3"), equalTo(1));
}
 
Example 14
protected IDatabaseConnection getConnection() throws Exception {
    Properties cfg = getHibernateConfiguration();
    String username = cfg.getProperty("hibernate.connection.username");
    String password = cfg.getProperty("hibernate.connection.password");
    String driver = cfg.getProperty("hibernate.connection.driver_class");
    String url = cfg.getProperty("hibernate.connection.url");

    Class.forName(driver);

    Connection jdbcConnection = DriverManager.getConnection(url, username, password);

    IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
    DatabaseConfig config = connection.getConfig();
    // config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new
    // H2DataTypeFactory());
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());

    return connection;
}
 
Example 15
Source Project: kylin   Source File: ITKylinQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidQuery() throws Exception {

    logger.info("-------------------- Test Invalid Query --------------------");
    String queryFolder = getQueryFolderPrefix() + "src/test/resources/query/sql_invalid";
    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        logger.info("Testing Query " + queryName);
        String sql = getTextFromFile(sqlFile);
        IDatabaseConnection cubeConn = new DatabaseConnection(cubeConnection);
        try {
            cubeConn.createQueryTable(queryName, sql);
        } catch (Throwable t) {
            continue;
        } finally {
            cubeConn.close();
        }
        throw new IllegalStateException(queryName + " should be error!");
    }
}
 
Example 16
Source Project: kylin   Source File: ITKylinQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testVersionQuery() throws Exception {
    String expectVersion = KylinVersion.getCurrentVersion().toString();
    logger.info("---------- verify expect version: " + expectVersion);

    String queryName = "QueryKylinVersion";
    String sql = "SELECT VERSION() AS version";

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

    // compare the result
    Assert.assertEquals(expectVersion, queriedVersion);
}
 
Example 17
Source Project: kylin   Source File: ITMassInQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
protected void run(String queryFolder, String[] exclusiveQuerys, boolean needSort) throws Exception {
    logger.info("---------- test folder: " + queryFolder);
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

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

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

    }
}
 
Example 18
Source Project: kylin   Source File: KylinTestBase.java    License: 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 19
Source Project: kylin   Source File: KylinTestBase.java    License: Apache License 2.0 6 votes vote down vote up
protected void execAndCompColumnCount(String input, int expectedColumnCount) throws Exception {
    logger.info("---------- test column count: " + input);
    Set<String> sqlSet = ImmutableSet.of(input);

    for (String sql : sqlSet) {
        // execute Kylin
        logger.info("Query Result from Kylin - " + sql);
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, sql, sql, false);

        try {
            // compare the result
            Assert.assertEquals(expectedColumnCount, kylinTable.getTableMetaData().getColumns().length);
        } catch (Throwable t) {
            logger.info("execAndCompColumnCount failed on: " + sql);
            throw t;
        }
    }
}
 
Example 20
Source Project: Kylin   Source File: KylinQueryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidQuery() throws Exception {

    printInfo("-------------------- Test Invalid Query --------------------");
    String queryFolder = "src/test/resources/query/sql_invalid";
    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        printInfo("Testing Query " + queryName);
        String sql = getTextFromFile(sqlFile);
        IDatabaseConnection cubeConn = new DatabaseConnection(cubeConnection);
        try {
            cubeConn.createQueryTable(queryName, sql);
        } catch (Throwable t) {
            continue;
        } finally {
            cubeConn.close();
        }
        throw new IllegalStateException(queryName + " should be error!");
    }
}
 
Example 21
Source Project: Kylin   Source File: KylinTestBase.java    License: Apache License 2.0 6 votes vote down vote up
protected void verifyResultRowCount(String queryFolder) throws Exception {
    printInfo("---------- verify result count 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");
        int expectRowCount = Integer.parseInt(Files.readFirstLine(expectResultFile, Charset.defaultCharset()));

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

        // compare the result
        Assert.assertEquals(expectRowCount, kylinTable.getRowCount());
        // Assertion.assertEquals(expectRowCount, kylinTable.getRowCount());
    }
}
 
Example 22
Source Project: kylin-on-parquet-v2   Source File: ITKylinQueryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testSingleExecuteQuery() throws Exception {

    String queryFileName = getQueryFolderPrefix() + "src/test/resources/query/sql/query01.sql";

    File sqlFile = new File(queryFileName);
    String sql = getTextFromFile(sqlFile);
    IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);

    executeQuery(kylinConn, queryFileName, sql, true);
}
 
Example 23
Source Project: kylin-on-parquet-v2   Source File: ITMassInQueryTest.java    License: Apache License 2.0 5 votes vote down vote up
protected void compare(String queryFolder, String[] exclusiveQuerys, boolean needSort) throws Exception {
    logger.info("---------- test folder: " + queryFolder);
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

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

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

        // execute H2
        sql = sql.replace("massin(test_kylin_fact.SELLER_ID,'vip_customers')", "test_kylin_fact.SELLER_ID in ( " + org.apache.commons.lang.StringUtils.join(vipSellers, ",") + ")");
        logger.info("Query Result from H2 - " + queryName);
        logger.info("Query for H2 - " + sql);
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql, needSort);

        try {
            // compare the result
            assertTableEquals(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }
    }
}
 
Example 24
Source Project: kylin-on-parquet-v2   Source File: KylinTestBase.java    License: Apache License 2.0 5 votes vote down vote up
protected void verifyResultRowColCount(String queryFolder) throws Exception {
    logger.info("---------- verify result count 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");
        Pair<Integer, Integer> pair = getExpectedRowAndCol(expectResultFile);
        int expectRowCount = pair.getFirst();
        int expectColCount = pair.getSecond();

        // 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
        if (BackdoorToggles.getPrepareOnly())
            Assert.assertEquals(queryName, 0, kylinTable.getRowCount());
        else if (expectRowCount >= 0)
            Assert.assertEquals(queryName, expectRowCount, kylinTable.getRowCount());

        if (expectColCount >= 0)
            Assert.assertEquals(queryName, expectColCount, kylinTable.getTableMetaData().getColumns().length);
    }
}
 
Example 25
Source Project: kylin-on-parquet-v2   Source File: KylinTestBase.java    License: Apache License 2.0 5 votes vote down vote up
protected void execAndCompResultSize(String queryFolder, String[] exclusiveQuerys, boolean needSort)
        throws Exception {
    logger.info("---------- test folder: " + queryFolder);
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

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

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

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql, needSort);

        try {
            // compare the result
            Assert.assertEquals(h2Table.getRowCount(), kylinTable.getRowCount());
        } catch (Throwable t) {
            logger.info("execAndCompResultSize failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql);
        }
    }
}
 
Example 26
Source Project: kylin-on-parquet-v2   Source File: KylinTestBase.java    License: Apache License 2.0 5 votes vote down vote up
protected void execLimitAndValidate(String queryFolder) throws Exception {
    logger.info("---------- test folder: " + new File(queryFolder).getAbsolutePath());

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

        String sqlWithLimit;
        if (sql.toLowerCase(Locale.ROOT).contains("limit ")) {
            sqlWithLimit = sql;
        } else {
            sqlWithLimit = sql + " limit 5";
            appendLimitQueries++;
        }

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

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql, false);

        try {
            assertTableContains(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql);
        }
    }
    logger.info("Queries appended with limit: " + appendLimitQueries);
}
 
Example 27
Source Project: kylin-on-parquet-v2   Source File: KylinTestBase.java    License: Apache License 2.0 5 votes vote down vote up
protected void execAndCompQuery(String queryFolder, String[] exclusiveQuerys, boolean needSort,
        ICompareQueryTranslator translator) throws Exception {
    logger.info("---------- test folder: " + new File(queryFolder).getAbsolutePath());
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        if (exclusiveSet.contains(queryName)) {
            continue;
        }
        String sql1 = getTextFromFile(sqlFile);
        String sql2 = translator.transform(sqlFile);

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

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        long currentTime = System.currentTimeMillis();
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql2, needSort);
        logger.info("H2 spent " + (System.currentTimeMillis() - currentTime) + " mili-seconds.");

        try {
            // compare the result
            assertTableEquals(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql1);
        }
    }
}
 
Example 28
Source Project: kylin-on-parquet-v2   Source File: KylinTestBase.java    License: Apache License 2.0 5 votes vote down vote up
protected void execAndCompDynamicQuery(String queryFolder, String[] exclusiveQuerys, boolean needSort)
        throws Exception {
    logger.info("---------- test folder: " + queryFolder);
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

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

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

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        ITable h2Table = executeDynamicQuery(newH2Connection(), queryName, sql, parameters, needSort);

        // compare the result
        assertTableEquals(h2Table, kylinTable);
    }
}
 
Example 29
/**
 * Run the migration using the given context.
 *
 * @param context the context to run under
 * @throws MigrationException if an unexpected error occurs
 */
public void migrate(MigrationContext context) throws MigrationException
{
    log.debug("Executing patch " + getLevel());
    // down casting, technically not safe, but everyone else is doing it.
    JdbcMigrationContext jdbcContext = (JdbcMigrationContext) context;
    // used to close connection in finally block
    Connection contextConnection = null;
    try
    {
        FlatXmlDataSet xmlDataSet = new FlatXmlDataSet(getXmlAsStream());
        // Set contextConnection so it can be accessed in the finally block.
        contextConnection = jdbcContext.getConnection();

        // run the data load
        IDatabaseConnection connection = new DatabaseConnection(contextConnection);
        DatabaseOperation.INSERT.execute(connection, xmlDataSet);
        context.commit();

        // Closing here instead of in finally block to keep the signature of this from throwing
        // a SqlException.  Exceptional condition handled in finally block to make sure
        // we don't leak a connection.
        connection.close();
    }
    catch (Exception e)
    {
        log.debug("Unable to patch due to " + e.getMessage());
        context.rollback();
        throw new MigrationException("Unable to patch", e);
    }
    finally
    {
        // Might already be closed if everything worked fine and connection.close was called 
        // above, in that case, calling close again shouldn't do any harm.  However, if an 
        // exception occurred the DBUnit based connection wrapper didn't get closed, so we 
        // catch that case here.
        SqlUtil.close(contextConnection, null, null);
    }
}
 
Example 30
Source Project: database-rider   Source File: RiderDataSource.java    License: Apache License 2.0 5 votes vote down vote up
private void initDBUnitConnection() throws SQLException {
    try {
        dbUnitConnection = new DatabaseConnection(getConnection(), dbUnitConfig.getSchema());
        configDatabaseProperties();
    } catch (DatabaseUnitException e) {
        throw new SQLException(e);
    }
}