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