1
package org.apache.commons.configuration;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
22
import java.util.Iterator;
23
import java.util.List;
25
import junit.framework.TestCase;
27
import org.apache.commons.collections.IteratorUtils;
28
import org.apache.commons.lang.StringUtils;
29
import org.apache.commons.configuration.BaseConfiguration;
32
* Test that the configuration factory returns keys in the same
33
* sequence as the properties configurator
35
* @version $Id: TestPropertiesSequence.java 439648 2006-09-02 20:42:10Z oheger $
37
public class TestPropertiesSequence extends TestCase
40
public void testConfigurationValuesInSameOrderFromFile() throws Exception
42
String simpleConfigurationFile = new File("conf/testSequence.properties").getAbsolutePath();
43
String compositeConfigurationFile = new File("conf/testSequenceDigester.xml").getAbsolutePath();
45
Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
47
ConfigurationFactory configurationFactory = new ConfigurationFactory();
48
configurationFactory.setConfigurationFileName(compositeConfigurationFile);
49
Configuration compositeConfiguration = configurationFactory.getConfiguration();
51
Configuration a = simpleConfiguration.subset("prefix");
52
Configuration b = compositeConfiguration.subset("prefix");
54
List keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
55
List keysCompositeConfiguration = IteratorUtils.toList(b.getKeys());
57
assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
58
assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());
60
for (int i = 0; i < keysSimpleConfiguration.size(); i++)
62
assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
66
public void testConfigurationValuesInSameOrderWithManualAdd() throws Exception
68
String simpleConfigurationFile = new File("conf/testSequence.properties").getAbsolutePath();
69
String compositeConfigurationFile = new File("conf/testSequenceDigester.xml").getAbsolutePath();
71
Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
73
ConfigurationFactory configurationFactory = new ConfigurationFactory();
74
configurationFactory.setConfigurationFileName(compositeConfigurationFile);
75
Configuration compositeConfiguration = configurationFactory.getConfiguration();
77
simpleConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
78
simpleConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
80
compositeConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
81
compositeConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
83
Configuration a = simpleConfiguration.subset("prefix");
84
Configuration b = compositeConfiguration.subset("prefix");
86
List keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
87
List keysCompositeConfiguration = IteratorUtils.toList(b.getKeys());
89
assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
90
assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());
92
for (int i = 0; i < keysSimpleConfiguration.size(); i++)
94
assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
98
public void testMappingInSameOrder() throws Exception
100
String simpleConfigurationFile = new File("conf/testSequence.properties").getAbsolutePath();
101
String compositeConfigurationFile = new File("conf/testSequenceDigester.xml").getAbsolutePath();
103
Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
105
ConfigurationFactory configurationFactory = new ConfigurationFactory();
106
configurationFactory.setConfigurationFileName(compositeConfigurationFile);
107
Configuration compositeConfiguration = configurationFactory.getConfiguration();
109
Configuration mapping = new BaseConfiguration();
110
Configuration mapping2 = new BaseConfiguration();
112
for (Iterator keys = simpleConfiguration.getKeys(); keys.hasNext();)
114
String key = (String) keys.next();
115
String[] keyParts = StringUtils.split(key, ".");
117
if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix"))
119
String serviceKey = keyParts[1];
121
if (!mapping.containsKey(serviceKey))
123
mapping.setProperty(serviceKey, simpleConfiguration.getString(key));
128
for (Iterator keys = compositeConfiguration.getKeys(); keys.hasNext();)
130
String key = (String) keys.next();
131
String[] keyParts = StringUtils.split(key, ".");
133
if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix"))
135
String serviceKey = keyParts[1];
137
if (!mapping2.containsKey(serviceKey))
139
mapping2.setProperty(serviceKey, compositeConfiguration.getString(key));