1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration2.spring;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import org.apache.commons.configuration2.PropertiesConfiguration;
23 import org.junit.jupiter.api.AfterAll;
24 import org.junit.jupiter.api.BeforeAll;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.context.annotation.Bean;
29 import org.springframework.context.annotation.Configuration;
30 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
31 import org.springframework.core.env.ConfigurableEnvironment;
32 import org.springframework.core.env.MutablePropertySources;
33 import org.springframework.test.context.ContextConfiguration;
34 import org.springframework.test.context.junit.jupiter.SpringExtension;
35
36
37
38
39 @ExtendWith(SpringExtension.class)
40 @ContextConfiguration
41 public class TestConfigurationPropertySource {
42
43 @Configuration
44 static class Config {
45
46 @Bean
47 public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(
48 final ConfigurableEnvironment env) {
49 final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
50
51
52 final MutablePropertySources sources = new MutablePropertySources();
53 sources.addLast(createConfigPropertySource());
54 configurer.setPropertySources(sources);
55 configurer.setEnvironment(env);
56 return configurer;
57 }
58 }
59
60 private static final String TEST_PROPERTY = "test.property";
61
62 private static final String TEST_SYSTEM_PROPERTY = "test.system.property";
63
64 private static final String TEST_VALUE = "testVALUE";
65
66 private static ConfigurationPropertySource createConfigPropertySource() {
67 final PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
68 propertiesConfiguration.addProperty(TEST_PROPERTY, TEST_VALUE);
69 propertiesConfiguration.addProperty(TEST_SYSTEM_PROPERTY, "${sys:" + TEST_SYSTEM_PROPERTY + "}");
70 return new ConfigurationPropertySource("test configuration", propertiesConfiguration);
71 }
72
73 @BeforeAll
74 public static void setUp() {
75 System.setProperty(TEST_SYSTEM_PROPERTY, TEST_VALUE);
76 }
77
78 @AfterAll
79 public static void tearDown() {
80 System.clearProperty(TEST_SYSTEM_PROPERTY);
81 }
82
83 @Value("${" + TEST_PROPERTY + "}")
84 private String value;
85
86 @Value("${" + TEST_SYSTEM_PROPERTY + "}")
87 private String systemPropertyValue;
88
89 @Test
90 public void testValueInjection() {
91 assertEquals(TEST_VALUE, value);
92 }
93
94 @Test
95 public void testSystemPropertyValueInjection() {
96 assertEquals(TEST_VALUE, systemPropertyValue);
97 }
98
99 }