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