Skip to content

Switch to TestPropertySourceUtils for property parsing #4384

@artembilan

Description

@artembilan

We would like to do something like:

@WebIntegrationTest({"script=script.groovy", "variables=foo=FOO\n bar=BAR"})

https://github.com/spring-cloud/spring-cloud-stream-modules/issues/56.

Unfortunately, the SpringApplicationContextLoader.extractEnvironmentProperties does this:

String content = StringUtils.arrayToDelimitedString(values, LINE_SEPARATOR);
Properties properties = new Properties();
try {
     properties.load(new StringReader(content));
     return asMap(properties);
}

So, we end up with the result:

0 = {Hashtable$Entry@1327} "org.springframework.boot.test.IntegrationTest" -> "true"
1 = {Hashtable$Entry@1328} "script" -> "script.groovy"
2 = {Hashtable$Entry@1329} "bar" -> "BAR"
3 = {Hashtable$Entry@1330} "variables" -> "foo=FOO"

instead of expected variables" -> "foo=FOO\n bar=BAR".

The solution is in the TestPropertySourceUtils:

public static Map<String, Object> convertInlinedPropertiesToMap(String[] inlinedProperties) {
        Assert.notNull(inlinedProperties, "inlinedProperties must not be null");
        Map<String, Object> map = new LinkedHashMap<String, Object>();

        Properties props = new Properties();
        for (String pair : inlinedProperties) {
            if (!StringUtils.hasText(pair)) {
                continue;
            }

            try {
                props.load(new StringReader(pair));
            }
            catch (Exception e) {
                throw new IllegalStateException("Failed to load test environment property from [" + pair + "].", e);
            }
            Assert.state(props.size() == 1, "Failed to load exactly one test environment property from [" + pair + "].");

            for (String name : props.stringPropertyNames()) {
                map.put(name, props.getProperty(name));
            }
            props.clear();
        }

        return map;
    }

In other words: load each values pair to the target map separately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions