commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@apache.org
Subject cvs commit: jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests ConfigurationReaderTest.java
Date Fri, 27 Dec 2002 21:15:18 GMT
craigmcc    2002/12/27 13:15:18

  Modified:    resources build.xml
               resources/src/java/org/apache/commons/resources
                        ConfigurationReader.java LocalStrings.properties
                        ResourcesManager.java XMLConfigurationReader.java
               resources/src/test/org/apache/commons/resources/tests
                        ConfigurationReaderTest.java
  Added:       resources/src/test/org/apache/commons/resources/junit
                        ResourcesManagerTestCase.java
                        XMLConfigurationReaderTestCase.java
                        test-resources.xml
  Log:
  Clean up the ConfigurationReader interface and XMLConfiguriationReader
  implementation, including instrumenting with commons-logging.
  
  Add beginnings of a suite of unit tests that only require JUnit, not Cactus.
  
  Revision  Changes    Path
  1.15      +40 -3     jakarta-commons-sandbox/resources/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/build.xml,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- build.xml	27 Dec 2002 19:33:05 -0000	1.14
  +++ build.xml	27 Dec 2002 21:15:18 -0000	1.15
  @@ -151,9 +151,10 @@
     <!-- Construct compile classpath -->
     <path id="compile.classpath">
       <pathelement location="${build.home}/classes"/>
  +    <pathelement location="${commons-beanutils.jar}"/>
       <pathelement location="${commons-collections.jar}"/>
       <pathelement location="${commons-digester.jar}"/>
  -    <pathelement location="${commons-beanutils.jar}"/>
  +    <pathelement location="${commons-logging.jar}"/>
       <pathelement location="${servlet.jar}" />
     </path>
   
  @@ -168,9 +169,9 @@
       <pathelement location="${commons-collections.jar}"/>
       <pathelement location="${junit.jar}"/>
       <pathelement location="${build.home}/classes"/>
  +    <pathelement location="${commons-beanutils.jar}"/>
       <pathelement location="${commons-collections.jar}"/>
       <pathelement location="${commons-digester.jar}"/>
  -    <pathelement location="${commons-beanutils.jar}"/>
       <pathelement location="${commons-logging.jar}"/>
       <pathelement location="${servlet.jar}" />
       <pathelement location="${cactus.jar}" />
  @@ -184,6 +185,17 @@
     <!-- The test runner to execute -->
     <property name="test.runner"             value="junit.textui.TestRunner"/>
   
  +  <!-- The Commons Logger LogFactory implementation to use -->
  +  <property name="test.factory"
  +           value="org.apache.commons.logging.impl.LogFactoryImpl"/>
  +
  +  <!-- The Commons Logger Log implementation to use (standard factory) -->
  +  <property name="test.log"
  +           value="org.apache.commons.logging.impl.SimpleLog"/>
  +
  +  <!-- The Commons Logger SimpleLog level for testing -->
  +  <property name="test.level"               value="error"/>
  +
   
   <!-- ========== Executable Targets ======================================== -->
   
  @@ -406,4 +418,29 @@
         <classpath refid="test.classpath"/>
       </java>
     </target>
  +
  +  <target name="test.junit" depends="build.tests"
  +   description="Unit Tests that require only JUnit, not Cactus">
  +
  +    <junit printsummary="yes" fork="yes"
  +          haltonfailure="yes" haltonerror="yes">
  +
  +      <sysproperty key="org.apache.commons.logging.LogFactory"
  +                 value="${test.factory}"/>
  +      <sysproperty key="org.apache.commons.logging.Log"
  +                 value="${test.log}"/>
  +      <sysproperty key="org.apache.commons.logging.simplelog.defaultlog"
  +                 value="${test.level}"/>
  +      <classpath refid="test.classpath"/>
  +      <formatter type="plain" usefile="false"/>
  +
  +      <batchtest>
  +        <fileset dir="${build.home}/tests"
  +            includes="org/apache/commons/resources/junit/*TestCase.class"/>
  +      </batchtest>
  +
  +    </junit>
  +
  +  </target>
  +
   </project>
  
  
  
  1.9       +30 -20    jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ConfigurationReader.java
  
  Index: ConfigurationReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ConfigurationReader.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ConfigurationReader.java	24 Oct 2001 19:35:55 -0000	1.8
  +++ ConfigurationReader.java	27 Dec 2002 21:15:18 -0000	1.9
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -58,32 +58,42 @@
    * <http://www.apache.org/>.
    *
    */
  +
   package org.apache.commons.resources;
   
  -import java.io.Serializable;
  -import java.io.IOException;
   
  +import java.io.Serializable;
   import java.util.Map;
  -import java.util.Collection;
  +
   
   /**
  - * This interface is responsible for reading configuration data for the resouces
  - * and exposing methods that the ResourcesManager uses to get data from
  - * resources.  See {@link XMLConfigurationReader XMLConfigurationReader} for
  - * an example implementation.
  + * <p>A {@link ConfigurationReader} is responsible for reading configuration
  + * data describing the characteristics of {@link Resources} instances that
  + * will be managed by a {@link ResourcesManager}.  Once the data has been
  + * read (by means that are specific to a particular implementation), the
  + * uninitialized {@link Resources} instances are made available via the
  + * <code>getResourcesMap()</code> method.</p>
  + *
  + * <p>See {@link XMLConfigurationReader} for an example implementation.</p>
  + *
    * @author Mike Schachter (mschachter@hp.com)
  + * @author Craig R. McClanahan
    * @version $Revision$ $Date$
    */
  +
   public interface ConfigurationReader extends Serializable {
   
  -    /**
  -     * Retrieve a Map of the resources where the key is the name of the resource
  -     * and the value is the uninitialized Resources object
  -     */
  -    public Map getResourceMap() throws ResourcesException;
   
       /**
  -     * Retrieve a Collection of the resources for this configuration
  +     * <p>Return a <code>Map</code> of the resources configuration information
  +     * that has been processed, where the key is the name of the
  +     * {@link Resources} entry, and the value is the
  +     * uninitialized {@link Resources} instance.</p>
  +     *
  +     * @exception ResourcesException if an error occurs retrieving the
  +     *  resources configuration map
        */
  -    public Collection getResources() throws ResourcesException;
  -}
  \ No newline at end of file
  +    public Map getResourcesMap() throws ResourcesException;
  +
  +
  +}
  
  
  
  1.6       +7 -4      jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/LocalStrings.properties,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- LocalStrings.properties	25 Oct 2001 19:33:21 -0000	1.5
  +++ LocalStrings.properties	27 Dec 2002 21:15:18 -0000	1.6
  @@ -1,8 +1,11 @@
  -resources.manager.ioexception=[ResourcesManager] IOException
  -resources.config.invalidfactory=[ConfigurationReader] factory class is not of type org.apache.commons.resources.ResourceFactory,
