Search in sources :

Example 1 with DatabaseTestSystem

use of liquibase.extension.testing.testsystem.DatabaseTestSystem in project liquibase by liquibase.

the class AbstractExecuteTest method test.

private void test(String[] expectedSql, Class<? extends Database>[] includeDatabases, Class<? extends Database>[] excludeDatabases) throws Exception {
    if (expectedSql != null) {
        for (Database database : TestContext.getInstance().getAllDatabases()) {
            if (shouldTestDatabase(database, includeDatabases, excludeDatabases)) {
                testedDatabases.add(database.getClass());
                if (database.getConnection() != null) {
                    ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).init();
                    LockServiceFactory.getInstance().getLockService(database).init();
                }
                Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statementUnderTest, database);
                assertNotNull("Null SQL for " + database, sql);
                assertEquals("Unexpected number of  SQL statements for " + database, expectedSql.length, sql.length);
                int index = 0;
                for (String convertedSql : expectedSql) {
                    convertedSql = replaceEscaping(convertedSql, database);
                    convertedSql = replaceDatabaseClauses(convertedSql, database);
                    convertedSql = replaceStandardTypes(convertedSql, database);
                    assertEquals("Incorrect SQL for " + database.getClass().getName(), convertedSql.toLowerCase().trim(), sql[index].toSql().toLowerCase());
                    index++;
                }
            }
        }
    }
    resetAvailableDatabases();
    for (DatabaseTestSystem testSystem : Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getAvailable(DatabaseTestSystem.class)) {
        testSystem.start();
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(testSystem.getConnection()));
        Statement statement = ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement();
        if (shouldTestDatabase(database, includeDatabases, excludeDatabases)) {
            String sqlToRun = SqlGeneratorFactory.getInstance().generateSql(statementUnderTest, database)[0].toSql();
            try {
                for (SqlListener listener : Scope.getCurrentScope().getListeners(SqlListener.class)) {
                    listener.writeSqlWillRun(sqlToRun);
                }
                statement.execute(sqlToRun);
            } catch (Exception e) {
                System.out.println("Failed to execute against " + database.getShortName() + ": " + sqlToRun);
                throw e;
            }
        }
    }
}
Also used : SqlListener(liquibase.listener.SqlListener) SqlStatement(liquibase.statement.SqlStatement) Statement(java.sql.Statement) UnsupportedDatabase(liquibase.database.core.UnsupportedDatabase) Database(liquibase.database.Database) MockDatabase(liquibase.database.core.MockDatabase) ExampleCustomDatabase(liquibase.database.example.ExampleCustomDatabase) JdbcConnection(liquibase.database.jvm.JdbcConnection) TestSystemFactory(liquibase.extension.testing.testsystem.TestSystemFactory) DatabaseTestSystem(liquibase.extension.testing.testsystem.DatabaseTestSystem) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) Sql(liquibase.sql.Sql)

Example 2 with DatabaseTestSystem

use of liquibase.extension.testing.testsystem.DatabaseTestSystem in project liquibase by liquibase.

the class AbstractExecuteTest method resetAvailableDatabases.

public void resetAvailableDatabases() throws Exception {
    for (DatabaseTestSystem testSystem : Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getAvailable(DatabaseTestSystem.class)) {
        testSystem.start();
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(testSystem.getConnection()));
        DatabaseConnection connection = database.getConnection();
        Statement connectionStatement = ((JdbcConnection) connection).getUnderlyingConnection().createStatement();
        connection.commit();
        try {
            database.dropDatabaseObjects(CatalogAndSchema.DEFAULT);
            CatalogAndSchema alt = new CatalogAndSchema(testSystem.getAltCatalog(), testSystem.getAltSchema());
            database.dropDatabaseObjects(alt);
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException("Error dropping objects for database " + database.getShortName(), e);
        }
        try {
            connectionStatement.executeUpdate("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogLockTableName()));
        } catch (SQLException e) {
        }
        connection.commit();
        try {
            connectionStatement.executeUpdate("drop table " + database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()));
        } catch (SQLException e) {
        }
        connection.commit();
        if (database.supportsSchemas()) {
            database.dropDatabaseObjects(new CatalogAndSchema(null, testSystem.getAltSchema()));
            connection.commit();
            try {
                connectionStatement.executeUpdate("drop table " + database.escapeTableName(testSystem.getAltCatalog(), testSystem.getAltSchema(), database.getDatabaseChangeLogLockTableName()));
            } catch (SQLException e) {
            // ok
            }
            connection.commit();
            try {
                connectionStatement.executeUpdate("drop table " + database.escapeTableName(testSystem.getAltCatalog(), testSystem.getAltSchema(), database.getDatabaseChangeLogTableName()));
            } catch (SQLException e) {
            // ok
            }
            connection.commit();
        }
        List<? extends SqlStatement> setupStatements = setupStatements(database);
        if (setupStatements != null) {
            for (SqlStatement statement : setupStatements) {
                Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).execute(statement);
            }
        }
        connectionStatement.close();
    }
}
Also used : SqlStatement(liquibase.statement.SqlStatement) SQLException(java.sql.SQLException) SqlStatement(liquibase.statement.SqlStatement) Statement(java.sql.Statement) UnsupportedDatabase(liquibase.database.core.UnsupportedDatabase) Database(liquibase.database.Database) MockDatabase(liquibase.database.core.MockDatabase) ExampleCustomDatabase(liquibase.database.example.ExampleCustomDatabase) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseConnection(liquibase.database.DatabaseConnection) TestSystemFactory(liquibase.extension.testing.testsystem.TestSystemFactory) CatalogAndSchema(liquibase.CatalogAndSchema) DatabaseTestSystem(liquibase.extension.testing.testsystem.DatabaseTestSystem) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 Database (liquibase.database.Database)2 MockDatabase (liquibase.database.core.MockDatabase)2 UnsupportedDatabase (liquibase.database.core.UnsupportedDatabase)2 ExampleCustomDatabase (liquibase.database.example.ExampleCustomDatabase)2 JdbcConnection (liquibase.database.jvm.JdbcConnection)2 DatabaseException (liquibase.exception.DatabaseException)2 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 DatabaseTestSystem (liquibase.extension.testing.testsystem.DatabaseTestSystem)2 TestSystemFactory (liquibase.extension.testing.testsystem.TestSystemFactory)2 SqlStatement (liquibase.statement.SqlStatement)2 CatalogAndSchema (liquibase.CatalogAndSchema)1 DatabaseConnection (liquibase.database.DatabaseConnection)1 SqlListener (liquibase.listener.SqlListener)1 Sql (liquibase.sql.Sql)1