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;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.mockito.ArgumentMatchers.any;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Properties;
29  
30  import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests the ConfigurationConverter class.
35   */
36  public class TestConfigurationConverter {
37  
38      /**
39       * Creates a configuration object with some test values.
40       *
41       * @return the test configuration
42       */
43      private static BaseConfiguration createTestConfiguration() {
44          final BaseConfiguration config = new BaseConfiguration();
45          config.addProperty("string", "teststring");
46          config.addProperty("array", "item 1");
47          config.addProperty("array", "item 2");
48          config.addProperty("interpolated", "${string}");
49          config.addProperty("interpolated-array", "${interpolated}");
50          config.addProperty("interpolated-array", "${interpolated}");
51          return config;
52      }
53  
54      @Test
55      void testConfigurationToMap() {
56          final Configuration config = new BaseConfiguration();
57          config.addProperty("string", "teststring");
58  
59          final Map<Object, Object> map = ConfigurationConverter.getMap(config);
60  
61          assertNotNull(map);
62          assertEquals("teststring", map.get("string"));
63      }
64  
65      /**
66       * Tests a conversion to Properties if the default list delimiter handler is used (which does not support list joining).
67       */
68      @Test
69      void testConfigurationToPropertiesDefaultListHandling() {
70          final BaseConfiguration config = createTestConfiguration();
71          final Properties props = ConfigurationConverter.getProperties(config);
72  
73          assertNotNull(props);
74          assertEquals("teststring", props.getProperty("string"));
75          assertEquals("teststring", props.getProperty("interpolated"));
76          assertEquals("item 1,item 2", props.getProperty("array"));
77          assertEquals("teststring,teststring", props.getProperty("interpolated-array"));
78      }
79  
80      /**
81       * Tests a conversion to Properties if the list delimiter handler supports list joining.
82       */
83      @Test
84      void testConfigurationToPropertiesListDelimiterHandler() {
85          final BaseConfiguration config = createTestConfiguration();
86          config.setListDelimiterHandler(new DefaultListDelimiterHandler(';'));
87          final Properties props = ConfigurationConverter.getProperties(config);
88          assertEquals("item 1;item 2", props.getProperty("array"));
89      }
90  
91      /**
92       * Tests a conversion to Properties if the source configuration does not extend AbstractConfiguration. In this case,
93       * properties with multiple values have to be handled in a special way.
94       */
95      @Test
96      void testConfigurationToPropertiesNoAbstractConfiguration() {
97          final Configuration src = mock(Configuration.class);
98          final BaseConfiguration config = createTestConfiguration();
99  
100         when(src.getKeys()).thenReturn(config.getKeys());
101         when(src.getList(any())).thenAnswer(invocation -> {
102             final String key = invocation.getArgument(0, String.class);
103             return config.getList(key);
104         });
105 
106         final Properties props = ConfigurationConverter.getProperties(src);
107         assertEquals("item 1,item 2", props.getProperty("array"));
108     }
109 
110     /**
111      * Tests the conversion of a configuration object to properties if scalar values are involved. This test is related to
112      * CONFIGURATION-432.
113      */
114     @Test
115     void testConfigurationToPropertiesScalarValue() {
116         final BaseConfiguration config = new BaseConfiguration();
117         config.addProperty("scalar", Integer.valueOf(42));
118         final Properties props = ConfigurationConverter.getProperties(config);
119         assertEquals("42", props.getProperty("scalar"));
120     }
121 
122     @Test
123     void testPropertiesToConfiguration() {
124         final Properties props = new Properties();
125         props.setProperty("string", "teststring");
126         props.setProperty("int", "123");
127         props.setProperty("list", "item 1, item 2");
128 
129         final AbstractConfiguration config = (AbstractConfiguration) ConfigurationConverter.getConfiguration(props);
130         config.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
131 
132         assertEquals("teststring", config.getString("string"));
133         final List<Object> item1 = config.getList("list");
134         assertEquals("item 1", item1.get(0));
135 
136         assertEquals(123, config.getInt("int"));
137     }
138 
139 }