Skip to content

Commit f47f699

Browse files
committed
Create ResourcePropertySourceLoaderTest.java
1 parent 95d3d02 commit f47f699

1 file changed

Lines changed: 277 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.spring.config.context.annotation;
19+
20+
import org.junit.After;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.springframework.beans.factory.BeanDefinitionStoreException;
24+
import org.springframework.context.ConfigurableApplicationContext;
25+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
26+
import org.springframework.core.env.CompositePropertySource;
27+
import org.springframework.core.env.ConfigurableEnvironment;
28+
import org.springframework.core.env.MutablePropertySources;
29+
import org.springframework.core.env.PropertySource;
30+
31+
import java.util.Collection;
32+
import java.util.Iterator;
33+
import java.util.ListIterator;
34+
import java.util.function.BiConsumer;
35+
36+
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertNotNull;
38+
import static org.junit.Assert.assertSame;
39+
import static org.junit.Assert.assertTrue;
40+
import static org.springframework.core.env.StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME;
41+
import static org.springframework.core.env.StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME;
42+
43+
/**
44+
* {@link ResourcePropertySourceLoader} Test
45+
*
46+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
47+
* @see ResourcePropertySourceLoader
48+
* @see ResourcePropertySource
49+
* @since 1.0.0
50+
*/
51+
public class ResourcePropertySourceLoaderTest {
52+
53+
static final Class<ResourcePropertySource> RESOURCE_PROPERTY_SOURCE_CLASS = ResourcePropertySource.class;
54+
55+
@Before
56+
public void before() {
57+
58+
}
59+
60+
@After
61+
public void after() {
62+
63+
}
64+
65+
@Test
66+
public void testOnDefaultConfig() {
67+
testInSpringContainer((context, environment) -> {
68+
assertDefaultPropertySource(environment, DefaultConfig.class);
69+
}, DefaultConfig.class);
70+
}
71+
72+
@Test
73+
public void testOnNamedConfig() {
74+
testInSpringContainer((context, environment) -> {
75+
assertNamedPropertySource(environment, "test-property-source");
76+
}, NamedConfig.class);
77+
}
78+
79+
@Test
80+
public void testOnFirstConfig() {
81+
testInSpringContainer((context, environment) -> {
82+
assertFirstPropertySource(environment, FirstConfig.class);
83+
}, FirstConfig.class);
84+
}
85+
86+
@Test
87+
public void testOnBeforeConfig() {
88+
testInSpringContainer((context, environment) -> {
89+
assertBeforePropertySource(environment, BeforeConfig.class);
90+
}, BeforeConfig.class);
91+
}
92+
93+
@Test
94+
public void testOnAfterConfig() {
95+
testInSpringContainer((context, environment) -> {
96+
assertAfterPropertySource(environment, AfterConfig.class);
97+
}, AfterConfig.class);
98+
}
99+
100+
@Test
101+
public void testOnIgnoreResourceNotFoundConfig() {
102+
testInSpringContainer((context, environment) -> {
103+
MutablePropertySources propertySources = environment.getPropertySources();
104+
assertEquals(2, propertySources.size());
105+
assertNotNull(propertySources.get(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME));
106+
assertNotNull(propertySources.get(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME));
107+
}, IgnoreResourceNotFoundConfig.class);
108+
}
109+
110+
@Test(expected = BeanDefinitionStoreException.class)
111+
public void testOnNotFoundConfig() {
112+
testInSpringContainer((context, environment) -> {
113+
}, NotFoundConfig.class);
114+
}
115+
116+
@Test
117+
public void testOnAutoRefreshedConfig() {
118+
testInSpringContainer((context, environment) -> {
119+
assertDefaultPropertySource(environment, AutoRefreshedConfig.class);
120+
}, AutoRefreshedConfig.class);
121+
}
122+
123+
void assertDefaultPropertySource(ConfigurableEnvironment environment, Class<?> introspectedClass) {
124+
PropertySource<?> propertySource = assertLastPropertySource(environment);
125+
assertDefaultPropertySourceName(propertySource, introspectedClass);
126+
assertPropertySource(propertySource);
127+
assertProperties(environment);
128+
}
129+
130+
void assertNamedPropertySource(ConfigurableEnvironment environment, String propertySourceName) {
131+
MutablePropertySources propertySources = environment.getPropertySources();
132+
PropertySource propertySource = propertySources.get(propertySourceName);
133+
assertSame(assertLastPropertySource(environment), propertySource);
134+
assertPropertySource(propertySource);
135+
assertProperties(environment);
136+
}
137+
138+
void assertFirstPropertySource(ConfigurableEnvironment environment, Class<?> introspectedClass) {
139+
MutablePropertySources propertySources = environment.getPropertySources();
140+
Iterator<PropertySource<?>> iterator = propertySources.iterator();
141+
PropertySource<?> propertySource = iterator.next();
142+
assertDefaultPropertySourceName(propertySource, introspectedClass);
143+
assertPropertySource(propertySource);
144+
assertProperties(environment);
145+
}
146+
147+
void assertBeforePropertySource(ConfigurableEnvironment environment, Class<?> introspectedClass) {
148+
MutablePropertySources propertySources = environment.getPropertySources();
149+
assertEquals(3, propertySources.size());
150+
151+
Iterator<PropertySource<?>> iterator = propertySources.iterator();
152+
assertTrue(iterator instanceof ListIterator);
153+
ListIterator<PropertySource<?>> listIterator = (ListIterator<PropertySource<?>>) iterator;
154+
ResourcePropertySource resourcePropertySource = introspectedClass.getAnnotation(RESOURCE_PROPERTY_SOURCE_CLASS);
155+
String before = resourcePropertySource.before();
156+
while (listIterator.hasNext()) {
157+
PropertySource<?> propertySource = listIterator.next();
158+
if (propertySource.getName().equals(before)) {
159+
break;
160+
}
161+
}
162+
listIterator.previous();
163+
PropertySource<?> propertySource = listIterator.previous();
164+
assertDefaultPropertySourceName(propertySource, introspectedClass);
165+
assertPropertySource(propertySource);
166+
assertProperties(environment);
167+
}
168+
169+
void assertAfterPropertySource(ConfigurableEnvironment environment, Class<?> introspectedClass) {
170+
MutablePropertySources propertySources = environment.getPropertySources();
171+
assertEquals(3, propertySources.size());
172+
173+
Iterator<PropertySource<?>> iterator = propertySources.iterator();
174+
ResourcePropertySource resourcePropertySource = introspectedClass.getAnnotation(RESOURCE_PROPERTY_SOURCE_CLASS);
175+
String after = resourcePropertySource.after();
176+
while (iterator.hasNext()) {
177+
PropertySource<?> propertySource = iterator.next();
178+
if (propertySource.getName().equals(after)) {
179+
break;
180+
}
181+
}
182+
PropertySource<?> propertySource = iterator.next();
183+
assertDefaultPropertySourceName(propertySource, introspectedClass);
184+
assertPropertySource(propertySource);
185+
assertProperties(environment);
186+
}
187+
188+
PropertySource<?> assertLastPropertySource(ConfigurableEnvironment environment) {
189+
MutablePropertySources propertySources = environment.getPropertySources();
190+
Iterator<PropertySource<?>> iterator = propertySources.iterator();
191+
int size = propertySources.size();
192+
PropertySource<?> propertySource = null;
193+
for (int i = 0; i < size; i++) {
194+
propertySource = iterator.next();
195+
}
196+
return propertySource;
197+
}
198+
199+
void assertDefaultPropertySourceName(PropertySource<?> propertySource, Class<?> introspectedClass) {
200+
String propertySourceName = introspectedClass.getName() + "@" + RESOURCE_PROPERTY_SOURCE_CLASS.getName();
201+
assertEquals(propertySourceName, propertySource.getName());
202+
}
203+
204+
void assertPropertySource(PropertySource<?> propertySource) {
205+
assertTrue(propertySource instanceof CompositePropertySource);
206+
CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource;
207+
Collection<PropertySource<?>> internalPropertySources = compositePropertySource.getPropertySources();
208+
assertEquals(2, internalPropertySources.size());
209+
assertProperties(propertySource);
210+
}
211+
212+
void assertProperties(PropertySource<?> propertySource) {
213+
assertEquals("1", propertySource.getProperty("a"));
214+
assertEquals("3", propertySource.getProperty("b"));
215+
}
216+
217+
void assertProperties(ConfigurableEnvironment environment) {
218+
assertEquals("1", environment.getProperty("a"));
219+
assertEquals("3", environment.getProperty("b"));
220+
}
221+
222+
void testInSpringContainer(BiConsumer<ConfigurableApplicationContext, ConfigurableEnvironment> consumer, Class<?>... configClasses) {
223+
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(configClasses);
224+
consumer.accept(context, context.getEnvironment());
225+
context.close();
226+
}
227+
228+
@ResourcePropertySource("classpath*:/META-INF/test/*.properties")
229+
static class DefaultConfig {
230+
}
231+
232+
@ResourcePropertySource(
233+
name = "test-property-source",
234+
value = "classpath*:/META-INF/test/*.properties"
235+
)
236+
static class NamedConfig {
237+
}
238+
239+
@ResourcePropertySource(
240+
value = "classpath*:/META-INF/test/*.properties",
241+
first = true
242+
)
243+
static class FirstConfig {
244+
}
245+
246+
@ResourcePropertySource(
247+
value = "classpath*:/META-INF/test/*.properties",
248+
before = SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
249+
)
250+
static class BeforeConfig {
251+
}
252+
253+
@ResourcePropertySource(
254+
value = "classpath*:/META-INF/test/*.properties",
255+
after = SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME
256+
)
257+
static class AfterConfig {
258+
}
259+
260+
@ResourcePropertySource(
261+
value = "classpath*:/not-found.properties",
262+
ignoreResourceNotFound = true
263+
)
264+
static class IgnoreResourceNotFoundConfig {
265+
}
266+
267+
@ResourcePropertySource("classpath*:/not-found.properties")
268+
static class NotFoundConfig {
269+
}
270+
271+
@ResourcePropertySource(
272+
value = "classpath*:/META-INF/test/*.properties",
273+
autoRefreshed = true
274+
)
275+
static class AutoRefreshedConfig {
276+
}
277+
}

0 commit comments

Comments
 (0)