    1   /*
    2    * Copyright 2001-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License")
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.apache.commons.configuration;
    18
    19  import java.util.ArrayList;
    20  import java.util.Iterator;
    21  import java.util.List;
    22  import java.util.NoSuchElementException;
    23  import java.util.Vector;
    24
    25  import junit.framework.TestCase;
    26
    27  /***
    28   * Test case for the {@link SubsetConfiguration} class.
    29   *
    30   * @author Emmanuel Bourg
    31   * @version $Revision: 1.6 $, $Date: 2004/10/05 21:17:25 $
    32   */
    33  public class TestSubsetConfiguration extends TestCase
    34  {
    35
    36      public void testGetProperty()
    37      {
    38          Configuration conf = new BaseConfiguration();
    39          conf.setProperty("test.key1", "value1");
    40          conf.setProperty("testing.key2", "value1");
    41
    42          Configuration subset = new SubsetConfiguration(conf, "test", ".");
    43          assertFalse("the subset is empty", subset.isEmpty());
    44          assertTrue("'key1' not found in the subset", subset.containsKey("key1"));
    45          assertFalse("'ng.key2' found in the subset", subset.containsKey("ng.key2"));
    46      }
    47
    48      public void testSetProperty()
    49      {
    50          Configuration conf = new BaseConfiguration();
    51          Configuration subset = new SubsetConfiguration(conf, "test", ".");
    52
    53          // set a property in the subset and check the parent
    54          subset.setProperty("key1", "value1");
    55          assertEquals("key1 in the subset configuration", "value1", subset.getProperty("key1"));
    56          assertEquals("test.key1 in the parent configuration", "value1", conf.getProperty("test.key1"));
    57
    58          // set a property in the parent and check in the subset
    59          conf.setProperty("test.key2", "value2");
    60          assertEquals("test.key2 in the parent configuration", "value2", conf.getProperty("test.key2"));
    61          assertEquals("key2 in the subset configuration", "value2", subset.getProperty("key2"));
    62      }
    63
    64      public void testGetParentKey()
    65      {
    66          // subset with delimiter
    67          SubsetConfiguration subset = new SubsetConfiguration(null, "prefix", ".");
    68          assertEquals("parent key for \"key\"", "prefix.key", subset.getParentKey("key"));
    69          assertEquals("parent key for \"\"", "prefix", subset.getParentKey(""));
    70
    71          // subset without delimiter
    72          subset = new SubsetConfiguration(null, "prefix", null);
    73          assertEquals("parent key for \"key\"", "prefixkey", subset.getParentKey("key"));
    74          assertEquals("parent key for \"\"", "prefix", subset.getParentKey(""));
    75      }
    76
    77      public void testGetChildKey()
    78      {
    79          // subset with delimiter
    80          SubsetConfiguration subset = new SubsetConfiguration(null, "prefix", ".");
    81          assertEquals("parent key for \"prefixkey\"", "key", subset.getChildKey("prefix.key"));
    82          assertEquals("parent key for \"prefix\"", "", subset.getChildKey("prefix"));
    83
    84          // subset without delimiter
    85          subset = new SubsetConfiguration(null, "prefix", null);
    86          assertEquals("parent key for \"prefixkey\"", "key", subset.getChildKey("prefixkey"));
    87          assertEquals("parent key for \"prefix\"", "", subset.getChildKey("prefix"));
    88      }
    89
    90      public void testGetKeys()
    91      {
    92          Configuration conf = new BaseConfiguration();
    93          conf.setProperty("test", "value0");
    94          conf.setProperty("test.key1", "value1");
    95          conf.setProperty("testing.key2", "value1");
    96
    97          Configuration subset = new SubsetConfiguration(conf, "test", ".");
    98
    99          Iterator it = subset.getKeys();
    100         assertEquals("1st key", "", it.next());
    101         assertEquals("2nd key", "key1", it.next());
    102         assertFalse("too many elements", it.hasNext());
    103     }
    104
    105     public void testGetKeysWithPrefix()
    106     {
    107         Configuration conf = new BaseConfiguration();
    108         conf.setProperty("test.abc", "value0");
    109         conf.setProperty("test.abc.key1", "value1");
    110         conf.setProperty("test.abcdef.key2", "value1");
    111
    112         Configuration subset = new SubsetConfiguration(conf, "test", ".");
    113
    114         Iterator it = subset.getKeys("abc");
    115         assertEquals("1st key", "abc", it.next());
    116         assertEquals("2nd key", "abc.key1", it.next());
    117         assertFalse("too many elements", it.hasNext());
    118     }
    119
    120     public void testGetList()
    121     {
    122         Configuration conf = new BaseConfiguration();
    123         conf.setProperty("test.abc", "value0,value1");
    124         conf.addProperty("test.abc", "value3");
    125
    126         Configuration subset = new SubsetConfiguration(conf, "test", ".");
    127         List list = subset.getList("abc", new ArrayList());
    128         assertEquals(3, list.size());
    129     }
    130
    131     public void testGetVector()
    132     {
    133         Configuration conf = new BaseConfiguration();
    134         conf.setProperty("test.abc", "value0,value1");
    135         conf.addProperty("test.abc", "value3");
    136
    137         Configuration subset = new SubsetConfiguration(conf, "test", ".");
    138         Vector vector = subset.getVector("abc", new Vector());
    139         assertEquals(3, vector.size());
    140     }
    141
    142     public void testGetParent()
    143     {
    144         Configuration conf = new BaseConfiguration();
    145         SubsetConfiguration subset = new SubsetConfiguration(conf, "prefix", ".");
    146
    147         assertEquals("parent", conf, subset.getParent());
    148     }
    149
    150     public void testGetPrefix()
    151     {
    152         Configuration conf = new BaseConfiguration();
    153         SubsetConfiguration subset = new SubsetConfiguration(conf, "prefix", ".");
    154
    155         assertEquals("prefix", "prefix", subset.getPrefix());
    156     }
    157
    158     public void testSetPrefix()
    159     {
    160         Configuration conf = new BaseConfiguration();
    161         SubsetConfiguration subset = new SubsetConfiguration(conf, null, ".");
    162         subset.setPrefix("prefix");
    163
    164         assertEquals("prefix", "prefix", subset.getPrefix());
    165     }
    166
    167     public void testThrowtExceptionOnMissing()
    168     {
    169         BaseConfiguration config = new BaseConfiguration();
    170         config.setThrowExceptionOnMissing(true);
    171
    172         SubsetConfiguration subset = new SubsetConfiguration(config, "prefix");
    173
    174         try
    175         {
    176             subset.getString("foo");
    177             fail("NoSuchElementException expected");
    178         }
    179         catch (NoSuchElementException e)
    180         {
    181             // expected
    182         }
    183
    184         config.setThrowExceptionOnMissing(false);
    185         assertNull(subset.getString("foo"));
    186
    187
    188         subset.setThrowExceptionOnMissing(true);
    189         try
    190         {
    191             config.getString("foo");
    192             fail("NoSuchElementException expected");
    193         }
    194         catch (NoSuchElementException e)
    195         {
    196             // expected
    197         }
    198     }
    199
    200
    201 }