Search in sources :

Example 66 with ValidationErrors

use of liquibase.exception.ValidationErrors in project liquibase by liquibase.

the class VerifyChangeClassesTest method extraParamsIsValidSql.

@Ignore
@Test
public void extraParamsIsValidSql() 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("createProcedure")) {
            //need to better handle strange "one of path or body is required" logic
            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 baseChange = changeFactory.create(changeName);
            if (!baseChange.supports(database)) {
                continue;
            }
            if (baseChange.generateStatementsVolatile(database)) {
                continue;
            }
            ChangeMetaData changeMetaData = ChangeFactory.getInstance().getChangeMetaData(baseChange);
            ArrayList<String> optionalParameters = new ArrayList<String>(changeMetaData.getOptionalParameters(database).keySet());
            Collections.sort(optionalParameters);
            List<List<String>> paramLists = powerSet(optionalParameters);
            Collections.sort(paramLists, new Comparator<List<String>>() {

                @Override
                public int compare(List<String> o1, List<String> o2) {
                    int comp = Integer.valueOf(o1.size()).compareTo(o2.size());
                    if (comp == 0) {
                        comp = StringUtils.join(o1, ",").compareTo(StringUtils.join(o2, ","));
                    }
                    return comp;
                }
            });
            for (List<String> permutation : paramLists) {
                Change change = changeFactory.create(changeName);
                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("Required Change Parameter: " + param.getParameterName() + "=" + serializedValue);
                    param.setValue(change, paramValue);
                }
                for (String paramName : permutation) {
                    ChangeParameterMetaData param = changeMetaData.getParameters().get(paramName);
                    if (!param.supports(database)) {
                        continue;
                    }
                    Object paramValue = param.getExampleValue(database);
                    String serializedValue;
                    serializedValue = formatParameter(paramValue);
                    state.addComment("Optional 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) {
            //                                state.addValue(line.toSql()+";");
            //                            }
            //                        }
            //                    }
            //                    state.test();
            }
        }
    }
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) Change(liquibase.change.Change) 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)

Example 67 with ValidationErrors

use of liquibase.exception.ValidationErrors 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)

Example 68 with ValidationErrors

use of liquibase.exception.ValidationErrors in project liquibase by liquibase.

the class AddColumnGeneratorDefaultClauseBeforeNotNullTest method validate_noAutoIncrementWithDerby.

@Test
public void validate_noAutoIncrementWithDerby() {
    ValidationErrors validationErrors = generatorUnderTest.validate(new AddColumnStatement(null, null, "table_name", "column_name", "int", null, new AutoIncrementConstraint("column_name")), new DerbyDatabase(), new MockSqlGeneratorChain());
    assertTrue(validationErrors.getErrorMessages().contains("Cannot add an identity column to derby"));
}
Also used : AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) ValidationErrors(liquibase.exception.ValidationErrors) AddColumnStatement(liquibase.statement.core.AddColumnStatement) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) Test(org.junit.Test)

Example 69 with ValidationErrors

use of liquibase.exception.ValidationErrors in project liquibase by liquibase.

the class SqlGeneratorChainTest method validate_nullGenerators.

@Test
public void validate_nullGenerators() {
    SqlGeneratorChain chain = new SqlGeneratorChain(null);
    ValidationErrors validationErrors = chain.validate(new MockSqlStatement(), new MockDatabase());
    assertFalse(validationErrors.hasErrors());
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) MockSqlStatement(liquibase.statement.core.MockSqlStatement) MockDatabase(liquibase.sdk.database.MockDatabase) Test(org.junit.Test)

Example 70 with ValidationErrors

use of liquibase.exception.ValidationErrors in project liquibase by liquibase.

the class SqlGeneratorChainTest method validate_twoGenerators_secondHasErrors.

@Test
public void validate_twoGenerators_secondHasErrors() {
    SortedSet<SqlGenerator> generators = new TreeSet<SqlGenerator>(new SqlGeneratorComparator());
    generators.add(new MockSqlGenerator(2, "B1", "B2"));
    generators.add(new MockSqlGenerator(1, "A1", "A2").addValidationError("E1"));
    SqlGeneratorChain chain = new SqlGeneratorChain(generators);
    ValidationErrors validationErrors = chain.validate(new MockSqlStatement(), new MockDatabase());
    assertTrue(validationErrors.hasErrors());
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) TreeSet(java.util.TreeSet) MockSqlStatement(liquibase.statement.core.MockSqlStatement) MockDatabase(liquibase.sdk.database.MockDatabase) Test(org.junit.Test)

Aggregations

ValidationErrors (liquibase.exception.ValidationErrors)89 Test (org.junit.Test)11 MockDatabase (liquibase.sdk.database.MockDatabase)9 Database (liquibase.database.Database)6 MockSqlStatement (liquibase.statement.core.MockSqlStatement)6 TreeSet (java.util.TreeSet)5 ArrayList (java.util.ArrayList)2 Change (liquibase.change.Change)2 ChangeFactory (liquibase.change.ChangeFactory)2 ChangeMetaData (liquibase.change.ChangeMetaData)2 ChangeParameterMetaData (liquibase.change.ChangeParameterMetaData)2 CreateTableChange (liquibase.change.core.CreateTableChange)2 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)2 RanChangeSet (liquibase.changelog.RanChangeSet)2 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 SqlStatement (liquibase.statement.SqlStatement)2 AddColumnStatement (liquibase.statement.core.AddColumnStatement)2 JUnitResourceAccessor (liquibase.test.JUnitResourceAccessor)2 AbstractVerifyTest (liquibase.verify.AbstractVerifyTest)2 Ignore (org.junit.Ignore)2