You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by mp...@apache.org on 2002/02/05 21:38:20 UTC

cvs commit: jakarta-turbine-stratum/src/test/org/apache/stratum/configuration TestBaseConfiguration.java

mpoeschl    02/02/05 12:38:20

  Modified:    .        build-test.xml
               src/java/org/apache/stratum/configuration
                        BaseConfiguration.java Configuration.java
  Added:       src/test/org/apache/stratum/configuration
                        TestBaseConfiguration.java
  Log:
  add test for BaseConfiguration and fix errors found :-)
  
  Revision  Changes    Path
  1.4       +9 -8      jakarta-turbine-stratum/build-test.xml
  
  Index: build-test.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-stratum/build-test.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- build-test.xml	18 Jan 2002 22:25:03 -0000	1.3
  +++ build-test.xml	5 Feb 2002 20:38:20 -0000	1.4
  @@ -16,6 +16,7 @@
       <pathelement location="${commons-beanutils.jar}"/>
       <pathelement location="${commons-util.jar}"/>
       <pathelement location="${commons-collections.jar}"/>
  +    <pathelement location="${servlet.jar}"/>
       <pathelement location="${build.dest}"/>
     </path>
   
  @@ -23,18 +24,18 @@
     <!-- T E S T                                                            -->
     <!-- ================================================================== -->
   
  -  <target 
  -    name="test" 
  -    depends="compile" 
  +  <target
  +    name="test"
  +    depends="compile"
       description="runs (junit) unit tests">
  -    
  +
       <echo>
         Running all JUnit tests
       </echo>
  -    
  +
       <delete dir="${test.reportsDirectory}"/>
       <mkdir dir="${test.reportsDirectory}"/>
  -    
  +
       <junit printSummary="yes">
         <formatter type="plain"/>
         <classpath refid="classpath"/>
  @@ -53,9 +54,9 @@
     <target
       name="compile"
       description="==> compiles the test source code">
  -    
  +
       <ant antfile="build.xml" target="compile"/>
  -    
  +
       <javac srcdir="${test.dir}"
         destdir="${build.dest}"
         excludes="**/package.html"
  
  
  
  1.2       +52 -4     jakarta-turbine-stratum/src/java/org/apache/stratum/configuration/BaseConfiguration.java
  
  Index: BaseConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-stratum/src/java/org/apache/stratum/configuration/BaseConfiguration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BaseConfiguration.java	4 Feb 2002 11:31:34 -0000	1.1
  +++ BaseConfiguration.java	5 Feb 2002 20:38:20 -0000	1.2
  @@ -81,9 +81,9 @@
    * @author <a href="mailto:ipriha@surfeu.fi">Ilkka Priha</a>
    * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
  - * @version $Id: BaseConfiguration.java,v 1.1 2002/02/04 11:31:34 mpoeschl Exp $
  + * @version $Id: BaseConfiguration.java,v 1.2 2002/02/05 20:38:20 mpoeschl Exp $
    */
  -public class BaseConfiguration
  +public class BaseConfiguration implements Configuration
   {
   
       /* stores the configuration key-value pairs */
  @@ -324,7 +324,7 @@
        */
       public Configuration subset(String prefix)
       {
  -        PropertiesConfiguration c = new PropertiesConfiguration();
  +        BaseConfiguration c = new BaseConfiguration();
           Iterator keys = this.getKeys();
           boolean validSubset = false;
   
  @@ -365,7 +365,7 @@
   
           if (validSubset)
           {
  -            return (Configuration)  c;
  +            return (Configuration) c;
           }
           else
           {
  @@ -440,6 +440,30 @@
       }
   
       /**
  +     * Get the list of the keys contained in the configuration
  +     * repository that match the specified prefix.
  +     *
  +     * @param prefix The prefix to test against.
  +     * @return An Iterator of keys that match the prefix.
  +     */
  +    public Iterator getKeys(String prefix)
  +    {
  +        Iterator keys = getKeys();
  +        ArrayList matchingKeys = new ArrayList();
  +
  +        while( keys.hasNext() )
  +        {
  +            Object key = keys.next();
  +
  +            if( key instanceof String && ((String) key).startsWith(prefix) )
  +            {
  +                matchingKeys.add(key);
  +            }
  +        }
  +        return matchingKeys.iterator();
  +    }
  +
  +    /**
        * Get a list of properties associated with the given
        * configuration key.
        *
  @@ -495,6 +519,30 @@
               }
           }
           return props;
  +    }
  +
  +    /**
  +     *  Gets a property from the configuration.
  +     *
  +     *  @param key property to retrieve
  +     *  @return value as object. Will return user value if exists,
  +     *          if not then default value if exists, otherwise null
  +     */
  +    public Object getProperty( String key)
  +    {
  +        // first, try to get from the 'user value' store
  +        Object o = store.get(key);
  +
  +        if ( o == null)
  +        {
  +            // if there isn't a value there, get it from the defaults if we have
  +            // them
  +            if (defaults != null)
  +            {
  +                o = defaults.getProperty(key);
  +            }
  +        }
  +        return o;
       }
   
       /**
  
  
  
  1.3       +2 -2      jakarta-turbine-stratum/src/java/org/apache/stratum/configuration/Configuration.java
  
  Index: Configuration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-stratum/src/java/org/apache/stratum/configuration/Configuration.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Configuration.java	4 Feb 2002 11:31:34 -0000	1.2
  +++ Configuration.java	5 Feb 2002 20:38:20 -0000	1.3
  @@ -66,7 +66,7 @@
    *
    * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
  - * @version $Id: Configuration.java,v 1.2 2002/02/04 11:31:34 mpoeschl Exp $
  + * @version $Id: Configuration.java,v 1.3 2002/02/05 20:38:20 mpoeschl Exp $
    */
   public interface Configuration
   {
  @@ -79,7 +79,7 @@
       void clearProperty(String key);
       Iterator getKeys(String key);
       Iterator getKeys();
  -    Properties getProperties();
  +    Properties getProperties(String key);
   
       // We probably want to at least try to be compatible with
       // the new preferences API.
  
  
  
  1.1                  jakarta-turbine-stratum/src/test/org/apache/stratum/configuration/TestBaseConfiguration.java
  
  Index: TestBaseConfiguration.java
  ===================================================================
  package org.apache.stratum.configuration;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-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 acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" 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",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * 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/>.
   */
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   * Tests some basic functions of the BaseConfiguration class
   *
   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
   * @version $Id: TestBaseConfiguration.java,v 1.1 2002/02/05 20:38:20 mpoeschl Exp $
   */
  public class TestBaseConfiguration extends TestCase
  {
      protected BaseConfiguration eprop = new BaseConfiguration();
  
      public TestBaseConfiguration(String testName)
      {
          super(testName);
      }
  
      public static Test suite()
      {
          return new TestSuite( TestBaseConfiguration.class );
      }
  
      public static void main(String args[])
      {
          String[] testCaseName = { TestBaseConfiguration.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
  
      public void testRetrieve()
      {
          /*
           * should be emptry and return null
           */
  
          assertEquals("This returns null", eprop.getProperty("foo"), null);
  
          /*
           *  add a real value, and get it two different ways
           */
          eprop.setProperty("number", "1");
          assertEquals("This returns '1'", eprop.getProperty("number"), "1");
          assertEquals("This returns '1'", eprop.getString("number"), "1");
  
          /*
           * now add another and get a Vector
           */
          eprop.addProperty("number", "2");
          assertTrue("This returns array", ( eprop.getVector("number")
                  instanceof java.util.Vector ) );
  
          /*
           *  now test dan's new fix where we get the first scalar
           *  when we access a vector valued
           *  property
           */
          assertTrue("This returns scalar", ( eprop.getString("number")
                  instanceof String ) );
  
          /*
           * test comma separated string properties
           */
          String prop = "hey, that's a test";
          eprop.setProperty("prop.string", prop);
          assertTrue("This returns vector", ( eprop.getVector("prop.string")
                  instanceof java.util.Vector ) );
  
          String prop2 = "hey\\, that's a test";
          eprop.clearProperty("prop.string");
          eprop.setProperty("prop.string", prop2);
          assertTrue("This returns string", ( eprop.getString("prop.string")
                  instanceof java.lang.String) );
  
          /*
           * test subset : we want to make sure that the EP doesn't reprocess the
           * data elements when generating the subset
           */
  
          Configuration subEprop = eprop.subset("prop");
  
          assertTrue("Returns the full string", subEprop.getString("string")
                  .equals( prop ) );
          assertTrue("This returns string for subset", ( subEprop
                  .getString("string") instanceof java.lang.String) );
          assertTrue("This returns array for subset", ( subEprop
                  .getVector("string") instanceof java.util.Vector) );
  
      }
  
      public void testInterpolation()
      {
          eprop.setProperty("applicationRoot", "/home/applicationRoot");
          eprop.setProperty("db", "${applicationRoot}/db/hypersonic");
          String dbProp = "/home/applicationRoot/db/hypersonic";
          assertTrue("Checking interpolated variable", eprop.getString("db")
                  .equals(dbProp));
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>