View Javadoc
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    *     https://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 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   * Tests {@link ConfigurationPropertySource}.
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              // https://jira.spring.io/browse/SPR-9631 may simplify this in
54              // future
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 }