KickJava   Java API By Example, From Geeks To Geeks.

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


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

31 public class TestEqualBehaviour extends TestCase
32 {
33     private Configuration setupSimpleConfiguration()
34             throws Exception JavaDoc
35     {
36         String JavaDoc simpleConfigurationFile = new File JavaDoc("conf/testEqual.properties").getAbsolutePath();
37         return new PropertiesConfiguration(simpleConfigurationFile);
38     }
39
40     private Configuration setupCompositeConfiguration()
41             throws Exception JavaDoc
42     {
43         String JavaDoc compositeConfigurationFile = new File JavaDoc("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 JavaDoc msg, Configuration c1, Configuration c2)
55     {
56         Iterator JavaDoc it1 = c1.getKeys();
57         Iterator JavaDoc it2 = c2.getKeys();
58
59         while(it1.hasNext() && it2.hasNext())
60         {
61             String JavaDoc key1 = (String JavaDoc) it1.next();
62             String JavaDoc key2 = (String JavaDoc) 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 JavaDoc msg, String JavaDoc key, Configuration c1, Configuration c2)
73     {
74         String JavaDoc [] s1 = c1.getStringArray(key);
75         String JavaDoc [] 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 JavaDoc list1 = c1.getList(key);
85         List JavaDoc list2 = c2.getList(key);
86
87         assertEquals(msg + ", Size: ", list1.size(), list2.size());
88
89         Iterator JavaDoc it1 = list1.iterator();
90         Iterator JavaDoc it2 = list2.iterator();
91
92         while(it1.hasNext() && it2.hasNext())
93         {
94             String JavaDoc val1 = (String JavaDoc) it1.next();
95             String JavaDoc val2 = (String JavaDoc) it2.next();
96             assertEquals(msg + ", List: ", val1, val2);
97         }
98         assertEquals(msg + ", Iterator End: ", it1.hasNext(), it2.hasNext());
99     }
100
101     private void checkSameKeyVector(String JavaDoc msg, String JavaDoc key, Configuration c1, Configuration c2)
102     {
103         String JavaDoc [] s1 = c1.getStringArray(key);
104         String JavaDoc [] s2 = c2.getStringArray(key);
105
106         assertEquals(msg + ", length: ", s1.length, s2.length);
107
108         for (int i = 0; i < s1.length ; i++)
109         {
110             assertEquals(msg + ", String Array: ", s1[i], s2[i]);
111         }
112     }
113
114     /**
115      * Are both configurations equal after loading?
116      */

117     public void testLoading() throws Exception JavaDoc
118     {
119         Configuration simple = setupSimpleConfiguration();
120         Configuration composite = setupCompositeConfiguration();
121
122         checkEquality("testLoading", simple, composite);
123     }
124
125     /**
126      * If we delete a key, does it vanish? Does it leave all
127      * the other keys unchanged? How about an unset key?
128      */

129     public void testDeletingExisting() throws Exception JavaDoc
130     {
131         Configuration simple = setupSimpleConfiguration();
132         Configuration composite = setupCompositeConfiguration();
133
134         String JavaDoc key = "clear.property";
135
136         assertTrue(simple.containsKey(key));
137         assertEquals(simple.containsKey(key), composite.containsKey(key));
138
139         simple.clearProperty(key);
140         composite.clearProperty(key);
141
142         assertFalse(simple.containsKey(key));
143         assertEquals(simple.containsKey(key), composite.containsKey(key));
144
145         checkEquality("testDeletingExisting", simple, composite);
146     }
147
148     public void testDeletingNonExisting() throws Exception JavaDoc
149     {
150         Configuration simple = setupSimpleConfiguration();
151         Configuration composite = setupCompositeConfiguration();
152
153         String JavaDoc key = "nonexisting.clear.property";
154
155         assertFalse(simple.containsKey(key));
156         assertEquals(simple.containsKey(key), composite.containsKey(key));
157
158         simple.clearProperty(key);
159         composite.clearProperty(key);
160
161         assertFalse(simple.containsKey(key));
162         assertEquals(simple.containsKey(key), composite.containsKey(key));
163
164         checkEquality("testDeletingNonExisting", simple, composite);
165     }
166
167     /**
168      * If we set a key, does it work? How about an existing
169      * key? Can we change it?
170      */

171     public void testSettingNonExisting() throws Exception JavaDoc
172     {
173         Configuration simple = setupSimpleConfiguration();
174         Configuration composite = setupCompositeConfiguration();
175
176         String JavaDoc key = "nonexisting.property";
177         String JavaDoc value = "new value";
178
179         assertFalse(simple.containsKey(key));
180         assertEquals(simple.containsKey(key), composite.containsKey(key));
181
182         simple.setProperty(key, value);
183         composite.setProperty(key, value);
184
185         assertTrue(simple.containsKey(key));
186         assertEquals(simple.containsKey(key), composite.containsKey(key));
187
188         checkSameKey("testSettingNonExisting", key, simple, composite);
189         checkEquality("testSettingNonExisting", simple, composite);
190     }
191
192     public void testSettingExisting() throws Exception JavaDoc
193     {
194         Configuration simple = setupSimpleConfiguration();
195         Configuration composite = setupCompositeConfiguration();
196
197         String JavaDoc key = "existing.property";
198         String JavaDoc value = "new value";
199
200         assertTrue(simple.containsKey(key));
201         assertFalse(simple.getString(key).equals(value));
202         assertEquals(simple.containsKey(key), composite.containsKey(key));
203
204         simple.setProperty(key, value);
205         composite.setProperty(key, value);
206
207         assertTrue(simple.containsKey(key));
208         assertEquals(simple.getString(key), value);
209         assertEquals(simple.containsKey(key), composite.containsKey(key));
210
211         checkSameKey("testSettingExisting", key, simple, composite);
212         checkEquality("testSettingExisting", simple, composite);
213     }
214
215     /**
216      * If we add a key, does it work?
217      */

218     public void testAddingUnset() throws Exception JavaDoc
219     {
220         Configuration simple = setupSimpleConfiguration();
221         Configuration composite = setupCompositeConfiguration();
222
223         String JavaDoc key = "nonexisting.property";
224         String JavaDoc value = "new value";
225
226         assertFalse(simple.containsKey(key));
227         assertEquals(simple.containsKey(key), composite.containsKey(key));
228
229         simple.addProperty(key, value);
230         composite.addProperty(key, value);
231
232         checkSameKey("testAddingUnset", key, simple, composite);
233         checkEquality("testAddingUnset", simple, composite);
234     }
235
236     /**
237      * If we add a to an existing key, does it work?
238      */

239     public void testAddingSet() throws Exception JavaDoc
240     {
241         Configuration simple = setupSimpleConfiguration();
242         Configuration composite = setupCompositeConfiguration();
243
244         String JavaDoc key = "existing.property";
245         String JavaDoc value = "new value";
246
247         assertTrue(simple.containsKey(key));
248         assertEquals(simple.containsKey(key), composite.containsKey(key));
249
250         simple.addProperty(key, value);
251         composite.addProperty(key, value);
252
253         assertTrue(simple.containsKey(key));
254         assertEquals(simple.containsKey(key), composite.containsKey(key));
255
256         checkSameKey("testAddingSet", key, simple, composite);
257         checkEquality("testAddingSet", simple, composite);
258     }
259 }
260
Popular Tags