/** Charge le jeu de données à partir d'un fichier XML d'import */ @Override protected IDataSet getDataSet() throws Exception { FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder(); FlatXmlDataSet loadedDataSet = flatXmlDataSetBuilder.build(new FileInputStream(getInputDataFileName())); return loadedDataSet; }
protected List<IDataSet> prepareDatasets(final Method testMethod) throws DataSetException, IOException { final List<DbUnitDataset> datasetAnnotations = this.collectDatasetAnnotations(testMethod); if (datasetAnnotations.isEmpty()) { return null; } else { FlatXmlDataSetBuilder builder = null; if (this.useFlatXmlDataset) { builder = new FlatXmlDataSetBuilder() .setDtdMetadata(this.dtdMetadata) .setColumnSensing(this.columnSensing) .setCaseSensitiveTableNames(this.caseSensitiveTableNames); } final List<IDataSet> datasets = new ArrayList<IDataSet>(); for (int i = 0; i < datasetAnnotations.size(); i++) { final DbUnitDataset datasetAnn = datasetAnnotations.get(i); for (final String resource : datasetAnn.value()) { final ClassPathResource classPathResource = new ClassPathResource(resource); if (!classPathResource.exists()) { throw new FileNotFoundException(resource); } final InputStream is = classPathResource.getInputStream(); DbUnitDatabasePopulator.LOGGER.info("Processing DbUnit script: " + resource); final InputStreamReader r = new InputStreamReader(is, this.datasetEncoding); final IDataSet dataset; if (this.useFlatXmlDataset) { dataset = builder.build(r); } else { dataset = new XmlDataSet(r); } datasets.add(dataset); } } return datasets; } }
/** {@inheritDoc} */ protected IDataSet fromResource(Resource resource, DataSetFormatOptions options) throws DataSetException, IOException { FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder(); builder.setColumnSensing(options.isColumnSensing()); builder.setDtdMetadata(options.isDtdMetadata()); builder.setCaseSensitiveTableNames(options.isCaseSensitiveTableNames()); if (StringUtils.hasText(options.getDtdLocation())) { IDataSet metaDataSet = FLAT_DTD.loadUnique(null, options.getDtdLocation()); builder.setMetaDataSet(metaDataSet); } return builder.build(resource.getInputStream()); }
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"); }
private IDataSet loadDataSetFromClassPath(String path) throws DataSetException { FlatXmlDataSetBuilder bld = new FlatXmlDataSetBuilder(); ClassLoader loader = getClass().getClassLoader(); return bld.build(loader.getResource(path)); }