    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
    21  import java.util.Iterator;
    22  import java.util.List;
    23  import java.util.Vector;
    24
    25  import junit.framework.TestCase;
    26
    27  /***
    28   * Compare the behaviour of various methods between CompositeConfiguration
    29   * and normal (Properties) Configuration
    30   *
    31   * @version $Id: TestEqualBehaviour.java,v 1.5 2004/08/16 22:16:31 henning Exp $
    32   */
    33  public class TestEqualBehaviour  extends TestCase
    34  {
    35      private Configuration setupSimpleConfiguration()
    36              throws Exception
    37      {
    38          String simpleConfigurationFile = new File("conf/testEqual.properties").getAbsolutePath();
    39          return new PropertiesConfiguration(simpleConfigurationFile);
    40      }
    41
    42      private Configuration setupCompositeConfiguration()
    43              throws Exception
    44      {
    45          String compositeConfigurationFile = new File("conf/testEqualDigester.xml").getAbsolutePath();
    46
    47          ConfigurationFactory configurationFactory = new ConfigurationFactory();
    48          configurationFactory.setConfigurationFileName(compositeConfigurationFile);
    49          return configurationFactory.getConfiguration();
    50      }
    51
    52      /***
    53       * Checks whether two configurations have the same size,
    54       * the same key sequence and contain the same key -> value mappings
    55       */
    56      private void checkEquality(String msg, Configuration c1, Configuration c2)
    57      {
    58          Iterator it1 = c1.getKeys();
    59          Iterator it2 = c2.getKeys();
    60
    61          while(it1.hasNext() && it2.hasNext())
    62          {
    63              String key1 = (String) it1.next();
    64              String key2 = (String) it2.next();
    65              assertEquals(msg + ", Keys: ", key1, key2);
    66              assertEquals(msg + ", Contains: ", c1.containsKey(key1), c2.containsKey(key2));
    67          }
    68          assertEquals(msg + ", Iterator: ", it1.hasNext(), it2.hasNext());
    69      }
    70
    71      /***
    72       * Checks whether two configurations have the same key -> value mapping
    73       */
    74      private void checkSameKey(String msg, String key, Configuration c1, Configuration c2)
    75      {
    76          String [] s1 = c1.getStringArray(key);
    77          String [] s2 = c2.getStringArray(key);
    78
    79          assertEquals(msg + ", length: ", s1.length, s2.length);
    80
    81          for (int i = 0; i < s1.length ; i++)
    82          {
    83              assertEquals(msg + ", String Array: ", s1[i], s2[i]);
    84          }
    85
    86          List list1 = c1.getList(key);
    87          List list2 = c2.getList(key);
    88
    89          assertEquals(msg + ", Size: ", list1.size(), list2.size());
    90
    91          Iterator it1 = list1.iterator();
    92          Iterator it2 = list2.iterator();
    93
    94          while(it1.hasNext() && it2.hasNext())
    95          {
    96              String val1 = (String) it1.next();
    97              String val2 = (String) it2.next();
    98              assertEquals(msg + ", List: ", val1, val2);
    99          }
    100         assertEquals(msg + ", Iterator End: ", it1.hasNext(), it2.hasNext());
    101     }
    102
    103     private void checkSameKeyVector(String msg, String key, Configuration c1, Configuration c2)
    104     {
    105         String [] s1 = c1.getStringArray(key);
    106         String [] s2 = c2.getStringArray(key);
    107
    108         assertEquals(msg + ", length: ", s1.length, s2.length);
    109
    110         for (int i = 0; i < s1.length ; i++)
    111         {
    112             assertEquals(msg + ", String Array: ", s1[i], s2[i]);
    113         }
    114
    115         Vector vector1 = c1.getVector(key);
    116         Vector vector2 = c2.getVector(key);
    117
    118         assertEquals(msg + ", Size: ", vector1.size(), vector2.size());
    119
    120         Iterator it1 = vector1.iterator();
    121         Iterator it2 = vector2.iterator();
    122
    123         while(it1.hasNext() && it2.hasNext())
    124         {
    125             String val1 = (String) it1.next();
    126             String val2 = (String) it2.next();
    127             assertEquals(msg + ", Vector: ", val1, val2);
    128         }
    129         assertEquals(msg + ", Iterator End: ", it1.hasNext(), it2.hasNext());
    130     }
    131
    132     /***
    133      * Are both configurations equal after loading?
    134      */
    135     public void testLoading() throws Exception
    136     {
    137         Configuration simple = setupSimpleConfiguration();
    138         Configuration composite = setupCompositeConfiguration();
    139
    140         checkEquality("testLoading", simple, composite);
    141     }
    142
    143     /***
    144      * If we delete a key, does it vanish? Does it leave all
    145      * the other keys unchanged? How about an unset key?
    146      */
    147     public void testDeletingExisting() throws Exception
    148     {
    149         Configuration simple = setupSimpleConfiguration();
    150         Configuration composite = setupCompositeConfiguration();
    151
    152         String key = "clear.property";
    153
    154         assertTrue(simple.containsKey(key));
    155         assertEquals(simple.containsKey(key), composite.containsKey(key));
    156
    157         simple.clearProperty(key);
    158         composite.clearProperty(key);
    159
    160         assertFalse(simple.containsKey(key));
    161         assertEquals(simple.containsKey(key), composite.containsKey(key));
    162
    163         checkEquality("testDeletingExisting", simple, composite);
    164     }
    165
    166     public void testDeletingNonExisting() throws Exception
    167     {
    168         Configuration simple = setupSimpleConfiguration();
    169         Configuration composite = setupCompositeConfiguration();
    170
    171         String key = "nonexisting.clear.property";
    172
    173         assertFalse(simple.containsKey(key));
    174         assertEquals(simple.containsKey(key), composite.containsKey(key));
    175
    176         simple.clearProperty(key);
    177         composite.clearProperty(key);
    178
    179         assertFalse(simple.containsKey(key));
    180         assertEquals(simple.containsKey(key), composite.containsKey(key));
    181
    182         checkEquality("testDeletingNonExisting", simple, composite);
    183     }
    184
    185     /***
    186      * If we set a key, does it work? How about an existing
    187      * key? Can we change it?
    188      */
    189     public void testSettingNonExisting() throws Exception
    190     {
    191         Configuration simple = setupSimpleConfiguration();
    192         Configuration composite = setupCompositeConfiguration();
    193
    194         String key = "nonexisting.property";
    195         String value = "new value";
    196
    197         assertFalse(simple.containsKey(key));
    198         assertEquals(simple.containsKey(key), composite.containsKey(key));
    199
    200         simple.setProperty(key, value);
    201         composite.setProperty(key, value);
    202
    203         assertTrue(simple.containsKey(key));
    204         assertEquals(simple.containsKey(key), composite.containsKey(key));
    205
    206         checkSameKey("testSettingNonExisting", key, simple, composite);
    207         checkEquality("testSettingNonExisting", simple, composite);
    208     }
    209
    210     public void testSettingExisting() throws Exception
    211     {
    212         Configuration simple = setupSimpleConfiguration();
    213         Configuration composite = setupCompositeConfiguration();
    214
    215         String key = "existing.property";
    216         String value = "new value";
    217
    218         assertTrue(simple.containsKey(key));
    219         assertFalse(simple.getString(key).equals(value));
    220         assertEquals(simple.containsKey(key), composite.containsKey(key));
    221
    222         simple.setProperty(key, value);
    223         composite.setProperty(key, value);
    224
    225         assertTrue(simple.containsKey(key));
    226         assertEquals(simple.getString(key), value);
    227         assertEquals(simple.containsKey(key), composite.containsKey(key));
    228
    229         checkSameKey("testSettingExisting", key, simple, composite);
    230         checkEquality("testSettingExisting", simple, composite);
    231     }
    232
    233     /***
    234      * If we add a key, does it work?
    235      */
    236     public void testAddingUnset() throws Exception
    237     {
    238         Configuration simple = setupSimpleConfiguration();
    239         Configuration composite = setupCompositeConfiguration();
    240
    241         String key = "nonexisting.property";
    242         String value = "new value";
    243
    244         assertFalse(simple.containsKey(key));
    245         assertEquals(simple.containsKey(key), composite.containsKey(key));
    246
    247         simple.addProperty(key, value);
    248         composite.addProperty(key, value);
    249
    250         checkSameKey("testAddingUnset", key, simple, composite);
    251         checkEquality("testAddingUnset", simple, composite);
    252     }
    253
    254     /***
    255      * If we add a to an existing key, does it work?
    256      */
    257     public void testAddingSet() throws Exception
    258     {
    259         Configuration simple = setupSimpleConfiguration();
    260         Configuration composite = setupCompositeConfiguration();
    261
    262         String key = "existing.property";
    263         String value = "new value";
    264
    265         assertTrue(simple.containsKey(key));
    266         assertEquals(simple.containsKey(key), composite.containsKey(key));
    267
    268         simple.addProperty(key, value);
    269         composite.addProperty(key, value);
    270
    271         assertTrue(simple.containsKey(key));
    272         assertEquals(simple.containsKey(key), composite.containsKey(key));
    273
    274         checkSameKey("testAddingSet", key, simple, composite);
    275         checkEquality("testAddingSet", simple, composite);
    276     }
    277 }