KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > TestPropertiesSequence


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 JavaDoc;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
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 155408 2005-02-26 12:56:39Z dirkv $
35  */

36 public class TestPropertiesSequence extends TestCase
37 {
38
39     public void testConfigurationValuesInSameOrderFromFile() throws Exception JavaDoc
40     {
41         String JavaDoc simpleConfigurationFile = new File JavaDoc("conf/testSequence.properties").getAbsolutePath();
42         String JavaDoc compositeConfigurationFile = new File JavaDoc("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 JavaDoc keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
54         List JavaDoc 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 JavaDoc
66     {
67         String JavaDoc simpleConfigurationFile = new File JavaDoc("conf/testSequence.properties").getAbsolutePath();
68         String JavaDoc compositeConfigurationFile = new File JavaDoc("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 JavaDoc keysSimpleConfiguration = IteratorUtils.toList(a.getKeys());
86         List JavaDoc 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 JavaDoc
98     {
99         String JavaDoc simpleConfigurationFile = new File JavaDoc("conf/testSequence.properties").getAbsolutePath();
100         String JavaDoc compositeConfigurationFile = new File JavaDoc("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 JavaDoc keys = simpleConfiguration.getKeys(); keys.hasNext();)
112         {
113             String JavaDoc key = (String JavaDoc) keys.next();
114             String JavaDoc[] keyParts = StringUtils.split(key, ".");
115
116             if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix"))
117             {
118                 String JavaDoc serviceKey = keyParts[1];
119
120                 if (!mapping.containsKey(serviceKey))
121                 {
122                     mapping.setProperty(serviceKey, simpleConfiguration.getString(key));
123                 }
124             }
125         }
126
127         for (Iterator JavaDoc keys = compositeConfiguration.getKeys(); keys.hasNext();)
128         {
129             String JavaDoc key = (String JavaDoc) keys.next();
130             String JavaDoc[] keyParts = StringUtils.split(key, ".");
131
132             if ((keyParts.length == 3) && keyParts[0].equals("prefix") && keyParts[2].equals("postfix"))
133             {
134                 String JavaDoc serviceKey = keyParts[1];
135
136                 if (!mapping2.containsKey(serviceKey))
137                 {
138                     mapping2.setProperty(serviceKey, compositeConfiguration.getString(key));
139                 }
140             }
141         }
142     }
143 }
144
Popular Tags