protected final void setupData(InputStream... dataSetStream) { try { IDataSet[] dataSets = new IDataSet[dataSetStream.length]; for (int i = 0; i < dataSetStream.length; i++) { ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(dataSetStream[i])); dataSet.addReplacementObject("[null]", null); dataSets[i] = dataSet; } CompositeDataSet compositeDataSet = new CompositeDataSet(dataSets); databaseTester.setDataSet(compositeDataSet); connection = databaseTester.getConnection(); connection .getConnection() .prepareStatement("set referential_integrity FALSE") .execute(); // HSQL DB DatabaseConfig config = connection.getConfig(); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory()); DatabaseOperation.CLEAN_INSERT.execute(connection, databaseTester.getDataSet()); connection .getConnection() .prepareStatement("set referential_integrity TRUE") .execute(); // HSQL DB } catch (Exception e) { throw translateException("Could not setup DBUnit data", e); } }
@BeforeMethod(alwaysRun = true) public void setUpTest() throws Exception { dbConn = new DatabaseDataSourceConnection(dataSource); try { IntegrationTestUtils.doCleanup(dbConn.getConnection()); } catch (Exception e) { logger.error("Error cleaning db."); } dbConn.getConfig().setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "`?`"); dbConn .getConfig() .setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory()); DatabaseOperation.INSERT.execute(dbConn, getDataSet()); System.setProperty("zeus_frontend_version", ZEUS_FRONTEND_VERSION); System.setProperty("zeus_lookup_version", ZEUS_LOOKUP_VERSION); }
/** * Print the contents of the given tableName to system.out<br> * <br> * Call this from any {@link BaseContextSensitiveTest} child by: * TestUtil.printOutTableContents(getConnection(), "encounter"); * * @param sqlConnection the connection to use * @param tableNames the name(s) of the table(s) to print out * @throws Exception */ public static void printOutTableContents(Connection sqlConnection, String... tableNames) throws Exception { for (String tableName : tableNames) { System.out.println("The contents of table: " + tableName); IDatabaseConnection connection = new DatabaseConnection(sqlConnection); DatabaseConfig config = connection.getConfig(); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory()); QueryDataSet outputSet = new QueryDataSet(connection); outputSet.addTable(tableName); FlatXmlDataSet.write(outputSet, System.out); } }
protected IDatabaseConnection getConnection() throws SQLException { if (isOracle()) { try { Class.forName(config.getDriver()).newInstance(); } catch (Exception ex) { logger.error(ex); } IDatabaseConnection connection = new DatabaseConnection( DriverManager.getConnection( config.getUrl(), config.getUsername(), config.getPassword())); connection.getConfig().setFeature("http://www.dbunit.org/features/qualifiedTableNames", true); connection .getConfig() .setProperty( "http://www.dbunit.org/properties/datatypeFactory", new org.dbunit.ext.oracle.OracleDataTypeFactory()); return connection; } return new DatabaseConnection(datasource.getConnection()); }
public void test() throws Exception { Connection jdbcConnection = null; IDatabaseConnection connection = new DatabaseConnection(jdbcConnection, ""); DatabaseConfig config = connection.getConfig(); // Use the ForwardOnlyResultSetTableFactory to export very large dataset. config.setProperty( DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, new ForwardOnlyResultSetTableFactory()); // Use the StreamingDataSet to import very large dataset. IDataSetProducer producer = new FlatXmlProducer(new InputSource("dataset.xml")); IDataSet dataSet = new StreamingDataSet(producer); }
/** * Override this method if you want to provide your own DBUnit <tt>IDatabaseConnection</tt> * instance. * * <p>If you do not override this, default behavior is to use the * configured datasource name and * to obtain a connection with a JNDI lookup. * * @return a DBUnit database connection (wrapped) */ protected IDatabaseConnection getConnection() { try { DataSource datasource = ((DataSource) getInitialContext().lookup(datasourceJndiName)); // Get a JDBC connection from JNDI datasource Connection con = datasource.getConnection(); IDatabaseConnection dbUnitCon = new DatabaseConnection(con); editConfig(dbUnitCon.getConfig()); return dbUnitCon; } catch (Exception ex) { throw new RuntimeException(ex); } }
@Override public void configure(IDatabaseConnection connection) { DatabaseConfig config = connection.getConfig(); config.setProperty(DBUnit.DATATYPE_FACTORY, new MySqlDataTypeFactory()); config.setProperty(DBUnit.ESCAPE_PATTERN, "`?`"); config.setProperty( DBUnit.METADATA_HANDLER, // new MySqlMetadataHandler()); config.setProperty(DBUnit.QUALIFIED_TABLE_NAMES, true); }
@Override protected IDatabaseConnection getConnection() throws Exception { Connection conn = dataSource.getConnection(); IDatabaseConnection connection = new DatabaseConnection( conn, dbType.equalsIgnoreCase("oracle") ? testProperties.getProperty("db.username") : null); DatabaseConfig config = connection.getConfig(); if (containsProperty(testProperties, DATATYPE_FACTORY)) { Class clazz = Class.forName(testProperties.getProperty(DATATYPE_FACTORY)); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, BeanUtils.getInstance(clazz)); } return connection; }
private synchronized IDatabaseConnection initialiseDbUnitConnection( ApplicationContext applicationContext) { if (dbUnitCon == null) { try { DataSource dataSource = applicationContext.getBean(DataSource.class); Preconditions.checkNotNull("dataSource not found", dataSource); dbUnitCon = new DatabaseDataSourceConnection(dataSource); DatabaseConfig config = dbUnitCon.getConfig(); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new H2DataTypeFactory()); checkDataBase(dbUnitCon.getConnection().getMetaData()); } catch (SQLException e) { log.error("error initialiseDbUnitConnection ", e); } } return dbUnitCon; }
@Override protected IDatabaseConnection getDbUnitConnection() throws SQLException, DatabaseUnitException { LOG.info("MSSQL driver selected"); Connection conn = getConnection(); DatabaseMetaData databaseMetaData = conn.getMetaData(); String url = databaseMetaData.getURL(); int start = url.lastIndexOf("/") + 1; int end = url.length(); String schema = url.substring(start, end); IDatabaseConnection iConn = new MsSqlConnection(conn, schema); iConn .getConfig() .setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MsSqlDataTypeFactory()); return iConn; }
public static void reload() throws Exception { if (dbReloaded) { return; } Connection jdbcConn = null; IDatabaseConnection dbunitConn = null; try { jdbcConn = DriverManager.getConnection(URL, USERNAME, PASSWORD); boolean caseSensitiveTableNames = false; if (URL.contains("postgres")) { dbunitConn = new DatabaseConnection(jdbcConn, SCHEMA); dbunitConn .getConfig() .setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory()); caseSensitiveTableNames = true; } else if (URL.contains("mysql")) { dbunitConn = new MySqlConnection(jdbcConn, null); if (System.getProperty("os.name").contains("Linux")) { caseSensitiveTableNames = true; } } // else if (URL.contains("oracle")) { // dbunitConn = new OracleConnection(jdbcConn, SCHEMA); // } // else if (URL.contains("sqlserver")) { // dbunitConn = new MsSqlConnection(jdbcConn, SCHEMA); // } // else if (URL.contains("db2")) { // dbunitConn = new Db2Connection(jdbcConn, SCHEMA); // if (System.getProperty("os.name").contains("Linux")) { // caseSensitiveTableNames = true; // } // } else { throw new IllegalStateException("Die Datenbank-URL " + URL + " wird nicht unterstuetzt"); } final ClassLoader cl = Thread.currentThread().getContextClassLoader(); final FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder(); flatXmlDataSetBuilder.setCaseSensitiveTableNames(caseSensitiveTableNames); FlatXmlDataSet xmlDataset = flatXmlDataSetBuilder.build(cl.getResource(XML_FLAT_DATASET)); DatabaseOperation dbOp = CLEAN_INSERT; // if (URL != null && URL.contains("sqlserver")) { // // Fuer SQL Server ist ein spezieller INSERT-Modus notwendig, // // damit IDENTITY-Spalten eingefuegt werden koennen // dbOp = InsertIdentityOperation.CLEAN_INSERT; // } dbOp.execute(dbunitConn, xmlDataset); } finally { if (dbunitConn != null) dbunitConn.close(); if (jdbcConn != null) jdbcConn.close(); } dbReloaded = true; System.out.println("Die Datenbank " + URL + " wurde neu geladen"); }