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.io.File;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import com.mockobjects.dynamic.Mock;
27
28 import junit.framework.TestCase;
29 import junitx.framework.ListAssert;
30
31 /***
32 * Tests the ConfigurationUtils class
33 *
34 * @version $Revision: 503227 $, $Date: 2007-02-03 17:19:15 +0100 (Sa, 03 Feb 2007) $
35 */
36 public class TestConfigurationUtils extends TestCase
37 {
38 protected Configuration config = new BaseConfiguration();
39
40 public void testToString()
41 {
42 String lineSeparator = System.getProperty("line.separator");
43
44 assertEquals("String representation of an empty configuration", "", ConfigurationUtils.toString(config));
45
46 config.setProperty("one", "1");
47 assertEquals("String representation of a configuration", "one=1", ConfigurationUtils.toString(config));
48
49 config.setProperty("two", "2");
50 assertEquals("String representation of a configuration", "one=1" + lineSeparator + "two=2" , ConfigurationUtils.toString(config));
51
52 config.clearProperty("one");
53 assertEquals("String representation of a configuration", "two=2" , ConfigurationUtils.toString(config));
54
55 config.setProperty("one","1");
56 assertEquals("String representation of a configuration", "two=2" + lineSeparator + "one=1" , ConfigurationUtils.toString(config));
57 }
58
59 public void testGetURL() throws Exception
60 {
61 assertEquals(
62 "http://localhost:8080/webapp/config/config.xml",
63 ConfigurationUtils
64 .getURL(
65 "http://localhost:8080/webapp/config/baseConfig.xml",
66 "config.xml")
67 .toString());
68 assertEquals(
69 "http://localhost:8080/webapp/config/config.xml",
70 ConfigurationUtils
71 .getURL(
72 "http://localhost:8080/webapp/baseConfig.xml",
73 "config/config.xml")
74 .toString());
75 URL url = ConfigurationUtils.getURL(null, "config.xml");
76 assertEquals("file", url.getProtocol());
77 assertEquals("", url.getHost());
78
79 assertEquals(
80 "http://localhost:8080/webapp/config/config.xml",
81 ConfigurationUtils
82 .getURL(
83 "ftp://ftp.server.com/downloads/baseConfig.xml",
84 "http://localhost:8080/webapp/config/config.xml")
85 .toString());
86 assertEquals(
87 "http://localhost:8080/webapp/config/config.xml",
88 ConfigurationUtils
89 .getURL(null, "http://localhost:8080/webapp/config/config.xml")
90 .toString());
91 File absFile = new File("config.xml").getAbsoluteFile();
92 assertEquals(
93 absFile.toURL(),
94 ConfigurationUtils.getURL(
95 "http://localhost:8080/webapp/config/baseConfig.xml",
96 absFile.getAbsolutePath()));
97 assertEquals(
98 absFile.toURL(),
99 ConfigurationUtils.getURL(null, absFile.getAbsolutePath()));
100
101 assertEquals(absFile.toURL(),
102 ConfigurationUtils.getURL(absFile.getParent(), "config.xml"));
103 }
104
105 public void testGetBasePath() throws Exception
106 {
107 URL url = new URL("http://xyz.net/foo/bar.xml");
108 assertEquals("base path of " + url, "http://xyz.net/foo/", ConfigurationUtils.getBasePath(url));
109
110 url = new URL("http://xyz.net/foo/");
111 assertEquals("base path of " + url, "http://xyz.net/foo/", ConfigurationUtils.getBasePath(url));
112
113 url = new URL("http://xyz.net/foo");
114 assertEquals("base path of " + url, "http://xyz.net/", ConfigurationUtils.getBasePath(url));
115
116 url = new URL("http://xyz.net/");
117 assertEquals("base path of " + url, "http://xyz.net/", ConfigurationUtils.getBasePath(url));
118
119 url = new URL("http://xyz.net");
120 assertEquals("base path of " + url, "http://xyz.net", ConfigurationUtils.getBasePath(url));
121 }
122
123 public void testGetFileName() throws Exception
124 {
125 assertEquals("file name for a null URL", null, ConfigurationUtils.getFileName(null));
126
127 URL url = new URL("http://xyz.net/foo/");
128 assertEquals("file for a directory URL " + url, null, ConfigurationUtils.getFileName(url));
129
130 url = new URL("http://xyz.net/foo/bar.xml");
131 assertEquals("file name for a valid URL " + url, "bar.xml", ConfigurationUtils.getFileName(url));
132 }
133
134 public void testCopy()
135 {
136
137 Configuration conf1 = new BaseConfiguration();
138 conf1.addProperty("key1", "value1");
139 conf1.addProperty("key2", "value2");
140
141
142 Configuration conf2 = new BaseConfiguration();
143 conf2.addProperty("key1", "value3");
144 conf2.addProperty("key2", "value4");
145
146
147 ConfigurationUtils.copy(conf1, conf2);
148
149 assertEquals("'key1' property", "value1", conf2.getProperty("key1"));
150 assertEquals("'key2' property", "value2", conf2.getProperty("key2"));
151 }
152
153 public void testAppend()
154 {
155
156 Configuration conf1 = new BaseConfiguration();
157 conf1.addProperty("key1", "value1");
158 conf1.addProperty("key2", "value2");
159
160
161 Configuration conf2 = new BaseConfiguration();
162 conf2.addProperty("key1", "value3");
163 conf2.addProperty("key2", "value4");
164
165
166 ConfigurationUtils.append(conf1, conf2);
167
168 List expected = new ArrayList();
169 expected.add("value3");
170 expected.add("value1");
171 ListAssert.assertEquals("'key1' property", expected, conf2.getList("key1"));
172
173 expected = new ArrayList();
174 expected.add("value4");
175 expected.add("value2");
176 ListAssert.assertEquals("'key2' property", expected, conf2.getList("key2"));
177 }
178
179 public void testGetFile() throws Exception
180 {
181 File directory = new File("target");
182 File reference = new File(directory, "test.txt").getAbsoluteFile();
183
184 assertEquals(reference, ConfigurationUtils.getFile(null, reference.getAbsolutePath()));
185 assertEquals(reference, ConfigurationUtils.getFile(directory.getAbsolutePath(), reference.getAbsolutePath()));
186 assertEquals(reference, ConfigurationUtils.getFile(directory.getAbsolutePath(), reference.getName()));
187 assertEquals(reference, ConfigurationUtils.getFile(directory.toURL().toString(), reference.getName()));
188 assertEquals(reference, ConfigurationUtils.getFile("invalid", reference.toURL().toString()));
189 assertEquals(reference, ConfigurationUtils.getFile(
190 "jar:file:/C:/myjar.jar!/my-config.xml/someprops.properties",
191 reference.getAbsolutePath()));
192 }
193
194 public void testLocateWithNullTCCL() throws Exception
195 {
196 ClassLoader cl = Thread.currentThread().getContextClassLoader();
197 try
198 {
199 Thread.currentThread().setContextClassLoader(null);
200 assertNull(ConfigurationUtils.locate("abase", "aname"));
201
202
203 }
204 finally
205 {
206 Thread.currentThread().setContextClassLoader(cl);
207 }
208 }
209
210 /***
211 * Tests converting a configuration into a hierarchical one.
212 */
213 public void testConvertToHierarchical()
214 {
215 Configuration conf = new BaseConfiguration();
216 for (int i = 0; i < 10; i++)
217 {
218 conf.addProperty("test" + i, "value" + i);
219 conf.addProperty("test.list", "item" + i);
220 }
221
222 HierarchicalConfiguration hc = ConfigurationUtils
223 .convertToHierarchical(conf);
224 for (Iterator it = conf.getKeys(); it.hasNext();)
225 {
226 String key = (String) it.next();
227 assertEquals("Wrong value for key " + key, conf.getProperty(key),
228 hc.getProperty(key));
229 }
230 }
231
232 /***
233 * Tests converting a configuration into a hierarchical one that is already
234 * hierarchical.
235 */
236 public void testConvertHierarchicalToHierarchical()
237 {
238 Configuration conf = new HierarchicalConfiguration();
239 conf.addProperty("test", "yes");
240 assertSame("Wrong configuration returned", conf, ConfigurationUtils
241 .convertToHierarchical(conf));
242 }
243
244 /***
245 * Tests converting a null configuration to a hierarchical one. The result
246 * should be null, too.
247 */
248 public void testConvertNullToHierarchical()
249 {
250 assertNull("Wrong conversion result for null config",
251 ConfigurationUtils.convertToHierarchical(null));
252 }
253
254 /***
255 * Tests cloning a configuration that supports this operation.
256 */
257 public void testCloneConfiguration()
258 {
259 HierarchicalConfiguration conf = new HierarchicalConfiguration();
260 conf.addProperty("test", "yes");
261 HierarchicalConfiguration copy = (HierarchicalConfiguration) ConfigurationUtils
262 .cloneConfiguration(conf);
263 assertNotSame("Same object was returned", conf, copy);
264 assertEquals("Property was not cloned", "yes", copy.getString("test"));
265 }
266
267 /***
268 * Tests cloning a configuration that does not support this operation. This
269 * should cause an exception.
270 */
271 public void testCloneConfigurationNotSupported()
272 {
273 Configuration myNonCloneableConfig = new NonCloneableConfiguration();
274 try
275 {
276 ConfigurationUtils.cloneConfiguration(myNonCloneableConfig);
277 fail("Could clone non cloneable config!");
278 }
279 catch (ConfigurationRuntimeException crex)
280 {
281
282 }
283 }
284
285 /***
286 * Tests cloning a <b>null</b> configuration.
287 */
288 public void testCloneConfigurationNull()
289 {
290 assertNull("Wrong return value", ConfigurationUtils
291 .cloneConfiguration(null));
292 }
293
294 /***
295 * Tests whether runtime exceptions can be enabled.
296 */
297 public void testEnableRuntimeExceptions()
298 {
299 PropertiesConfiguration config = new PropertiesConfiguration()
300 {
301 protected void addPropertyDirect(String key, Object value)
302 {
303
304 fireError(EVENT_ADD_PROPERTY, key, value, new RuntimeException(
305 "A faked exception!"));
306 }
307 };
308 config.clearErrorListeners();
309 ConfigurationUtils.enableRuntimeExceptions(config);
310 try
311 {
312 config.addProperty("test", "testValue");
313 fail("No runtime exception was thrown!");
314 }
315 catch (ConfigurationRuntimeException crex)
316 {
317
318 }
319 }
320
321 /***
322 * Tries to enable runtime exceptions for a configurtion that does not
323 * inherit from EventSource. This should cause an exception.
324 */
325 public void testEnableRuntimeExceptionsInvalid()
326 {
327 try
328 {
329 ConfigurationUtils
330 .enableRuntimeExceptions((Configuration) new Mock(
331 Configuration.class).proxy());
332 fail("Could enable exceptions for non EventSource configuration!");
333 }
334 catch (IllegalArgumentException iex)
335 {
336
337 }
338 }
339
340 /***
341 * Tries to enable runtime exceptions for a null configuration. This should
342 * cause an exception.
343 */
344 public void testEnableRuntimeExceptionsNull()
345 {
346 try
347 {
348 ConfigurationUtils.enableRuntimeExceptions(null);
349 fail("Could enable exceptions for a null configuration!");
350 }
351 catch (IllegalArgumentException iex)
352 {
353
354 }
355 }
356 }