/** @inheritdoc */
  public void injectDatasets(final Method testMethod)
      throws IOException, DatabaseUnitException, SQLException {

    final List<IDataSet> datasets = this.prepareDatasets(testMethod);

    if (datasets != null && !datasets.isEmpty()) {

      final DatabaseConnection connection = this.prepareConnection();

      if (this.configProperties != null) {
        final DatabaseConfig config = connection.getConfig();
        this.applyConfigProperties(config);
      }

      if (this.useCompositeDataset) {
        final IDataSet[] array = datasets.toArray(new IDataSet[datasets.size()]);
        final CompositeDataSet dataset = new CompositeDataSet(array);
        this.operation.execute(connection, dataset);
      } else {
        for (final IDataSet dataset : datasets) {
          this.operation.execute(connection, dataset);
        }
      }
    }
  }
  /**
   * Import data into database.
   *
   * @param connection database connection
   * @throws Exception throws various exceptions
   */
  public void importData(Connection connection) throws Exception {
    InputStream inStream;
    IDataSet dataSet;

    DatabaseConnection dbconnection = new DatabaseConnection(connection);

    // initialize the data set
    DatabaseConfig config = dbconnection.getConfig();
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());

    inStream = this.getClass().getResourceAsStream(getDataFile());

    try {

      // put check here to ensure, finally is always executed
      if (inStream == null) {
        throw new FileNotFoundException("Datafile: " + getDataFile() + " not found");
      }

      // if no modified data set is available the test data set is loaded
      dataSet = new FlatXmlDataSet(inStream);

      DatabaseOperation.CLEAN_INSERT.execute(dbconnection, dataSet);
    } finally {
      dbconnection.close();
      connection.close();
    }
  }
Example #3
0
  /**
   * @return
   * @throws Exception
   */
  protected IDatabaseConnection getConnection() throws Exception {
    logger.entry();

    DatabaseConnection connection = null;

    Connection con = datasource.getConnection();
    connection = new DatabaseConnection(con);
    DatabaseConfig config = connection.getConfig();
    config.setProperty(
        "http://www.dbunit.org/properties/datatypeFactory", new MsSqlDataTypeFactory());

    // Disable foreign key constraint checking
    con.prepareStatement(
            "EXEC sp_MSforeachtable @command1=\"ALTER TABLE ? NOCHECK CONSTRAINT ALL\"")
        .execute();

    logger.exit();

    return connection;
  }
  public DatabaseUpgradeTestUtil(String initialDatabasePath) throws IOException, SQLException {
    InputStream databaseInputStream = getClass().getResourceAsStream(initialDatabasePath);

    tempDir = File.createTempFile("openmrs-tests-temp-", "");
    tempDir.delete();
    tempDir.mkdir();
    tempDir.deleteOnExit();

    tempDBFile = new File(tempDir, "openmrs.h2.db");
    tempDBFile.delete();
    try {
      tempDBFile.createNewFile();
    } catch (IOException e) {
      tempDir.delete();
      throw e;
    }
    tempDBFile.deleteOnExit();

    FileOutputStream tempDBOutputStream = new FileOutputStream(tempDBFile);

    try {
      IOUtils.copy(databaseInputStream, tempDBOutputStream);

      databaseInputStream.close();
      tempDBOutputStream.close();
    } catch (IOException e) {
      tempDBFile.delete();
      tempDir.delete();

      throw e;
    } finally {
      IOUtils.closeQuietly(databaseInputStream);
      IOUtils.closeQuietly(tempDBOutputStream);
    }

    String databaseUrl = tempDir.getAbsolutePath().replace("\\", "/") + "/openmrs";

    connectionUrl = "jdbc:h2:" + databaseUrl + ";AUTO_RECONNECT=TRUE;DB_CLOSE_DELAY=-1";

    connection = DriverManager.getConnection(connectionUrl, "sa", "sa");
    connection.setAutoCommit(true);

    try {
      liqubaseConnection =
          DatabaseFactory.getInstance()
              .findCorrectDatabaseImplementation(new JdbcConnection(connection));
      liqubaseConnection.setDatabaseChangeLogTableName("LIQUIBASECHANGELOG");
      liqubaseConnection.setDatabaseChangeLogLockTableName("LIQUIBASECHANGELOGLOCK");
    } catch (LiquibaseException e) {
      tempDir.delete();
      tempDBFile.delete();

      throw new SQLException(e);
    }

    try {
      dbUnitConnection = new DatabaseConnection(connection);
      dbUnitConnection
          .getConfig()
          .setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new H2DataTypeFactory());
    } catch (DatabaseUnitException e) {
      tempDir.delete();
      tempDBFile.delete();

      throw new SQLException(e);
    }
  }