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.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import org.apache.commons.configuration2.PropertiesConfiguration;
26 import org.junit.jupiter.api.AfterAll;
27 import org.junit.jupiter.api.BeforeAll;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.springframework.beans.factory.annotation.Value;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.Configuration;
33 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
34 import org.springframework.core.env.ConfigurableEnvironment;
35 import org.springframework.core.env.MutablePropertySources;
36 import org.springframework.test.context.ContextConfiguration;
37 import org.springframework.test.context.junit.jupiter.SpringExtension;
38
39
40
41
42 @ExtendWith(SpringExtension.class)
43 @ContextConfiguration
44 public class TestConfigurationPropertySource {
45
46 @Configuration
47 static class Config {
48
49 @Bean
50 public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(
51 final ConfigurableEnvironment env) {
52 final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
53
54
55 final MutablePropertySources sources = new MutablePropertySources();
56 sources.addLast(createConfigPropertySource());
57 configurer.setPropertySources(sources);
58 configurer.setEnvironment(env);
59 return configurer;
60 }
61 }
62
63 private static final String TEST_PROPERTY = "test.property";
64
65 private static final String TEST_LIST_PROPERTY = "test.list.property";
66
67 private static final String TEST_SYSTEM_PROPERTY = "test.system.property";
68
69 private static final String TEST_NULL_PROPERTY = "test.null.property";
70
71 private static final String TEST_EMPTY_PROPERTY = "test.empty.property";
72
73 private static final String TEST_VALUE = "testVALUE";
74
75 private static final String TEST_SYSTEM_VALUE = "testVALUEforSystemEnv";
76
77 private static final String TEST_SYSTEM_PROPERTY_VALUE = "${sys:" + TEST_SYSTEM_PROPERTY + "}";
78
79 private static final String[] TEST_LIST_PROPERTY_VALUE = {TEST_SYSTEM_PROPERTY_VALUE, TEST_VALUE};
80
81 private static final String[] TEST_LIST_VALUE = {TEST_SYSTEM_VALUE, TEST_VALUE};
82
83 private static ConfigurationPropertySource createConfigPropertySource() {
84 final PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
85 propertiesConfiguration.addProperty(TEST_PROPERTY, TEST_VALUE);
86 propertiesConfiguration.addProperty(TEST_LIST_PROPERTY, TEST_LIST_PROPERTY_VALUE);
87 propertiesConfiguration.addProperty(TEST_SYSTEM_PROPERTY, TEST_SYSTEM_PROPERTY_VALUE);
88 propertiesConfiguration.addProperty(TEST_NULL_PROPERTY, null);
89 propertiesConfiguration.addProperty(TEST_EMPTY_PROPERTY, "");
90 return new ConfigurationPropertySource("test configuration", propertiesConfiguration);
91 }
92
93 @BeforeAll
94 public static void setUp() {
95 System.setProperty(TEST_SYSTEM_PROPERTY, TEST_SYSTEM_VALUE);
96 }
97
98 @AfterAll
99 public static void tearDown() {
100 System.clearProperty(TEST_SYSTEM_PROPERTY);
101 }
102
103 @Value("${" + TEST_PROPERTY + "}")
104 private String value;
105
106 @Value("${" + TEST_LIST_PROPERTY + "}")
107 private String[] listValue;
108
109 @Value("${" + TEST_SYSTEM_PROPERTY + "}")
110 private String systemPropertyValue;
111
112 @Value("${" + TEST_NULL_PROPERTY + ":false}")
113 private boolean booleanNullValueDefaultFalse;
114
115 @Value("${" + TEST_NULL_PROPERTY + ":true}")
116 private boolean booleanNullValueDefaultTrue;
117
118 @Value("${" + TEST_EMPTY_PROPERTY + ":defaultShouldNotApply}")
119 private String emptyPropertyValue;
120
121 @Test
122 void testEmptyStringValueInjection() {
123 assertEquals("", emptyPropertyValue);
124 }
125
126 @Test
127 void testListValueInjection() {
128 assertArrayEquals(TEST_LIST_VALUE, listValue);
129 }
130
131 @Test
132 void testNullValueInjection() {
133 assertFalse(booleanNullValueDefaultFalse);
134 assertTrue(booleanNullValueDefaultTrue);
135 }
136
137 @Test
138 void testSystemPropertyValueInjection() {
139 assertEquals(TEST_SYSTEM_VALUE, systemPropertyValue);
140 }
141 }