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 junit.framework.TestCase;
25  import junitx.framework.ListAssert;
26  
27  /***
28   * Abstract TestCase for implementations of {@link AbstractConfiguration}.
29   *
30   * @author Emmanuel Bourg
31   * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
32   */
33  public abstract class TestAbstractConfiguration extends TestCase
34  {
35      /***
36       * Return an abstract configuration with 2 key/value pairs:<br>
37       * <pre>
38       * key1 = value1
39       * key2 = value2
40       * list = value1, value2
41       * </pre>
42       */
43      protected abstract AbstractConfiguration getConfiguration();
44  
45      /***
46       * Return an empty configuration.
47       */
48      protected abstract AbstractConfiguration getEmptyConfiguration();
49  
50      public void testGetProperty()
51      {
52          Configuration config = getConfiguration();
53          assertEquals("key1", "value1", config.getProperty("key1"));
54          assertEquals("key2", "value2", config.getProperty("key2"));
55          assertNull("key3", config.getProperty("key3"));
56      }
57  
58      public void testList()
59      {
60          Configuration config = getConfiguration();
61  
62          List list = config.getList("list");
63          assertNotNull("list not found", config.getProperty("list"));
64          assertEquals("list size", 2, list.size());
65          assertTrue("'value1' is not in the list", list.contains("value1"));
66          assertTrue("'value2' is not in the list", list.contains("value2"));
67      }
68  
69      public void testAddPropertyDirect()
70      {
71          AbstractConfiguration config = getConfiguration();
72          config.addPropertyDirect("key3", "value3");
73          assertEquals("key3", "value3", config.getProperty("key3"));
74  
75          config.addPropertyDirect("key3", "value4");
76          config.addPropertyDirect("key3", "value5");
77          List list = config.getList("key3");
78          assertNotNull("no list found for the 'key3' property", list);
79  
80          List expected = new ArrayList();
81          expected.add("value3");
82          expected.add("value4");
83          expected.add("value5");
84  
85          ListAssert.assertEquals("values for the 'key3' property", expected, list);
86      }
87  
88      public void testIsEmpty()
89      {
90          Configuration config = getConfiguration();
91          assertFalse("the configuration is empty", config.isEmpty());
92          assertTrue("the configuration is not empty", getEmptyConfiguration().isEmpty());
93      }
94  
95      public void testContainsKey()
96      {
97          Configuration config = getConfiguration();
98          assertTrue("key1 not found", config.containsKey("key1"));
99          assertFalse("key3 found", config.containsKey("key3"));
100     }
101 
102     public void testClearProperty()
103     {
104         Configuration config = getConfiguration();
105         config.clearProperty("key2");
106         assertFalse("key2 not cleared", config.containsKey("key2"));
107     }
108 
109     public void testGetKeys()
110     {
111         Configuration config = getConfiguration();
112         Iterator keys = config.getKeys();
113 
114         List expectedKeys = new ArrayList();
115         expectedKeys.add("key1");
116         expectedKeys.add("key2");
117         expectedKeys.add("list");
118 
119         assertNotNull("null iterator", keys);
120         assertTrue("empty iterator", keys.hasNext());
121 
122         List actualKeys = new ArrayList();
123         while (keys.hasNext())
124         {
125             actualKeys.add(keys.next());
126         }
127 
128         ListAssert.assertEquals("keys", expectedKeys, actualKeys);
129     }
130 
131 }