    1   package org.apache.commons.configuration;
    2
    3   /*
    4    * Copyright 2001-2004 The Apache Software Foundation.
    5    *
    6    * Licensed under the Apache License, Version 2.0 (the "License")
    7    * you may not use this file except in compliance with the License.
    8    * You may obtain a copy of the License at
    9    *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18
    19  import java.io.File;
    20  import java.util.ArrayList;
    21  import java.util.Iterator;
    22  import java.util.List;
    23  import java.util.Vector;
    24
    25  import junit.framework.TestCase;
    26
    27  /***
    28   * Test for loading and saving properties files.
    29   *
    30   * @version $Id: TestPropertiesConfiguration.java,v 1.14 2004/09/23 11:47:57 ebourg Exp $
    31   */
    32  public class TestPropertiesConfiguration extends TestCase
    33  {
    34      private PropertiesConfiguration conf;
    35
    36      /*** The File that we test with */
    37      private String testProperties = new File("conf/test.properties").getAbsolutePath();
    38
    39      private String testBasePath = new File("conf").getAbsolutePath();
    40      private String testBasePath2 = new File("conf").getAbsoluteFile().getParentFile().getAbsolutePath();
    41      private File testSavePropertiesFile = new File("target/testsave.properties");
    42
    43      protected void setUp() throws Exception
    44      {
    45          conf = new PropertiesConfiguration(testProperties);
    46      }
    47
    48      public void testLoad() throws Exception
    49      {
    50          String loaded = conf.getString("configuration.loaded");
    51          assertEquals("true", loaded);
    52      }
    53
    54      /***
    55       * Tests that empty properties are treated as the empty string
    56       * (rather than as null).
    57       */
    58      public void testEmpty() throws Exception
    59      {
    60          String empty = conf.getString("test.empty");
    61          assertNotNull(empty);
    62          assertEquals("", empty);
    63      }
    64
    65      /***
    66       * Tests that references to other properties work
    67       */
    68      public void testReference() throws Exception
    69      {
    70          assertEquals("baseextra", conf.getString("base.reference"));
    71      }
    72
    73      /***
    74       * test if includes properties get loaded too
    75       */
    76      public void testLoadInclude() throws Exception
    77      {
    78          String loaded = conf.getString("include.loaded");
    79          assertEquals("true", loaded);
    80      }
    81
    82      public void testSetInclude() throws Exception
    83      {
    84          // change the include key
    85          PropertiesConfiguration.setInclude("import");
    86
    87          // load the configuration
    88          PropertiesConfiguration conf = new PropertiesConfiguration();
    89          conf.load("conf/test.properties");
    90
    91          // restore the previous value for the other tests
    92          PropertiesConfiguration.setInclude("include");
    93
    94          assertNull(conf.getString("include.loaded"));
    95      }
    96
    97      /***
    98       * Tests <code>List</code> parsing.
    99       */
    100     public void testList() throws Exception
    101     {
    102         List packages = conf.getList("packages");
    103         // we should get 3 packages here
    104         assertEquals(3, packages.size());
    105     }
    106
    107     /***
    108      * Tests <code>Vector</code> parsing.
    109      */
    110     public void testVector() throws Exception
    111     {
    112         Vector packages = conf.getVector("packages");
    113         // we should get 3 packages here
    114         assertEquals(3, packages.size());
    115     }
    116
    117     public void testSave() throws Exception
    118     {
    119         // remove the file previously saved if necessary
    120         if (testSavePropertiesFile.exists())
    121         {
    122             assertTrue(testSavePropertiesFile.delete());
    123         }
    124
    125         // add an array of strings to the configuration
    126         conf.addProperty("string", "value1");
    127         List list = new ArrayList();
    128         for (int i = 1; i < 5; i++)
    129         {
    130             list.add("value" + i);
    131         }
    132         conf.addProperty("array", list);
    133
    134         // save the configuration
    135         String filename = testSavePropertiesFile.getAbsolutePath();
    136         conf.save(filename);
    137
    138         assertTrue("The saved file doesn't exist", testSavePropertiesFile.exists());
    139
    140         // read the configuration and compare the properties
    141         PropertiesConfiguration checkConfig = new PropertiesConfiguration(filename);
    142         for (Iterator i = conf.getKeys(); i.hasNext();)
    143         {
    144             String key = (String) i.next();
    145             assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
    146             assertEquals("Value of the '" + key + "' property", conf.getProperty(key), checkConfig.getProperty(key));
    147         }
    148
    149         // Save it again, verifing a save with a filename works.
    150         checkConfig.save();
    151     }
    152
    153     public void testSaveMissingFilename()
    154     {
    155         PropertiesConfiguration pc = new PropertiesConfiguration();
    156         try
    157         {
    158             pc.save();
    159             fail("Should have throw ConfigurationException");
    160         }
    161         catch (ConfigurationException ce)
    162         {
    163             //good
    164         }
    165     }
    166
    167     public void testLoadViaProperty() throws Exception
    168     {
    169         PropertiesConfiguration pc = new PropertiesConfiguration();
    170         pc.setFileName(testProperties);
    171         pc.load();
    172
    173         assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
    174     }
    175
    176     public void testLoadViaPropertyWithBasePath() throws Exception
    177     {
    178         PropertiesConfiguration pc = new PropertiesConfiguration();
    179         pc.setBasePath(testBasePath);
    180         pc.setFileName("test.properties");
    181         pc.load();
    182
    183         assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
    184     }
    185
    186     public void testLoadViaPropertyWithBasePath2() throws Exception
    187     {
    188         PropertiesConfiguration pc = new PropertiesConfiguration();
    189         pc.setBasePath(testBasePath2);
    190         pc.setFileName("conf/test.properties");
    191         pc.load();
    192
    193         assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
    194
    195         pc = new PropertiesConfiguration();
    196         pc.setBasePath(testBasePath2);
    197         pc.setFileName("conf/test.properties");
    198         pc.load();
    199
    200         assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
    201     }
    202
    203     public void testLoadFromJAR() throws Exception
    204     {
    205         conf = new PropertiesConfiguration();
    206         conf.setIncludesAllowed(true);
    207         conf.setFileName("test-jar.properties");
    208         conf.load();
    209
    210         assertEquals("jar", conf.getProperty("configuration.location"));
    211         assertEquals("property in an included file", "jar", conf.getProperty("include.location"));
    212     }
    213
    214     public void testLoadFromFile() throws Exception
    215     {
    216         File file = new File("conf/test.properties");
    217         conf = new PropertiesConfiguration(file);
    218
    219         assertEquals("true", conf.getString("configuration.loaded"));
    220     }
    221
    222     public void testGetStringWithEscapedChars()
    223     {
    224         String property = conf.getString("test.unescape");
    225         assertEquals("String with escaped characters", "This \n string \t contains \" escaped // characters", property);
    226     }
    227
    228     public void testGetStringWithEscapedComma()
    229     {
    230         String property = conf.getString("test.unescape.list-separator");
    231         assertEquals("String with an escaped list separator", "This string contains , an escaped list separator", property);
    232     }
    233
    234     public void testUnescapeJava()
    235     {
    236         assertEquals("test//,test", PropertiesConfiguration.unescapeJava("test//,test"));
    237     }
    238
    239     public void testMixedArray()
    240     {
    241         String[] array = conf.getStringArray("test.mixed.array");
    242
    243         assertEquals("array length", 4, array.length);
    244         assertEquals("1st element", "a", array[0]);
    245         assertEquals("2nd element", "b", array[1]);
    246         assertEquals("3rd element", "c", array[2]);
    247         assertEquals("4th element", "d", array[3]);
    248     }
    249
    250     public void testMultilines()
    251     {
    252         String property = "This is a value spread out across several adjacent "
    253                 + "natural lines by escaping the line terminator with "
    254                 + "a backslash character.";
    255
    256         assertEquals("'test.multilines' property", property, conf.getString("test.multilines"));
    257     }
    258
    259     public void testChangingDelimiter() throws Exception
    260     {
    261         PropertiesConfiguration pc = new PropertiesConfiguration(testProperties);
    262         assertEquals(4, pc.getList("test.mixed.array").size());
    263
    264         char delimiter = PropertiesConfiguration.getDelimiter();
    265         PropertiesConfiguration.setDelimiter('^');
    266         pc = new PropertiesConfiguration(testProperties);
    267         assertEquals(2, pc.getList("test.mixed.array").size());
    268         PropertiesConfiguration.setDelimiter(delimiter);
    269     }
    270
    271 }