but type {0}
   resources.config.classnotfound=[ConfigurationReader] class {0} not found
  +resources.config.createresource=[ConfigurationReader] no resource created for factory {0}
with config String {1}
  +resources.config.illegalaccess=[ConfigurationReader] IllegalAccessException
   resources.config.instantiation=[ConfigurationReader] InstantiationException for class {0}
  +resources.config.invalidfactory=[ConfigurationReader] factory class is not of type org.apache.commons.resources.ResourceFactory,
but type {0}
  +resources.config.open=[ConfigurationReader] IOException opening {0}
   resources.config.sax=[ConfigurationReader] SAXException while parsing configuration
  -resources.config.illegalaccess=[ConfigurationReader] IllegalAccessException
  -resources.config.createresource=[ConfigurationReader] no resource created for factory {0}
with config String {1}
  +resources.config.stream=[ConfigurationReader] Stream parameter is required
  +resources.config.url=[ConfigurationReader] URL parameter is required
   resources.file.nofilefound=[FileResources] no file found for key {0}, locale {1}, and time
zone {2}
  +resources.manager.ioexception=[ResourcesManager] IOException
  
  
  
  1.3       +7 -14     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesManager.java
  
  Index: ResourcesManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ResourcesManager.java	24 Oct 2001 20:46:03 -0000	1.2
  +++ ResourcesManager.java	27 Dec 2002 21:15:18 -0000	1.3
  @@ -114,7 +114,7 @@
        * @param initialize Whether or not to call on initResources()
        */
       public ResourcesManager(ConfigurationReader configReader, boolean initialize) throws
