Skip to content

Commit

Permalink
Test System Framework: add support for "liquibase.sdk.testSystem.skip…
Browse files Browse the repository at this point in the history
…" property (liquibase#2838)

Add a property called "liquibase.sdk.testSystem.skip" to the test system framework. This property works in conjunction with the "liquibase.sdk.testSystem.test" property, allowing developers to run all test system frameworks, and exclude a subset of them.
  • Loading branch information
StevenMassaro authored May 11, 2022
1 parent b6fab96 commit 8d3abcd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.plugin.Plugin;
import liquibase.util.ObjectUtil;
import liquibase.servicelocator.ServiceLocator;
import liquibase.util.CollectionUtil;
import liquibase.util.StringUtil;
import org.junit.Assume;
import org.junit.rules.TestRule;
Expand All @@ -17,6 +18,7 @@
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* TestSystem implementations define and manage a connection to an external system to test.
Expand All @@ -30,6 +32,7 @@ public abstract class TestSystem implements TestRule, Plugin {

private static final SortedSet<TestSystem.Definition> testSystems = new TreeSet<>();
private static final String configuredTestSystems;
private static final String skippedTestSystems;

private final Definition definition;

Expand All @@ -38,12 +41,36 @@ public abstract class TestSystem implements TestRule, Plugin {
));

static {
//cache configured test systems for faster lookup
configuredTestSystems = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class).getCurrentConfiguredValue(ConfigurationValueConverter.STRING, null, "liquibase.sdk.testSystem.test").getValue();
if (configuredTestSystems != null) {
for (String definition : StringUtil.splitAndTrim(configuredTestSystems, ","))
testSystems.add(TestSystem.Definition.parse(definition));
skippedTestSystems = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class).getCurrentConfiguredValue(ConfigurationValueConverter.STRING, null, "liquibase.sdk.testSystem.skip").getValue();

for (String definition : getEnabledTestSystems(configuredTestSystems, skippedTestSystems)) {
testSystems.add(TestSystem.Definition.parse(definition));
}
}

/**
* Determine which test systems are considered enabled and should have tests run against them.
* @param configuredTestSystems the value of the "liquibase.sdk.testSystem.test" property
* @param skippedTestSystems the value of the "liquibase.sdk.testSystem.skip" property
* @return the list of test system names that are enabled
*/
public static List<String> getEnabledTestSystems(String configuredTestSystems, String skippedTestSystems) {
List<String> returnList;

if (StringUtil.isNotEmpty(configuredTestSystems) && configuredTestSystems.equals("all")) {
ServiceLocator serviceLocator = Scope.getCurrentScope().getServiceLocator();
returnList = serviceLocator.findInstances(TestSystem.class).stream()
.map(testSystem -> testSystem.getDefinition().getName()).distinct().collect(Collectors.toList());
} else {
returnList = CollectionUtil.createIfNull(StringUtil.splitAndTrim(configuredTestSystems, ","));
}

if (StringUtil.isNotEmpty(skippedTestSystems)) {
List<String> skippedTestSystemsList = CollectionUtil.createIfNull(StringUtil.splitAndTrim(skippedTestSystems, ","));
returnList = returnList.stream().filter(ts -> !skippedTestSystemsList.contains(ts)).collect(Collectors.toList());
}
return returnList;
}

/**
Expand Down Expand Up @@ -85,7 +112,7 @@ public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Assume.assumeTrue("Not running test against " + TestSystem.this.getDefinition() + ": liquibase.sdk.testSystem.test is " + configuredTestSystems, shouldTest());
Assume.assumeTrue("Not running test against " + TestSystem.this.getDefinition() + ": liquibase.sdk.testSystem.test is " + configuredTestSystems + " and liquibase.sdk.testSystem.skip is " + skippedTestSystems, shouldTest());

List<Throwable> errors = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
import liquibase.Scope;
import liquibase.configuration.ConfigurationValueConverter;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.database.Database;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.plugin.AbstractPluginFactory;
import liquibase.util.CollectionUtil;
import liquibase.util.StringUtil;

import java.lang.reflect.Constructor;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -67,8 +62,9 @@ public TestSystem getTestSystem(TestSystem.Definition definition) {

private List<String> getConfiguredTestSystems() {
final String testSystems = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class).getCurrentConfiguredValue(ConfigurationValueConverter.STRING, null, "liquibase.sdk.testSystem.test").getValue();
final String skippedTestSystems = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class).getCurrentConfiguredValue(ConfigurationValueConverter.STRING, null, "liquibase.sdk.testSystem.skip").getValue();

return CollectionUtil.createIfNull(StringUtil.splitAndTrim(testSystems, ","));
return TestSystem.getEnabledTestSystems(testSystems, skippedTestSystems);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import liquibase.configuration.ConfigurationValueConverter;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.extension.testing.testsystem.TestSystem;
import liquibase.util.StringUtil;
import org.junit.Assume;
import org.spockframework.runtime.extension.AbstractMethodInterceptor;
import org.spockframework.runtime.extension.IMethodInvocation;
Expand All @@ -24,13 +23,14 @@ public class LiquibaseIntegrationMethodInterceptor extends AbstractMethodInterce
private final LiquibaseIntegrationTestExtension.ErrorListener errorListener;

private static final String configuredTestSystems;
private static final String skippedTestSystems;

static {
//cache configured test systems for faster lookup
configuredTestSystems = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class).getCurrentConfiguredValue(ConfigurationValueConverter.STRING, null, "liquibase.sdk.testSystem.test").getValue();
if (configuredTestSystems != null) {
for (String definition : StringUtil.splitAndTrim(configuredTestSystems, ","))
testSystems.add(TestSystem.Definition.parse(definition));
skippedTestSystems = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class).getCurrentConfiguredValue(ConfigurationValueConverter.STRING, null, "liquibase.sdk.testSystem.skip").getValue();

for (String definition : TestSystem.getEnabledTestSystems(configuredTestSystems, skippedTestSystems)) {
testSystems.add(TestSystem.Definition.parse(definition));
}
}

Expand Down Expand Up @@ -71,7 +71,7 @@ private static void startContainers(List<FieldInfo> containers, IMethodInvocatio
for (FieldInfo field : containers) {
TestSystem testSystem = readContainerFromField(field, invocation);

Assume.assumeTrue("Not running test against " + testSystem.getDefinition() + ": liquibase.sdk.testSystem.test is " + configuredTestSystems, testSystem.shouldTest());
Assume.assumeTrue("Not running test against " + testSystem.getDefinition() + ": liquibase.sdk.testSystem.test is " + configuredTestSystems + " and liquibase.sdk.testSystem.skip is " + skippedTestSystems, testSystem.shouldTest());

testSystem.start();

Expand Down

0 comments on commit 8d3abcd

Please sign in to comment.