    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.util.ArrayList;
    20  import java.util.Collection;
    21  import java.util.Iterator;
    22  import java.util.List;
    23
    24  import junit.framework.TestCase;
    25
    26  /***
    27   * Test class for HierarchicalConfiguration.
    28   *
    29   * @version $Id: TestHierarchicalConfiguration.java,v 1.5 2004/03/13 17:04:04 epugh Exp $
    30   */
    31  public class TestHierarchicalConfiguration extends TestCase
    32  {
    33      private static String[] tables = { "users", "documents" };
    34
    35      private static String[][] fields =
    36      {
    37          { "uid", "uname", "firstName", "lastName", "email" },
    38          { "docid", "name", "creationDate", "authorID", "version" }
    39      };
    40
    41      private HierarchicalConfiguration config;
    42
    43      protected void setUp() throws Exception
    44      {
    45          config = new HierarchicalConfiguration();
    46          HierarchicalConfiguration.Node nodeTables =
    47          new HierarchicalConfiguration.Node("tables");
    48          for(int i = 0; i < tables.length; i++)
    49          {
    50              HierarchicalConfiguration.Node nodeTable =
    51              new HierarchicalConfiguration.Node("table");
    52              nodeTables.addChild(nodeTable);
    53              HierarchicalConfiguration.Node nodeName =
    54              new HierarchicalConfiguration.Node("name");
    55              nodeName.setValue(tables[i]);
    56              nodeTable.addChild(nodeName);
    57              HierarchicalConfiguration.Node nodeFields =
    58              new HierarchicalConfiguration.Node("fields");
    59              nodeTable.addChild(nodeFields);
    60              for(int j = 0; j < fields[i].length; j++)
    61              {
    62                  HierarchicalConfiguration.Node nodeField =
    63                  new HierarchicalConfiguration.Node("field");
    64                  HierarchicalConfiguration.Node nodeFieldName =
    65                  new HierarchicalConfiguration.Node("name");
    66                  nodeFieldName.setValue(fields[i][j]);
    67                  nodeField.addChild(nodeFieldName);
    68                  nodeFields.addChild(nodeField);
    69              }  /* for */
    70          }  /* for */
    71          config.getRoot().addChild(nodeTables);
    72      }
    73
    74      public void testIsEmpty()
    75      {
    76          assertFalse(config.isEmpty());
    77          HierarchicalConfiguration conf2 = new HierarchicalConfiguration();
    78          assertTrue(conf2.isEmpty());
    79          HierarchicalConfiguration.Node child1 =
    80          new HierarchicalConfiguration.Node("child1");
    81          HierarchicalConfiguration.Node child2 =
    82          new HierarchicalConfiguration.Node("child2");
    83          child1.addChild(child2);
    84          conf2.getRoot().addChild(child1);
    85          assertTrue(conf2.isEmpty());
    86      }
    87
    88      public void testGetProperty()
    89      {
    90          assertNull(config.getProperty("tables.table.resultset"));
    91          assertNull(config.getProperty("tables.table.fields.field"));
    92
    93          Object prop = config.getProperty("tables.table(0).fields.field.name");
    94          assertNotNull(prop);
    95          assertTrue(prop instanceof Collection);
    96          assertEquals(5, ((Collection) prop).size());
    97
    98          prop = config.getProperty("tables.table.fields.field.name");
    99          assertNotNull(prop);
    100         assertTrue(prop instanceof Collection);
    101         assertEquals(10, ((Collection) prop).size());
    102
    103         prop = config.getProperty("tables.table.fields.field(3).name");
    104         assertNotNull(prop);
    105         assertTrue(prop instanceof Collection);
    106         assertEquals(2, ((Collection) prop).size());
    107
    108         prop = config.getProperty("tables.table(1).fields.field(2).name");
    109         assertNotNull(prop);
    110         assertEquals("creationDate", prop.toString());
    111     }
    112
    113     public void testClearProperty()
    114     {
    115         Object prop = config.getProperty("tables.table(0).fields.field.name");
    116         assertNotNull(prop);
    117         config.clearProperty("tables.table(0).fields.field(3)");
    118         prop = config.getProperty("tables.table(0).fields.field.name");
    119         assertNotNull(prop);
    120         assertTrue(prop instanceof Collection);
    121         assertEquals(4, ((Collection) prop).size());
    122
    123         config.clearProperty("tables.table(0).fields");
    124         assertNull(config.getProperty("tables.table(0).fields.field.name"));
    125         prop = config.getProperty("tables.table.fields.field.name");
    126         assertNotNull(prop);
    127         assertTrue(prop instanceof Collection);
    128         assertEquals(5, ((Collection) prop).size());
    129
    130         config.clearProperty("tables.table(1)");
    131         assertNull(config.getProperty("tables.table.fields.field.name"));
    132     }
    133
    134     public void testContainsKey()
    135     {
    136         assertTrue(config.containsKey("tables.table(0).name"));
    137         assertTrue(config.containsKey("tables.table(1).name"));
    138         assertFalse(config.containsKey("tables.table(2).name"));
    139
    140         assertTrue(config.containsKey("tables.table(0).fields.field.name"));
    141         assertFalse(config.containsKey("tables.table(0).fields.field"));
    142         config.clearProperty("tables.table(0).fields");
    143         assertFalse(config.containsKey("tables.table(0).fields.field.name"));
    144
    145         assertTrue(config.containsKey("tables.table.fields.field.name"));
    146     }
    147
    148     public void testGetKeys()
    149     {
    150         List keys = new ArrayList();
    151         for(Iterator it = config.getKeys(); it.hasNext();)
    152         {
    153             keys.add(it.next());
    154         }  /* for */
    155
    156         assertEquals(2, keys.size());
    157         assertTrue(keys.contains("tables.table.name"));
    158         assertTrue(keys.contains("tables.table.fields.field.name"));
    159     }
    160
    161     public void testAddProperty()
    162     {
    163         config.addProperty("tables.table(0).fields.field(-1).name", "phone");
    164         Object prop = config.getProperty("tables.table(0).fields.field.name");
    165         assertNotNull(prop);
    166         assertTrue(prop instanceof Collection);
    167         assertEquals(6, ((Collection) prop).size());
    168
    169         config.addProperty("tables.table(0).fields.field.name", "fax");
    170         prop = config.getProperty("tables.table.fields.field(5).name");
    171         assertNotNull(prop);
    172         assertTrue(prop instanceof List);
    173         List list = (List) prop;
    174         assertEquals("phone", list.get(0));
    175         assertEquals("fax", list.get(1));
    176
    177         config.addProperty("tables.table(-1).name", "config");
    178         prop = config.getProperty("tables.table.name");
    179         assertNotNull(prop);
    180         assertTrue(prop instanceof Collection);
    181         assertEquals(3, ((Collection) prop).size());
    182         config.addProperty("tables.table(2).fields.field(0).name", "cid");
    183         config.addProperty("tables.table(2).fields.field(-1).name",
    184         "confName");
    185         prop = config.getProperty("tables.table(2).fields.field.name");
    186         assertNotNull(prop);
    187         assertTrue(prop instanceof Collection);
    188         assertEquals(2, ((Collection) prop).size());
    189         assertEquals("confName",
    190         config.getProperty("tables.table(2).fields.field(1).name"));
    191
    192         config.addProperty("connection.user", "scott");
    193         config.addProperty("connection.passwd", "tiger");
    194         assertEquals("tiger", config.getProperty("connection.passwd"));
    195
    196         ConfigurationKey key = new ConfigurationKey();
    197         key.append("tables").append("table").appendIndex(0);
    198         key.appendAttribute("tableType");
    199         config.addProperty(key.toString(), "system");
    200         assertEquals("system", config.getProperty(key.toString()));
    201     }
    202
    203     public void testGetMaxIndex()
    204     {
    205         assertEquals(4, config.getMaxIndex("tables.table(0).fields.field"));
    206         assertEquals(4, config.getMaxIndex("tables.table(1).fields.field"));
    207         assertEquals(1, config.getMaxIndex("tables.table"));
    208         assertEquals(1, config.getMaxIndex("tables.table.name"));
    209         assertEquals(0, config.getMaxIndex("tables.table(0).name"));
    210         assertEquals(0, config.getMaxIndex("tables.table(1).fields.field(1)"));
    211         assertEquals(-1, config.getMaxIndex("tables.table(2).fields"));
    212
    213         int maxIdx = config.getMaxIndex("tables.table(0).fields.field.name");
    214         for(int i = 0; i <= maxIdx; i++)
    215         {
    216             ConfigurationKey key = new ConfigurationKey("tables.table(0).fields");
    217             key.append("field").appendIndex(i).append("name");
    218             assertNotNull(config.getProperty(key.toString()));
    219         }  /* for */
    220     }
    221
    222     public void testSubset()
    223     {
    224         Configuration conf = config.subset("tables.table(0)");
    225         assertEquals("users", conf.getProperty("name"));
    226         Object prop = conf.getProperty("fields.field.name");
    227         assertNotNull(prop);
    228         assertTrue(prop instanceof Collection);
    229         assertEquals(5, ((Collection) prop).size());
    230
    231         for(int i = 0; i < fields[0].length; i++)
    232         {
    233             ConfigurationKey key = new ConfigurationKey();
    234             key.append("fields").append("field").appendIndex(i);
    235             key.append("name");
    236             assertEquals(fields[0][i], conf.getProperty(key.toString()));
    237         }  /* for */
    238
    239         assertTrue("subset is not empty", config.subset("tables.table(2)").isEmpty());
    240
    241         conf = config.subset("tables.table.fields.field");
    242         prop = conf.getProperty("name");
    243         assertTrue("prop is not a collection", prop instanceof Collection);
    244         assertEquals(10, ((Collection) prop).size());
    245
    246         conf = config.subset("tables.table.fields.field.name");
    247         assertTrue("subset is not empty", conf.isEmpty());
    248     }
    249 }