    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;
    18
    19  import java.math.BigDecimal;
    20  import java.util.List;
    21  import java.util.Iterator;
    22
    23  import junit.framework.TestCase;
    24
    25  /***
    26   * Test class for PropertyConverter.
    27   *
    28   * @author Emmanuel Bourg
    29   * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
    30   */
    31  public class TestPropertyConverter extends TestCase
    32  {
    33      public void testSplit()
    34      {
    35          String s = "abc, xyz , 123";
    36          List list = PropertyConverter.split(s, ',');
    37
    38          assertEquals("size", 3, list.size());
    39          assertEquals("1st token for '" + s + "'", "abc", list.get(0));
    40          assertEquals("2nd token for '" + s + "'", "xyz", list.get(1));
    41          assertEquals("3rd token for '" + s + "'", "123", list.get(2));
    42      }
    43
    44      public void testSplitWithEscapedSeparator()
    45      {
    46          String s = "abc//,xyz, 123";
    47          List list = PropertyConverter.split(s, ',');
    48
    49          assertEquals("size", 2, list.size());
    50          assertEquals("1st token for '" + s + "'", "abc,xyz", list.get(0));
    51          assertEquals("2nd token for '" + s + "'", "123", list.get(1));
    52      }
    53
    54      public void testSplitEmptyValues()
    55      {
    56          String s = ",,";
    57          List list = PropertyConverter.split(s, ',');
    58
    59          assertEquals("size", 3, list.size());
    60          assertEquals("1st token for '" + s + "'", "", list.get(0));
    61          assertEquals("2nd token for '" + s + "'", "", list.get(1));
    62          assertEquals("3rd token for '" + s + "'", "", list.get(2));
    63      }
    64
    65      public void testSplitWithEndingSlash()
    66      {
    67          String s = "abc, xyz//";
    68          List list = PropertyConverter.split(s, ',');
    69
    70          assertEquals("size", 2, list.size());
    71          assertEquals("1st token for '" + s + "'", "abc", list.get(0));
    72          assertEquals("2nd token for '" + s + "'", "xyz//", list.get(1));
    73      }
    74
    75      public void testSplitNull()
    76      {
    77          List list = PropertyConverter.split(null, ',');
    78          assertNotNull(list);
    79          assertTrue(list.isEmpty());
    80      }
    81
    82      public void testToIterator()
    83      {
    84          int[] array = new int[]{1, 2, 3};
    85
    86          Iterator it = PropertyConverter.toIterator(array, ',');
    87
    88          assertEquals("1st element", new Integer(1), it.next());
    89          assertEquals("2nd element", new Integer(2), it.next());
    90          assertEquals("3rd element", new Integer(3), it.next());
    91      }
    92
    93      /***
    94       * Tests the interpolation features.
    95       */
    96      public void testInterpolateString()
    97      {
    98          PropertiesConfiguration config = new PropertiesConfiguration();
    99          config.addProperty("animal", "quick brown fox");
    100         config.addProperty("target", "lazy dog");
    101         assertEquals("Wrong interpolation",
    102                 "The quick brown fox jumps over the lazy dog.",
    103                 PropertyConverter.interpolate(
    104                         "The ${animal} jumps over the ${target}.", config));
    105     }
    106
    107     /***
    108      * Tests interpolation of an object. Here nothing should be substituted.
    109      */
    110     public void testInterpolateObject()
    111     {
    112         assertEquals("Object was not correctly interpolated", new Integer(42),
    113                 PropertyConverter.interpolate(new Integer(42),
    114                         new PropertiesConfiguration()));
    115     }
    116
    117     /***
    118      * Tests complex interpolation where the variables' values contain in turn
    119      * other variables.
    120      */
    121     public void testInterpolateRecursive()
    122     {
    123         PropertiesConfiguration config = new PropertiesConfiguration();
    124         config.addProperty("animal", "${animal_attr} fox");
    125         config.addProperty("target", "${target_attr} dog");
    126         config.addProperty("animal_attr", "quick brown");
    127         config.addProperty("target_attr", "lazy");
    128         assertEquals("Wrong complex interpolation",
    129                 "The quick brown fox jumps over the lazy dog.",
    130                 PropertyConverter.interpolate(
    131                         "The ${animal} jumps over the ${target}.", config));
    132     }
    133
    134     /***
    135      * Tests an interpolation that leads to a cycle. This should throw an
    136      * exception.
    137      */
    138     public void testCyclicInterpolation()
    139     {
    140         PropertiesConfiguration config = new PropertiesConfiguration();
    141         config.addProperty("animal", "${animal_attr} ${species}");
    142         config.addProperty("animal_attr", "quick brown");
    143         config.addProperty("species", "${animal}");
    144         try
    145         {
    146             PropertyConverter.interpolate("This is a ${animal}", config);
    147             fail("Cyclic interpolation was not detected!");
    148         }
    149         catch (IllegalStateException iex)
    150         {
    151             // ok
    152         }
    153     }
    154
    155     /***
    156      * Tests interpolation if a variable is unknown. Then the variable won't be
    157      * substituted.
    158      */
    159     public void testInterpolationUnknownVariable()
    160     {
    161         PropertiesConfiguration config = new PropertiesConfiguration();
    162         config.addProperty("animal", "quick brown fox");
    163         assertEquals("Wrong interpolation",
    164                 "The quick brown fox jumps over ${target}.", PropertyConverter
    165                         .interpolate("The ${animal} jumps over ${target}.",
    166                                 config));
    167     }
    168
    169     /***
    170      * Tests conversion to numbers when the passed in objects are already
    171      * numbers.
    172      */
    173     public void testToNumberDirect()
    174     {
    175         Integer i = new Integer(42);
    176         assertSame("Wrong integer", i, PropertyConverter.toNumber(i,
    177                 Integer.class));
    178         BigDecimal d = new BigDecimal("3.1415");
    179         assertSame("Wrong BigDecimal", d, PropertyConverter.toNumber(d,
    180                 Integer.class));
    181     }
    182
    183     /***
    184      * Tests conversion to numbers when the passed in objects have a compatible
    185      * string representation.
    186      */
    187     public void testToNumberFromString()
    188     {
    189         assertEquals("Incorrect Integer value", new Integer(42),
    190                 PropertyConverter.toNumber("42", Integer.class));
    191         assertEquals("Incorrect Short value", new Short((short) 10),
    192                 PropertyConverter.toNumber(new StringBuffer("10"), Short.class));
    193     }
    194
    195     /***
    196      * Tests conversion to numbers when the passed in objects are strings with
    197      * prefixes for special radices.
    198      */
    199     public void testToNumberFromHexString()
    200     {
    201         Number n = PropertyConverter.toNumber("0x10", Integer.class);
    202         assertEquals("Incorrect Integer value", 16, n.intValue());
    203     }
    204
    205     /***
    206      * Tests conversion to numbers when an invalid Hex value is passed in. This
    207      * should cause an exception.
    208      */
    209     public void testToNumberFromInvalidHexString()
    210     {
    211         try
    212         {
    213             PropertyConverter.toNumber("0xNotAHexValue", Integer.class);
    214             fail("Could convert invalid hex value!");
    215         }
    216         catch (ConversionException cex)
    217         {
    218             // ok
    219         }
    220     }
    221
    222     /***
    223      * Tests conversion to numbers when the passed in objects have no numeric
    224      * String representation. This should cause an exception.
    225      */
    226     public void testToNumberFromInvalidString()
    227     {
    228         try
    229         {
    230             PropertyConverter.toNumber("Not a number", Byte.class);
    231             fail("Could convert invalid String!");
    232         }
    233         catch (ConversionException cex)
    234         {
    235             // ok
    236         }
    237     }
    238
    239     /***
    240      * Tests conversion to numbers when the passed in target class is invalid.
    241      * This should cause an exception.
    242      */
    243     public void testToNumberWithInvalidClass()
    244     {
    245         try
    246         {
    247             PropertyConverter.toNumber("42", Object.class);
    248             fail("Could convert to invalid target class!");
    249         }
    250         catch (ConversionException cex)
    251         {
    252             //ok
    253         }
    254     }
    255 }