Search in sources :

Example 1 with ConfigurationValueConverter

use of liquibase.configuration.ConfigurationValueConverter in project liquibase by liquibase.

the class TestSystem method getConfiguredValue.

/**
 * Returns the configured value for the given propertyName. It will check (in priority order):
 * <ol>
 * <li>properties set directly on this object</li>
 * <li>liquibase.sdk.testSystem.[name].[profile(s)].propertyName in the order the profiles are set on this object</li>
 * <li>liquibase.sdk.testSystem.[name].propertyName</li>
 * <li>liquibase.sdk.testSystem.default.propertyName</li>
 * </ol>
 * <br>
 * If a value is not found, it will return null or throw an {@link UnexpectedLiquibaseException} if 'required' is true.
 */
public <T> T getConfiguredValue(String propertyName, ConfigurationValueConverter<T> converter, boolean required) {
    ConfigurationValueConverter<T> finalConverter = value -> {
        if (value instanceof String && ((String) value).contains("${")) {
            final Matcher matcher = Pattern.compile("(\\$\\{.+?})").matcher((String) value);
            while (matcher.find()) {
                final String config = matcher.group(1).replace("${", "").replace("}", "").trim();
                value = ((String) value).replace(matcher.group(1), getConfiguredValue(config, String.class));
            }
        }
        if (converter == null) {
            return (T) value;
        } else {
            return converter.convert(value);
        }
    };
    final SortedMap<String, Object> properties = definition.getProperties();
    if (properties.containsKey(propertyName)) {
        return finalConverter.convert(properties.get(propertyName));
    }
    final LiquibaseConfiguration config = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class);
    ConfiguredValue<T> configuredValue;
    // first check profiles
    for (String profile : definition.getProfiles()) {
        configuredValue = config.getCurrentConfiguredValue(finalConverter, null, "liquibase.sdk.testSystem." + getDefinition().getName() + ".profiles." + profile + "." + propertyName);
        if (configuredValue.found()) {
            return configuredValue.getValue();
        }
    }
    configuredValue = config.getCurrentConfiguredValue(finalConverter, null, "liquibase.sdk.testSystem." + getDefinition().getName() + "." + propertyName);
    if (configuredValue.found()) {
        return configuredValue.getValue();
    }
    // fall back to "default" setup
    configuredValue = config.getCurrentConfiguredValue(finalConverter, null, "liquibase.sdk.testSystem.default." + propertyName);
    if (configuredValue.found()) {
        return configuredValue.getValue();
    }
    if (required) {
        throw new UnexpectedLiquibaseException("No required liquibase.sdk.testSystem configuration for " + getDefinition().getName() + " of " + propertyName + " set");
    }
    return null;
}
Also used : Statement(org.junit.runners.model.Statement) java.util(java.util) TestRule(org.junit.rules.TestRule) Plugin(liquibase.plugin.Plugin) ConfiguredValue(liquibase.configuration.ConfiguredValue) Description(org.junit.runner.Description) MultipleFailureException(org.junit.runners.model.MultipleFailureException) StringUtil(liquibase.util.StringUtil) Matcher(java.util.regex.Matcher) ConfigurationValueConverter(liquibase.configuration.ConfigurationValueConverter) LiquibaseConfiguration(liquibase.configuration.LiquibaseConfiguration) ObjectUtil(liquibase.util.ObjectUtil) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) Assume(org.junit.Assume) Pattern(java.util.regex.Pattern) Scope(liquibase.Scope) Matcher(java.util.regex.Matcher) LiquibaseConfiguration(liquibase.configuration.LiquibaseConfiguration) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

java.util (java.util)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Scope (liquibase.Scope)1 ConfigurationValueConverter (liquibase.configuration.ConfigurationValueConverter)1 ConfiguredValue (liquibase.configuration.ConfiguredValue)1 LiquibaseConfiguration (liquibase.configuration.LiquibaseConfiguration)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 Plugin (liquibase.plugin.Plugin)1 ObjectUtil (liquibase.util.ObjectUtil)1 StringUtil (liquibase.util.StringUtil)1 Assume (org.junit.Assume)1 TestRule (org.junit.rules.TestRule)1 Description (org.junit.runner.Description)1 MultipleFailureException (org.junit.runners.model.MultipleFailureException)1 Statement (org.junit.runners.model.Statement)1