Package org.dbunit.database

Examples of org.dbunit.database.DatabaseConnection


    @DatabaseTearDown(value = "/dbunit/dao/admin/uzer-create_no-id.xml", type = DatabaseOperation.DELETE_ALL)
    @Ignore("May fail because of a conflict with the admin uzer (the account created on setUp)")
    @Override
    public void testSave() throws Exception {
        try {
            DatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
            IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(this.getClass().getResource("/dbunit/dao/admin/uzer-create_no-id.xml")
                    .openStream());
            Uzer uzer = new Uzer();
            Uzer nuzer;
            Program program = new Program();
            Profile profile = new Profile();

            program.setName(PROGRAM_NAME);
            program.setVersion(1);
            profile.setId(1L);
            profile.setAdministrator(false);
            profile.setLicensesConsultation(false);
            profile.setLicensesEdition(false);
            profile.setProgramsConsultation(false);
            profile.setProgramsEdition(false);
            profile.setName(PROFILE_NAME);
            profile.setVersion(1);
            uzer.setId(1L);
            uzer.setEmail(DEFAULT_USER_EMAIL);
            uzer.setName(USER_NAME);
            uzer.setFirstname(USER_FIRSTNAME);
            uzer.setProfile(profile);
            uzer.setVersion(1);
            nuzer = uzerDAO.save(uzer);
            assertNotNull(nuzer);
            assertEquals(nuzer.getEmail(), uzer.getEmail());
            entityManager.getTransaction().commit();
            DatabaseAssertionMode.NON_STRICT.getDatabaseAssertion().assertEquals(expectedDataSet,
                    connection.createDataSet(expectedDataSet.getTableNames()));
            connection.close();
        } catch (DatabaseUnitException e) {
            e.printStackTrace();
            fail(e.getLocalizedMessage());
        } catch (SQLException e) {
            e.printStackTrace();
View Full Code Here


    @Test
    @DatabaseSetup(value = CREATE_DATASET)
    @DatabaseTearDown(value = UPDATE_DATASET, type = DatabaseOperation.DELETE_ALL)
    public void testSave2() throws Exception {
        try {
            DatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
            IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(this.getClass().getResource("/dbunit/dao/admin/uzer-update_aftersave.xml")
                    .openStream());
            Uzer uzer = new Uzer();
            Uzer nuzer;
            Program program = new Program();
            Profile profile = new Profile();
            Set<Program> programsWithConsultationRights = new HashSet<Program>();
            Set<Program> programsWithEditionRights = new HashSet<Program>();

            program.setId(1L);
            program.setName(PROGRAM_NAME);
            program.setVersion(1);
            profile.setId(1L);
            profile.setAdministrator(false);
            profile.setLicensesConsultation(false);
            profile.setLicensesEdition(false);
            profile.setProgramsConsultation(false);
            profile.setProgramsEdition(false);
            profile.setName(PROFILE_NAME);
            profile.setVersion(1);
            uzer.setId(1L);
            uzer.setEmail(UPDATED_USER_EMAIL);
            uzer.setName(USER_NAME);
            uzer.setFirstname(USER_FIRSTNAME);
            uzer.setProfile(profile);
            programsWithConsultationRights.add(program);
            uzer.getProgramsWithConsultationRights().addAll(programsWithConsultationRights);
            programsWithEditionRights.add(program);
            uzer.getProgramsWithEditionRights().addAll(programsWithEditionRights);
            uzer.setVersion(1);
            nuzer = uzerDAO.save(uzer);
            assertNotNull(nuzer);
            assertEquals(nuzer.getEmail(), uzer.getEmail());
            entityManager.getTransaction().commit();
            // programsWithConsultationRights and programsWithEditionRights are many-to-many relationships
            // whenever they are set, both users and programs are updated
            program.setVersion(2);
            DatabaseAssertionMode.NON_STRICT.getDatabaseAssertion().assertEquals(expectedDataSet,
                    connection.createDataSet(expectedDataSet.getTableNames()));
            connection.close();
        } catch (DatabaseUnitException e) {
            e.printStackTrace();
            fail(e.getLocalizedMessage());
        } catch (SQLException e) {
            e.printStackTrace();
View Full Code Here

    /**
     * Add an Admin account.
     */
    private void addAdminAccount() {
        try {
            DatabaseConnection connection;
            connection = new DatabaseConnection(dataSource.getConnection());
            URL resource = this.getClass().getResource(DATASET_ADMIN_USER);
            IDataSet dataSet = new FlatXmlDataSetBuilder().build(resource.openStream());
            org.dbunit.operation.DatabaseOperation.INSERT.execute(connection, dataSet);
            connection.close();
        } catch (DatabaseUnitException e) {
            e.printStackTrace();
            fail(e.getLocalizedMessage());
        } catch (SQLException e) {
            e.printStackTrace();
View Full Code Here

    /**
     * Remove admin account.
     */
    private void removeAdminAccount() {
        try {
            DatabaseConnection connection;
            connection = new DatabaseConnection(dataSource.getConnection());
            URL resource = this.getClass().getResource(DATASET_ADMIN_USER);
            IDataSet dataSet = new FlatXmlDataSetBuilder().build(resource.openStream());
            org.dbunit.operation.DatabaseOperation.DELETE.execute(connection, dataSet);
            connection.close();
        } catch (DatabaseUnitException e) {
            e.printStackTrace();
            fail(e.getLocalizedMessage());
        } catch (SQLException e) {
            e.printStackTrace();
View Full Code Here

    final Connection realConn = unwrappedConn != null
        ? unwrappedConn
        : connection;
   
    try {
      IDatabaseConnection conn = new DatabaseConnection(realConn, this.schemaName);
      for (String key : this.configProperties.keySet()) {
        conn.getConfig().setProperty(key, this.configProperties.get(key));
      }
      work.execute(conn);
    } catch (DatabaseUnitException ex) {
      throw new JuDbException("Couldn't execute DbUnitWork", ex);
    }
View Full Code Here

    final Connection realConn = unwrappedConn != null
        ? unwrappedConn
        : connection;
   
    try {
      IDatabaseConnection conn = new DatabaseConnection(realConn, this.schemaName);
      for (String key : this.configProperties.keySet()) {
        conn.getConfig().setProperty(key, this.configProperties.get(key));
      }
      work.execute(conn);
    } catch (DatabaseUnitException ex) {
      throw new JuDbException("Couldn't execute DbUnitWork", ex);
    }
View Full Code Here

        DatabaseOperation operation = phase.getOperation(dataSetConfiguration);
        try {
            IDataSet dataSet = decorateDataSetIfNeeded(dataSetConfiguration.getDataSet(), dataSetConfiguration.getDecorators());
            String schema = dataSetConfiguration.getSchema();
            DatabaseConnection databaseConnection = getDatabaseConnection(connection, schema, dataSetConfiguration);
            sw.start("populating");
            operation.execute(databaseConnection, dataSet);
            sw.stop();
            LOGGER.debug(sw.prettyPrint());
View Full Code Here

public class DefaultDataReader implements DataReader {

    @Override
    public IDataSet execute(ApplicationContext applicationContext, ExpectedDataSetConfiguration expectedDataSetConfiguration, String tableName) throws Exception {
        if (expectedDataSetConfiguration != null) {
            DatabaseConnection databaseConnection = getDatabaseConnection(applicationContext, expectedDataSetConfiguration);
            return databaseConnection.createDataSet(new String[] { tableName });
        }
        return null;
    }
View Full Code Here

        applicationContext.getBean(DataSource.class);
  }

  public static DatabaseConnection getDatabaseConnection(Connection connection, String schema, DatabaseConnectionConfigurer databaseConnectionConfigurer) throws DatabaseUnitException {

    DatabaseConnection databaseConnection = StringUtils.hasLength(schema) ? new DatabaseConnection(connection, schema) : new DatabaseConnection(connection);
    DatabaseConfig databaseConfig = databaseConnection.getConfig();
    databaseConnectionConfigurer.configure(databaseConfig);

    return databaseConnection;
  }
View Full Code Here

   
    try {
      // Check if we get the same connection. If so, we'll recycle the DatabaseConnection to avoid
      // requerying of all the DB meta data
      if (this.dbConn == null || this.dbConn.getConnection() != connection) {
        this.dbConn = new DatabaseConnection(realConn, this.schemaName);
       
        for (String key : this.configProperties.keySet()) {
          this.dbConn.getConfig().setProperty(key, this.configProperties.get(key));
        }
        logger.debug("Created DatabaseConnection: {}", this.dbConn);
View Full Code Here

TOP

Related Classes of org.dbunit.database.DatabaseConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.