    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 junit.framework.TestCase;
    27  import junitx.framework.ListAssert;
    28
    29  /***
    30   * Tests the ConfigurationUtils class
    31   *
    32   * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
    33   */
    34  public class TestConfigurationUtils extends TestCase
    35  {
    36      protected Configuration config = new BaseConfiguration();
    37
    38      public void testToString()
    39      {
    40          String lineSeparator = System.getProperty("line.separator");
    41
    42          assertEquals("String representation of an empty configuration", "", ConfigurationUtils.toString(config));
    43
    44          config.setProperty("one", "1");
    45          assertEquals("String representation of a configuration", "one=1", ConfigurationUtils.toString(config));
    46
    47          config.setProperty("two", "2");
    48          assertEquals("String representation of a configuration", "one=1" + lineSeparator + "two=2" , ConfigurationUtils.toString(config));
    49
    50          config.clearProperty("one");
    51          assertEquals("String representation of a configuration", "two=2" , ConfigurationUtils.toString(config));
    52
    53          config.setProperty("one","1");
    54          assertEquals("String representation of a configuration", "two=2" + lineSeparator + "one=1" , ConfigurationUtils.toString(config));
    55      }
    56
    57      public void testGetURL() throws Exception
    58      {
    59          assertEquals(
    60              "http://localhost:8080/webapp/config/config.xml",
    61              ConfigurationUtils
    62                  .getURL(
    63                      "http://localhost:8080/webapp/config/baseConfig.xml",
    64                      "config.xml")
    65                  .toString());
    66          assertEquals(
    67              "http://localhost:8080/webapp/config/config.xml",
    68              ConfigurationUtils
    69                  .getURL(
    70                      "http://localhost:8080/webapp/baseConfig.xml",
    71                      "config/config.xml")
    72                  .toString());
    73          URL url = ConfigurationUtils.getURL(null, "config.xml");
    74          assertEquals("file", url.getProtocol());
    75          assertEquals("", url.getHost());
    76
    77          assertEquals(
    78              "http://localhost:8080/webapp/config/config.xml",
    79              ConfigurationUtils
    80                  .getURL(
    81                      "ftp://ftp.server.com/downloads/baseConfig.xml",
    82                      "http://localhost:8080/webapp/config/config.xml")
    83                  .toString());
    84          assertEquals(
    85              "http://localhost:8080/webapp/config/config.xml",
    86              ConfigurationUtils
    87                  .getURL(null, "http://localhost:8080/webapp/config/config.xml")
    88                  .toString());
    89          File absFile = new File("config.xml").getAbsoluteFile();
    90          assertEquals(
    91              absFile.toURL(),
    92              ConfigurationUtils.getURL(
    93                  "http://localhost:8080/webapp/config/baseConfig.xml",
    94                  absFile.getAbsolutePath()));
    95          assertEquals(
    96              absFile.toURL(),
    97              ConfigurationUtils.getURL(null, absFile.getAbsolutePath()));
    98
    99  		assertEquals(absFile.toURL(),
    100 		ConfigurationUtils.getURL(absFile.getParent(), "config.xml"));
    101     }
    102
    103     public void testGetBasePath() throws Exception
    104     {
    105         URL url = new URL("http://xyz.net/foo/bar.xml");
    106         assertEquals("base path of " + url, "http://xyz.net/foo/", ConfigurationUtils.getBasePath(url));
    107
    108         url = new URL("http://xyz.net/foo/");
    109         assertEquals("base path of " + url, "http://xyz.net/foo/", ConfigurationUtils.getBasePath(url));
    110
    111         url = new URL("http://xyz.net/foo");
    112         assertEquals("base path of " + url, "http://xyz.net/", ConfigurationUtils.getBasePath(url));
    113
    114         url = new URL("http://xyz.net/");
    115         assertEquals("base path of " + url, "http://xyz.net/", ConfigurationUtils.getBasePath(url));
    116
    117         url = new URL("http://xyz.net");
    118         assertEquals("base path of " + url, "http://xyz.net", ConfigurationUtils.getBasePath(url));
    119     }
    120
    121     public void testGetFileName() throws Exception
    122     {
    123         assertEquals("file name for a null URL", null, ConfigurationUtils.getFileName(null));
    124
    125         URL url = new URL("http://xyz.net/foo/");
    126         assertEquals("file for a directory URL " + url, null, ConfigurationUtils.getFileName(url));
    127
    128         url = new URL("http://xyz.net/foo/bar.xml");
    129         assertEquals("file name for a valid URL " + url, "bar.xml", ConfigurationUtils.getFileName(url));
    130     }
    131
    132     public void testCopy()
    133     {
    134         // create the source configuration
    135         Configuration conf1 = new BaseConfiguration();
    136         conf1.addProperty("key1", "value1");
    137         conf1.addProperty("key2", "value2");
    138
    139         // create the target configuration
    140         Configuration conf2 = new BaseConfiguration();
    141         conf2.addProperty("key1", "value3");
    142         conf2.addProperty("key2", "value4");
    143
    144         // copy the source configuration into the target configuration
    145         ConfigurationUtils.copy(conf1, conf2);
    146
    147         assertEquals("'key1' property", "value1", conf2.getProperty("key1"));
    148         assertEquals("'key2' property", "value2", conf2.getProperty("key2"));
    149     }
    150
    151     public void testAppend()
    152     {
    153         // create the source configuration
    154         Configuration conf1 = new BaseConfiguration();
    155         conf1.addProperty("key1", "value1");
    156         conf1.addProperty("key2", "value2");
    157
    158         // create the target configuration
    159         Configuration conf2 = new BaseConfiguration();
    160         conf2.addProperty("key1", "value3");
    161         conf2.addProperty("key2", "value4");
    162
    163         // append the source configuration to the target configuration
    164         ConfigurationUtils.append(conf1, conf2);
    165
    166         List expected = new ArrayList();
    167         expected.add("value3");
    168         expected.add("value1");
    169         ListAssert.assertEquals("'key1' property", expected, conf2.getList("key1"));
    170
    171         expected = new ArrayList();
    172         expected.add("value4");
    173         expected.add("value2");
    174         ListAssert.assertEquals("'key2' property", expected, conf2.getList("key2"));
    175     }
    176
    177     public void testGetFile() throws Exception
    178     {
    179         File directory = new File("target");
    180         File reference = new File(directory, "test.txt").getAbsoluteFile();
    181
    182         assertEquals(reference, ConfigurationUtils.getFile(null, reference.getAbsolutePath()));
    183         assertEquals(reference, ConfigurationUtils.getFile(directory.getAbsolutePath(), reference.getAbsolutePath()));
    184         assertEquals(reference, ConfigurationUtils.getFile(directory.getAbsolutePath(), reference.getName()));
    185         assertEquals(reference, ConfigurationUtils.getFile(directory.toURL().toString(), reference.getName()));
    186         assertEquals(reference, ConfigurationUtils.getFile("invalid", reference.toURL().toString()));
    187     }
    188
    189     public void testLocateWithNullTCCL() throws Exception
    190     {
    191         ClassLoader cl = Thread.currentThread().getContextClassLoader();
    192         try
    193         {
    194             Thread.currentThread().setContextClassLoader(null);
    195             assertNull(ConfigurationUtils.locate("abase", "aname"));
    196             assertNotNull(ConfigurationUtils.locate("test.xml"));
    197         }
    198         finally
    199         {
    200             Thread.currentThread().setContextClassLoader(cl);
    201         }
    202     }
    203
    204     /***
    205      * Tests converting a configuration into a hierarchical one.
    206      */
    207     public void testConvertToHierarchical()
    208     {
    209         Configuration conf = new BaseConfiguration();
    210         for (int i = 0; i < 10; i++)
    211         {
    212             conf.addProperty("test" + i, "value" + i);
    213             conf.addProperty("test.list", "item" + i);
    214         }
    215
    216         HierarchicalConfiguration hc = ConfigurationUtils
    217                 .convertToHierarchical(conf);
    218         for (Iterator it = conf.getKeys(); it.hasNext();)
    219         {
    220             String key = (String) it.next();
    221             assertEquals("Wrong value for key " + key, conf.getProperty(key),
    222                     hc.getProperty(key));
    223         }
    224     }
    225
    226     /***
    227      * Tests converting a configuration into a hierarchical one that is already
    228      * hierarchical.
    229      */
    230     public void testConvertHierarchicalToHierarchical()
    231     {
    232         Configuration conf = new HierarchicalConfiguration();
    233         conf.addProperty("test", "yes");
    234         assertSame("Wrong configuration returned", conf, ConfigurationUtils
    235                 .convertToHierarchical(conf));
    236     }
    237
    238     /***
    239      * Tests converting a null configuration to a hierarchical one. The result
    240      * should be null, too.
    241      */
    242     public void testConvertNullToHierarchical()
    243     {
    244         assertNull("Wrong conversion result for null config",
    245                 ConfigurationUtils.convertToHierarchical(null));
    246     }
    247
    248     /***
    249      * Tests cloning a configuration that supports this operation.
    250      */
    251     public void testCloneConfiguration()
    252     {
    253         HierarchicalConfiguration conf = new HierarchicalConfiguration();
    254         conf.addProperty("test", "yes");
    255         HierarchicalConfiguration copy = (HierarchicalConfiguration) ConfigurationUtils
    256                 .cloneConfiguration(conf);
    257         assertNotSame("Same object was returned", conf, copy);
    258         assertEquals("Property was not cloned", "yes", copy.getString("test"));
    259     }
    260
    261     /***
    262      * Tests cloning a configuration that does not support this operation. This
    263      * should cause an exception.
    264      */
    265     public void testCloneConfigurationNotSupported()
    266     {
    267         Configuration myNonCloneableConfig = new NonCloneableConfiguration();
    268         try
    269         {
    270             ConfigurationUtils.cloneConfiguration(myNonCloneableConfig);
    271             fail("Could clone non cloneable config!");
    272         }
    273         catch (ConfigurationRuntimeException crex)
    274         {
    275             // ok
    276         }
    277     }
    278
    279     /***
    280      * Tests cloning a <b>null</b> configuration.
    281      */
    282     public void testCloneConfigurationNull()
    283     {
    284         assertNull("Wrong return value", ConfigurationUtils
    285                 .cloneConfiguration(null));
    286     }
    287 }