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;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import junit.framework.TestCase;
028: import junitx.framework.ListAssert;
029:
030: /**
031: * Abstract TestCase for implementations of {@link AbstractConfiguration}.
032: *
033: * @author Emmanuel Bourg
034: * @version $Revision: 515306 $, $Date: 2007-03-06 22:15:00 +0100 (Di, 06 Mrz 2007) $
035: */
036: public abstract class TestAbstractConfiguration extends TestCase {
037: /**
038: * Return an abstract configuration with the following data:<br>
039: * <pre>
040: * key1 = value1
041: * key2 = value2
042: * list = value1, value2
043: * listesc = value1\\,value2
044: * </pre>
045: */
046: protected abstract AbstractConfiguration getConfiguration();
047:
048: /**
049: * Return an empty configuration.
050: */
051: protected abstract AbstractConfiguration getEmptyConfiguration();
052:
053: public void testGetProperty() {
054: Configuration config = getConfiguration();
055: assertEquals("key1", "value1", config.getProperty("key1"));
056: assertEquals("key2", "value2", config.getProperty("key2"));
057: assertNull("key3", config.getProperty("key3"));
058: }
059:
060: public void testList() {
061: Configuration config = getConfiguration();
062:
063: List list = config.getList("list");
064: assertNotNull("list not found", config.getProperty("list"));
065: assertEquals("list size", 2, list.size());
066: assertTrue("'value1' is not in the list", list
067: .contains("value1"));
068: assertTrue("'value2' is not in the list", list
069: .contains("value2"));
070: }
071:
072: /**
073: * Tests whether the escape character for list delimiters is recocknized and
074: * removed.
075: */
076: public void testListEscaped() {
077: assertEquals("Wrong value for escaped list", "value1,value2",
078: getConfiguration().getString("listesc"));
079: }
080:
081: public void testAddPropertyDirect() {
082: AbstractConfiguration config = getConfiguration();
083: config.addPropertyDirect("key3", "value3");
084: assertEquals("key3", "value3", config.getProperty("key3"));
085:
086: config.addPropertyDirect("key3", "value4");
087: config.addPropertyDirect("key3", "value5");
088: List list = config.getList("key3");
089: assertNotNull("no list found for the 'key3' property", list);
090:
091: List expected = new ArrayList();
092: expected.add("value3");
093: expected.add("value4");
094: expected.add("value5");
095:
096: ListAssert.assertEquals("values for the 'key3' property",
097: expected, list);
098: }
099:
100: public void testIsEmpty() {
101: Configuration config = getConfiguration();
102: assertFalse("the configuration is empty", config.isEmpty());
103: assertTrue("the configuration is not empty",
104: getEmptyConfiguration().isEmpty());
105: }
106:
107: public void testContainsKey() {
108: Configuration config = getConfiguration();
109: assertTrue("key1 not found", config.containsKey("key1"));
110: assertFalse("key3 found", config.containsKey("key3"));
111: }
112:
113: public void testClearProperty() {
114: Configuration config = getConfiguration();
115: config.clearProperty("key2");
116: assertFalse("key2 not cleared", config.containsKey("key2"));
117: }
118:
119: public void testGetKeys() {
120: Configuration config = getConfiguration();
121: Iterator keys = config.getKeys();
122:
123: List expectedKeys = new ArrayList();
124: expectedKeys.add("key1");
125: expectedKeys.add("key2");
126: expectedKeys.add("list");
127: expectedKeys.add("listesc");
128:
129: assertNotNull("null iterator", keys);
130: assertTrue("empty iterator", keys.hasNext());
131:
132: List actualKeys = new ArrayList();
133: while (keys.hasNext()) {
134: actualKeys.add(keys.next());
135: }
136:
137: ListAssert.assertEquals("keys", expectedKeys, actualKeys);
138: }
139:
140: /**
141: * Tests accessing the configuration's logger.
142: */
143: public void testSetLogger() {
144: AbstractConfiguration config = getEmptyConfiguration();
145: assertNotNull("Default logger is null", config.getLogger());
146: Log log = LogFactory.getLog(config.getClass());
147: config.setLogger(log);
148: assertSame("Logger was not set", log, config.getLogger());
149: }
150: }
|