-
Notifications
You must be signed in to change notification settings - Fork 40.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add YamlPropertySourceFactory and @TestYamlPropertySource
that can be applied to a test class to add PropertySource loaded from YAML files to the Environment This commit introduces new functionality to load properties from YAML files in tests. The @TestYamlPropertySource annotation is added to allow test classes to specify YAML files as property sources. The @TestYamlPropertySource provides a convenient alternative for @TestPropertySource(factory=YamlPropertySourceFactory.class). The YamlPropertySourceFactory class is also introduced to enable the loading of YAML files into the Environment through @TestPropertySource.
- Loading branch information
Showing
12 changed files
with
380 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...boot-test/src/main/java/org/springframework/boot/test/context/TestYamlPropertySource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2012-2024 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.boot.test.context; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.core.annotation.AliasFor; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
/** | ||
* {@code @TestYamlPropertySource} is an annotation that can be applied to a test class to | ||
* configure the locations of YAML files and inlined properties to be added to the | ||
* Environment's set of PropertySources for an ApplicationContext for integration tests. | ||
* <p> | ||
* Provides a convenient alternative for | ||
* {@code @TestPropertySource(locations = "...", factory = YamlPropertySourceFactory.class)}. | ||
* <p> | ||
* {@code @TestYamlPropertySource} should be considered as {@code @TestPropertySource} but | ||
* for YAML files. It intentionally does not support multi-document YAML files to maintain | ||
* consistency with the behavior of {@code @TestPropertySource}. | ||
* | ||
* @author Dmytro Nosan | ||
* @since 3.5.0 | ||
* @see YamlPropertySourceFactory | ||
* @see TestPropertySource | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Inherited | ||
@TestPropertySource(factory = YamlPropertySourceFactory.class) | ||
@Repeatable(TestYamlPropertySources.class) | ||
public @interface TestYamlPropertySource { | ||
|
||
/** | ||
* Alias for {@link TestPropertySource#value()}. | ||
* @return The resource locations of YAML files. | ||
* @see TestPropertySource#value() for more details. | ||
*/ | ||
@AliasFor(attribute = "value", annotation = TestPropertySource.class) | ||
String[] value() default {}; | ||
|
||
/** | ||
* Alias for {@link TestPropertySource#locations()}. | ||
* @return The resource locations of YAML files. | ||
* @see TestPropertySource#locations() for more details. | ||
*/ | ||
@AliasFor(attribute = "locations", annotation = TestPropertySource.class) | ||
String[] locations() default {}; | ||
|
||
/** | ||
* Alias for {@link TestPropertySource#inheritLocations()}. | ||
* @return Whether test property source {@link #locations} from superclasses and | ||
* enclosing classes should be <em>inherited</em>. | ||
* @see TestPropertySource#inheritLocations() for more details. | ||
*/ | ||
@AliasFor(attribute = "inheritLocations", annotation = TestPropertySource.class) | ||
boolean inheritLocations() default true; | ||
|
||
/** | ||
* Alias for {@link TestPropertySource#properties()}. | ||
* @return <em>Inlined properties</em> in the form of <em>key-value</em> pairs that | ||
* should be added to the Environment | ||
* @see TestPropertySource#properties() for more details. | ||
*/ | ||
@AliasFor(attribute = "properties", annotation = TestPropertySource.class) | ||
String[] properties() default {}; | ||
|
||
/** | ||
* Alias for {@link TestPropertySource#inheritProperties()}. | ||
* @return Whether inlined test {@link #properties} from superclasses and enclosing | ||
* classes should be <em>inherited</em>. | ||
* @see TestPropertySource#inheritProperties() for more details. | ||
*/ | ||
@AliasFor(attribute = "inheritProperties", annotation = TestPropertySource.class) | ||
boolean inheritProperties() default true; | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...oot-test/src/main/java/org/springframework/boot/test/context/TestYamlPropertySources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2012-2024 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.boot.test.context; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* {@code @TestYamlPropertySources} is a container for one or more | ||
* {@link TestYamlPropertySource @TestYamlPropertySource} declarations. | ||
* | ||
* @author Dmytro Nosan | ||
* @since 3.5.0 | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Inherited | ||
public @interface TestYamlPropertySources { | ||
|
||
/** | ||
* An array of one or more {@link TestYamlPropertySource @TestYamlPropertySource} | ||
* declarations. | ||
* @return {@link TestYamlPropertySource @TestYamlPropertySource} annotations. | ||
*/ | ||
TestYamlPropertySource[] value(); | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...t-test/src/main/java/org/springframework/boot/test/context/YamlPropertySourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2012-2024 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.boot.test.context; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.springframework.boot.env.YamlPropertySourceLoader; | ||
import org.springframework.core.env.MapPropertySource; | ||
import org.springframework.core.env.PropertySource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.support.EncodedResource; | ||
import org.springframework.core.io.support.PropertySourceFactory; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* An implementation of {@link PropertySourceFactory} that delegates the loading of | ||
* {@code PropertySource} to {@link YamlPropertySourceLoader}. | ||
* <p> | ||
* Even though {@link YamlPropertySourceLoader} supports multi-document YAML files, the | ||
* {@code YamlPropertySourceFactory} intentionally does not allow this. | ||
* | ||
* @author Dmytro Nosan | ||
* @since 3.5.0 | ||
* @see TestYamlPropertySource | ||
*/ | ||
public class YamlPropertySourceFactory implements PropertySourceFactory { | ||
|
||
private static final YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); | ||
|
||
@Override | ||
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) throws IOException { | ||
Resource resource = encodedResource.getResource(); | ||
String propertySourceName = getPropertySourceName(name, resource); | ||
List<PropertySource<?>> propertySources = loader.load(propertySourceName, resource); | ||
Assert.isTrue(propertySources.size() <= 1, () -> resource + " is a multi-document YAML file"); | ||
if (propertySources.isEmpty()) { | ||
return new MapPropertySource(name, Collections.emptyMap()); | ||
} | ||
return propertySources.get(0); | ||
} | ||
|
||
private static String getPropertySourceName(String name, Resource resource) { | ||
if (StringUtils.hasText(name)) { | ||
return name; | ||
} | ||
String description = resource.getDescription(); | ||
if (StringUtils.hasText(description)) { | ||
return description; | ||
} | ||
return resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...st/java/org/springframework/boot/test/context/TestYamlPropertySourceIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2012-2024 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.boot.test.context; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Integration tests for {@link YamlPropertySourceFactory} with | ||
* {@link TestYamlPropertySource}. | ||
* | ||
* @author Dmytro Nosan | ||
*/ | ||
@SpringJUnitConfig | ||
@TestYamlPropertySource({ "test.yaml", "test1.yaml" }) | ||
@TestYamlPropertySource(locations = "test2.yaml", properties = "key:value") | ||
class TestYamlPropertySourceIntegrationTests { | ||
|
||
@Autowired | ||
private Environment environment; | ||
|
||
@Test | ||
void loadProperties() { | ||
assertThat(this.environment.getProperty("spring.bar")).isEqualTo("bar"); | ||
assertThat(this.environment.getProperty("spring.foo")).isEqualTo("baz"); | ||
assertThat(this.environment.getProperty("spring.buzz")).isEqualTo("fazz"); | ||
assertThat(this.environment.getProperty("spring.boot")).isEqualTo("boot"); | ||
assertThat(this.environment.getProperty("key")).isEqualTo("value"); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
static class Config { | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.