From 8d3abcdf19de204dbc519d350f7374f4137eb8bb Mon Sep 17 00:00:00 2001 From: Steven Massaro Date: Wed, 11 May 2022 12:46:21 -0600 Subject: [PATCH] Test System Framework: add support for "liquibase.sdk.testSystem.skip" property (#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. --- .../testing/testsystem/TestSystem.java | 39 ++++++++++++++++--- .../testing/testsystem/TestSystemFactory.java | 8 +--- ...LiquibaseIntegrationMethodInterceptor.java | 12 +++--- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/TestSystem.java b/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/TestSystem.java index 244cca9241d..8aecdb7f7a5 100644 --- a/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/TestSystem.java +++ b/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/TestSystem.java @@ -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; @@ -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. @@ -30,6 +32,7 @@ public abstract class TestSystem implements TestRule, Plugin { private static final SortedSet testSystems = new TreeSet<>(); private static final String configuredTestSystems; + private static final String skippedTestSystems; private final Definition definition; @@ -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 getEnabledTestSystems(String configuredTestSystems, String skippedTestSystems) { + List 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 skippedTestSystemsList = CollectionUtil.createIfNull(StringUtil.splitAndTrim(skippedTestSystems, ",")); + returnList = returnList.stream().filter(ts -> !skippedTestSystemsList.contains(ts)).collect(Collectors.toList()); } + return returnList; } /** @@ -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 errors = new ArrayList<>(); diff --git a/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/TestSystemFactory.java b/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/TestSystemFactory.java index 4ac92c9ffd7..28b4b3e57a5 100644 --- a/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/TestSystemFactory.java +++ b/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/TestSystemFactory.java @@ -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; /** @@ -67,8 +62,9 @@ public TestSystem getTestSystem(TestSystem.Definition definition) { private List 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); } /** diff --git a/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/spock/LiquibaseIntegrationMethodInterceptor.java b/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/spock/LiquibaseIntegrationMethodInterceptor.java index 2ff657c7e97..a3a9d1869fd 100644 --- a/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/spock/LiquibaseIntegrationMethodInterceptor.java +++ b/liquibase-extension-testing/src/main/java/liquibase/extension/testing/testsystem/spock/LiquibaseIntegrationMethodInterceptor.java @@ -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; @@ -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)); } } @@ -71,7 +71,7 @@ private static void startContainers(List 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();