-
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 that can be used for loading YAML files…
… through the @TestPropertySource annotation.
- Loading branch information
Showing
6 changed files
with
206 additions
and
1 deletion.
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
72 changes: 72 additions & 0 deletions
72
...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,72 @@ | ||
/* | ||
* 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.List; | ||
|
||
import org.springframework.boot.env.YamlPropertySourceLoader; | ||
import org.springframework.core.env.CompositePropertySource; | ||
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.StringUtils; | ||
|
||
/** | ||
* An implementation of {@link PropertySourceFactory} that delegates the loading of | ||
* {@code PropertySource} to {@link YamlPropertySourceLoader}. If the provided YAML file | ||
* contains multiple documents (separated by {@code ---}), the property sources from later | ||
* documents will take precedence, overriding any conflicting values defined in earlier | ||
* documents. | ||
* | ||
* @author Dmytro Nosan | ||
* @since 3.4.0 | ||
*/ | ||
public class YamlPropertySourceFactory implements PropertySourceFactory { | ||
|
||
private 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 = this.loader.load(propertySourceName, resource); | ||
return new YamlCompositePropertySource(propertySourceName, propertySources); | ||
} | ||
|
||
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); | ||
} | ||
|
||
private static final class YamlCompositePropertySource extends CompositePropertySource { | ||
|
||
YamlCompositePropertySource(String name, List<PropertySource<?>> propertySources) { | ||
super(name); | ||
propertySources.forEach(this::addFirstPropertySource); | ||
} | ||
|
||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...java/org/springframework/boot/test/context/YamlPropertySourceFactoryIntegrationTests.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,55 @@ | ||
/* | ||
* 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.TestPropertySource; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Integration tests for {@link YamlPropertySourceFactory} with | ||
* {@link TestPropertySource}. | ||
* | ||
* @author Dmytro Nosan | ||
*/ | ||
@SpringJUnitConfig | ||
@TestPropertySource(locations = { "classpath:test.yaml", "classpath:test1.yaml" }, | ||
factory = YamlPropertySourceFactory.class) | ||
class YamlPropertySourceFactoryIntegrationTests { | ||
|
||
@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"); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
static class Config { | ||
|
||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...t/src/test/java/org/springframework/boot/test/context/YamlPropertySourceFactoryTests.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 org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.core.env.PropertySource; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.core.io.support.EncodedResource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.willReturn; | ||
import static org.mockito.Mockito.spy; | ||
|
||
/** | ||
* Tests for {@link YamlPropertySourceFactory}. | ||
* | ||
* @author Dmytro Nosan | ||
*/ | ||
class YamlPropertySourceFactoryTests { | ||
|
||
private final YamlPropertySourceFactory factory = new YamlPropertySourceFactory(); | ||
|
||
@Test | ||
void shouldCreatePropertySourceWithYamlPropertySourceLoaderWithGivenName() throws IOException { | ||
EncodedResource resource = new EncodedResource(new ClassPathResource("test.yaml")); | ||
PropertySource<?> propertySource = this.factory.createPropertySource("test", resource); | ||
assertThat(propertySource.getName()).isEqualTo("test"); | ||
assertProperties(propertySource); | ||
} | ||
|
||
@Test | ||
void shouldCreatePropertySourceWithYamlPropertySourceLoaderWithResourceDescriptionName() throws IOException { | ||
EncodedResource resource = new EncodedResource(new ClassPathResource("test.yaml")); | ||
PropertySource<?> propertySource = this.factory.createPropertySource(null, resource); | ||
assertThat(propertySource.getName()).isEqualTo("class path resource [test.yaml]"); | ||
assertProperties(propertySource); | ||
} | ||
|
||
@Test | ||
void shouldCreatePropertySourceWithYamlPropertySourceLoaderWithGeneratedName() throws IOException { | ||
ClassPathResource resource = spy(new ClassPathResource("test.yaml")); | ||
willReturn(null).given(resource).getDescription(); | ||
PropertySource<?> propertySource = this.factory.createPropertySource(null, new EncodedResource(resource)); | ||
assertThat(propertySource.getName()).startsWith("ClassPathResource@"); | ||
assertProperties(propertySource); | ||
} | ||
|
||
private static void assertProperties(PropertySource<?> propertySource) { | ||
assertThat(propertySource.getProperty("spring.bar")).isEqualTo("bar"); | ||
assertThat(propertySource.getProperty("spring.foo")).isEqualTo("baz"); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
spring-boot-project/spring-boot-test/src/test/resources/test.yaml
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,6 @@ | ||
spring: | ||
bar: foo | ||
--- | ||
spring: | ||
bar: bar | ||
foo: baz |
2 changes: 2 additions & 0 deletions
2
spring-boot-project/spring-boot-test/src/test/resources/test1.yaml
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,2 @@ | ||
spring: | ||
buzz: fazz |