1 package org.apache.commons.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 155408 2005-02-26 12:56:39Z dirkv $
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 private void checkSameKeyVector(String msg, String key, Configuration c1, Configuration c2)
102 {
103 String [] s1 = c1.getStringArray(key);
104 String [] 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
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
130 {
131 Configuration simple = setupSimpleConfiguration();
132 Configuration composite = setupCompositeConfiguration();
133
134 String 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
149 {
150 Configuration simple = setupSimpleConfiguration();
151 Configuration composite = setupCompositeConfiguration();
152
153 String 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
172 {
173 Configuration simple = setupSimpleConfiguration();
174 Configuration composite = setupCompositeConfiguration();
175
176 String key = "nonexisting.property";
177 String 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
193 {
194 Configuration simple = setupSimpleConfiguration();
195 Configuration composite = setupCompositeConfiguration();
196
197 String key = "existing.property";
198 String 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
219 {
220 Configuration simple = setupSimpleConfiguration();
221 Configuration composite = setupCompositeConfiguration();
222
223 String key = "nonexisting.property";
224 String 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
240 {
241 Configuration simple = setupSimpleConfiguration();
242 Configuration composite = setupCompositeConfiguration();
243
244 String key = "existing.property";
245 String 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 }