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  import java.util.Iterator;
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  
25  /***
26   * Compare the behaviour of various methods between CompositeConfiguration
27   * and normal (Properties) Configuration
28   * 
29   * @version $Id: TestEqualBehaviour.java 331141 2005-11-06 19:06:44Z oheger $
30   */
31  public class TestEqualBehaviour  extends TestCase
32  {
33      private Configuration setupSimpleConfiguration()
34              throws Exception
35      {
36          String simpleConfigurationFile = new File("conf/testEqual.properties").getAbsolutePath();
37          return new PropertiesConfiguration(simpleConfigurationFile);
38      }
39  
40      private Configuration setupCompositeConfiguration()
41              throws Exception
42      {
43          String compositeConfigurationFile = new File("conf/testEqualDigester.xml").getAbsolutePath();
44  
45          ConfigurationFactory configurationFactory = new ConfigurationFactory();
46          configurationFactory.setConfigurationFileName(compositeConfigurationFile);
47          return configurationFactory.getConfiguration();
48      }
49  
50      /***
51       * Checks whether two configurations have the same size, 
52       * the same key sequence and contain the same key -> value mappings
53       */
54      private void checkEquality(String msg, Configuration c1, Configuration c2)
55      {
56          Iterator it1 = c1.getKeys();
57          Iterator it2 = c2.getKeys();
58  
59          while(it1.hasNext() && it2.hasNext())
60          {
61              String key1 = (String) it1.next();
62              String key2 = (String) it2.next();
63              assertEquals(msg + ", Keys: ", key1, key2);
64              assertEquals(msg + ", Contains: ", c1.containsKey(key1), c2.containsKey(key2));
65          }
66          assertEquals(msg + ", Iterator: ", it1.hasNext(), it2.hasNext());
67      }
68  
69      /***
70       * Checks whether two configurations have the same key -> value mapping
71       */
72      private void checkSameKey(String msg, String key, Configuration c1, Configuration c2)
73      {
74          String [] s1 = c1.getStringArray(key);
75          String [] s2 = c2.getStringArray(key);
76  
77          assertEquals(msg + ", length: ", s1.length, s2.length);
78  
79          for (int i = 0; i < s1.length ; i++)
80          {
81              assertEquals(msg + ", String Array: ", s1[i], s2[i]);
82          }
83  
84          List list1 = c1.getList(key);
85          List list2 = c2.getList(key);
86  
87          assertEquals(msg + ", Size: ", list1.size(), list2.size());
88  
89          Iterator it1 = list1.iterator();
90          Iterator it2 = list2.iterator();
91  
92          while(it1.hasNext() && it2.hasNext())
93          {
94              String val1 = (String) it1.next();
95              String val2 = (String) it2.next();
96              assertEquals(msg + ", List: ", val1, val2);
97          }
98          assertEquals(msg + ", Iterator End: ", it1.hasNext(), it2.hasNext());
99      }
100 
101     /***
102      * Are both configurations equal after loading?
103      */
104     public void testLoading() throws Exception
105     {
106         Configuration simple = setupSimpleConfiguration();
107         Configuration composite = setupCompositeConfiguration();
108 
109         checkEquality("testLoading", simple, composite);
110     }
111 
112     /***
113      * If we delete a key, does it vanish? Does it leave all
114      * the other keys unchanged? How about an unset key?
115      */
116     public void testDeletingExisting() throws Exception
117     {
118         Configuration simple = setupSimpleConfiguration();
119         Configuration composite = setupCompositeConfiguration();
120 
121         String key = "clear.property";
122 
123         assertTrue(simple.containsKey(key));
124         assertEquals(simple.containsKey(key), composite.containsKey(key));
125 
126         simple.clearProperty(key);
127         composite.clearProperty(key);
128 
129         assertFalse(simple.containsKey(key));
130         assertEquals(simple.containsKey(key), composite.containsKey(key));
131 
132         checkEquality("testDeletingExisting", simple, composite);
133     }
134 
135     public void testDeletingNonExisting() throws Exception
136     {
137         Configuration simple = setupSimpleConfiguration();
138         Configuration composite = setupCompositeConfiguration();
139 
140         String key = "nonexisting.clear.property";
141 
142         assertFalse(simple.containsKey(key));
143         assertEquals(simple.containsKey(key), composite.containsKey(key));
144 
145         simple.clearProperty(key);
146         composite.clearProperty(key);
147 
148         assertFalse(simple.containsKey(key));
149         assertEquals(simple.containsKey(key), composite.containsKey(key));
150 
151         checkEquality("testDeletingNonExisting", simple, composite);
152     }
153 
154     /***
155      * If we set a key, does it work? How about an existing
156      * key? Can we change it?
157      */
158     public void testSettingNonExisting() throws Exception
159     {
160         Configuration simple = setupSimpleConfiguration();
161         Configuration composite = setupCompositeConfiguration();
162 
163         String key = "nonexisting.property";
164         String value = "new value";
165 
166         assertFalse(simple.containsKey(key));
167         assertEquals(simple.containsKey(key), composite.containsKey(key));
168 
169         simple.setProperty(key, value);
170         composite.setProperty(key, value);
171 
172         assertTrue(simple.containsKey(key));
173         assertEquals(simple.containsKey(key), composite.containsKey(key));
174 
175         checkSameKey("testSettingNonExisting", key, simple, composite);
176         checkEquality("testSettingNonExisting", simple, composite);
177     }
178 
179     public void testSettingExisting() throws Exception
180     {
181         Configuration simple = setupSimpleConfiguration();
182         Configuration composite = setupCompositeConfiguration();
183 
184         String key = "existing.property";
185         String value = "new value";
186 
187         assertTrue(simple.containsKey(key));
188         assertFalse(simple.getString(key).equals(value));
189         assertEquals(simple.containsKey(key), composite.containsKey(key));
190 
191         simple.setProperty(key, value);
192         composite.setProperty(key, value);
193 
194         assertTrue(simple.containsKey(key));
195         assertEquals(simple.getString(key), value);
196         assertEquals(simple.containsKey(key), composite.containsKey(key));
197 
198         checkSameKey("testSettingExisting", key, simple, composite);
199         checkEquality("testSettingExisting", simple, composite);
200     }
201 
202     /***
203      * If we add a key, does it work?
204      */
205     public void testAddingUnset() throws Exception
206     {
207         Configuration simple = setupSimpleConfiguration();
208         Configuration composite = setupCompositeConfiguration();
209 
210         String key = "nonexisting.property";
211         String value = "new value";
212 
213         assertFalse(simple.containsKey(key));
214         assertEquals(simple.containsKey(key), composite.containsKey(key));
215 
216         simple.addProperty(key, value);
217         composite.addProperty(key, value);
218 
219         checkSameKey("testAddingUnset", key, simple, composite);
220         checkEquality("testAddingUnset", simple, composite);
221     }
222 
223     /***
224      * If we add a to an existing key, does it work?
225      */
226     public void testAddingSet() throws Exception
227     {
228         Configuration simple = setupSimpleConfiguration();
229         Configuration composite = setupCompositeConfiguration();
230 
231         String key = "existing.property";
232         String value = "new value";
233 
234         assertTrue(simple.containsKey(key));
235         assertEquals(simple.containsKey(key), composite.containsKey(key));
236 
237         simple.addProperty(key, value);
238         composite.addProperty(key, value);
239 
240         assertTrue(simple.containsKey(key));
241         assertEquals(simple.containsKey(key), composite.containsKey(key));
242 
243         checkSameKey("testAddingSet", key, simple, composite);
244         checkEquality("testAddingSet", simple, composite);
245     }
246 }