View Javadoc

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 static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertSame;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import junitx.framework.ListAssert;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.junit.Test;
37  
38  /**
39   * Abstract TestCase for implementations of {@link AbstractConfiguration}.
40   *
41   * @author Emmanuel Bourg
42   * @version $Id: TestAbstractConfiguration.java 1222465 2011-12-22 21:32:56Z oheger $
43   */
44  public abstract class TestAbstractConfiguration
45  {
46      /**
47       * Return an abstract configuration with the following data:<br>
48       * <pre>
49       * key1 = value1
50       * key2 = value2
51       * list = value1, value2
52       * listesc = value1\\,value2
53       * </pre>
54       */
55      protected abstract AbstractConfiguration getConfiguration();
56  
57      /**
58       * Return an empty configuration.
59       */
60      protected abstract AbstractConfiguration getEmptyConfiguration();
61  
62      @Test
63      public void testGetProperty()
64      {
65          Configuration config = getConfiguration();
66          assertEquals("key1", "value1", config.getProperty("key1"));
67          assertEquals("key2", "value2", config.getProperty("key2"));
68          assertNull("key3", config.getProperty("key3"));
69      }
70  
71      @Test
72      public void testList()
73      {
74          Configuration config = getConfiguration();
75  
76          List<?> list = config.getList("list");
77          assertNotNull("list not found", config.getProperty("list"));
78          assertEquals("list size", 2, list.size());
79          assertTrue("'value1' is not in the list", list.contains("value1"));
80          assertTrue("'value2' is not in the list", list.contains("value2"));
81      }
82  
83      /**
84       * Tests whether the escape character for list delimiters is recocknized and
85       * removed.
86       */
87      @Test
88      public void testListEscaped()
89      {
90          assertEquals("Wrong value for escaped list", "value1,value2",
91                  getConfiguration().getString("listesc"));
92      }
93  
94      @Test
95      public void testAddPropertyDirect()
96      {
97          AbstractConfiguration config = getConfiguration();
98          config.addPropertyDirect("key3", "value3");
99          assertEquals("key3", "value3", config.getProperty("key3"));
100 
101         config.addPropertyDirect("key3", "value4");
102         config.addPropertyDirect("key3", "value5");
103         List<Object> list = config.getList("key3");
104         assertNotNull("no list found for the 'key3' property", list);
105 
106         List<Object> expected = new ArrayList<Object>();
107         expected.add("value3");
108         expected.add("value4");
109         expected.add("value5");
110 
111         ListAssert.assertEquals("values for the 'key3' property", expected, list);
112     }
113 
114     @Test
115     public void testIsEmpty()
116     {
117         Configuration config = getConfiguration();
118         assertFalse("the configuration is empty", config.isEmpty());
119         assertTrue("the configuration is not empty", getEmptyConfiguration().isEmpty());
120     }
121 
122     @Test
123     public void testContainsKey()
124     {
125         Configuration config = getConfiguration();
126         assertTrue("key1 not found", config.containsKey("key1"));
127         assertFalse("key3 found", config.containsKey("key3"));
128     }
129 
130     @Test
131     public void testClearProperty()
132     {
133         Configuration config = getConfiguration();
134         config.clearProperty("key2");
135         assertFalse("key2 not cleared", config.containsKey("key2"));
136     }
137 
138     @Test
139     public void testGetKeys()
140     {
141         Configuration config = getConfiguration();
142         Iterator<String> keys = config.getKeys();
143 
144         List<String> expectedKeys = new ArrayList<String>();
145         expectedKeys.add("key1");
146         expectedKeys.add("key2");
147         expectedKeys.add("list");
148         expectedKeys.add("listesc");
149 
150         assertNotNull("null iterator", keys);
151         assertTrue("empty iterator", keys.hasNext());
152 
153         List<String> actualKeys = new ArrayList<String>();
154         while (keys.hasNext())
155         {
156             actualKeys.add(keys.next());
157         }
158 
159         ListAssert.assertEquals("keys", expectedKeys, actualKeys);
160     }
161 
162     /**
163      * Tests accessing the configuration's logger.
164      */
165     @Test
166     public void testSetLogger()
167     {
168         AbstractConfiguration config = getEmptyConfiguration();
169         assertNotNull("Default logger is null", config.getLogger());
170         Log log = LogFactory.getLog(config.getClass());
171         config.setLogger(log);
172         assertSame("Logger was not set", log, config.getLogger());
173     }
174 
175     /**
176      * Tests the exception message triggered by the conversion to BigInteger.
177      * This test is related to CONFIGURATION-357.
178      */
179     @Test
180     public void testGetBigIntegerConversion()
181     {
182         Configuration config = getConfiguration();
183         try
184         {
185             config.getBigInteger("key1");
186             fail("No conversion exception thrown!");
187         }
188         catch (ConversionException cex)
189         {
190             assertEquals("Wrong exception message",
191                     "'key1' doesn't map to a BigInteger object", cex
192                             .getMessage());
193         }
194     }
195 }