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.assertFalse;
22  
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
27  import org.apache.commons.configuration2.builder.combined.CombinedConfigurationBuilder;
28  import org.apache.commons.configuration2.io.FileHandler;
29  import org.apache.commons.lang3.StringUtils;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Test that the configuration factory returns keys in the same sequence as the properties configurator
34   */
35  public class TestPropertiesSequence {
36      @Test
37      public void testConfigurationValuesInSameOrderFromFile() throws Exception {
38          final String simpleConfigurationFile = ConfigurationAssert.getTestFile("testSequence.properties").getAbsolutePath();
39          final String compositeConfigurationFile = ConfigurationAssert.getTestFile("testSequenceDigester.xml").getAbsolutePath();
40  
41          final PropertiesConfiguration simpleConfiguration = new PropertiesConfiguration();
42          final FileHandler handler = new FileHandler(simpleConfiguration);
43          handler.setFileName(simpleConfigurationFile);
44          handler.load();
45  
46          final CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder();
47          builder.configure(new FileBasedBuilderParametersImpl().setFileName(compositeConfigurationFile));
48          final Configuration compositeConfiguration = builder.getConfiguration();
49  
50          final Configuration a = simpleConfiguration.subset("prefix");
51          final Configuration b = compositeConfiguration.subset("prefix");
52  
53          final List<String> keysSimpleConfiguration = ConfigurationAssert.keysToList(a);
54          final List<String> keysCompositeConfiguration = ConfigurationAssert.keysToList(b);
55  
56          assertFalse(keysSimpleConfiguration.isEmpty());
57          assertEquals(keysSimpleConfiguration, keysCompositeConfiguration);
58      }
59  
60      @Test
61      public void testConfigurationValuesInSameOrderWithManualAdd() throws Exception {
62          final String simpleConfigurationFile = ConfigurationAssert.getTestFile("testSequence.properties").getAbsolutePath();
63          final String compositeConfigurationFile = ConfigurationAssert.getTestFile("testSequenceDigester.xml").getAbsolutePath();
64  
65          final PropertiesConfiguration simpleConfiguration = new PropertiesConfiguration();
66          final FileHandler handler = new FileHandler(simpleConfiguration);
67          handler.setFileName(simpleConfigurationFile);
68          handler.load();
69  
70          final CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder();
71          builder.configure(new FileBasedBuilderParametersImpl().setFileName(compositeConfigurationFile));
72          final Configuration compositeConfiguration = builder.getConfiguration();
73  
74          simpleConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
75          simpleConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
76  
77          compositeConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
78          compositeConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
79  
80          final Configuration a = simpleConfiguration.subset("prefix");
81          final Configuration b = compositeConfiguration.subset("prefix");
82  
83          final List<String> keysSimpleConfiguration = ConfigurationAssert.keysToList(a);
84          final List<String> keysCompositeConfiguration = ConfigurationAssert.keysToList(b);
85  
86          assertFalse(keysSimpleConfiguration.isEmpty());
87          assertEquals(keysSimpleConfiguration, keysCompositeConfiguration);
88      }
89  
90      @Test
91      public void testMappingInSameOrder() throws Exception {
92          final String simpleConfigurationFile = ConfigurationAssert.getTestFile("testSequence.properties").getAbsolutePath();
93          final String compositeConfigurationFile = ConfigurationAssert.getTestFile("testSequenceDigester.xml").getAbsolutePath();
94  
95          final PropertiesConfiguration simpleConfiguration = new PropertiesConfiguration();
96          final FileHandler handler = new FileHandler(simpleConfiguration);
97          handler.setFileName(simpleConfigurationFile);
98          handler.load();
99  
100         final CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder();
101         builder.configure(new FileBasedBuilderParametersImpl().setFileName(compositeConfigurationFile));
102         final Configuration compositeConfiguration = builder.getConfiguration();
103 
104         final Configuration mapping = new BaseConfiguration();
105         final Configuration mapping2 = new BaseConfiguration();
106 
107         for (final Iterator<String> keys = simpleConfiguration.getKeys(); keys.hasNext();) {
108             final String key = keys.next();
109             final String[] keyParts = StringUtils.split(key, ".");
110 
111             if (keyParts.length == 3 && keyParts[0].equals("prefix") && keyParts[2].equals("postfix")) {
112                 final String serviceKey = keyParts[1];
113 
114                 if (!mapping.containsKey(serviceKey)) {
115                     mapping.setProperty(serviceKey, simpleConfiguration.getString(key));
116                 }
117             }
118         }
119 
120         for (final Iterator<String> keys = compositeConfiguration.getKeys(); keys.hasNext();) {
121             final String key = keys.next();
122             final String[] keyParts = StringUtils.split(key, ".");
123 
124             if (keyParts.length == 3 && keyParts[0].equals("prefix") && keyParts[2].equals("postfix")) {
125                 final String serviceKey = keyParts[1];
126 
127                 if (!mapping2.containsKey(serviceKey)) {
128                     mapping2.setProperty(serviceKey, compositeConfiguration.getString(key));
129                 }
130             }
131         }
132     }
133 }