Support text blocks for inlined properties in @TestPropertySource
Prior to this commit, inlined properties could only be supplied as an
array of Strings as follows.
@TestPropertySource(properties = {
"key1 = value1",
"key2 = value2"
})
Although a user could supply a text block, it was previously rejected
due to a "single key-value pair per string" check in
TestPropertySourceUtils.convertInlinedPropertiesToMap(String...).
This commit removes that restriction and allows the above example to be
refactored to use a text block as follows.
@TestPropertySource(properties = """
key1 = value1
key2 = value2
"""
)
Closes gh-31053
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -36,6 +36,7 @@ import static org.springframework.test.context.support.TestPropertySourceUtils.I
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 4.1
|
||||
* @see InlinedPropertiesWithTextBlockTestPropertySourceTests
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration
|
||||
@@ -44,40 +45,37 @@ import static org.springframework.test.context.support.TestPropertySourceUtils.I
|
||||
class InlinedPropertiesTestPropertySourceTests {
|
||||
|
||||
@Autowired
|
||||
private ConfigurableEnvironment env;
|
||||
ConfigurableEnvironment env;
|
||||
|
||||
|
||||
private String property(String key) {
|
||||
return env.getProperty(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
void propertiesAreAvailableInEnvironment() {
|
||||
// Simple key/value pairs
|
||||
assertThat(property("foo")).isEqualTo("bar");
|
||||
assertThat(property("baz")).isEqualTo("quux");
|
||||
assertThat(property("enigma")).isEqualTo("42");
|
||||
assertEnvironmentProperty("foo", "bar");
|
||||
assertEnvironmentProperty("baz", "quux");
|
||||
assertEnvironmentProperty("enigma", "42");
|
||||
|
||||
// Values containing key/value delimiters (":", "=", " ")
|
||||
assertThat(property("x.y.z")).isEqualTo("a=b=c");
|
||||
assertThat(property("server.url")).isEqualTo("https://example.com");
|
||||
assertThat(property("key.value.1")).isEqualTo("key=value");
|
||||
assertThat(property("key.value.2")).isEqualTo("key=value");
|
||||
assertThat(property("key.value.3")).isEqualTo("key:value");
|
||||
assertEnvironmentProperty("x.y.z", "a=b=c");
|
||||
assertEnvironmentProperty("server.url", "https://example.com");
|
||||
assertEnvironmentProperty("key.value.1", "key=value");
|
||||
assertEnvironmentProperty("key.value.2", "key=value");
|
||||
assertEnvironmentProperty("key.value.3", "key:value");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
void propertyNameOrderingIsPreservedInEnvironment() {
|
||||
final String[] expectedPropertyNames = new String[] { "foo", "baz", "enigma", "x.y.z", "server.url",
|
||||
"key.value.1", "key.value.2", "key.value.3" };
|
||||
EnumerablePropertySource eps = (EnumerablePropertySource) env.getPropertySources().get(
|
||||
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
assertThat(eps.getPropertyNames()).isEqualTo(expectedPropertyNames);
|
||||
assertThat(eps.getPropertyNames()).containsExactly("foo", "baz", "enigma", "x.y.z", "server.url",
|
||||
"key.value.1", "key.value.2", "key.value.3" );
|
||||
}
|
||||
|
||||
private void assertEnvironmentProperty(String name, Object value) {
|
||||
assertThat(this.env.getProperty(name)).as("environment property '%s'", name).isEqualTo(value);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.env;
|
||||
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.EnumerablePropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.context.support.TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link TestPropertySource @TestPropertySource} support
|
||||
* with inlined properties supplied via text blocks.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 6.1
|
||||
* @see InlinedPropertiesTestPropertySourceTests
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration
|
||||
@DirtiesContext
|
||||
class InlinedPropertiesWithTextBlockTestPropertySourceTests {
|
||||
|
||||
@Nested
|
||||
@DirtiesContext
|
||||
@TestPropertySource(properties = """
|
||||
foo = bar
|
||||
baz quux
|
||||
enigma: 42
|
||||
x.y.z = a=b=c
|
||||
server.url = https://example.com
|
||||
key.value.1: key=value
|
||||
key.value.2 key=value
|
||||
key.value.3 key:value
|
||||
""")
|
||||
class AllInOneTextBlockTests {
|
||||
|
||||
@Autowired
|
||||
ConfigurableEnvironment env;
|
||||
|
||||
@Test
|
||||
void propertiesAreAvailableInEnvironment() {
|
||||
// Simple key/value pairs
|
||||
assertEnvironmentProperty(this.env, "foo", "bar");
|
||||
assertEnvironmentProperty(this.env, "baz", "quux");
|
||||
assertEnvironmentProperty(this.env, "enigma", "42");
|
||||
|
||||
// Values containing key/value delimiters (":", "=", " ")
|
||||
assertEnvironmentProperty(this.env, "x.y.z", "a=b=c");
|
||||
assertEnvironmentProperty(this.env, "server.url", "https://example.com");
|
||||
assertEnvironmentProperty(this.env, "key.value.1", "key=value");
|
||||
assertEnvironmentProperty(this.env, "key.value.2", "key=value");
|
||||
assertEnvironmentProperty(this.env, "key.value.3", "key:value");
|
||||
}
|
||||
|
||||
/**
|
||||
* Not necessarily preserved because the properties are all added at the
|
||||
* same time.
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
void propertyNameOrderingIsNotNecessarilyPreservedInEnvironment() {
|
||||
EnumerablePropertySource eps = (EnumerablePropertySource) env.getPropertySources().get(
|
||||
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
assertThat(eps.getPropertyNames()).containsExactlyInAnyOrder("foo", "baz", "enigma", "x.y.z",
|
||||
"server.url", "key.value.1", "key.value.2", "key.value.3");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DirtiesContext
|
||||
@TestPropertySource(properties = {
|
||||
"""
|
||||
foo = bar
|
||||
""",
|
||||
"""
|
||||
bar = baz
|
||||
""",
|
||||
"""
|
||||
baz = quux
|
||||
"""
|
||||
})
|
||||
class MultipleTextBlockTests {
|
||||
|
||||
@Autowired
|
||||
ConfigurableEnvironment env;
|
||||
|
||||
@Test
|
||||
void propertiesAreAvailableInEnvironment() {
|
||||
assertEnvironmentProperty(this.env, "foo", "bar");
|
||||
assertEnvironmentProperty(this.env, "bar", "baz");
|
||||
assertEnvironmentProperty(this.env, "baz", "quux");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
void propertyNameOrderingIsPreservedInEnvironment() {
|
||||
EnumerablePropertySource eps = (EnumerablePropertySource) env.getPropertySources().get(
|
||||
INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
assertThat(eps.getPropertyNames()).containsExactly("foo", "bar", "baz");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
static void assertEnvironmentProperty(Environment env, String name, Object value) {
|
||||
assertThat(env.getProperty(name)).as("environment property '%s'", name).isEqualTo(value);
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
/* no user beans required for these tests */
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,7 +19,6 @@ package org.springframework.test.context.support;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.assertj.core.api.SoftAssertions;
|
||||
@@ -28,7 +27,9 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.annotation.AnnotationConfigurationException;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.PropertySourceDescriptor;
|
||||
@@ -40,9 +41,11 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.test.context.support.TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME;
|
||||
import static org.springframework.test.context.support.TestPropertySourceUtils.addInlinedPropertiesToEnvironment;
|
||||
import static org.springframework.test.context.support.TestPropertySourceUtils.addPropertiesFilesToEnvironment;
|
||||
import static org.springframework.test.context.support.TestPropertySourceUtils.buildMergedTestPropertySources;
|
||||
@@ -58,19 +61,20 @@ class TestPropertySourceUtilsTests {
|
||||
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
|
||||
private static final String[] KEY_VALUE_PAIR = new String[] {"key = value"};
|
||||
private static final String[] KEY_VALUE_PAIR = {"key = value"};
|
||||
|
||||
private static final String[] FOO_LOCATIONS = new String[] {"classpath:/foo.properties"};
|
||||
private static final String[] FOO_LOCATIONS = {"classpath:/foo.properties"};
|
||||
|
||||
|
||||
@Test
|
||||
void emptyAnnotation() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> buildMergedTestPropertySources(EmptyPropertySources.class))
|
||||
.withMessageStartingWith("Could not detect default properties file for test class")
|
||||
.withMessageContaining("class path resource")
|
||||
.withMessageContaining("does not exist")
|
||||
.withMessageContaining("EmptyPropertySources.properties");
|
||||
.withMessageContainingAll(
|
||||
"Could not detect default properties file for test class",
|
||||
"class path resource",
|
||||
"does not exist",
|
||||
"EmptyPropertySources.properties");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -260,16 +264,26 @@ class TestPropertySourceUtilsTests {
|
||||
|
||||
@Test
|
||||
void addInlinedPropertiesToEnvironmentWithMalformedUnicodeInValue() {
|
||||
String properties = "key = \\uZZZZ";
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> addInlinedPropertiesToEnvironment(new MockEnvironment(), asArray("key = \\uZZZZ")))
|
||||
.withMessageContaining("Failed to load test environment property");
|
||||
.isThrownBy(() -> addInlinedPropertiesToEnvironment(new MockEnvironment(), properties))
|
||||
.withMessageContaining("Failed to load test environment properties from [%s]", properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addInlinedPropertiesToEnvironmentWithMultipleKeyValuePairsInSingleInlinedProperty() {
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> addInlinedPropertiesToEnvironment(new MockEnvironment(), asArray("a=b\nx=y")))
|
||||
.withMessageContaining("Failed to load exactly one test environment property");
|
||||
ConfigurableEnvironment environment = new MockEnvironment();
|
||||
MutablePropertySources propertySources = environment.getPropertySources();
|
||||
propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
assertThat(propertySources).isEmpty();
|
||||
addInlinedPropertiesToEnvironment(environment, """
|
||||
a=b
|
||||
x=y
|
||||
""");
|
||||
assertThat(propertySources).hasSize(1);
|
||||
PropertySource<?> propertySource = propertySources.get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
assertThat(propertySource).isInstanceOf(MapPropertySource.class);
|
||||
assertThat(((MapPropertySource) propertySource).getSource()).containsExactly(entry("a", "b"), entry("x", "y"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -279,9 +293,11 @@ class TestPropertySourceUtilsTests {
|
||||
MutablePropertySources propertySources = environment.getPropertySources();
|
||||
propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
assertThat(propertySources).isEmpty();
|
||||
addInlinedPropertiesToEnvironment(environment, asArray(" "));
|
||||
addInlinedPropertiesToEnvironment(environment, " ");
|
||||
assertThat(propertySources).hasSize(1);
|
||||
assertThat(((Map<?, ?>) propertySources.iterator().next().getSource())).isEmpty();
|
||||
PropertySource<?> propertySource = propertySources.get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
assertThat(propertySource).isInstanceOf(MapPropertySource.class);
|
||||
assertThat(((MapPropertySource) propertySource).getSource()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user