    1   package org.apache.commons.configuration;
    2
    3   /*
    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
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    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.
    18   */
    19
    20  import java.io.File;
    21
    22  import java.util.Iterator;
    23  import java.util.List;
    24
    25  import junit.framework.TestCase;
    26
    27  import org.apache.commons.collections.IteratorUtils;
    28  import org.apache.commons.lang.StringUtils;
    29  import org.apache.commons.configuration.BaseConfiguration;
    30
    31  /***
    32   * Test that the configuration factory returns keys in the same
    33   * sequence as the properties configurator
    34   *
    35   * @version $Id: TestPropertiesSequence.java 439648 2006-09-02 20:42:10Z oheger $
    36   */
    37  public class TestPropertiesSequence extends TestCase
    38  {
    39
    40      public void testConfigurationValuesInSameOrderFromFile() throws Exception
    41      {
    42          String simpleConfigurationFile = new File("conf/testSequence.properties").getAbsolutePath();
    43          String compositeConfigurationFile = new File("conf/testSequenceDigester.xml").getAbsolutePath();
    44
    45          Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
    46
    47          ConfigurationFactory configurationFactory = new ConfigurationFactory();
    48          configurationFactory.setConfigurationFileName(compositeConfigurationFile);
    49          Configuration compositeConfiguration = configurationFactory.getConfiguration();
    50
    51          Configuration a = simpleConfiguration.subset("prefix");
    52          Configuration b = compositeConfiguration.subset("prefix");
    53
    54          List keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
    55          List keysCompositeConfiguration = IteratorUtils.toList(b.getKeys());
    56
    57          assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
    58          assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());
    59
    60          for (int i = 0; i < keysSimpleConfiguration.size(); i++)
    61          {
    62              assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
    63          }
    64      }
    65
    66      public void testConfigurationValuesInSameOrderWithManualAdd() throws Exception
    67      {
    68          String simpleConfigurationFile = new File("conf/testSequence.properties").getAbsolutePath();
    69          String compositeConfigurationFile = new File("conf/testSequenceDigester.xml").getAbsolutePath();
    70
    71          Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
    72
    73          ConfigurationFactory configurationFactory = new ConfigurationFactory();
    74          configurationFactory.setConfigurationFileName(compositeConfigurationFile);
    75          Configuration compositeConfiguration = configurationFactory.getConfiguration();
    76
    77          simpleConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
    78          simpleConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
    79
    80          compositeConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
    81          compositeConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
    82
    83          Configuration a = simpleConfiguration.subset("prefix");
    84          Configuration b = compositeConfiguration.subset("prefix");
    85
    86          List keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
    87          List keysCompositeConfiguration = IteratorUtils.toList(b.getKeys());
    88
    89          assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
    90          assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());
    91
    92          for (int i = 0; i < keysSimpleConfiguration.size(); i++)
    93          {
    94              assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
    95          }
    96      }
    97
    98      public void testMappingInSameOrder() throws Exception
    99      {
    100         String simpleConfigurationFile = new File("conf/testSequence.properties").getAbsolutePath();
    101         String compositeConfigurationFile = new File("conf/testSequenceDigester.xml").getAbsolutePath();
    102
    103         Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
    104
    105         ConfigurationFactory configurationFactory = new ConfigurationFactory();
    106         configurationFactory.setConfigurationFileName(compositeConfigurationFile);
    107         Configuration compositeConfiguration = configurationFactory.getConfiguration();
    108
    109         Configuration mapping = new BaseConfiguration();
    110         Configuration mapping2 = new BaseConfiguration();
    111
    112         for (Iterator keys = simpleConfiguration.getKeys(); keys.hasNext();)
    113         {
    114             String key = (String) keys.next();
    115             String[] keyParts = StringUtils.split(key, ".");
    116
    117             if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix"))
    118             {
    119                 String serviceKey = keyParts[1];
    120
    121                 if (!mapping.containsKey(serviceKey))
    122                 {
    123                     mapping.setProperty(serviceKey, simpleConfiguration.getString(key));
    124                 }
    125             }
    126         }
    127
    128         for (Iterator keys = compositeConfiguration.getKeys(); keys.hasNext();)
    129         {
    130             String key = (String) keys.next();
    131             String[] keyParts = StringUtils.split(key, ".");
    132
    133             if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix"))
    134             {
    135                 String serviceKey = keyParts[1];
    136
    137                 if (!mapping2.containsKey(serviceKey))
    138                 {
    139                     mapping2.setProperty(serviceKey, compositeConfiguration.getString(key));
    140                 }
    141             }
    142         }
    143     }
    144 }