|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.test.context; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.core.env.PropertySource; |
| 24 | +import org.springframework.core.io.ClassPathResource; |
| 25 | +import org.springframework.core.io.support.EncodedResource; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.mockito.BDDMockito.willReturn; |
| 29 | +import static org.mockito.Mockito.spy; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for {@link YamlPropertySourceFactory}. |
| 33 | + * |
| 34 | + * @author Dmytro Nosan |
| 35 | + */ |
| 36 | +class YamlPropertySourceFactoryTests { |
| 37 | + |
| 38 | + private final YamlPropertySourceFactory factory = new YamlPropertySourceFactory(); |
| 39 | + |
| 40 | + @Test |
| 41 | + void shouldCreatePropertySourceWithYamlPropertySourceLoaderWithGivenName() throws IOException { |
| 42 | + EncodedResource resource = new EncodedResource(new ClassPathResource("test.yaml")); |
| 43 | + PropertySource<?> propertySource = this.factory.createPropertySource("test", resource); |
| 44 | + assertThat(propertySource.getName()).isEqualTo("test"); |
| 45 | + assertProperties(propertySource); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void shouldCreatePropertySourceWithYamlPropertySourceLoaderWithResourceDescriptionName() throws IOException { |
| 50 | + EncodedResource resource = new EncodedResource(new ClassPathResource("test.yaml")); |
| 51 | + PropertySource<?> propertySource = this.factory.createPropertySource(null, resource); |
| 52 | + assertThat(propertySource.getName()).isEqualTo("class path resource [test.yaml]"); |
| 53 | + assertProperties(propertySource); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void shouldCreatePropertySourceWithYamlPropertySourceLoaderWithGeneratedName() throws IOException { |
| 58 | + ClassPathResource resource = spy(new ClassPathResource("test.yaml")); |
| 59 | + willReturn(null).given(resource).getDescription(); |
| 60 | + PropertySource<?> propertySource = this.factory.createPropertySource(null, new EncodedResource(resource)); |
| 61 | + assertThat(propertySource.getName()).startsWith("ClassPathResource@"); |
| 62 | + assertProperties(propertySource); |
| 63 | + } |
| 64 | + |
| 65 | + private static void assertProperties(PropertySource<?> propertySource) { |
| 66 | + assertThat(propertySource.getProperty("spring.bar")).isEqualTo("bar"); |
| 67 | + assertThat(propertySource.getProperty("spring.foo")).isEqualTo("baz"); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments