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.io.StringReader;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import junit.framework.TestCase;
026: import junitx.framework.ArrayAssert;
027: import junitx.framework.ListAssert;
028: import junitx.framework.ObjectAssert;
029: import org.apache.commons.configuration.Configuration;
030: import org.apache.commons.configuration.ConfigurationComparator;
031: import org.apache.commons.configuration.ConfigurationException;
032: import org.apache.commons.configuration.StrictConfigurationComparator;
033:
034: /**
035: * @author Emmanuel Bourg
036: * @version $Revision$, $Date$
037: */
038: public class TestPropertyListConfiguration extends TestCase {
039: private PropertyListConfiguration config;
040:
041: private String testProperties = new File("conf/test.plist")
042: .getAbsolutePath();
043:
044: protected void setUp() throws Exception {
045: config = new PropertyListConfiguration();
046: config.setFileName(testProperties);
047: config.load();
048: }
049:
050: public void testLoad() {
051: assertFalse("the configuration is empty", config.isEmpty());
052: }
053:
054: public void testLoadWithError() {
055: config = new PropertyListConfiguration();
056: try {
057: config.load(new StringReader(""));
058: fail("No exception thrown on loading an empty file");
059: } catch (ConfigurationException e) {
060: // expected
061: assertNotNull(e.getMessage());
062: }
063: }
064:
065: public void testString() {
066: assertEquals("simple-string", "string1", config
067: .getProperty("simple-string"));
068: }
069:
070: public void testQuotedString() {
071: assertEquals("quoted-string", "string2", config
072: .getProperty("quoted-string"));
073: assertEquals("quoted-string2", "this is a string", config
074: .getProperty("quoted-string2"));
075: assertEquals("complex-string",
076: "this is a \"complex\" string {(=,;)}", config
077: .getProperty("complex-string"));
078: }
079:
080: public void testEmptyArray() {
081: String key = "empty-array";
082: assertNotNull("array null", config.getProperty(key));
083:
084: List list = (List) config.getProperty(key);
085: assertTrue("array is not empty", list.isEmpty());
086: }
087:
088: public void testArray() {
089: String key = "array";
090: assertNotNull("array null", config.getProperty(key));
091:
092: List list = (List) config.getProperty(key);
093: assertFalse("array is empty", list.isEmpty());
094:
095: assertEquals("1st value", "value1", list.get(0));
096: assertEquals("2nd value", "value2", list.get(1));
097: assertEquals("3rd value", "value3", list.get(2));
098: }
099:
100: public void testNestedArrays() {
101: String key = "nested-arrays";
102:
103: Object array = config.getProperty(key);
104:
105: // root array
106: assertNotNull("array not found", array);
107: ObjectAssert.assertInstanceOf(
108: "the array element is not parsed as a List",
109: List.class, array);
110: List list = config.getList(key);
111:
112: assertFalse("empty array", list.isEmpty());
113: assertEquals("size", 2, list.size());
114:
115: // 1st array
116: ObjectAssert.assertInstanceOf(
117: "the array element is not parsed as a List",
118: List.class, list.get(0));
119: List list1 = (List) list.get(0);
120: assertFalse("nested array 1 is empty", list1.isEmpty());
121: assertEquals("size", 2, list1.size());
122: assertEquals("1st element", "a", list1.get(0));
123: assertEquals("2nd element", "b", list1.get(1));
124:
125: // 2nd array
126: ObjectAssert.assertInstanceOf(
127: "the array element is not parsed as a List",
128: List.class, list.get(1));
129: List list2 = (List) list.get(1);
130: assertFalse("nested array 2 is empty", list2.isEmpty());
131: assertEquals("size", 2, list2.size());
132: assertEquals("1st element", "c", list2.get(0));
133: assertEquals("2nd element", "d", list2.get(1));
134: }
135:
136: public void testDictionary() {
137: assertEquals("1st element in dictionary", "bar1", config
138: .getProperty("dictionary.foo1"));
139: assertEquals("2nd element in dictionary", "bar2", config
140: .getProperty("dictionary.foo2"));
141: }
142:
143: public void testDictionaryArray() {
144: String key = "dictionary-array";
145:
146: Object array = config.getProperty(key);
147:
148: // root array
149: assertNotNull("array not found", array);
150: ObjectAssert.assertInstanceOf(
151: "the array element is not parsed as a List",
152: List.class, array);
153: List list = config.getList(key);
154:
155: assertFalse("empty array", list.isEmpty());
156: assertEquals("size", 2, list.size());
157:
158: // 1st dictionary
159: ObjectAssert.assertInstanceOf(
160: "the dict element is not parsed as a Configuration",
161: Configuration.class, list.get(0));
162: Configuration conf1 = (Configuration) list.get(0);
163: assertFalse("configuration 1 is empty", conf1.isEmpty());
164: assertEquals("configuration element", "bar", conf1
165: .getProperty("foo"));
166:
167: // 2nd dictionary
168: ObjectAssert.assertInstanceOf(
169: "the dict element is not parsed as a Configuration",
170: Configuration.class, list.get(1));
171: Configuration conf2 = (Configuration) list.get(1);
172: assertFalse("configuration 2 is empty", conf2.isEmpty());
173: assertEquals("configuration element", "value", conf2
174: .getProperty("key"));
175: }
176:
177: public void testNestedDictionaries() {
178: assertEquals("nested property", "value", config
179: .getString("nested-dictionaries.foo.bar.key"));
180: }
181:
182: public void testData() {
183: ObjectAssert.assertInstanceOf("data", (new byte[0]).getClass(),
184: config.getProperty("data"));
185: ArrayAssert.assertEquals("data", "foo bar".getBytes(),
186: (byte[]) config.getProperty("data"));
187: }
188:
189: public void testSave() throws Exception {
190: File savedFile = new File("target/testsave.plist");
191:
192: // remove the file previously saved if necessary
193: if (savedFile.exists()) {
194: assertTrue(savedFile.delete());
195: }
196:
197: // save the configuration
198: String filename = savedFile.getAbsolutePath();
199: config.save(filename);
200:
201: assertTrue("The saved file doesn't exist", savedFile.exists());
202:
203: // read the configuration and compare the properties
204: Configuration checkConfig = new PropertyListConfiguration(
205: new File(filename));
206:
207: Iterator it = config.getKeys();
208: while (it.hasNext()) {
209: String key = (String) it.next();
210: assertTrue(
211: "The saved configuration doesn't contain the key '"
212: + key + "'", checkConfig.containsKey(key));
213:
214: Object value = checkConfig.getProperty(key);
215: if (value instanceof byte[]) {
216: byte[] array = (byte[]) value;
217: ArrayAssert.assertEquals("Value of the '" + key
218: + "' property", (byte[]) config
219: .getProperty(key), array);
220: } else if (value instanceof List) {
221: List list1 = (List) config.getProperty(key);
222: List list2 = (List) value;
223:
224: assertEquals("The size of the list for the key '" + key
225: + "' doesn't match", list1.size(), list2.size());
226:
227: for (int i = 0; i < list2.size(); i++) {
228: Object value1 = list1.get(i);
229: Object value2 = list2.get(i);
230:
231: if (value1 instanceof Configuration) {
232: ConfigurationComparator comparator = new StrictConfigurationComparator();
233: assertTrue("The dictionnary at index " + i
234: + " for the key '" + key
235: + "' doesn't match", comparator
236: .compare((Configuration) value1,
237: (Configuration) value2));
238: } else {
239: assertEquals("Element at index " + i
240: + " for the key '" + key + "'", value1,
241: value2);
242: }
243: }
244:
245: ListAssert.assertEquals("Value of the '" + key
246: + "' property", (List) config.getProperty(key),
247: list1);
248: } else {
249: assertEquals("Value of the '" + key + "' property",
250: config.getProperty(key), checkConfig
251: .getProperty(key));
252: }
253:
254: }
255: }
256:
257: public void testQuoteString() {
258: assertEquals("null string", null, config.quoteString(null));
259: assertEquals("simple string", "abcd", config
260: .quoteString("abcd"));
261: assertEquals("string with a space", "\"ab cd\"", config
262: .quoteString("ab cd"));
263: assertEquals("string with a quote", "\"foo\\\"bar\"", config
264: .quoteString("foo\"bar"));
265: assertEquals("string with a special char", "\"foo;bar\"",
266: config.quoteString("foo;bar"));
267: }
268:
269: public void testInitCopy() {
270: PropertyListConfiguration copy = new PropertyListConfiguration(
271: config);
272: assertFalse("Nothing was copied", copy.isEmpty());
273: }
274: }
|