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: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 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  
75      public void testIsEmpty()
76      {
77          Configuration config = getConfiguration();
78          assertFalse("the configuration is empty", config.isEmpty());
79          assertTrue("the configuration is not empty", getEmptyConfiguration().isEmpty());
80      }
81  
82      public void testContainsKey()
83      {
84          Configuration config = getConfiguration();
85          assertTrue("key1 not found", config.containsKey("key1"));
86          assertFalse("key3 found", config.containsKey("key3"));
87      }
88  
89      public void testClearProperty()
90      {
91          Configuration config = getConfiguration();
92          config.clearProperty("key2");
93          assertFalse("key2 not cleared", config.containsKey("key2"));
94      }
95  
96      public void testGetKeys()
97      {
98          Configuration config = getConfiguration();
99          Iterator keys = config.getKeys();
100 
101         List expectedKeys = new ArrayList();
102         expectedKeys.add("key1");
103         expectedKeys.add("key2");
104         expectedKeys.add("list");
105 
106         assertNotNull("null iterator", keys);
107         assertTrue("empty iterator", keys.hasNext());
108 
109         List actualKeys = new ArrayList();
110         while (keys.hasNext())
111         {
112             actualKeys.add(keys.next());
113         }
114 
115         ListAssert.assertEquals("keys", expectedKeys, actualKeys);
116     }
117 
118 }