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;
}
}
}
}
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();
}
}
Aggregations