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