org.dbunit.ext.h2.H2Connection Java Examples

The following examples show how to use org.dbunit.ext.h2.H2Connection. 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: DBUnitUtil.java    From sharding-jdbc-1.5.1 with 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 File: DatabaseConnectionFactoryTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testOpenConnectionToH2DbHavingAllSupportedPersistenceProperties() throws ClassNotFoundException {
    // GIVEN
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(H2_DRIVER_CLASS_PROP_VALUE);
    ds.setUsername(USERNAME_PROP_VALUE);
    ds.setPassword(PASSWORD_PROP_VALUE);
    ds.setUrl(H2_CONNECTION_URL_PROP_VALUE);

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

    // THEN
    assertThat(connection, notNullValue());
    assertThat(connection, instanceOf(H2Connection.class));
}
 
Example #3
Source File: Fixtures.java    From base-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 对XML文件中的数据在H2数据库中执行Operation.
 * 
 * @param xmlFilePaths 符合Spring Resource路径格式的文件列表.
 */
private static void execute(DatabaseOperation operation, DataSource dataSource, String... xmlFilePaths)
		throws DatabaseUnitException, SQLException {
	//注意这里HardCode了使用H2的Connetion
	IDatabaseConnection connection = new H2Connection(dataSource.getConnection(), null);

	for (String xmlPath : xmlFilePaths) {
		try {
			InputStream input = resourceLoader.getResource(xmlPath).getInputStream();
			IDataSet dataSet = new FlatXmlDataSetBuilder().setColumnSensing(true).build(input);
			operation.execute(connection, dataSet);
		} catch (IOException e) {
			logger.warn(xmlPath + " file not found", e);
		}finally{
			connection.close();
		}
	}
}
 
Example #4
Source File: KylinTestBase.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
protected static H2Connection newH2Connection() throws DatabaseUnitException {
    H2Connection h2Conn = new H2Connection(h2Connection, null);
    h2Conn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new TestH2DataTypeFactory());
    h2Conn.getConfig().setFeature(DatabaseConfig.FEATURE_DATATYPE_WARNING, false);
    return h2Conn;
}
 
Example #5
Source File: DatabaseConnectionFactoryTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testOpenConnectionToH2DbHavingUsernameAndPasswordEncodedIntoConnectionUrl() throws ClassNotFoundException {
    // GIVEN
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName(H2_DRIVER_CLASS_PROP_VALUE);
    ds.setUrl(H2_CONNECTION_URL_PROP_VALUE + ";USER=" + USERNAME_PROP_VALUE + ";PASSWORD=" + PASSWORD_PROP_VALUE);

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

    // THEN
    assertThat(connection, notNullValue());
    assertThat(connection, instanceOf(H2Connection.class));
}
 
Example #6
Source File: KylinTestBase.java    From kylin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
protected static H2Connection newH2Connection() throws DatabaseUnitException {
    H2Connection h2Conn = new H2Connection(h2Connection, null);
    h2Conn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new TestH2DataTypeFactory());
    h2Conn.getConfig().setFeature(DatabaseConfig.FEATURE_DATATYPE_WARNING, false);
    return h2Conn;
}
 
Example #7
Source File: KylinTestBase.java    From Kylin with Apache License 2.0 5 votes vote down vote up
protected void execQueryUsingH2(String queryFolder, boolean needSort) throws Exception {
    printInfo("---------- Running H2 queries: " + queryFolder);

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

        // execute H2
        printInfo("Query Result from H2 - " + queryName);
        H2Connection h2Conn = new H2Connection(h2Connection, null);
        h2Conn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new TestH2DataTypeFactory());
        executeQuery(h2Conn, queryName, sql, needSort);
    }
}
 
Example #8
Source File: KylinTestBase.java    From Kylin with Apache License 2.0 5 votes vote down vote up
protected void execAndCompQuery(String queryFolder, String[] exclusiveQuerys, boolean needSort) throws Exception {
    printInfo("---------- 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
        printInfo("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql, needSort);

        // execute H2
        printInfo("Query Result from H2 - " + queryName);
        H2Connection h2Conn = new H2Connection(h2Connection, null);
        h2Conn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new TestH2DataTypeFactory());
        ITable h2Table = executeQuery(h2Conn, queryName, sql, needSort);

        // compare the result
        Assertion.assertEquals(h2Table, kylinTable);

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql);
        }
    }
}
 
Example #9
Source File: H2ConnectionFactory.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@Override
public IDatabaseConnection createConnection(final Connection connection, final String schema) throws DatabaseUnitException {
    return new H2Connection(connection, schema);
}