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 static org.junit.Assert.assertEquals;
021    import static org.junit.Assert.assertTrue;
022    
023    import java.util.Iterator;
024    import java.util.List;
025    
026    import org.apache.commons.collections.IteratorUtils;
027    import org.apache.commons.lang.StringUtils;
028    import org.junit.Test;
029    
030    /**
031     * Test that the configuration factory returns keys in the same
032     * sequence as the properties configurator
033     *
034     * @version $Id: TestPropertiesSequence.java 1225011 2011-12-27 20:46:13Z oheger $
035     */
036    @SuppressWarnings("deprecation")
037    public class TestPropertiesSequence
038    {
039        @Test
040        public void testConfigurationValuesInSameOrderFromFile() throws Exception
041        {
042            String simpleConfigurationFile = ConfigurationAssert.getTestFile("testSequence.properties").getAbsolutePath();
043            String compositeConfigurationFile = ConfigurationAssert.getTestFile("testSequenceDigester.xml").getAbsolutePath();
044    
045            Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
046    
047            ConfigurationFactory configurationFactory = new ConfigurationFactory();
048            configurationFactory.setConfigurationFileName(compositeConfigurationFile);
049            Configuration compositeConfiguration = configurationFactory.getConfiguration();
050    
051            Configuration a = simpleConfiguration.subset("prefix");
052            Configuration b = compositeConfiguration.subset("prefix");
053    
054            List<?> keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
055            List<?> keysCompositeConfiguration = IteratorUtils.toList(b.getKeys());
056    
057            assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
058            assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());
059    
060            for (int i = 0; i < keysSimpleConfiguration.size(); i++)
061            {
062                assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
063            }
064        }
065    
066        @Test
067        public void testConfigurationValuesInSameOrderWithManualAdd() throws Exception
068        {
069            String simpleConfigurationFile = ConfigurationAssert.getTestFile("testSequence.properties").getAbsolutePath();
070            String compositeConfigurationFile = ConfigurationAssert.getTestFile("testSequenceDigester.xml").getAbsolutePath();
071    
072            Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
073    
074            ConfigurationFactory configurationFactory = new ConfigurationFactory();
075            configurationFactory.setConfigurationFileName(compositeConfigurationFile);
076            Configuration compositeConfiguration = configurationFactory.getConfiguration();
077    
078            simpleConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
079            simpleConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
080    
081            compositeConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
082            compositeConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
083    
084            Configuration a = simpleConfiguration.subset("prefix");
085            Configuration b = compositeConfiguration.subset("prefix");
086    
087            List<?> keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
088            List<?> keysCompositeConfiguration = IteratorUtils.toList(b.getKeys());
089    
090            assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
091            assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());
092    
093            for (int i = 0; i < keysSimpleConfiguration.size(); i++)
094            {
095                assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
096            }
097        }
098    
099        @Test
100        public void testMappingInSameOrder() throws Exception
101        {
102            String simpleConfigurationFile = ConfigurationAssert.getTestFile("testSequence.properties").getAbsolutePath();
103            String compositeConfigurationFile = ConfigurationAssert.getTestFile("testSequenceDigester.xml").getAbsolutePath();
104    
105            Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
106    
107            ConfigurationFactory configurationFactory = new ConfigurationFactory();
108            configurationFactory.setConfigurationFileName(compositeConfigurationFile);
109            Configuration compositeConfiguration = configurationFactory.getConfiguration();
110    
111            Configuration mapping = new BaseConfiguration();
112            Configuration mapping2 = new BaseConfiguration();
113    
114            for (Iterator<String> keys = simpleConfiguration.getKeys(); keys.hasNext();)
115            {
116                String key = keys.next();
117                String[] keyParts = StringUtils.split(key, ".");
118    
119                if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix"))
120                {
121                    String serviceKey = keyParts[1];
122    
123                    if (!mapping.containsKey(serviceKey))
124                    {
125                        mapping.setProperty(serviceKey, simpleConfiguration.getString(key));
126                    }
127                }
128            }
129    
130            for (Iterator<String> keys = compositeConfiguration.getKeys(); keys.hasNext();)
131            {
132                String key = keys.next();
133                String[] keyParts = StringUtils.split(key, ".");
134    
135                if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix"))
136                {
137                    String serviceKey = keyParts[1];
138    
139                    if (!mapping2.containsKey(serviceKey))
140                    {
141                        mapping2.setProperty(serviceKey, compositeConfiguration.getString(key));
142                    }
143                }
144            }
145        }
146    }