View Javadoc

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 static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.collections.IteratorUtils;
27  import org.apache.commons.lang.StringUtils;
28  import org.junit.Test;
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 1225011 2011-12-27 20:46:13Z oheger $
35   */
36  @SuppressWarnings("deprecation")
37  public class TestPropertiesSequence
38  {
39      @Test
40      public void testConfigurationValuesInSameOrderFromFile() throws Exception
41      {
42          String simpleConfigurationFile = ConfigurationAssert.getTestFile("testSequence.properties").getAbsolutePath();
43          String compositeConfigurationFile = ConfigurationAssert.getTestFile("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      @Test
67      public void testConfigurationValuesInSameOrderWithManualAdd() throws Exception
68      {
69          String simpleConfigurationFile = ConfigurationAssert.getTestFile("testSequence.properties").getAbsolutePath();
70          String compositeConfigurationFile = ConfigurationAssert.getTestFile("testSequenceDigester.xml").getAbsolutePath();
71  
72          Configuration simpleConfiguration = new PropertiesConfiguration(simpleConfigurationFile);
73  
74          ConfigurationFactory configurationFactory = new ConfigurationFactory();
75          configurationFactory.setConfigurationFileName(compositeConfigurationFile);
76          Configuration compositeConfiguration = configurationFactory.getConfiguration();
77  
78          simpleConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
79          simpleConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
80  
81          compositeConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
82          compositeConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);
83  
84          Configuration a = simpleConfiguration.subset("prefix");
85          Configuration b = compositeConfiguration.subset("prefix");
86  
87          List<?> keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
88          List<?> keysCompositeConfiguration = IteratorUtils.toList(b.getKeys());
89  
90          assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
91          assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());
92  
93          for (int i = 0; i < keysSimpleConfiguration.size(); i++)
94          {
95              assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
96          }
97      }
98  
99      @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 }