001: package org.apache.commons.configuration;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.File;
021:
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.commons.collections.IteratorUtils;
028: import org.apache.commons.lang.StringUtils;
029: import org.apache.commons.configuration.BaseConfiguration;
030:
031: /**
032: * Test that the configuration factory returns keys in the same
033: * sequence as the properties configurator
034: *
035: * @version $Id: TestPropertiesSequence.java 439648 2006-09-02 20:42:10Z oheger $
036: */
037: public class TestPropertiesSequence extends TestCase {
038:
039: public void testConfigurationValuesInSameOrderFromFile()
040: throws Exception {
041: String simpleConfigurationFile = new File(
042: "conf/testSequence.properties").getAbsolutePath();
043: String compositeConfigurationFile = new File(
044: "conf/testSequenceDigester.xml").getAbsolutePath();
045:
046: Configuration simpleConfiguration = new PropertiesConfiguration(
047: simpleConfigurationFile);
048:
049: ConfigurationFactory configurationFactory = new ConfigurationFactory();
050: configurationFactory
051: .setConfigurationFileName(compositeConfigurationFile);
052: Configuration compositeConfiguration = configurationFactory
053: .getConfiguration();
054:
055: Configuration a = simpleConfiguration.subset("prefix");
056: Configuration b = compositeConfiguration.subset("prefix");
057:
058: List keysSimpleConfiguration = IteratorUtils
059: .toList(a.getKeys());
060: List keysCompositeConfiguration = IteratorUtils.toList(b
061: .getKeys());
062:
063: assertTrue("Size:" + keysSimpleConfiguration.size(),
064: keysSimpleConfiguration.size() > 0);
065: assertEquals(keysSimpleConfiguration.size(),
066: keysCompositeConfiguration.size());
067:
068: for (int i = 0; i < keysSimpleConfiguration.size(); i++) {
069: assertEquals(keysSimpleConfiguration.get(i),
070: keysCompositeConfiguration.get(i));
071: }
072: }
073:
074: public void testConfigurationValuesInSameOrderWithManualAdd()
075: throws Exception {
076: String simpleConfigurationFile = new File(
077: "conf/testSequence.properties").getAbsolutePath();
078: String compositeConfigurationFile = new File(
079: "conf/testSequenceDigester.xml").getAbsolutePath();
080:
081: Configuration simpleConfiguration = new PropertiesConfiguration(
082: simpleConfigurationFile);
083:
084: ConfigurationFactory configurationFactory = new ConfigurationFactory();
085: configurationFactory
086: .setConfigurationFileName(compositeConfigurationFile);
087: Configuration compositeConfiguration = configurationFactory
088: .getConfiguration();
089:
090: simpleConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
091: simpleConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
092:
093: compositeConfiguration.setProperty("prefix.Co.test",
094: Boolean.TRUE);
095: compositeConfiguration.setProperty("prefix.Av.test",
096: Boolean.TRUE);
097:
098: Configuration a = simpleConfiguration.subset("prefix");
099: Configuration b = compositeConfiguration.subset("prefix");
100:
101: List keysSimpleConfiguration = IteratorUtils
102: .toList(a.getKeys());
103: List keysCompositeConfiguration = IteratorUtils.toList(b
104: .getKeys());
105:
106: assertTrue("Size:" + keysSimpleConfiguration.size(),
107: keysSimpleConfiguration.size() > 0);
108: assertEquals(keysSimpleConfiguration.size(),
109: keysCompositeConfiguration.size());
110:
111: for (int i = 0; i < keysSimpleConfiguration.size(); i++) {
112: assertEquals(keysSimpleConfiguration.get(i),
113: keysCompositeConfiguration.get(i));
114: }
115: }
116:
117: public void testMappingInSameOrder() throws Exception {
118: String simpleConfigurationFile = new File(
119: "conf/testSequence.properties").getAbsolutePath();
120: String compositeConfigurationFile = new File(
121: "conf/testSequenceDigester.xml").getAbsolutePath();
122:
123: Configuration simpleConfiguration = new PropertiesConfiguration(
124: simpleConfigurationFile);
125:
126: ConfigurationFactory configurationFactory = new ConfigurationFactory();
127: configurationFactory
128: .setConfigurationFileName(compositeConfigurationFile);
129: Configuration compositeConfiguration = configurationFactory
130: .getConfiguration();
131:
132: Configuration mapping = new BaseConfiguration();
133: Configuration mapping2 = new BaseConfiguration();
134:
135: for (Iterator keys = simpleConfiguration.getKeys(); keys
136: .hasNext();) {
137: String key = (String) keys.next();
138: String[] keyParts = StringUtils.split(key, ".");
139:
140: if ((keyParts.length == 3) && keyParts[0].equals("prefix")
141: && keyParts[2].equals("postfix")) {
142: String serviceKey = keyParts[1];
143:
144: if (!mapping.containsKey(serviceKey)) {
145: mapping.setProperty(serviceKey, simpleConfiguration
146: .getString(key));
147: }
148: }
149: }
150:
151: for (Iterator keys = compositeConfiguration.getKeys(); keys
152: .hasNext();) {
153: String key = (String) keys.next();
154: String[] keyParts = StringUtils.split(key, ".");
155:
156: if ((keyParts.length == 3) && keyParts[0].equals("prefix")
157: && keyParts[2].equals("postfix")) {
158: String serviceKey = keyParts[1];
159:
160: if (!mapping2.containsKey(serviceKey)) {
161: mapping2.setProperty(serviceKey,
162: compositeConfiguration.getString(key));
163: }
164: }
165: }
166: }
167: }
|