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