    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.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: 555737 $, $Date: 2007-07-12 21:59:25 +0200 (Do, 12 Jul 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         // create the source configuration
    137         Configuration conf1 = new BaseConfiguration();
    138         conf1.addProperty("key1", "value1");
    139         conf1.addProperty("key2", "value2");
    140
    141         // create the target configuration
    142         Configuration conf2 = new BaseConfiguration();
    143         conf2.addProperty("key1", "value3");
    144         conf2.addProperty("key2", "value4");
    145
    146         // copy the source configuration into the target configuration
    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         // create the source configuration
    156         Configuration conf1 = new BaseConfiguration();
    157         conf1.addProperty("key1", "value1");
    158         conf1.addProperty("key2", "value2");
    159
    160         // create the target configuration
    161         Configuration conf2 = new BaseConfiguration();
    162         conf2.addProperty("key1", "value3");
    163         conf2.addProperty("key2", "value4");
    164
    165         // append the source configuration to the target configuration
    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             // This assert fails when maven 2 is used, so commented out
    202             //assertNotNull(ConfigurationUtils.locate("test.xml"));
    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 converting a configuration into a hierarchical one if some of its
    256      * properties contain escaped list delimiter characters.
    257      */
    258     public void testConvertToHierarchicalDelimiters()
    259     {
    260         Configuration conf = new BaseConfiguration();
    261         conf.addProperty("test.key", "1//,2//,3");
    262         assertEquals("Wrong property value", "1,2,3", conf
    263                 .getString("test.key"));
    264         HierarchicalConfiguration hc = ConfigurationUtils
    265                 .convertToHierarchical(conf);
    266         assertEquals("Escaped list delimiters not correctly handled", "1,2,3",
    267                 hc.getString("test.key"));
    268     }
    269
    270     /***
    271      * Tests cloning a configuration that supports this operation.
    272      */
    273     public void testCloneConfiguration()
    274     {
    275         HierarchicalConfiguration conf = new HierarchicalConfiguration();
    276         conf.addProperty("test", "yes");
    277         HierarchicalConfiguration copy = (HierarchicalConfiguration) ConfigurationUtils
    278                 .cloneConfiguration(conf);
    279         assertNotSame("Same object was returned", conf, copy);
    280         assertEquals("Property was not cloned", "yes", copy.getString("test"));
    281     }
    282
    283     /***
    284      * Tests cloning a configuration that does not support this operation. This
    285      * should cause an exception.
    286      */
    287     public void testCloneConfigurationNotSupported()
    288     {
    289         Configuration myNonCloneableConfig = new NonCloneableConfiguration();
    290         try
    291         {
    292             ConfigurationUtils.cloneConfiguration(myNonCloneableConfig);
    293             fail("Could clone non cloneable config!");
    294         }
    295         catch (ConfigurationRuntimeException crex)
    296         {
    297             // ok
    298         }
    299     }
    300
    301     /***
    302      * Tests cloning a <b>null</b> configuration.
    303      */
    304     public void testCloneConfigurationNull()
    305     {
    306         assertNull("Wrong return value", ConfigurationUtils
    307                 .cloneConfiguration(null));
    308     }
    309
    310     /***
    311      * Tests whether runtime exceptions can be enabled.
    312      */
    313     public void testEnableRuntimeExceptions()
    314     {
    315         PropertiesConfiguration config = new PropertiesConfiguration()
    316         {
    317             protected void addPropertyDirect(String key, Object value)
    318             {
    319                 // always simulate an exception
    320                 fireError(EVENT_ADD_PROPERTY, key, value, new RuntimeException(
    321                         "A faked exception!"));
    322             }
    323         };
    324         config.clearErrorListeners();
    325         ConfigurationUtils.enableRuntimeExceptions(config);
    326         try
    327         {
    328             config.addProperty("test", "testValue");
    329             fail("No runtime exception was thrown!");
    330         }
    331         catch (ConfigurationRuntimeException crex)
    332         {
    333             // ok
    334         }
    335     }
    336
    337     /***
    338      * Tries to enable runtime exceptions for a configurtion that does not
    339      * inherit from EventSource. This should cause an exception.
    340      */
    341     public void testEnableRuntimeExceptionsInvalid()
    342     {
    343         try
    344         {
    345             ConfigurationUtils
    346                     .enableRuntimeExceptions((Configuration) new Mock(
    347                             Configuration.class).proxy());
    348             fail("Could enable exceptions for non EventSource configuration!");
    349         }
    350         catch (IllegalArgumentException iex)
    351         {
    352             // ok
    353         }
    354     }
    355
    356     /***
    357      * Tries to enable runtime exceptions for a null configuration. This should
    358      * cause an exception.
    359      */
    360     public void testEnableRuntimeExceptionsNull()
    361     {
    362         try
    363         {
    364             ConfigurationUtils.enableRuntimeExceptions(null);
    365             fail("Could enable exceptions for a null configuration!");
    366         }
    367         catch (IllegalArgumentException iex)
    368         {
    369             //ok
    370         }
    371     }
    372 }