View Javadoc

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 static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNotSame;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertSame;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.io.File;
28  import java.net.MalformedURLException;
29  import java.net.URL;
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  import junitx.framework.ListAssert;
35  
36  import org.apache.commons.configuration.tree.DefaultExpressionEngine;
37  import org.apache.commons.configuration.tree.ExpressionEngine;
38  import org.junit.Test;
39  
40  import com.mockobjects.dynamic.Mock;
41  
42  /**
43   * Tests the ConfigurationUtils class
44   *
45   * @version $Id: TestConfigurationUtils.java 1301996 2012-03-17 20:30:39Z sebb $
46   */
47  public class TestConfigurationUtils
48  {
49      protected Configuration config = new BaseConfiguration();
50  
51      @Test
52      public void testToString()
53      {
54          String lineSeparator = System.getProperty("line.separator");
55  
56          assertEquals("String representation of an empty configuration", "", ConfigurationUtils.toString(config));
57  
58          config.setProperty("one", "1");
59          assertEquals("String representation of a configuration", "one=1", ConfigurationUtils.toString(config));
60  
61          config.setProperty("two", "2");
62          assertEquals("String representation of a configuration", "one=1" + lineSeparator + "two=2" , ConfigurationUtils.toString(config));
63  
64          config.clearProperty("one");
65          assertEquals("String representation of a configuration", "two=2" , ConfigurationUtils.toString(config));
66  
67          config.setProperty("one","1");
68          assertEquals("String representation of a configuration", "two=2" + lineSeparator + "one=1" , ConfigurationUtils.toString(config));
69      }
70  
71      @Test
72      public void testGetURL() throws Exception
73      {
74          assertEquals(
75              "http://localhost:8080/webapp/config/config.xml",
76              ConfigurationUtils
77                  .getURL(
78                      "http://localhost:8080/webapp/config/baseConfig.xml",
79                      "config.xml")
80                  .toString());
81          assertEquals(
82              "http://localhost:8080/webapp/config/config.xml",
83              ConfigurationUtils
84                  .getURL(
85                      "http://localhost:8080/webapp/baseConfig.xml",
86                      "config/config.xml")
87                  .toString());
88          URL url = ConfigurationUtils.getURL(null, "config.xml");
89          assertEquals("file", url.getProtocol());
90          assertEquals("", url.getHost());
91  
92          assertEquals(
93              "http://localhost:8080/webapp/config/config.xml",
94              ConfigurationUtils
95                  .getURL(
96                      "ftp://ftp.server.com/downloads/baseConfig.xml",
97                      "http://localhost:8080/webapp/config/config.xml")
98                  .toString());
99          assertEquals(
100             "http://localhost:8080/webapp/config/config.xml",
101             ConfigurationUtils
102                 .getURL(null, "http://localhost:8080/webapp/config/config.xml")
103                 .toString());
104         File absFile = new File("config.xml").getAbsoluteFile();
105         assertEquals(
106             absFile.toURI().toURL(),
107             ConfigurationUtils.getURL(
108                 "http://localhost:8080/webapp/config/baseConfig.xml",
109                 absFile.getAbsolutePath()));
110         assertEquals(
111             absFile.toURI().toURL(),
112             ConfigurationUtils.getURL(null, absFile.getAbsolutePath()));
113 
114         assertEquals(absFile.toURI().toURL(),
115         ConfigurationUtils.getURL(absFile.getParent(), "config.xml"));
116     }
117 
118     @Test
119     public void testGetBasePath() throws Exception
120     {
121         URL url = new URL("http://xyz.net/foo/bar.xml");
122         assertEquals("base path of " + url, "http://xyz.net/foo/", ConfigurationUtils.getBasePath(url));
123 
124         url = new URL("http://xyz.net/foo/");
125         assertEquals("base path of " + url, "http://xyz.net/foo/", ConfigurationUtils.getBasePath(url));
126 
127         url = new URL("http://xyz.net/foo");
128         assertEquals("base path of " + url, "http://xyz.net/", ConfigurationUtils.getBasePath(url));
129 
130         url = new URL("http://xyz.net/");
131         assertEquals("base path of " + url, "http://xyz.net/", ConfigurationUtils.getBasePath(url));
132 
133         url = new URL("http://xyz.net");
134         assertEquals("base path of " + url, "http://xyz.net", ConfigurationUtils.getBasePath(url));
135     }
136 
137     @Test
138     public void testGetFileName() throws Exception
139     {
140         assertEquals("file name for a null URL", null, ConfigurationUtils.getFileName(null));
141 
142         URL url = new URL("http://xyz.net/foo/");
143         assertEquals("file for a directory URL " + url, null, ConfigurationUtils.getFileName(url));
144 
145         url = new URL("http://xyz.net/foo/bar.xml");
146         assertEquals("file name for a valid URL " + url, "bar.xml", ConfigurationUtils.getFileName(url));
147     }
148 
149     @Test
150     public void testCopy()
151     {
152         // create the source configuration
153         Configuration conf1 = new BaseConfiguration();
154         conf1.addProperty("key1", "value1");
155         conf1.addProperty("key2", "value2");
156 
157         // create the target configuration
158         Configuration conf2 = new BaseConfiguration();
159         conf2.addProperty("key1", "value3");
160         conf2.addProperty("key2", "value4");
161 
162         // copy the source configuration into the target configuration
163         ConfigurationUtils.copy(conf1, conf2);
164 
165         assertEquals("'key1' property", "value1", conf2.getProperty("key1"));
166         assertEquals("'key2' property", "value2", conf2.getProperty("key2"));
167     }
168 
169     @Test
170     public void testAppend()
171     {
172         // create the source configuration
173         Configuration conf1 = new BaseConfiguration();
174         conf1.addProperty("key1", "value1");
175         conf1.addProperty("key2", "value2");
176 
177         // create the target configuration
178         Configuration conf2 = new BaseConfiguration();
179         conf2.addProperty("key1", "value3");
180         conf2.addProperty("key2", "value4");
181 
182         // append the source configuration to the target configuration
183         ConfigurationUtils.append(conf1, conf2);
184 
185         List<Object> expected = new ArrayList<Object>();
186         expected.add("value3");
187         expected.add("value1");
188         ListAssert.assertEquals("'key1' property", expected, conf2.getList("key1"));
189 
190         expected = new ArrayList<Object>();
191         expected.add("value4");
192         expected.add("value2");
193         ListAssert.assertEquals("'key2' property", expected, conf2.getList("key2"));
194     }
195 
196     @Test
197     public void testGetFile() throws Exception
198     {
199         File directory = new File("target");
200         File reference = new File(directory, "test.txt").getAbsoluteFile();
201 
202         assertEquals(reference, ConfigurationUtils.getFile(null, reference.getAbsolutePath()));
203         assertEquals(reference, ConfigurationUtils.getFile(directory.getAbsolutePath(), reference.getAbsolutePath()));
204         assertEquals(reference, ConfigurationUtils.getFile(directory.getAbsolutePath(), reference.getName()));
205         assertEquals(reference, ConfigurationUtils.getFile(directory.toURI().toURL().toString(), reference.getName()));
206         assertEquals(reference, ConfigurationUtils.getFile("invalid", reference.toURI().toURL().toString()));
207         assertEquals(reference, ConfigurationUtils.getFile(
208                 "jar:file:/C:/myjar.jar!/my-config.xml/someprops.properties",
209                 reference.getAbsolutePath()));
210     }
211 
212     /**
213      * Tests whether a "+" character in the file name is handled correctly by
214      * fileFromURL(). This test is related to CONFIGURATION-415.
215      */
216     @Test
217     public void testFileFromURLWithPlus() throws MalformedURLException
218     {
219         File file = new File(new File("target"), "foo+bar.txt")
220                 .getAbsoluteFile();
221         URL fileURL = file.toURI().toURL();
222         File file2 = ConfigurationUtils.fileFromURL(fileURL);
223         assertEquals("Wrong file", file, file2);
224     }
225 
226     /**
227      * Tests whether fileFromURL() handles null URLs correctly.
228      */
229     @Test
230     public void testFileFromURLNull() throws Exception
231     {
232         assertNull("Wrong file for null URL", ConfigurationUtils
233                 .fileFromURL(null));
234     }
235 
236     @Test
237     public void testLocateWithNullTCCL() throws Exception
238     {
239         ClassLoader cl = Thread.currentThread().getContextClassLoader();
240         try
241         {
242             Thread.currentThread().setContextClassLoader(null);
243             assertNull(ConfigurationUtils.locate("abase", "aname"));
244             // This assert fails when maven 2 is used, so commented out
245             //assertNotNull(ConfigurationUtils.locate("test.xml"));
246         }
247         finally
248         {
249             Thread.currentThread().setContextClassLoader(cl);
250         }
251     }
252 
253     /**
254      * Tests converting a configuration into a hierarchical one.
255      */
256     @Test
257     public void testConvertToHierarchical()
258     {
259         Configuration conf = new BaseConfiguration();
260         for (int i = 0; i < 10; i++)
261         {
262             conf.addProperty("test" + i, "value" + i);
263             conf.addProperty("test.list", "item" + i);
264         }
265 
266         HierarchicalConfiguration hc = ConfigurationUtils
267                 .convertToHierarchical(conf);
268         for (Iterator<String> it = conf.getKeys(); it.hasNext();)
269         {
270             String key = it.next();
271             assertEquals("Wrong value for key " + key, conf.getProperty(key),
272                     hc.getProperty(key));
273         }
274     }
275 
276     /**
277      * Tests converting a configuration into a hierarchical one that is already
278      * hierarchical.
279      */
280     @Test
281     public void testConvertHierarchicalToHierarchical()
282     {
283         Configuration conf = new HierarchicalConfiguration();
284         conf.addProperty("test", "yes");
285         assertSame("Wrong configuration returned", conf, ConfigurationUtils
286                 .convertToHierarchical(conf));
287     }
288 
289     /**
290      * Tests converting a null configuration to a hierarchical one. The result
291      * should be null, too.
292      */
293     @Test
294     public void testConvertNullToHierarchical()
295     {
296         assertNull("Wrong conversion result for null config",
297                 ConfigurationUtils.convertToHierarchical(null));
298     }
299 
300     /**
301      * Tests converting a configuration into a hierarchical one if some of its
302      * properties contain escaped list delimiter characters.
303      */
304     @Test
305     public void testConvertToHierarchicalDelimiters()
306     {
307         Configuration conf = new BaseConfiguration();
308         conf.addProperty("test.key", "1\\,2\\,3");
309         assertEquals("Wrong property value", "1,2,3", conf
310                 .getString("test.key"));
311         HierarchicalConfiguration hc = ConfigurationUtils
312                 .convertToHierarchical(conf);
313         assertEquals("Escaped list delimiters not correctly handled", "1,2,3",
314                 hc.getString("test.key"));
315     }
316 
317     /**
318      * Tests converting a configuration to a hierarchical one using a specific
319      * expression engine.
320      */
321     @Test
322     public void testConvertToHierarchicalEngine()
323     {
324         Configuration conf = new BaseConfiguration();
325         conf.addProperty("test(a)", Boolean.TRUE);
326         conf.addProperty("test(b)", Boolean.FALSE);
327         DefaultExpressionEngine engine = new DefaultExpressionEngine();
328         engine.setIndexStart("[");
329         engine.setIndexEnd("]");
330         HierarchicalConfiguration hc = ConfigurationUtils
331                 .convertToHierarchical(conf, engine);
332         assertTrue("Wrong value for test(a)", hc.getBoolean("test(a)"));
333         assertFalse("Wrong value for test(b)", hc.getBoolean("test(b)"));
334     }
335 
336     /**
337      * Tests converting an already hierarchical configuration using an
338      * expression engine. The new engine should be set.
339      */
340     @Test
341     public void testConvertHierarchicalToHierarchicalEngine()
342     {
343         HierarchicalConfiguration hc = new HierarchicalConfiguration();
344         ExpressionEngine engine = new DefaultExpressionEngine();
345         assertSame("Created new configuration", hc, ConfigurationUtils
346                 .convertToHierarchical(hc, engine));
347         assertSame("Engine was not set", engine, hc.getExpressionEngine());
348     }
349 
350     /**
351      * Tests converting an already hierarchical configuration using a null
352      * expression engine. In this case the expression engine of the
353      * configuration should not be touched.
354      */
355     @Test
356     public void testConvertHierarchicalToHierarchicalNullEngine()
357     {
358         HierarchicalConfiguration hc = new HierarchicalConfiguration();
359         ExpressionEngine engine = new DefaultExpressionEngine();
360         hc.setExpressionEngine(engine);
361         assertSame("Created new configuration", hc, ConfigurationUtils
362                 .convertToHierarchical(hc, null));
363         assertSame("Expression engine was changed", engine, hc
364                 .getExpressionEngine());
365     }
366 
367     /**
368      * Tests converting a configuration to a hierarchical one that contains a
369      * property with multiple values. This test is related to CONFIGURATION-346.
370      */
371     @Test
372     public void testConvertToHierarchicalMultiValues()
373     {
374         BaseConfiguration config = new BaseConfiguration();
375         config.addProperty("test", "1,2,3");
376         HierarchicalConfiguration hc = ConfigurationUtils
377                 .convertToHierarchical(config);
378         assertEquals("Wrong value 1", 1, hc.getInt("test(0)"));
379         assertEquals("Wrong value 2", 2, hc.getInt("test(1)"));
380         assertEquals("Wrong value 3", 3, hc.getInt("test(2)"));
381     }
382 
383     /**
384      * Tests cloning a configuration that supports this operation.
385      */
386     @Test
387     public void testCloneConfiguration()
388     {
389         HierarchicalConfiguration conf = new HierarchicalConfiguration();
390         conf.addProperty("test", "yes");
391         HierarchicalConfiguration copy = (HierarchicalConfiguration) ConfigurationUtils
392                 .cloneConfiguration(conf);
393         assertNotSame("Same object was returned", conf, copy);
394         assertEquals("Property was not cloned", "yes", copy.getString("test"));
395     }
396 
397     /**
398      * Tests cloning a configuration that does not support this operation. This
399      * should cause an exception.
400      */
401     @Test(expected = ConfigurationRuntimeException.class)
402     public void testCloneConfigurationNotSupported()
403     {
404         Configuration myNonCloneableConfig = new NonCloneableConfiguration();
405         ConfigurationUtils.cloneConfiguration(myNonCloneableConfig);
406     }
407 
408     /**
409      * Tests cloning a <b>null</b> configuration.
410      */
411     @Test
412     public void testCloneConfigurationNull()
413     {
414         assertNull("Wrong return value", ConfigurationUtils
415                 .cloneConfiguration(null));
416     }
417 
418     /**
419      * Tests whether runtime exceptions can be enabled.
420      */
421     @Test(expected = ConfigurationRuntimeException.class)
422     public void testEnableRuntimeExceptions()
423     {
424         PropertiesConfiguration config = new PropertiesConfiguration()
425         {
426             @Override
427             protected void addPropertyDirect(String key, Object value)
428             {
429                 // always simulate an exception
430                 fireError(EVENT_ADD_PROPERTY, key, value, new RuntimeException(
431                         "A faked exception!"));
432             }
433         };
434         config.clearErrorListeners();
435         ConfigurationUtils.enableRuntimeExceptions(config);
436         config.addProperty("test", "testValue");
437     }
438 
439     /**
440      * Tries to enable runtime exceptions for a configuration that does not
441      * inherit from EventSource. This should cause an exception.
442      */
443     @Test(expected = IllegalArgumentException.class)
444     public void testEnableRuntimeExceptionsInvalid()
445     {
446         ConfigurationUtils.enableRuntimeExceptions((Configuration) new Mock(
447                 Configuration.class).proxy());
448     }
449 
450     /**
451      * Tries to enable runtime exceptions for a null configuration. This should
452      * cause an exception.
453      */
454     @Test(expected = IllegalArgumentException.class)
455     public void testEnableRuntimeExceptionsNull()
456     {
457         ConfigurationUtils.enableRuntimeExceptions(null);
458     }
459 }