    1   package org.apache.commons.configuration;
    2
    3   /*
    4    * Copyright 2001-2004 The Apache Software Foundation.
    5    *
    6    * Licensed under the Apache License, Version 2.0 (the "License")
    7    * you may not use this file except in compliance with the License.
    8    * You may obtain a copy of the License at
    9    *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18
    19  import java.io.File;
    20
    21  import java.util.Iterator;
    22  import java.util.List;
    23
    24  import junit.framework.TestCase;
    25
    26  import org.apache.commons.collections.IteratorUtils;
    27  import org.apache.commons.lang.StringUtils;
    28  import org.apache.commons.configuration.BaseConfiguration;
    29
    30  /***
    31   * Test that the configuration factory returns keys in the same
    32   * sequence as the properties configurator
    33   *
    34   * @version $Id: TestPropertiesSequence.java,v 1.3 2004/02/27 17:41:34 epugh Exp $
    35   */
    36  public class TestPropertiesSequence extends TestCase
    37  {
    38
    39      public void testConfigurationValuesInSameOrderFromFile() throws Exception
    40      {
    41          String simpleConfigurationFile = new File("conf/testSequence.properties").getAbsolutePath();
    42          String compositeConfigurationFile = new File("conf/testSequenceDigester.xml").getAbsolutePath();
    43
    44          Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
    45
    46          ConfigurationFactory configurationFactory = new ConfigurationFactory();
    47          configurationFactory.setConfigurationFileName(compositeConfigurationFile);
    48          Configuration compositeConfiguration = configurationFactory.getConfiguration();
    49
    50          Configuration a = simpleConfiguration.subset("prefix");
    51          Configuration b = compositeConfiguration.subset("prefix");
    52
    53          List keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
    54          List keysCompositeConfiguration = IteratorUtils.toList(b.getKeys());
    55
    56          assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
    57          assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());
    58
    59          for (int i = 0; i < keysSimpleConfiguration.size(); i++)
    60          {
    61              assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
    62          }
    63      }
    64
    65      public void testConfigurationValuesInSameOrderWithManualAdd() throws Exception
    66      {
    67          String simpleConfigurationFile = new File("conf/testSequence.properties").getAbsolutePath();
    68          String compositeConfigurationFile = new File("conf/testSequenceDigester.xml").getAbsolutePath();
    69
    70          Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
    71
    72          ConfigurationFactory configurationFactory = new ConfigurationFactory();
    73          configurationFactory.setConfigurationFileName(compositeConfigurationFile);
    74          Configuration compositeConfiguration = configurationFactory.getConfiguration();
    75
    76          simpleConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
    77          simpleConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
    78
    79          compositeConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
    80          compositeConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
    81
    82          Configuration a = simpleConfiguration.subset("prefix");
    83          Configuration b = compositeConfiguration.subset("prefix");
    84
    85          List keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
    86          List keysCompositeConfiguration = IteratorUtils.toList(b.getKeys());
    87
    88          assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
    89          assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());
    90
    91          for (int i = 0; i < keysSimpleConfiguration.size(); i++)
    92          {
    93              assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
    94          }
    95      }
    96
    97      public void testMappingInSameOrder() throws Exception
    98      {
    99          String simpleConfigurationFile = new File("conf/testSequence.properties").getAbsolutePath();
    100         String compositeConfigurationFile = new File("conf/testSequenceDigester.xml").getAbsolutePath();
    101
    102         Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
    103
    104         ConfigurationFactory configurationFactory = new ConfigurationFactory();
    105         configurationFactory.setConfigurationFileName(compositeConfigurationFile);
    106         Configuration compositeConfiguration = configurationFactory.getConfiguration();
    107
    108         Configuration mapping = new BaseConfiguration();
    109         Configuration mapping2 = new BaseConfiguration();
    110
    111         for (Iterator keys = simpleConfiguration.getKeys(); keys.hasNext();)
    112         {
    113             String key = (String) keys.next();
    114             String[] keyParts = StringUtils.split(key, ".");
    115
    116             if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix"))
    117             {
    118                 String serviceKey = keyParts[1];
    119
    120                 if (!mapping.containsKey(serviceKey))
    121                 {
    122                     mapping.setProperty(serviceKey, simpleConfiguration.getString(key));
    123                 }
    124             }
    125         }
    126
    127         for (Iterator keys = compositeConfiguration.getKeys(); keys.hasNext();)
    128         {
    129             String key = (String) keys.next();
    130             String[] keyParts = StringUtils.split(key, ".");
    131
    132             if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix"))
    133             {
    134                 String serviceKey = keyParts[1];
    135
    136                 if (!mapping2.containsKey(serviceKey))
    137                 {
    138                     mapping2.setProperty(serviceKey, compositeConfiguration.getString(key));
    139                 }
    140             }
    141         }
    142     }
    143 }