    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.IOException;
    20  import java.util.Arrays;
    21  import java.util.Iterator;
    22  import java.util.List;
    23
    24  import org.dom4j.Document;
    25  import org.dom4j.Element;
    26  import org.dom4j.io.SAXReader;
    27  import org.xml.sax.SAXException;
    28  import org.xml.sax.helpers.DefaultHandler;
    29
    30  import junit.framework.TestCase;
    31
    32  /***
    33   * Test class for BaseConfigurationXMLReader.
    34   *
    35   * @version $Id: TestBaseConfigurationXMLReader.java,v 1.3 2004/02/27 17:41:34 epugh Exp $
    36   */
    37  public class TestBaseConfigurationXMLReader extends TestCase
    38  {
    39      private static final String[] CONTINENTS =
    40      {
    41          "Africa", "America", "Asia", "Australia", "Europe"
    42      };
    43
    44      private BaseConfiguration config;
    45      private BaseConfigurationXMLReader configReader;
    46
    47      protected void setUp() throws Exception
    48      {
    49          config = new BaseConfiguration();
    50          config.addProperty("world.continents.continent", Arrays.asList(CONTINENTS));
    51          config.addProperty("world.greeting", "Hello");
    52          config.addProperty("world.greeting", "Salute");
    53          config.addProperty("world.wish", "Peace");
    54          config.addProperty("application.mail.smtp", "smtp.mymail.org");
    55          config.addProperty("application.mail.pop", "pop3.mymail.org");
    56          config.addProperty("application.mail.account.type", "pop3");
    57          config.addProperty("application.mail.account.user", "postmaster");
    58          config.addProperty("application.mail.account.pwd", "?.-gulp*#");
    59          config.addProperty("application.mail.timeout", new Integer(42));
    60          config.addProperty("test", Boolean.TRUE);
    61
    62          configReader = new BaseConfigurationXMLReader(config);
    63      }
    64
    65      public void testParse() throws Exception
    66      {
    67          checkDocument(configReader, "config");
    68      }
    69
    70      public void testParseSAXException() throws IOException
    71      {
    72          configReader.setContentHandler(new TestContentHandler());
    73          try
    74          {
    75              configReader.parse("systemID");
    76              fail("Expected exception was not thrown!");
    77          }
    78          catch(SAXException ex)
    79          {
    80          }
    81      }
    82
    83      public void testParseIOException() throws SAXException
    84      {
    85          BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader();
    86          try
    87          {
    88              reader.parse("document");
    89              fail("Expected exception was not thrown!");
    90          }
    91          catch(IOException ex)
    92          {
    93          }
    94      }
    95
    96      public void testSetRootName() throws Exception
    97      {
    98          BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader(config);
    99          reader.setRootName("apache");
    100         checkDocument(reader, "apache");
    101     }
    102
    103     private void checkDocument(BaseConfigurationXMLReader creader,
    104     String rootName) throws Exception
    105     {
    106         SAXReader reader = new SAXReader(creader);
    107         Document document = reader.read("config");
    108
    109         Element root = document.getRootElement();
    110         assertEquals(rootName, root.getName());
    111         assertEquals(3, root.elements().size());
    112
    113         check(root, "world.continents.continent", CONTINENTS);
    114         check(root, "world.greeting", new String[] { "Hello", "Salute" });
    115         check(root, "world.wish", "Peace");
    116         check(root, "application.mail.smtp", "smtp.mymail.org");
    117         check(root, "application.mail.timeout", "42");
    118         check(root, "application.mail.account.type", "pop3");
    119         check(root, "application.mail.account.user", "postmaster");
    120         check(root, "test", "true");
    121     }
    122
    123     /***
    124      * Helper method for checking values in the DOM4J document.
    125      * @param root the root element
    126      * @param path the path to be checked
    127      * @param values the expected element values
    128      */
    129     private void check(Element root, String path, String[] values)
    130     {
    131         ConfigurationKey.KeyIterator keyIt =
    132         new ConfigurationKey(path).iterator();
    133         Element e = root;
    134
    135         for(keyIt.nextKey(); keyIt.hasNext(); keyIt.nextKey())
    136         {
    137             Element child = e.element(keyIt.currentKey());
    138             assertNotNull(child);
    139             e = child;
    140         }  /* for */
    141
    142         List elems = e.elements(keyIt.currentKey());
    143         assertEquals(values.length, elems.size());
    144         Iterator it = elems.iterator();
    145         for(int i = 0; i < values.length; i++)
    146         {
    147             Element child = (Element) it.next();
    148             assertEquals(values[i], child.getTextTrim());
    149         }  /* for */
    150     }
    151
    152     private void check(Element root, String path, String value)
    153     {
    154         check(root, path, new String[] { value });
    155     }
    156
    157     // A ContentHandler that raises an exception
    158     private static class TestContentHandler extends DefaultHandler
    159      {
    160         public void characters(char[] ch, int start, int length)
    161             throws SAXException
    162         {
    163             throw new SAXException("Test exception during parsing");
    164         }
    165     }
    166 }