1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.configuration;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  import junitx.framework.ListAssert;
25  
26  /***
27   * Abstract TestCase for implementations of {@link AbstractConfiguration}.
28   *
29   * @author Emmanuel Bourg
30   * @version $Revision$, $Date: 2005-08-24 13:08:23 +0200 (Wed, 24 Aug 2005) $
31   */
32  public abstract class TestAbstractConfiguration extends TestCase
33  {
34      /***
35       * Return an abstract configuration with 2 key/value pairs:<br>
36       * <pre>
37       * key1 = value1
38       * key2 = value2
39       * list = value1, value2
40       * </pre>
41       */
42      protected abstract AbstractConfiguration getConfiguration();
43  
44      /***
45       * Return an empty configuration.
46       */
47      protected abstract AbstractConfiguration getEmptyConfiguration();
48  
49      public void testGetProperty()
50      {
51          Configuration config = getConfiguration();
52          assertEquals("key1", "value1", config.getProperty("key1"));
53          assertEquals("key2", "value2", config.getProperty("key2"));
54          assertNull("key3", config.getProperty("key3"));
55      }
56  
57      public void testList()
58      {
59          Configuration config = getConfiguration();
60  
61          List list = config.getList("list");
62          assertNotNull("list not found", config.getProperty("list"));
63          assertEquals("list size", 2, list.size());
64          assertTrue("'value1' is not in the list", list.contains("value1"));
65          assertTrue("'value2' is not in the list", list.contains("value2"));
66      }
67  
68      public void testAddPropertyDirect()
69      {
70          AbstractConfiguration config = getConfiguration();
71          config.addPropertyDirect("key3", "value3");
72          assertEquals("key3", "value3", config.getProperty("key3"));
73  
74          config.addPropertyDirect("key3", "value4");
75          config.addPropertyDirect("key3", "value5");
76          List list = config.getList("key3");
77          assertNotNull("no list found for the 'key3' property", list);
78  
79          List expected = new ArrayList();
80          expected.add("value3");
81          expected.add("value4");
82          expected.add("value5");
83  
84          ListAssert.assertEquals("values for the 'key3' property", expected, list);
85      }
86  
87      public void testIsEmpty()
88      {
89          Configuration config = getConfiguration();
90          assertFalse("the configuration is empty", config.isEmpty());
91          assertTrue("the configuration is not empty", getEmptyConfiguration().isEmpty());
92      }
93  
94      public void testContainsKey()
95      {
96          Configuration config = getConfiguration();
97          assertTrue("key1 not found", config.containsKey("key1"));
98          assertFalse("key3 found", config.containsKey("key3"));
99      }
100 
101     public void testClearProperty()
102     {
103         Configuration config = getConfiguration();
104         config.clearProperty("key2");
105         assertFalse("key2 not cleared", config.containsKey("key2"));
106     }
107 
108     public void testGetKeys()
109     {
110         Configuration config = getConfiguration();
111         Iterator keys = config.getKeys();
112 
113         List expectedKeys = new ArrayList();
114         expectedKeys.add("key1");
115         expectedKeys.add("key2");
116         expectedKeys.add("list");
117 
118         assertNotNull("null iterator", keys);
119         assertTrue("empty iterator", keys.hasNext());
120 
121         List actualKeys = new ArrayList();
122         while (keys.hasNext())
123         {
124             actualKeys.add(keys.next());
125         }
126 
127         ListAssert.assertEquals("keys", expectedKeys, actualKeys);
128     }
129 
130 }