forked from prospero238/liquibase-runner
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge branch 'feature/JENKINS-37637-changesets-in-classpath' into dev…
…elop
- Loading branch information
Showing
5 changed files
with
201 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/test/java/org/jenkinsci/plugins/liquibase/evaluator/AbstractLiquibaseBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package org.jenkinsci.plugins.liquibase.evaluator; | ||
|
||
import hudson.Launcher; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.Build; | ||
import hudson.model.BuildListener; | ||
import hudson.model.Descriptor; | ||
import hudson.tasks.Builder; | ||
import liquibase.Contexts; | ||
import liquibase.Liquibase; | ||
import liquibase.changelog.ChangeSet; | ||
import liquibase.changelog.DatabaseChangeLog; | ||
import liquibase.exception.LiquibaseException; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.jenkinsci.plugins.liquibase.integration.LiquibaseTestUtil; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class AbstractLiquibaseBuilderTest { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(AbstractLiquibaseBuilderTest.class); | ||
|
||
@Rule | ||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
@Mock | ||
Build build; | ||
|
||
@Mock | ||
BuildListener buildListener; | ||
|
||
@Mock | ||
Launcher launcher; | ||
|
||
protected String dbUrl; | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
temporaryFolder.create(); | ||
File dbfiles = temporaryFolder.newFolder("dbfiles"); | ||
File dbFile = new File(dbfiles, "h2"); | ||
dbUrl = "jdbc:h2:file:" + dbFile.getAbsolutePath(); | ||
when(buildListener.getLogger()).thenReturn(System.out); | ||
} | ||
|
||
@Test | ||
public void should_load_changeset_from_system_classpath() throws IOException, LiquibaseException { | ||
AbstractLiquibaseBuilder liquibaseBuilder = new LiquibaseBuilderStub(); | ||
|
||
Properties configProperties = createLiquibaseProperties(); | ||
// this changelogFile is available only through this test class's classloader | ||
configProperties.setProperty("changeLogFile", "example-changesets/single-changeset.xml"); | ||
|
||
Liquibase liquibase = liquibaseBuilder | ||
.createLiquibase(build, buildListener, new ExecutedChangesetAction(build), configProperties, launcher); | ||
liquibase.update(new Contexts()); | ||
|
||
assertThatOneChangesetExecuted(liquibase); | ||
} | ||
|
||
@Test | ||
public void should_load_changeset_from_dynamic_classpath() throws IOException, LiquibaseException { | ||
Properties liquibaseProperties = createLiquibaseProperties(); | ||
LiquibaseBuilderStub stub = new LiquibaseBuilderStub(); | ||
|
||
File changesets = temporaryFolder.newFolder("changesets"); | ||
|
||
String subDirectoryName = RandomStringUtils.randomAlphabetic(5); | ||
File randomSubDir = new File(changesets, subDirectoryName); | ||
|
||
LiquibaseTestUtil.createFileFromResource(randomSubDir, "/example-changesets/single-changeset.xml"); | ||
|
||
String changeLogResourcePath = subDirectoryName + "/single-changeset.xml"; | ||
liquibaseProperties.setProperty("changeLogFile", changeLogResourcePath); | ||
stub.setClasspath(changesets.getAbsolutePath()); | ||
|
||
Liquibase liquibase = | ||
stub.createLiquibase(build, buildListener, new ExecutedChangesetAction(build), liquibaseProperties, | ||
launcher); | ||
liquibase.update(new Contexts("")); | ||
|
||
assertThatOneChangesetExecuted(liquibase); | ||
|
||
} | ||
|
||
private static void assertThatOneChangesetExecuted(Liquibase liquibase) throws LiquibaseException { | ||
DatabaseChangeLog databaseChangeLog = liquibase.getDatabaseChangeLog(); | ||
List<ChangeSet> changeSets = databaseChangeLog.getChangeSets(); | ||
assertThat(changeSets, hasSize(1)); | ||
} | ||
|
||
private Properties createLiquibaseProperties() throws IOException { | ||
Properties configProperties = new Properties(); | ||
configProperties.load(getClass().getResourceAsStream("/example-changesets/unit-test.h2.liquibase.properties")); | ||
configProperties.setProperty("url", dbUrl); | ||
return configProperties; | ||
} | ||
|
||
private static class LiquibaseBuilderStub extends AbstractLiquibaseBuilder { | ||
@Override | ||
public void doPerform(AbstractBuild<?, ?> build, | ||
BuildListener listener, | ||
Liquibase liquibase, | ||
Contexts contexts, | ||
ExecutedChangesetAction executedChangesetAction) | ||
throws InterruptedException, IOException, LiquibaseException { | ||
|
||
} | ||
|
||
@Override | ||
public Descriptor<Builder> getDescriptor() { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/test/resources/example-changesets/single-changeset.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<databaseChangeLog | ||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> | ||
|
||
<changeSet author="keith" id="create-table"> | ||
<createTable | ||
remarks="sample table" | ||
tableName="shape"> | ||
<column name="id" type="int" autoIncrement="true"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
</createTable> | ||
<comment>This is a simple create table</comment> | ||
</changeSet> | ||
</databaseChangeLog> | ||
|