Search in sources :

Example 11 with JUnitResourceAccessor

use of liquibase.test.JUnitResourceAccessor in project liquibase by liquibase.

the class VerifyChangeClassesTest method minimumRequiredIsValidSql.

@Ignore
@Test
public void minimumRequiredIsValidSql() throws Exception {
    ChangeFactory changeFactory = ChangeFactory.getInstance();
    for (String changeName : changeFactory.getDefinedChanges()) {
        if (changeName.equals("addDefaultValue")) {
            //need to better handle strange "one of defaultValue* is required" logic
            continue;
        }
        if (changeName.equals("changeWithNestedTags") || changeName.equals("sampleChange")) {
            //not a real change
            continue;
        }
        for (Database database : DatabaseFactory.getInstance().getImplementedDatabases()) {
            if (database.getShortName() == null) {
                continue;
            }
            TestState state = new TestState(name.getMethodName(), changeName, database.getShortName(), TestState.Type.SQL);
            state.addComment("Database: " + database.getShortName());
            Change change = changeFactory.create(changeName);
            if (!change.supports(database)) {
                continue;
            }
            if (change.generateStatementsVolatile(database)) {
                continue;
            }
            ChangeMetaData changeMetaData = ChangeFactory.getInstance().getChangeMetaData(change);
            change.setResourceAccessor(new JUnitResourceAccessor());
            for (String paramName : new TreeSet<String>(changeMetaData.getRequiredParameters(database).keySet())) {
                ChangeParameterMetaData param = changeMetaData.getParameters().get(paramName);
                Object paramValue = param.getExampleValue(database);
                String serializedValue;
                serializedValue = formatParameter(paramValue);
                state.addComment("Change Parameter: " + param.getParameterName() + "=" + serializedValue);
                param.setValue(change, paramValue);
            }
            ValidationErrors errors = change.validate(database);
            assertFalse("Validation errors for " + changeMetaData.getName() + " on " + database.getShortName() + ": " + errors.toString(), errors.hasErrors());
            SqlStatement[] sqlStatements = change.generateStatements(database);
            for (SqlStatement statement : sqlStatements) {
                Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statement, database);
                if (sql == null) {
                    System.out.println("Null sql for " + statement + " on " + database.getShortName());
                } else {
                    for (Sql line : sql) {
                        String sqlLine = line.toSql();
                        assertFalse("Change " + changeMetaData.getName() + " contains 'null' for " + database.getShortName() + ": " + sqlLine, sqlLine.contains(" null "));
                        state.addValue(sqlLine + ";");
                    }
                }
            }
            state.test();
        }
    }
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) Change(liquibase.change.Change) Sql(liquibase.sql.Sql) SqlStatement(liquibase.statement.SqlStatement) ChangeFactory(liquibase.change.ChangeFactory) Database(liquibase.database.Database) ChangeParameterMetaData(liquibase.change.ChangeParameterMetaData) ChangeMetaData(liquibase.change.ChangeMetaData) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractVerifyTest(liquibase.verify.AbstractVerifyTest)

Aggregations

JUnitResourceAccessor (liquibase.test.JUnitResourceAccessor)11 Test (org.junit.Test)11 XMLChangeLogSAXParser (liquibase.parser.core.xml.XMLChangeLogSAXParser)6 SqlChangeLogParser (liquibase.parser.core.sql.SqlChangeLogParser)5 Database (liquibase.database.Database)4 Change (liquibase.change.Change)3 ChangeFactory (liquibase.change.ChangeFactory)3 ChangeMetaData (liquibase.change.ChangeMetaData)3 ChangeParameterMetaData (liquibase.change.ChangeParameterMetaData)3 AbstractVerifyTest (liquibase.verify.AbstractVerifyTest)3 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)2 ValidationErrors (liquibase.exception.ValidationErrors)2 ChangeLogParser (liquibase.parser.ChangeLogParser)2 Ignore (org.junit.Ignore)2 ArrayList (java.util.ArrayList)1 Contexts (liquibase.Contexts)1 LabelExpression (liquibase.LabelExpression)1 Liquibase (liquibase.Liquibase)1 RuntimeEnvironment (liquibase.RuntimeEnvironment)1 ChangeLogIterator (liquibase.changelog.ChangeLogIterator)1