1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }