    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.io.File;
    20  import java.io.IOException;
    21  import java.util.Iterator;
    22  import java.util.List;
    23  import java.util.Vector;
    24
    25  import junit.framework.TestCase;
    26  import junitx.framework.ArrayAssert;
    27
    28  /***
    29   * test for loading and saving xml properties files
    30   *
    31   * @version $Id: TestXMLConfiguration.java,v 1.14 2004/10/11 09:26:38 henning Exp $
    32   */
    33  public class TestXMLConfiguration extends TestCase
    34  {
    35      /*** The File that we test with */
    36      private String testProperties = new File("conf/test.xml").getAbsolutePath();
    37      private String testBasePath = new File("conf").getAbsolutePath();
    38      private File testSaveConf = new File("target/testsave.xml");
    39
    40      private XMLConfiguration conf;
    41
    42      protected void setUp() throws Exception
    43      {
    44          conf = new XMLConfiguration(new File(testProperties));
    45      }
    46
    47      public void testGetProperty()
    48      {
    49          assertEquals("value", conf.getProperty("element"));
    50      }
    51
    52      public void testGetCommentedProperty()
    53      {
    54          assertEquals(null, conf.getProperty("test.comment"));
    55      }
    56
    57      public void testGetPropertyWithXMLEntity()
    58      {
    59          assertEquals("1<2", conf.getProperty("test.entity"));
    60      }
    61
    62      public void testClearProperty() throws ConfigurationException, IOException
    63      {
    64          // test non-existent element
    65          String key = "clearly";
    66          conf.clearProperty(key);
    67          assertNull(key, conf.getProperty(key));
    68          assertNull(key, conf.getXmlProperty(key));
    69
    70          // test single element
    71          conf.load();
    72          key = "clear.element";
    73          conf.clearProperty(key);
    74          assertNull(key, conf.getProperty(key));
    75          assertNull(key, conf.getXmlProperty(key));
    76
    77          // test single element with attribute
    78          conf.load();
    79          key = "clear.element2";
    80          conf.clearProperty(key);
    81          assertNull(key, conf.getProperty(key));
    82          assertNull(key, conf.getXmlProperty(key));
    83          key = "clear.element2[@id]";
    84          assertNotNull(key, conf.getProperty(key));
    85          assertNotNull(key, conf.getXmlProperty(key));
    86
    87          // test non-text/cdata element
    88          conf.load();
    89          key = "clear.comment";
    90          conf.clearProperty(key);
    91          assertNull(key, conf.getProperty(key));
    92          assertNull(key, conf.getXmlProperty(key));
    93
    94          // test cdata element
    95          conf.load();
    96          key = "clear.cdata";
    97          conf.clearProperty(key);
    98          assertNull(key, conf.getProperty(key));
    99          assertNull(key, conf.getXmlProperty(key));
    100
    101         // test multiple sibling elements
    102         conf.load();
    103         key = "clear.list.item";
    104         conf.clearProperty(key);
    105         assertNull(key, conf.getProperty(key));
    106         assertNull(key, conf.getXmlProperty(key));
    107         key = "clear.list.item[@id]";
    108         assertNotNull(key, conf.getProperty(key));
    109         assertNotNull(key, conf.getXmlProperty(key));
    110
    111         // test multiple, disjoined elements
    112         conf.load();
    113         key = "list.item";
    114         conf.clearProperty(key);
    115         assertNull(key, conf.getProperty(key));
    116         assertNull(key, conf.getXmlProperty(key));
    117     }
    118
    119     public void testGetXmlProperty() {
    120         // test non-leaf element
    121         Object property = conf.getXmlProperty("clear");
    122         assertNull(property);
    123
    124         // test non-existent element
    125         property = conf.getXmlProperty("e");
    126         assertNull(property);
    127
    128         // test non-existent element
    129         property = conf.getXmlProperty("element3[@n]");
    130         assertNull(property);
    131
    132         // test single element
    133         property = conf.getXmlProperty("element");
    134         assertNotNull(property);
    135         assertTrue(property instanceof String);
    136         assertEquals("value", property);
    137
    138         // test single attribute
    139         property = conf.getXmlProperty("element3[@name]");
    140         assertNotNull(property);
    141         assertTrue(property instanceof String);
    142         assertEquals("foo", property);
    143
    144         // test non-text/cdata element
    145         property = conf.getXmlProperty("test.comment");
    146         assertNull(property);
    147
    148         // test cdata element
    149         property = conf.getXmlProperty("test.cdata");
    150         assertNotNull(property);
    151         assertTrue(property instanceof String);
    152         assertEquals("<cdata value>", property);
    153
    154         // test multiple sibling elements
    155         property = conf.getXmlProperty("list.sublist.item");
    156         assertNotNull(property);
    157         assertTrue(property instanceof List);
    158         List list = (List)property;
    159         assertEquals(2, list.size());
    160         assertEquals("five", list.get(0));
    161         assertEquals("six", list.get(1));
    162
    163         // test multiple, disjoined elements
    164         property = conf.getXmlProperty("list.item");
    165         assertNotNull(property);
    166         assertTrue(property instanceof List);
    167         list = (List)property;
    168         assertEquals(4, list.size());
    169         assertEquals("one", list.get(0));
    170         assertEquals("two", list.get(1));
    171         assertEquals("three", list.get(2));
    172         assertEquals("four", list.get(3));
    173
    174         // test multiple, disjoined attributes
    175         property = conf.getXmlProperty("list.item[@name]");
    176         assertNotNull(property);
    177         assertTrue(property instanceof List);
    178         list = (List)property;
    179         assertEquals(2, list.size());
    180         assertEquals("one", list.get(0));
    181         assertEquals("three", list.get(1));
    182     }
    183
    184     public void testGetAttribute()
    185     {
    186         assertEquals("element3[@name]", "foo", conf.getProperty("element3[@name]"));
    187     }
    188
    189     public void testClearAttribute() throws Exception
    190     {
    191         // test non-existent attribute
    192         String key = "clear[@id]";
    193         conf.clearProperty(key);
    194         assertNull(key, conf.getProperty(key));
    195         assertNull(key, conf.getXmlProperty(key));
    196
    197         // test single attribute
    198         conf.load();
    199         key = "clear.element2[@id]";
    200         Object p = conf.getXmlProperty(key);
    201         p = conf.getXmlProperty("clear.element2");
    202         conf.clearProperty(key);
    203         assertNull(key, conf.getProperty(key));
    204         assertNull(key, conf.getXmlProperty(key));
    205         key = "clear.element2";
    206         assertNotNull(key, conf.getProperty(key));
    207         assertNotNull(key, conf.getXmlProperty(key));
    208
    209         // test multiple, disjoined attributes
    210         conf.load();
    211         key = "clear.list.item[@id]";
    212         conf.clearProperty(key);
    213         assertNull(key, conf.getProperty(key));
    214         assertNull(key, conf.getXmlProperty(key));
    215         key = "clear.list.item";
    216         assertNotNull(key, conf.getProperty(key));
    217         assertNotNull(key, conf.getXmlProperty(key));
    218     }
    219
    220     public void testSetAttribute()
    221     {
    222         // replace an existing attribute
    223         conf.setProperty("element3[@name]", "bar");
    224         assertEquals("element3[@name]", "bar", conf.getProperty("element3[@name]"));
    225
    226         // set a new attribute
    227         conf.setProperty("foo[@bar]", "value");
    228         assertEquals("foo[@bar]", "value", conf.getProperty("foo[@bar]"));
    229
    230         conf.setProperty("name1","value1");
    231         assertEquals("value1",conf.getProperty("name1"));
    232     }
    233
    234     public void testAddAttribute()
    235     {
    236         conf.addProperty("element3[@name]", "bar");
    237
    238         List list = conf.getList("element3[@name]");
    239         assertNotNull("null list", list);
    240         assertTrue("'foo' element missing", list.contains("foo"));
    241         assertTrue("'bar' element missing", list.contains("bar"));
    242         assertEquals("list size", 2, list.size());
    243     }
    244
    245     public void testAddObjectAttribute()
    246     {
    247         conf.addProperty("test.boolean[@value]", Boolean.TRUE);
    248         assertTrue("test.boolean[@value]", conf.getBoolean("test.boolean[@value]"));
    249     }
    250
    251     public void testAddVectorAttribute()
    252     {
    253         conf.addProperty("element3[@name]", "bar");
    254
    255         Vector vector = conf.getVector("element3[@name]");
    256         assertNotNull("null vector", vector);
    257         assertTrue("'foo' element missing", vector.contains("foo"));
    258         assertTrue("'bar' element missing", vector.contains("bar"));
    259         assertEquals("vector size", 2, vector.size());
    260     }
    261
    262     public void testAddList()
    263     {
    264         conf.addProperty("test.array", "value1");
    265         conf.addProperty("test.array", "value2");
    266
    267         List list = conf.getList("test.array");
    268         assertNotNull("null list", list);
    269         assertTrue("'value1' element missing", list.contains("value1"));
    270         assertTrue("'value2' element missing", list.contains("value2"));
    271         assertEquals("list size", 2, list.size());
    272     }
    273
    274     public void testAddVector()
    275     {
    276         conf.addProperty("test.array", "value1");
    277         conf.addProperty("test.array", "value2");
    278
    279         Vector vector = conf.getVector("test.array");
    280         assertNotNull("null vector", vector);
    281         assertTrue("'value1' element missing", vector.contains("value1"));
    282         assertTrue("'value2' element missing", vector.contains("value2"));
    283         assertEquals("vector size", 2, vector.size());
    284     }
    285
    286     public void testGetComplexProperty()
    287     {
    288         assertEquals("I'm complex!", conf.getProperty("element2.subelement.subsubelement"));
    289     }
    290
    291     public void testSettingFileNames()
    292     {
    293         conf = new XMLConfiguration();
    294         conf.setFileName(testProperties);
    295         assertEquals(testProperties.toString(), conf.getFileName());
    296
    297         conf.setBasePath(testBasePath);
    298         conf.setFileName("hello.xml");
    299         assertEquals("hello.xml", conf.getFileName());
    300         assertEquals(testBasePath.toString(), conf.getBasePath());
    301         assertEquals(new File(testBasePath, "hello.xml"), conf.getFile());
    302
    303         conf.setBasePath(testBasePath);
    304         conf.setFileName("subdir/hello.xml");
    305         assertEquals("subdir/hello.xml", conf.getFileName());
    306         assertEquals(testBasePath.toString(), conf.getBasePath());
    307         assertEquals(new File(testBasePath, "subdir/hello.xml"), conf.getFile());
    308     }
    309
    310     public void testLoad() throws Exception
    311     {
    312         conf = new XMLConfiguration();
    313         conf.setFileName(testProperties);
    314         conf.load();
    315
    316         assertEquals("I'm complex!", conf.getProperty("element2.subelement.subsubelement"));
    317     }
    318
    319     public void testLoadWithBasePath() throws Exception
    320     {
    321         conf = new XMLConfiguration();
    322
    323         conf.setFileName("test.xml");
    324         conf.setBasePath(testBasePath);
    325         conf.load();
    326
    327         assertEquals("I'm complex!", conf.getProperty("element2.subelement.subsubelement"));
    328     }
    329
    330     public void testLoadFromJAR() throws Exception
    331     {
    332         conf = new XMLConfiguration();
    333         conf.setFileName("test-jar.xml");
    334         conf.load();
    335
    336         assertEquals("I'm complex!", conf.getProperty("element2.subelement.subsubelement"));
    337     }
    338
    339     public void testSetProperty() throws Exception
    340     {
    341         conf.setProperty("element.string", "hello");
    342
    343         assertEquals("'element.string'", "hello", conf.getString("element.string"));
    344         assertEquals("XML value of element.string", "hello", conf.getXmlProperty("element.string"));
    345     }
    346
    347     public void testAddProperty()
    348     {
    349         // add a property to a non initialized xml configuration
    350         XMLConfiguration config = new XMLConfiguration();
    351         config.addProperty("test.string", "hello");
    352
    353         assertEquals("'test.string'", "hello", config.getString("test.string"));
    354     }
    355
    356     public void testAddObjectProperty()
    357     {
    358         // add a non string property
    359         conf.addProperty("test.boolean", Boolean.TRUE);
    360         assertTrue("'test.boolean'", conf.getBoolean("test.boolean"));
    361     }
    362
    363     public void testSave() throws Exception
    364     {
    365     	// remove the file previously saved if necessary
    366         if(testSaveConf.exists()){
    367     		assertTrue(testSaveConf.delete());
    368     	}
    369
    370         // add an array of strings to the configuration
    371     	conf.addProperty("string", "value1");
    372         for (int i = 1; i < 5; i++)
    373         {
    374             conf.addProperty("test.array", "value" + i);
    375         }
    376
    377         // add an array of strings in an attribute
    378         for (int i = 1; i < 5; i++)
    379         {
    380            conf.addProperty("test.attribute[@array]", "value" + i);
    381         }
    382
    383         // save the configuration
    384         conf.save(testSaveConf.getAbsolutePath());
    385
    386         // read the configuration and compare the properties
    387         XMLConfiguration checkConfig = new XMLConfiguration();
    388         checkConfig.setFileName(testSaveConf.getAbsolutePath());
    389         checkConfig.load();
    390
    391         for (Iterator i = conf.getKeys(); i.hasNext();)
    392         {
    393         	String key = (String) i.next();
    394         	assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
    395         	assertEquals("Value of the '" + key + "' property", conf.getProperty(key), checkConfig.getProperty(key));
    396         }
    397     }
    398
    399     public void testAutoSave() throws Exception
    400     {
    401         conf.setFile(new File("target/testsave.xml"));
    402         conf.setAutoSave(true);
    403         conf.setProperty("autosave", "ok");
    404
    405         // reload the configuration
    406         XMLConfiguration conf2 = new XMLConfiguration(conf.getFile());
    407         assertEquals("'autosave' property", "ok", conf2.getString("autosave"));
    408     }
    409
    410     public void testParseElementsNames()
    411     {
    412         // without attribute
    413         String key = "x.y.z";
    414         String[] array = new String[] {"x", "y", "z"};
    415         ArrayAssert.assertEquals("key without attribute", array, XMLConfiguration.parseElementNames(key));
    416
    417         // with attribute
    418         key = "x.y.z[@name]";
    419         ArrayAssert.assertEquals("key with attribute", array, XMLConfiguration.parseElementNames(key));
    420
    421         // null key
    422         ArrayAssert.assertEquals("null key", new String[] {}, XMLConfiguration.parseElementNames(null));
    423     }
    424
    425     public void testParseAttributeName()
    426     {
    427         // no attribute
    428         String key = "x.y.z";
    429         assertEquals("no attribute", null, XMLConfiguration.parseAttributeName(key));
    430
    431         // simple attribute
    432         key = "x.y.z[@name]";
    433         assertEquals("simple attribute", "name", XMLConfiguration.parseAttributeName(key));
    434
    435         // missing end marker
    436         key = "x.y.z[@name";
    437         assertEquals("missing end marker", "name", XMLConfiguration.parseAttributeName(key));
    438
    439         // null key
    440         assertEquals("null key", null, XMLConfiguration.parseAttributeName(null));
    441     }
    442 }