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 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
40
41
42
43
44 public abstract class TestAbstractConfiguration
45 {
46
47
48
49
50
51
52
53
54
55 protected abstract AbstractConfiguration getConfiguration();
56
57
58
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
85
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
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
177
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 }