ResourcesException {
  -        this.resources = configReader.getResourceMap();
  +        this.resources = configReader.getResourcesMap();
           if (initialize) {
               initResources();
           }
  @@ -154,15 +154,8 @@
        */
       public ResourcesManager(InputStream inputStream, boolean initialize) throws ResourcesException
{
           XMLConfigurationReader configReader = new XMLConfigurationReader();
  -        try {
  -            configReader.read(inputStream);
  -        }
  -        catch (IOException ioe) {
  -            throw new ResourcesException(
  -                messageResources.getMessage("resources.manager.ioexception",
  -                    ioe.getMessage()),ioe);
  -        }
  -        resources = configReader.getResourceMap();
  +        configReader.read(inputStream);
  +        resources = configReader.getResourcesMap();
           if (initialize) {
               initResources();
           }
  
  
  
  1.7       +178 -73   jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/XMLConfigurationReader.java
  
  Index: XMLConfigurationReader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/XMLConfigurationReader.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- XMLConfigurationReader.java	27 Dec 2002 19:18:04 -0000	1.6
  +++ XMLConfigurationReader.java	27 Dec 2002 21:15:18 -0000	1.7
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -58,33 +58,50 @@
    * <http://www.apache.org/>.
    *
    */
  +
   package org.apache.commons.resources;
   
  -import org.apache.commons.resources.message.MessageResources;
  -import org.apache.commons.resources.message.MessageResourcesFactory;
  +
  +import java.io.InputStream;
  +import java.io.IOException;
  +import java.net.URL;
  +import java.util.HashMap;
  +import java.util.Map;
   import org.apache.commons.digester.Digester;
   import org.apache.commons.digester.Rule;
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
  +import org.apache.commons.resources.message.MessageResources;
  +import org.apache.commons.resources.message.MessageResourcesFactory;
   import org.xml.sax.SAXException;
   import org.xml.sax.Attributes;
   
  -import java.util.Map;
  -import java.util.Collection;
  -import java.util.HashMap;
  -import java.io.InputStream;
  -import java.io.IOException;
  -import java.io.Serializable;
   
   /**
  - * This class loads a resource configuration from an XML configuration file.
  + * <p>Concrete implemetion of the {@link ConfigurationReader} interface
  + * that loads the required information from an XML configuration file.</p>
  + *
    * @author Mike Schachter (mschachter@hp.com)
  + * @author Craig R. McClanahan
    * @version $Revision$ $Date$
    */
  +
   public class XMLConfigurationReader implements ConfigurationReader {
   
  +
  +    // ----------------------------------------------------- Instance Variables
  +
  +
  +    /**
  +     * <p>The log instance for this class.</p>
  +     */
  +    protected static Log log = LogFactory.getLog(XMLConfigurationReader.class);
  +
  +
       /**
  -     * The message resources for this class
  +     * <p>The local message resources for this class.</p>
        */
  -    protected static MessageResources messageResources =
  +    protected static MessageResources messages =
                   MessageResourcesFactory.createFactory().createResources(
                                     "org.apache.commons.resources.LocalStrings");
   
  @@ -92,115 +109,203 @@
        * The Map that represents the resources, where the key is the name of the
        * resource and the value is the uninitialized Resources object
        */
  -    protected Map resources;
  +    protected Map resources = new HashMap();
  +
  +
  +    // -------------------------------------------- ConfigurationReader Methods
  +
   
       /**
  -     * Retrieve a Map of the resources where the key is the name of the resource
  -     * and the value is the uninitialized Resources object
  +     * <p>Return a <code>Map</code> of the resources configuration information
  +     * that has been processed, where the key is the name of the
  +     * {@link Resources} entry, and the value is the
  +     * uninitialized {@link Resources} instance.</p>
  +     *
  +     * @exception ResourcesException if an error occurs retrieving the
  +     *  resources configuration map
        */
  -    public Map getResourceMap() {
  -        return resources;
  +    public Map getResourcesMap() {
  +
  +        return (resources);
  +
       }
   
  +
  +    // --------------------------------------------------------- Public Methods
  +
  +
       /**
  -     * Retrieve a Collection of the resources for this configuration
  +     * <p>Read the XML configuration file at the specified URL.</p>
  +     *
  +     * @param url URL of the XML configuration file to be read
  +     *
  +     * @exception IllegalArgumentException if <code>url</code>
  +     *  is <code>null</code>
  +     * @exception ResourcesException if an error occurs reading this URL
        */
  -    public Collection getResources() {
  -        return resources.values();
  +    public void read(URL url) throws ResourcesException {
  +
  +        if (url == null) {
  +            throw new IllegalArgumentException
  +                (messages.getMessage("resources.config.url"));
  +        }
  +        if (log.isDebugEnabled()) {
  +            log.debug("Reading configuration from " + url.toExternalForm());
  +        }
  +
  +        // Acquire an input stream for this URL
  +        InputStream stream = null;
  +        try {
  +            stream = url.openStream();
  +        } catch (IOException e) {
  +            throw new ResourcesException
  +                (messages.getMessage("resources.config.open",
  +                                     url.toExternalForm()), e);
  +        }
  +
  +        // Read the configuration information from this input stream
  +        try {
  +            read(stream);
  +        } finally {
  +            if (stream != null) {
  +                try {
  +                    stream.close();
  +                } catch (Throwable t) {
  +                    ;
  +                }
  +                stream = null;
  +            }
  +        }
  +
       }
   
  +
       /**
  -     * Read the URL "config" and populate the internal resource Map with data
  +     * <p>Read the XML configuration file from the specified input stream,
  +     * and populate our internal map of resources information.  The
  +     * input stream will <strong>NOT</strong> be closed upon completion.</p>
  +     *
  +     * @param stream Input stream containing the configuration data
  +     *
  +     * @exception IllegalArgumentException if <code>stream</code>
  +     *  is <code>null</code>
  +     * @exception ResourcesException if an error occurs reading this stream
        */
  -    public void read(InputStream inputStream) throws ResourcesException, IOException {
  -        //wipe out the old resources
  -        resources = null;
  -        resources = new HashMap();
  -        //parse the config file
  +    public void read(InputStream stream) throws ResourcesException {
  +
  +        if (stream == null) {
  +            throw new IllegalArgumentException
  +                (messages.getMessage("resources.config.stream"));
  +        }
  +
  +        // Replace any previous Map containing configuration information
  +        this.resources = new HashMap();
  +
  +        // Parse the configuration information
           Digester digester = new Digester();
           digester.addRule("resources-config/resources/resource",
  -                        new AddResourceRule(this));
  -        digester.addSetProperty("resources-config/resources/resource/set-property",
  -                                    "property", "value");
  +                         new AddResourceRule(this));
  +        digester.addSetProperty
  +            ("resources-config/resources/resource/set-property",
  +             "property", "value");
           try {
  -            digester.parse(inputStream);
  -        }
  -        catch (SAXException sax) {
  -            throw new ResourcesException(
  -                messageResources.getMessage("resources.config.sax"), sax);
  +            digester.parse(stream);
  +        } catch (IOException ioe) {
  +            throw new ResourcesException
  +                (messages.getMessage("resources.config.io"), ioe);
  +        } catch (SAXException sax) {
  +            throw new ResourcesException
  +                (messages.getMessage("resources.config.sax"), sax);
           }
  +
       }
  +
   }
   
  +
  +// -------------------------------------------------------------- Private Class
  +
  +
   /**
  - * This subclass of Rule creates a resource from a resource tag and
  - * pushes it to the top of the stack, then after the resource is populated
  - * with properties, pops the resource back out and puts it into the
  - * ConfigurationReader's resource Map
  + * <p>Subclass of <code>Rule</code> that creates a {@link Resources}
entry
  + * from a <code>resource</code> element, and adds it to our reader's map
  + * of entries.</p>
    */
   class AddResourceRule extends Rule {
   
  +
       protected ConfigurationReader reader;
   
  +
       public AddResourceRule(ConfigurationReader reader) {
           this.reader = reader;
       }
   
  +
       public void begin(String namespace, String elementName,
                         Attributes attributes) throws ResourcesException {
   
  -        //create instance of resource from factory instance
  +        // Acquire the relevant attributes from this element
           String factoryClass = attributes.getValue("factory");
           String config = attributes.getValue("config");
           String name = attributes.getValue("name");
  +
  +        //create instance of Resource from the specified factory
           Resources resource = null;
           try {
               ResourcesFactory factory;
  -            Class cFactory  = Class.forName(factoryClass);
  -            if ((ResourcesFactory.class).isAssignableFrom(cFactory)) {
  -                factory = (ResourcesFactory) cFactory.newInstance();
  +            ClassLoader loader =
  +                Thread.currentThread().getContextClassLoader();
  +            if (loader == null) {
  +                loader = this.getClass().getClassLoader();
  +            }
  +            Class clazz = loader.loadClass(factoryClass);
  +            if ((ResourcesFactory.class).isAssignableFrom(clazz)) {
  +                factory = (ResourcesFactory) clazz.newInstance();
                   resource = factory.createResource(config);
               }
               else {
                   throw new ResourcesException(
  -                    XMLConfigurationReader.messageResources.getMessage(
  -                                                "resource.config.invalidfactory",
  -                                                cFactory.getClass().getName()));
  +                    XMLConfigurationReader.messages.getMessage
  +                    ("resource.config.invalidfactory",
  +                     clazz.getClass().getName()));
               }
  -        }
  -        catch (ClassNotFoundException cnfe) {
  -            throw new ResourcesException(
  -                    XMLConfigurationReader.messageResources.getMessage(
  -                                                "resources.config.classnotfound",
  -                                                factoryClass), cnfe);
  -        }
  -        catch (IllegalAccessException iae) {
  -            throw new ResourcesException(
  -                XMLConfigurationReader.messageResources.getMessage(
  -                                            "resources.config.illegalaccess",
  -                                            factoryClass), iae);
  -        }
  -        catch (InstantiationException ie) {
  -            throw new ResourcesException(
  -                XMLConfigurationReader.messageResources.getMessage(
  -                                            "resources.config.instantiation",
  -                                            factoryClass), ie);
  +        } catch (ClassNotFoundException cnfe) {
  +            throw new ResourcesException
  +                (XMLConfigurationReader.messages.getMessage
  +                 ("resources.config.classnotfound",
  +                  factoryClass), cnfe);
  +        } catch (IllegalAccessException iae) {
  +            throw new ResourcesException
  +                (XMLConfigurationReader.messages.getMessage
  +                 ("resources.config.illegalaccess",
  +                  factoryClass), iae);
  +        } catch (InstantiationException ie) {
  +            throw new ResourcesException
  +                (XMLConfigurationReader.messages.getMessage
  +                 ("resources.config.instantiation",
  +                  factoryClass), ie);
           }
           if (resource == null) {
  -            throw new ResourcesException(
  -                XMLConfigurationReader.messageResources.getMessage(
  -                                            "resources.config.createresource",
  -                                            factoryClass, config));
  +            throw new ResourcesException
  +                (XMLConfigurationReader.messages.getMessage
  +                 ("resources.config.createresource",
  +                  factoryClass, config));
           }
  -        resource.setName(name);
  +
           //push to top of digester stack for property population
  +        resource.setName(name);
           digester.push(resource);
  +
       }
   
  +
       public void end(String namespace, String elementName)
           throws ResourcesException {
           Resources resource = (Resources) digester.pop();
  -        reader.getResourceMap().put(resource.getName(), resource);
  +        reader.getResourcesMap().put(resource.getName(), resource);
       }
  +
  +
   }
   
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/junit/ResourcesManagerTestCase.java
  
  Index: ResourcesManagerTestCase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/junit/ResourcesManagerTestCase.java,v
1.1 2002/12/27 21:15:18 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/12/27 21:15:18 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.resources.junit;
  
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  
  /**
   * <p>Unit tests for
   * <code>org.apache.commons.resources.ResourcesManager</code>.
   * </p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/12/27 21:15:18 $
   */
  
  public class ResourcesManagerTestCase extends TestCase {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      // ----------------------------------------------------------- Constructors
  
  
      public ResourcesManagerTestCase(String name) {
          super(name);
      }
  
  
      // --------------------------------------------------- Overall Test Methods
  
  
      // Set up instance variables required by this test case
      public void setUp() {
      }
  
      // Return the tests included in this test suite
      public static Test suite() {
          return (new TestSuite(ResourcesManagerTestCase.class));
      }
  
      // Tear down the instance variables required by this test case
      public void tearDown() {
      }
  
  
      // ------------------------------------------------ Individual Test Methods
  
  
      public void testDummy() {
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/junit/XMLConfigurationReaderTestCase.java
  
  Index: XMLConfigurationReaderTestCase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/junit/XMLConfigurationReaderTestCase.java,v
1.1 2002/12/27 21:15:18 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/12/27 21:15:18 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.resources.junit;
  
  
  import java.io.InputStream;
  import java.net.URL;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.Map;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.apache.commons.resources.ConfigurationReader;
  import org.apache.commons.resources.Resources;
  import org.apache.commons.resources.ResourcesException;
  import org.apache.commons.resources.XMLConfigurationReader;
  
  
  /**
   * <p>Unit tests for
   * <code>org.apache.commons.resources.XMLConfigurationReader</code>.
   * </p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/12/27 21:15:18 $
   */
  
  public class XMLConfigurationReaderTestCase extends TestCase {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      // The XMLConfigurationReader instance to be tested
      protected XMLConfigurationReader reader = null;
  
  
      // ----------------------------------------------------------- Constructors
  
  
      public XMLConfigurationReaderTestCase(String name) {
          super(name);
      }
  
  
      // --------------------------------------------------- Overall Test Methods
  
  
      // Set up instance variables required by this test case
      public void setUp() {
          reader = new XMLConfigurationReader();
      }
  
      // Return the tests included in this test suite
      public static Test suite() {
          return (new TestSuite(XMLConfigurationReaderTestCase.class));
      }
  
      // Tear down the instance variables required by this test case
      public void tearDown() {
          reader = null;
      }
  
  
      // ------------------------------------------------ Individual Test Methods
  
  
      // Test the characteristics of a pristine instance of the reader
      public void testPristine() {
  
          Map map = reader.getResourcesMap();
          assertNotNull("Map was returned", map);
          int n = 0;
          Iterator keys = map.keySet().iterator();
          while (keys.hasNext()) {
              String key = (String) keys.next();
              n++;
          }
          assertEquals("Map should be empty", 0, n);
  
      }
  
  
      // Test the ability to read the configuration from an input stream
      public void testReadStream() throws Exception {
          readConfigurationStream();
      }
  
  
      // Test the ability to read the configuration from a URL
      public void testReadURL() throws Exception {
          readConfigurationURL();
      }
  
  
      // Test the getResourceMap() method with data from a stream
      public void testGetResourceMapStream() throws Exception {
  
          readConfigurationStream();
          Map map = reader.getResourcesMap();
          assertNotNull("Map was returned", map);
          checkResourceMap(map);
  
      }
  
  
      // Test the getResourceMap() method with data from a URL
      public void testGetResourceMapURL() throws Exception {
  
          readConfigurationURL();
          Map map = reader.getResourcesMap();
          assertNotNull("Map was returned", map);
          checkResourceMap(map);
  
      }
  
  
  
      // -------------------------------------------------------- Support Methods
  
  
      protected void checkResourceMap(Map map) {
  
          Object value = null;
  
          value = map.get("message");
          assertNotNull("Found 'message' resources");
          assertTrue("Correct 'message' resources type",
                     value instanceof Resources);
          assertEquals("Correct 'message' resources name",
                       "message",
                       ((Resources) value).getName());
  
          value = map.get("file");
          assertNotNull("Found 'file' resources");
          assertTrue("Correct 'file' resources type",
                     value instanceof Resources);
          assertEquals("Correct 'file' resources name",
                       "file",
                       ((Resources) value).getName());
  
      }
  
  
      protected void readConfigurationStream() throws Exception {
  
          // Acquire an InputStream to our test configuration file
          InputStream is = this.getClass().getResourceAsStream
              ("/org/apache/commons/resources/junit/test-resources.xml");
          assertNotNull("Got an InputStream");
  
          // Attempt to read the test configuration file
          try {
              reader.read(is);
          } catch (ResourcesException re) {
              if (re.getRootCause() != null) {
                  throw re.getRootCause();
              } else {
                  throw re;
              }
          } finally {
              if (is != null) {
                  try {
                      is.close();
                  } catch (Exception e) {
                      ;
                  }
                  is = null;
              }
          }
  
      }
  
  
      protected void readConfigurationURL() throws Exception {
  
          // Acquire a URL to our test configuration file
          URL url = this.getClass().getResource
              ("/org/apache/commons/resources/junit/test-resources.xml");
          assertNotNull("Got a URL");
  
          // Attempt to read the test configuration file
          reader.read(url);
  
      }
  
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/junit/test-resources.xml
  
  Index: test-resources.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1" ?>
  
  <resources-config>
      <resources>
  
          <!-- Configuration for a resource that is based on Java ResourceBundles -->
          <resource name="message"
                    factory="org.apache.commons.resources.message.PropertyMessageResourcesFactory"
                    config="org.apache.commons.resources.tests.ExampleStrings">
  
              <set-property property="returnNull" value="true" />
  
          </resource>
  
  
          <!-- Configuration for a resource that pulls its data from flat files -->
          <resource name="file"
                    factory="org.apache.commons.resources.file.FileResourcesFactory"
                    config="">
  
              <set-property property="path" value="c:\temp" />
              <set-property property="cache" value="true" />
              <set-property property="maxCacheSize" value="10240" />
  
          </resource>
  
          <!-- Configuration for a resource that pulls its data from flat files -->
  <!--
          <resource name="file-test"
                    factory="org.apache.commons.resources.tests.FileResourcesExposerFactory"
                    config="">
  
              <set-property property="path" value="c:\temp" />
              <set-property property="cache" value="true" />
              <set-property property="maxCacheSize" value="10240" />
          </resource>
  -->
  
      </resources>
  </resources-config>
  
  
  
  1.6       +1 -37     jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/ConfigurationReaderTest.java
  
  Index: ConfigurationReaderTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/tests/ConfigurationReaderTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ConfigurationReaderTest.java	24 Oct 2001 19:35:56 -0000	1.5
  +++ ConfigurationReaderTest.java	27 Dec 2002 21:15:18 -0000	1.6
  @@ -66,7 +66,7 @@
           
           try {
               configReader.read(inputStream);        
  -            Map resourceMap = configReader.getResourceMap();
  +            Map resourceMap = configReader.getResourcesMap();
   
               Resources fileResource = (Resources) resourceMap.get("file");
               assertNotNull(fileResource);
  @@ -91,42 +91,6 @@
               }
           }
       }   
  -    /**
  -     * Test the getResources() method
  -     */
  -    public void testGetResources() throws ResourcesException, IOException, Exception {
  -        try {
  -            boolean foundMessage = false;
  -            boolean foundFile = false;
  -            int count = 0;
  -
  -            configReader.read(inputStream);         
  -            Collection values = configReader.getResources();
  -            Iterator iterator = values.iterator();
  -            while (iterator.hasNext()) {
  -                Object object = iterator.next();
  -                assertEquals(true, (object instanceof Resources));
  -                Resources resource = (Resources) object;
  -                if (resource.getName().equals("message")) {
  -                    foundMessage = true;
  -                }
  -                if (resource.getName().equals("file")) {
  -                    foundFile = true;
  -                }
  -                count++;
  -            }        
  -            assertEquals(true, (foundMessage && foundFile));
  -            assertEquals(TOTAL_RESOURCE_NUMBER, count);        
  -        }
  -        catch (ResourcesException re) {
  -            if (re.getRootCause() != null) {
  -                throw re.getRootCause();
  -            }
  -            else {
  -                throw re;
  -            }
  -        }
  -    }
       
       public static Test suite() {
           return new TestSuite(ConfigurationReaderTest.class);       
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message