Search in sources :

Example 6 with JUnitResourceAccessor

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

the class AbstractIntegrationTest method testAbsolutePathChangeLog.

@Test
public void testAbsolutePathChangeLog() throws Exception {
    if (database == null) {
        return;
    }
    Set<String> urls = new JUnitResourceAccessor().list(null, includedChangeLog, true, false, true);
    String absolutePathOfChangeLog = urls.iterator().next();
    absolutePathOfChangeLog = absolutePathOfChangeLog.replaceFirst("file:\\/", "");
    if (System.getProperty("os.name").startsWith("Windows ")) {
        absolutePathOfChangeLog = absolutePathOfChangeLog.replace('/', '\\');
    } else {
        absolutePathOfChangeLog = "/" + absolutePathOfChangeLog;
    }
    Liquibase liquibase = createLiquibase(absolutePathOfChangeLog, new FileSystemResourceAccessor());
    clearDatabase(liquibase);
    liquibase.update(this.contexts);
    //try again, make sure there are no errors
    liquibase.update(this.contexts);
    clearDatabase(liquibase);
}
Also used : Liquibase(liquibase.Liquibase) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) Test(org.junit.Test)

Example 7 with JUnitResourceAccessor

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

the class XMLChangeLogSAXParserTest method testIgnoreDuplicateChangeSets.

@Test
public void testIgnoreDuplicateChangeSets() throws ChangeLogParseException, Exception {
    XMLChangeLogSAXParser xmlParser = new XMLChangeLogSAXParser();
    DatabaseChangeLog changeLog = xmlParser.parse("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/master.changelog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
    final List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
    new ChangeLogIterator(changeLog).run(new ChangeSetVisitor() {

        @Override
        public Direction getDirection() {
            return Direction.FORWARD;
        }

        @Override
        public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Set<ChangeSetFilterResult> filterResults) throws LiquibaseException {
            changeSets.add(changeSet);
        }
    }, new RuntimeEnvironment(new MockDatabase(), new Contexts(), new LabelExpression()));
    Assert.assertEquals(8, changeSets.size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(0).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(1).toString());
    Assert.assertEquals(1, changeSets.get(1).getContexts().getContexts().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(2).toString());
    Assert.assertEquals(1, changeSets.get(2).getLabels().getLabels().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(3).toString());
    Assert.assertEquals(2, changeSets.get(3).getLabels().getLabels().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(4).toString());
    Assert.assertEquals(1, changeSets.get(4).getDbmsSet().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog1.xml::1::testuser", changeSets.get(5).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog3.xml::1::testuser", changeSets.get(6).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog2.xml::1::testuser", changeSets.get(7).toString());
}
Also used : ChangeSetVisitor(liquibase.changelog.visitor.ChangeSetVisitor) RuntimeEnvironment(liquibase.RuntimeEnvironment) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) ArrayList(java.util.ArrayList) Contexts(liquibase.Contexts) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) ChangeLogIterator(liquibase.changelog.ChangeLogIterator) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) MockDatabase(liquibase.sdk.database.MockDatabase) Database(liquibase.database.Database) LabelExpression(liquibase.LabelExpression) MockDatabase(liquibase.sdk.database.MockDatabase) LiquibaseException(liquibase.exception.LiquibaseException) ChangeSetFilterResult(liquibase.changelog.filter.ChangeSetFilterResult) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 8 with JUnitResourceAccessor

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

the class ChangeLogParserFactoryTest method getParser_byExtension.

@Test
public void getParser_byExtension() throws Exception {
    ChangeLogParserFactory.getInstance().getParsers().clear();
    XMLChangeLogSAXParser xmlChangeLogParser = new XMLChangeLogSAXParser();
    ChangeLogParserFactory.getInstance().register(xmlChangeLogParser);
    ChangeLogParserFactory.getInstance().register(new SqlChangeLogParser());
    ChangeLogParser parser = ChangeLogParserFactory.getInstance().getParser("xml", new JUnitResourceAccessor());
    assertNotNull(parser);
    assertTrue(xmlChangeLogParser == parser);
}
Also used : SqlChangeLogParser(liquibase.parser.core.sql.SqlChangeLogParser) SqlChangeLogParser(liquibase.parser.core.sql.SqlChangeLogParser) ChangeLogParser(liquibase.parser.ChangeLogParser) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) XMLChangeLogSAXParser(liquibase.parser.core.xml.XMLChangeLogSAXParser) Test(org.junit.Test)

Example 9 with JUnitResourceAccessor

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

the class VerifyChangeClassesTest method lessThanMinimumFails.

@Test
public void lessThanMinimumFails() throws Exception {
    ChangeFactory changeFactory = ChangeFactory.getInstance();
    for (String changeName : changeFactory.getDefinedChanges()) {
        for (Database database : DatabaseFactory.getInstance().getImplementedDatabases()) {
            if (database.getShortName() == null) {
                continue;
            }
            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());
            ArrayList<String> requiredParams = new ArrayList<String>(changeMetaData.getRequiredParameters(database).keySet());
            for (String paramName : requiredParams) {
                ChangeParameterMetaData param = changeMetaData.getParameters().get(paramName);
                Object paramValue = param.getExampleValue(database);
                param.setValue(change, paramValue);
            }
            for (int i = 0; i < requiredParams.size(); i++) {
                String paramToRemove = requiredParams.get(i);
                ChangeParameterMetaData paramToRemoveMetadata = changeMetaData.getParameters().get(paramToRemove);
                Object currentValue = paramToRemoveMetadata.getCurrentValue(change);
                paramToRemoveMetadata.setValue(change, null);
                assertTrue("No errors even with " + changeMetaData.getName() + " with a null " + paramToRemove + " on " + database.getShortName(), change.validate(database).hasErrors());
                paramToRemoveMetadata.setValue(change, currentValue);
            }
        }
    }
}
Also used : ChangeFactory(liquibase.change.ChangeFactory) Database(liquibase.database.Database) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) ChangeParameterMetaData(liquibase.change.ChangeParameterMetaData) Change(liquibase.change.Change) ChangeMetaData(liquibase.change.ChangeMetaData) Test(org.junit.Test) AbstractVerifyTest(liquibase.verify.AbstractVerifyTest)

Example 10 with JUnitResourceAccessor

use of liquibase.test.JUnitResourceAccessor 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)

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