001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.configuration.plist;
019:
020: import java.io.File;
021: import java.util.*;
022:
023: import junit.framework.TestCase;
024: import junitx.framework.ObjectAssert;
025: import junitx.framework.ArrayAssert;
026: import junitx.framework.ListAssert;
027: import org.apache.commons.configuration.FileConfiguration;
028: import org.apache.commons.configuration.Configuration;
029: import org.apache.commons.configuration.HierarchicalConfiguration;
030: import org.apache.commons.configuration.StrictConfigurationComparator;
031: import org.apache.commons.configuration.ConfigurationComparator;
032:
033: /**
034: * @author Emmanuel Bourg
035: * @version $Revision$, $Date$
036: */
037: public class TestXMLPropertyListConfiguration extends TestCase {
038: private FileConfiguration config;
039:
040: protected void setUp() throws Exception {
041: config = new XMLPropertyListConfiguration();
042: config.setFileName("conf/test.plist.xml");
043: config.load();
044: }
045:
046: public void testString() throws Exception {
047: assertEquals("'string' property", "value1", config
048: .getString("string"));
049: }
050:
051: public void testInteger() throws Exception {
052: assertEquals("'integer' property", 12345, config
053: .getInt("integer"));
054: }
055:
056: public void testReal() throws Exception {
057: assertEquals("'real' property", -12.345, config
058: .getDouble("real"), 0);
059: }
060:
061: public void testBoolean() throws Exception {
062: assertEquals("'boolean1' property", true, config
063: .getBoolean("boolean1"));
064: assertEquals("'boolean2' property", false, config
065: .getBoolean("boolean2"));
066: }
067:
068: public void testDictionary() {
069: assertEquals("1st element", "value1", config
070: .getProperty("dictionary.key1"));
071: assertEquals("2nd element", "value2", config
072: .getProperty("dictionary.key2"));
073: assertEquals("3rd element", "value3", config
074: .getProperty("dictionary.key3"));
075: }
076:
077: public void testSubset() {
078: Configuration subset = config.subset("dictionary");
079: Iterator keys = subset.getKeys();
080:
081: String key = (String) keys.next();
082: assertEquals("1st key", "key1", key);
083: assertEquals("1st value", "value1", subset.getString(key));
084:
085: key = (String) keys.next();
086: assertEquals("2nd key", "key2", key);
087: assertEquals("2nd value", "value2", subset.getString(key));
088:
089: key = (String) keys.next();
090: assertEquals("3rd key", "key3", key);
091: assertEquals("3rd value", "value3", subset.getString(key));
092:
093: assertFalse("more than 3 properties founds", keys.hasNext());
094: }
095:
096: public void testArray() {
097: Object array = config.getProperty("array");
098:
099: assertNotNull("array not found", array);
100: ObjectAssert.assertInstanceOf(
101: "the array element is not parsed as a List",
102: List.class, array);
103: List list = config.getList("array");
104:
105: assertFalse("empty array", list.isEmpty());
106: assertEquals("size", 3, list.size());
107: assertEquals("1st element", "value1", list.get(0));
108: assertEquals("2nd element", "value2", list.get(1));
109: assertEquals("3rd element", "value3", list.get(2));
110: }
111:
112: public void testNestedArray() {
113: String key = "nested-array";
114:
115: Object array = config.getProperty(key);
116:
117: // root array
118: assertNotNull("array not found", array);
119: ObjectAssert.assertInstanceOf(
120: "the array element is not parsed as a List",
121: List.class, array);
122: List list = config.getList(key);
123:
124: assertFalse("empty array", list.isEmpty());
125: assertEquals("size", 2, list.size());
126:
127: // 1st array
128: ObjectAssert.assertInstanceOf(
129: "the array element is not parsed as a List",
130: List.class, list.get(0));
131: List list1 = (List) list.get(0);
132: assertFalse("nested array 1 is empty", list1.isEmpty());
133: assertEquals("size", 2, list1.size());
134: assertEquals("1st element", "a", list1.get(0));
135: assertEquals("2nd element", "b", list1.get(1));
136:
137: // 2nd array
138: ObjectAssert.assertInstanceOf(
139: "the array element is not parsed as a List",
140: List.class, list.get(1));
141: List list2 = (List) list.get(1);
142: assertFalse("nested array 2 is empty", list2.isEmpty());
143: assertEquals("size", 2, list2.size());
144: assertEquals("1st element", "c", list2.get(0));
145: assertEquals("2nd element", "d", list2.get(1));
146: }
147:
148: public void testDictionaryArray() {
149: String key = "dictionary-array";
150:
151: Object array = config.getProperty(key);
152:
153: // root array
154: assertNotNull("array not found", array);
155: ObjectAssert.assertInstanceOf(
156: "the array element is not parsed as a List",
157: List.class, array);
158: List list = config.getList(key);
159:
160: assertFalse("empty array", list.isEmpty());
161: assertEquals("size", 2, list.size());
162:
163: // 1st dictionary
164: ObjectAssert.assertInstanceOf(
165: "the dict element is not parsed as a Configuration",
166: Configuration.class, list.get(0));
167: Configuration conf1 = (Configuration) list.get(0);
168: assertFalse("configuration 1 is empty", conf1.isEmpty());
169: assertEquals("configuration element", "bar", conf1
170: .getProperty("foo"));
171:
172: // 2nd dictionary
173: ObjectAssert.assertInstanceOf(
174: "the dict element is not parsed as a Configuration",
175: Configuration.class, list.get(1));
176: Configuration conf2 = (Configuration) list.get(1);
177: assertFalse("configuration 2 is empty", conf2.isEmpty());
178: assertEquals("configuration element", "value", conf2
179: .getProperty("key"));
180: }
181:
182: public void testNested() {
183: assertEquals("nested property", "value", config
184: .getString("nested.node1.node2.node3"));
185: }
186:
187: public void testSave() throws Exception {
188: File savedFile = new File("target/testsave.plist.xml");
189:
190: // remove the file previously saved if necessary
191: if (savedFile.exists()) {
192: assertTrue(savedFile.delete());
193: }
194:
195: // add an array of strings to the configuration
196: /*
197: config.addProperty("string", "value1");
198: List list = new ArrayList();
199: for (int i = 1; i < 5; i++)
200: {
201: list.add("value" + i);
202: }
203: config.addProperty("newarray", list);*/
204: // todo : investigate why the array structure of 'newarray' is lost in the saved file
205: // add a map of strings
206: /*
207: Map map = new HashMap();
208: map.put("foo", "bar");
209: map.put("int", new Integer(123));
210: config.addProperty("newmap", map);
211: */
212: // todo : a Map added to a HierarchicalConfiguration should be decomposed as list of nodes
213: // save the configuration
214: String filename = savedFile.getAbsolutePath();
215: config.save(filename);
216:
217: assertTrue("The saved file doesn't exist", savedFile.exists());
218:
219: // read the configuration and compare the properties
220: Configuration checkConfig = new XMLPropertyListConfiguration(
221: new File(filename));
222:
223: Iterator it = config.getKeys();
224: while (it.hasNext()) {
225: String key = (String) it.next();
226: assertTrue(
227: "The saved configuration doesn't contain the key '"
228: + key + "'", checkConfig.containsKey(key));
229:
230: Object value = checkConfig.getProperty(key);
231: if (value instanceof byte[]) {
232: byte[] array = (byte[]) value;
233: ArrayAssert.assertEquals("Value of the '" + key
234: + "' property", (byte[]) config
235: .getProperty(key), array);
236: } else if (value instanceof List) {
237: List list1 = (List) config.getProperty(key);
238: List list2 = (List) value;
239:
240: assertEquals("The size of the list for the key '" + key
241: + "' doesn't match", list1.size(), list2.size());
242:
243: for (int i = 0; i < list2.size(); i++) {
244: Object value1 = list1.get(i);
245: Object value2 = list2.get(i);
246:
247: if (value1 instanceof Configuration) {
248: ConfigurationComparator comparator = new StrictConfigurationComparator();
249: assertTrue("The dictionnary at index " + i
250: + " for the key '" + key
251: + "' doesn't match", comparator
252: .compare((Configuration) value1,
253: (Configuration) value2));
254: } else {
255: assertEquals("Element at index " + i
256: + " for the key '" + key + "'", value1,
257: value2);
258: }
259: }
260:
261: ListAssert.assertEquals("Value of the '" + key
262: + "' property", (List) config.getProperty(key),
263: list1);
264: } else {
265: assertEquals("Value of the '" + key + "' property",
266: config.getProperty(key), checkConfig
267: .getProperty(key));
268: }
269:
270: }
271: }
272:
273: public void testInitCopy() {
274: XMLPropertyListConfiguration copy = new XMLPropertyListConfiguration(
275: (HierarchicalConfiguration) config);
276: StrictConfigurationComparator comp = new StrictConfigurationComparator();
277: assertTrue("Configurations are not equal", comp.compare(config,
278: copy));
279: }
280: }
|