    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  package org.apache.commons.configuration.interpol;
    18
    19  import java.util.HashMap;
    20  import java.util.Iterator;
    21  import java.util.Map;
    22  import java.util.Properties;
    23
    24  import org.apache.commons.lang.text.StrLookup;
    25
    26  import junit.framework.TestCase;
    27
    28  /***
    29   * Test class for ConfigurationInterpolator.
    30   *
    31   * @version $Id: TestConfigurationInterpolator.java 531087 2007-04-21 19:03:58Z oheger $
    32   */
    33  public class TestConfigurationInterpolator extends TestCase
    34  {
    35      /*** Constant for a test variable prefix. */
    36      private static final String TEST_PREFIX = "prefix";
    37
    38      /*** Constant for a test variable name. */
    39      private static final String TEST_NAME = "varname";
    40
    41      /*** Constant for the value of the test variable. */
    42      private static final String TEST_VALUE = "TestVariableValue";
    43
    44      /*** Stores the object to be tested. */
    45      private ConfigurationInterpolator interpolator;
    46
    47      protected void setUp() throws Exception
    48      {
    49          super.setUp();
    50          interpolator = new ConfigurationInterpolator();
    51      }
    52
    53      /***
    54       * Cleans the test environment. Deregisters the test lookup object if
    55       * necessary.
    56       */
    57      protected void tearDown() throws Exception
    58      {
    59          ConfigurationInterpolator.deregisterGlobalLookup(TEST_PREFIX);
    60      }
    61
    62      /***
    63       * Tests creating an instance. Does it contain some predefined lookups?
    64       */
    65      public void testInit()
    66      {
    67          assertNull("A default lookup is set", interpolator.getDefaultLookup());
    68          assertFalse("No predefined lookups", interpolator.prefixSet().isEmpty());
    69      }
    70
    71      /***
    72       * Tries to register a global lookup for a null prefix. This should cause an
    73       * exception.
    74       */
    75      public void testRegisterGlobalLookupNullPrefix()
    76      {
    77          try
    78          {
    79              ConfigurationInterpolator.registerGlobalLookup(null, StrLookup
    80                      .noneLookup());
    81              fail("Could register global lookup with null prefix!");
    82          }
    83          catch (IllegalArgumentException iex)
    84          {
    85              // ok
    86          }
    87      }
    88
    89      /***
    90       * Tries to register a global null lookup. This should cause an exception.
    91       */
    92      public void testRegisterGlobalLookupNull()
    93      {
    94          try
    95          {
    96              ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX, null);
    97              fail("Could register global null lookup!");
    98          }
    99          catch (IllegalArgumentException iex)
    100         {
    101             // ok
    102         }
    103     }
    104
    105     /***
    106      * Tests registering a global lookup object. This lookup object should then
    107      * be available for instances created later on.
    108      */
    109     public void testRegisterGlobalLookup()
    110     {
    111         ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX, StrLookup
    112                 .noneLookup());
    113         ConfigurationInterpolator int2 = new ConfigurationInterpolator();
    114         assertTrue("No lookup registered for test prefix", int2.prefixSet()
    115                 .contains(TEST_PREFIX));
    116         assertFalse("Existing instance was modified", interpolator.prefixSet()
    117                 .contains(TEST_PREFIX));
    118     }
    119
    120     /***
    121      * Tests deregistering a global lookup object.
    122      */
    123     public void testDeregisterGlobalLookup()
    124     {
    125         ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX, StrLookup
    126                 .noneLookup());
    127         assertTrue("Lookup could not be deregistered",
    128                 ConfigurationInterpolator.deregisterGlobalLookup(TEST_PREFIX));
    129         ConfigurationInterpolator int2 = new ConfigurationInterpolator();
    130         assertFalse("Deregistered lookup still available", int2.prefixSet()
    131                 .contains(TEST_PREFIX));
    132     }
    133
    134     /***
    135      * Tests deregistering an unknown lookup.
    136      */
    137     public void testDeregisterGlobalLookupNonExisting()
    138     {
    139         assertFalse("Could deregister unknown global lookup",
    140                 ConfigurationInterpolator.deregisterGlobalLookup(TEST_PREFIX));
    141     }
    142
    143     /***
    144      * Tests registering a lookup object at an instance.
    145      */
    146     public void testRegisterLookup()
    147     {
    148         int cnt = interpolator.prefixSet().size();
    149         interpolator.registerLookup(TEST_PREFIX, StrLookup.noneLookup());
    150         assertTrue("New lookup not registered", interpolator.prefixSet()
    151                 .contains(TEST_PREFIX));
    152         assertEquals("Wrong number of registered lookups", cnt + 1,
    153                 interpolator.prefixSet().size());
    154         ConfigurationInterpolator int2 = new ConfigurationInterpolator();
    155         assertFalse("Local registration has global impact", int2.prefixSet()
    156                 .contains(TEST_PREFIX));
    157     }
    158
    159     /***
    160      * Tests registering a null lookup object. This should cause an exception.
    161      */
    162     public void testRegisterLookupNull()
    163     {
    164         try
    165         {
    166             interpolator.registerLookup(TEST_PREFIX, null);
    167             fail("Could register null lookup!");
    168         }
    169         catch (IllegalArgumentException iex)
    170         {
    171             // ok
    172         }
    173     }
    174
    175     /***
    176      * Tests registering a lookup object for an undefined prefix. This should
    177      * cause an exception.
    178      */
    179     public void testRegisterLookupNullPrefix()
    180     {
    181         try
    182         {
    183             interpolator.registerLookup(null, StrLookup.noneLookup());
    184             fail("Could register lookup for null prefix!");
    185         }
    186         catch (IllegalArgumentException iex)
    187         {
    188             // ok
    189         }
    190     }
    191
    192     /***
    193      * Tests deregistering a local lookup object.
    194      */
    195     public void testDeregisterLookup()
    196     {
    197         interpolator.registerLookup(TEST_PREFIX, StrLookup.noneLookup());
    198         assertTrue("Derigstration not successfull", interpolator
    199                 .deregisterLookup(TEST_PREFIX));
    200         assertFalse("Deregistered prefix still contained", interpolator
    201                 .prefixSet().contains(TEST_PREFIX));
    202     }
    203
    204     /***
    205      * Tests deregistering an unknown lookup object.
    206      */
    207     public void testDeregisterLookupNonExisting()
    208     {
    209         assertFalse("Could deregister unknown lookup", interpolator
    210                 .deregisterLookup(TEST_PREFIX));
    211     }
    212
    213     /***
    214      * Tests whether a variable can be resolved using the associated lookup
    215      * object. The lookup is identified by the variable's prefix.
    216      */
    217     public void testLookupWithPrefix()
    218     {
    219         interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
    220         assertEquals("Wrong variable value", TEST_VALUE, interpolator
    221                 .lookup(TEST_PREFIX + ':' + TEST_NAME));
    222     }
    223
    224     /***
    225      * Tests the behavior of the lookup method for variables with an unknown
    226      * prefix. These variables should not be resolved.
    227      */
    228     public void testLookupWithUnknownPrefix()
    229     {
    230         interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
    231         assertNull("Variable could be resolved", interpolator
    232                 .lookup("UnknownPrefix:" + TEST_NAME));
    233         assertNull("Variable with empty prefix could be resolved", interpolator
    234                 .lookup(":" + TEST_NAME));
    235     }
    236
    237     /***
    238      * Tests looking up a variable without a prefix. This should trigger the
    239      * default lookup object.
    240      */
    241     public void testLookupDefault()
    242     {
    243         interpolator.setDefaultLookup(setUpTestLookup());
    244         assertEquals("Wrong variable value", TEST_VALUE, interpolator
    245                 .lookup(TEST_NAME));
    246     }
    247
    248     /***
    249      * Tests looking up a variable without a prefix when no default lookup is
    250      * specified. Result should be null in this case.
    251      */
    252     public void testLookupNoDefault()
    253     {
    254         assertNull("Variable could be resolved", interpolator.lookup(TEST_NAME));
    255     }
    256
    257     /***
    258      * Tests the empty variable prefix. This is a special case, but legal.
    259      */
    260     public void testLookupEmptyPrefix()
    261     {
    262         interpolator.registerLookup("", setUpTestLookup());
    263         assertEquals("Wrong variable value", TEST_VALUE, interpolator
    264                 .lookup(":" + TEST_NAME));
    265     }
    266
    267     /***
    268      * Tests an empty variable name.
    269      */
    270     public void testLookupEmptyVarName()
    271     {
    272         Map map = new HashMap();
    273         map.put("", TEST_VALUE);
    274         interpolator.registerLookup(TEST_PREFIX, StrLookup.mapLookup(map));
    275         assertEquals("Wrong variable value", TEST_VALUE, interpolator
    276                 .lookup(TEST_PREFIX + ":"));
    277     }
    278
    279     /***
    280      * Tests an empty variable name without a prefix.
    281      */
    282     public void testLookupDefaultEmptyVarName()
    283     {
    284         Map map = new HashMap();
    285         map.put("", TEST_VALUE);
    286         interpolator.setDefaultLookup(StrLookup.mapLookup(map));
    287         assertEquals("Wrong variable value", TEST_VALUE, interpolator
    288                 .lookup(""));
    289     }
    290
    291     /***
    292      * Tests looking up a null variable. Result shoult be null, too.
    293      */
    294     public void testLookupNull()
    295     {
    296         assertNull("Could resolve null variable", interpolator.lookup(null));
    297     }
    298
    299     /***
    300      * Creates a lookup object that can resolve the test variable.
    301      *
    302      * @return the test lookup object
    303      */
    304     private StrLookup setUpTestLookup()
    305     {
    306         Map map = new HashMap();
    307         map.put(TEST_NAME, TEST_VALUE);
    308         return StrLookup.mapLookup(map);
    309     }
    310
    311     /***
    312      * Tests whether system properties can be correctly resolved.
    313      */
    314     public void testLookupSysProperties()
    315     {
    316         Properties sysProps = System.getProperties();
    317         for (Iterator it = sysProps.keySet().iterator(); it.hasNext();)
    318         {
    319             String key = (String) it.next();
    320             assertEquals("Wrong value for system property " + key, sysProps
    321                     .getProperty(key), interpolator
    322                     .lookup(ConfigurationInterpolator.PREFIX_SYSPROPERTIES
    323                             + ":" + key));
    324         }
    325     }
    326
    327     /***
    328      * Tests whether constants can be correctly resolved.
    329      */
    330     public void testLookupConstants()
    331     {
    332         String varName = ConfigurationInterpolator.class.getName()
    333                 + ".PREFIX_CONSTANTS";
    334         assertEquals("Wrong constant value",
    335                 ConfigurationInterpolator.PREFIX_CONSTANTS, interpolator
    336                         .lookup(ConfigurationInterpolator.PREFIX_CONSTANTS
    337                                 + ":" + varName));
    338     }
    339
    340     /***
    341      * Tests whether the default lookup is called for variables with a prefix
    342      * when the lookup that was registered for this prefix is not able to
    343      * resolve the variable.
    344      */
    345     public void testLookupDefaultAfterPrefixFails()
    346     {
    347         final String varName = TEST_PREFIX + ':' + TEST_NAME + "2";
    348         interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
    349         Map map = new HashMap();
    350         map.put(varName, TEST_VALUE);
    351         interpolator.setDefaultLookup(StrLookup.mapLookup(map));
    352         assertEquals("Variable is not resolved by default lookup", TEST_VALUE,
    353                 interpolator.lookup(varName));
    354     }
    355 }