001: package org.apache.commons.configuration;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.File;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Compare the behaviour of various methods between CompositeConfiguration
028: * and normal (Properties) Configuration
029: *
030: * @version $Id: TestEqualBehaviour.java 439648 2006-09-02 20:42:10Z oheger $
031: */
032: public class TestEqualBehaviour extends TestCase {
033: private Configuration setupSimpleConfiguration() throws Exception {
034: String simpleConfigurationFile = new File(
035: "conf/testEqual.properties").getAbsolutePath();
036: return new PropertiesConfiguration(simpleConfigurationFile);
037: }
038:
039: private Configuration setupCompositeConfiguration()
040: throws Exception {
041: String compositeConfigurationFile = new File(
042: "conf/testEqualDigester.xml").getAbsolutePath();
043:
044: ConfigurationFactory configurationFactory = new ConfigurationFactory();
045: configurationFactory
046: .setConfigurationFileName(compositeConfigurationFile);
047: return configurationFactory.getConfiguration();
048: }
049:
050: /**
051: * Checks whether two configurations have the same size,
052: * the same key sequence and contain the same key -> value mappings
053: */
054: private void checkEquality(String msg, Configuration c1,
055: Configuration c2) {
056: Iterator it1 = c1.getKeys();
057: Iterator it2 = c2.getKeys();
058:
059: while (it1.hasNext() && it2.hasNext()) {
060: String key1 = (String) it1.next();
061: String key2 = (String) it2.next();
062: assertEquals(msg + ", Keys: ", key1, key2);
063: assertEquals(msg + ", Contains: ", c1.containsKey(key1), c2
064: .containsKey(key2));
065: }
066: assertEquals(msg + ", Iterator: ", it1.hasNext(), it2.hasNext());
067: }
068:
069: /**
070: * Checks whether two configurations have the same key -> value mapping
071: */
072: private void checkSameKey(String msg, String key, Configuration c1,
073: Configuration c2) {
074: String[] s1 = c1.getStringArray(key);
075: String[] s2 = c2.getStringArray(key);
076:
077: assertEquals(msg + ", length: ", s1.length, s2.length);
078:
079: for (int i = 0; i < s1.length; i++) {
080: assertEquals(msg + ", String Array: ", s1[i], s2[i]);
081: }
082:
083: List list1 = c1.getList(key);
084: List list2 = c2.getList(key);
085:
086: assertEquals(msg + ", Size: ", list1.size(), list2.size());
087:
088: Iterator it1 = list1.iterator();
089: Iterator it2 = list2.iterator();
090:
091: while (it1.hasNext() && it2.hasNext()) {
092: String val1 = (String) it1.next();
093: String val2 = (String) it2.next();
094: assertEquals(msg + ", List: ", val1, val2);
095: }
096: assertEquals(msg + ", Iterator End: ", it1.hasNext(), it2
097: .hasNext());
098: }
099:
100: /**
101: * Are both configurations equal after loading?
102: */
103: public void testLoading() throws Exception {
104: Configuration simple = setupSimpleConfiguration();
105: Configuration composite = setupCompositeConfiguration();
106:
107: checkEquality("testLoading", simple, composite);
108: }
109:
110: /**
111: * If we delete a key, does it vanish? Does it leave all
112: * the other keys unchanged? How about an unset key?
113: */
114: public void testDeletingExisting() throws Exception {
115: Configuration simple = setupSimpleConfiguration();
116: Configuration composite = setupCompositeConfiguration();
117:
118: String key = "clear.property";
119:
120: assertTrue(simple.containsKey(key));
121: assertEquals(simple.containsKey(key), composite
122: .containsKey(key));
123:
124: simple.clearProperty(key);
125: composite.clearProperty(key);
126:
127: assertFalse(simple.containsKey(key));
128: assertEquals(simple.containsKey(key), composite
129: .containsKey(key));
130:
131: checkEquality("testDeletingExisting", simple, composite);
132: }
133:
134: public void testDeletingNonExisting() throws Exception {
135: Configuration simple = setupSimpleConfiguration();
136: Configuration composite = setupCompositeConfiguration();
137:
138: String key = "nonexisting.clear.property";
139:
140: assertFalse(simple.containsKey(key));
141: assertEquals(simple.containsKey(key), composite
142: .containsKey(key));
143:
144: simple.clearProperty(key);
145: composite.clearProperty(key);
146:
147: assertFalse(simple.containsKey(key));
148: assertEquals(simple.containsKey(key), composite
149: .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: Configuration simple = setupSimpleConfiguration();
160: Configuration composite = setupCompositeConfiguration();
161:
162: String key = "nonexisting.property";
163: String value = "new value";
164:
165: assertFalse(simple.containsKey(key));
166: assertEquals(simple.containsKey(key), composite
167: .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
174: .containsKey(key));
175:
176: checkSameKey("testSettingNonExisting", key, simple, composite);
177: checkEquality("testSettingNonExisting", simple, composite);
178: }
179:
180: public void testSettingExisting() throws Exception {
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
190: .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
198: .containsKey(key));
199:
200: checkSameKey("testSettingExisting", key, simple, composite);
201: checkEquality("testSettingExisting", simple, composite);
202: }
203:
204: /**
205: * If we add a key, does it work?
206: */
207: public void testAddingUnset() throws Exception {
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
216: .containsKey(key));
217:
218: simple.addProperty(key, value);
219: composite.addProperty(key, value);
220:
221: checkSameKey("testAddingUnset", key, simple, composite);
222: checkEquality("testAddingUnset", simple, composite);
223: }
224:
225: /**
226: * If we add a to an existing key, does it work?
227: */
228: public void testAddingSet() throws Exception {
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
237: .containsKey(key));
238:
239: simple.addProperty(key, value);
240: composite.addProperty(key, value);
241:
242: assertTrue(simple.containsKey(key));
243: assertEquals(simple.containsKey(key), composite
244: .containsKey(key));
245:
246: checkSameKey("testAddingSet", key, simple, composite);
247: checkEquality("testAddingSet", simple, composite);
248: }
249: }
|