1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.configuration;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import junit.framework.TestCase;
28  import junitx.framework.ListAssert;
29  
30  /***
31   * Abstract TestCase for implementations of {@link AbstractConfiguration}.
32   *
33   * @author Emmanuel Bourg
34   * @version $Revision: 515306 $, $Date: 2007-03-06 22:15:00 +0100 (Di, 06 Mrz 2007) $
35   */
36  public abstract class TestAbstractConfiguration extends TestCase
37  {
38      /***
39       * Return an abstract configuration with the following data:<br>
40       * <pre>
41       * key1 = value1
42       * key2 = value2
43       * list = value1, value2
44       * listesc = value1//,value2
45       * </pre>
46       */
47      protected abstract AbstractConfiguration getConfiguration();
48  
49      /***
50       * Return an empty configuration.
51       */
52      protected abstract AbstractConfiguration getEmptyConfiguration();
53  
54      public void testGetProperty()
55      {
56          Configuration config = getConfiguration();
57          assertEquals("key1", "value1", config.getProperty("key1"));
58          assertEquals("key2", "value2", config.getProperty("key2"));
59          assertNull("key3", config.getProperty("key3"));
60      }
61  
62      public void testList()
63      {
64          Configuration config = getConfiguration();
65  
66          List list = config.getList("list");
67          assertNotNull("list not found", config.getProperty("list"));
68          assertEquals("list size", 2, list.size());
69          assertTrue("'value1' is not in the list", list.contains("value1"));
70          assertTrue("'value2' is not in the list", list.contains("value2"));
71      }
72  
73      /***
74       * Tests whether the escape character for list delimiters is recocknized and
75       * removed.
76       */
77      public void testListEscaped()
78      {
79          assertEquals("Wrong value for escaped list", "value1,value2",
80                  getConfiguration().getString("listesc"));
81      }
82  
83      public void testAddPropertyDirect()
84      {
85          AbstractConfiguration config = getConfiguration();
86          config.addPropertyDirect("key3", "value3");
87          assertEquals("key3", "value3", config.getProperty("key3"));
88  
89          config.addPropertyDirect("key3", "value4");
90          config.addPropertyDirect("key3", "value5");
91          List list = config.getList("key3");
92          assertNotNull("no list found for the 'key3' property", list);
93  
94          List expected = new ArrayList();
95          expected.add("value3");
96          expected.add("value4");
97          expected.add("value5");
98  
99          ListAssert.assertEquals("values for the 'key3' property", expected, list);
100     }
101 
102     public void testIsEmpty()
103     {
104         Configuration config = getConfiguration();
105         assertFalse("the configuration is empty", config.isEmpty());
106         assertTrue("the configuration is not empty", getEmptyConfiguration().isEmpty());
107     }
108 
109     public void testContainsKey()
110     {
111         Configuration config = getConfiguration();
112         assertTrue("key1 not found", config.containsKey("key1"));
113         assertFalse("key3 found", config.containsKey("key3"));
114     }
115 
116     public void testClearProperty()
117     {
118         Configuration config = getConfiguration();
119         config.clearProperty("key2");
120         assertFalse("key2 not cleared", config.containsKey("key2"));
121     }
122 
123     public void testGetKeys()
124     {
125         Configuration config = getConfiguration();
126         Iterator keys = config.getKeys();
127 
128         List expectedKeys = new ArrayList();
129         expectedKeys.add("key1");
130         expectedKeys.add("key2");
131         expectedKeys.add("list");
132         expectedKeys.add("listesc");
133 
134         assertNotNull("null iterator", keys);
135         assertTrue("empty iterator", keys.hasNext());
136 
137         List actualKeys = new ArrayList();
138         while (keys.hasNext())
139         {
140             actualKeys.add(keys.next());
141         }
142 
143         ListAssert.assertEquals("keys", expectedKeys, actualKeys);
144     }
145 
146     /***
147      * Tests accessing the configuration's logger.
148      */
149     public void testSetLogger()
150     {
151         AbstractConfiguration config = getEmptyConfiguration();
152         assertNotNull("Default logger is null", config.getLogger());
153         Log log = LogFactory.getLog(config.getClass());
154         config.setLogger(log);
155         assertSame("Logger was not set", log, config.getLogger());
156     }
157 }