commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mpoes...@apache.org
Subject cvs commit: jakarta-commons-sandbox/configuration/src/test/org/apache/commons/configuration TestBaseConfiguration.java
Date Fri, 07 Feb 2003 00:21:50 GMT
mpoeschl    2003/02/06 16:21:50

  Modified:    configuration maven.xml project.xml
               configuration/src/java/org/apache/commons/configuration
                        BaseConfiguration.java
                        BasePropertiesConfiguration.java
               configuration/src/test/org/apache/commons/configuration
                        TestBaseConfiguration.java
  Log:
  Bugzilla Bug 16502 Enhancement: multiple interpolation
  patch by Jeff Barrett [JBarrett@sawyermedia.com]
  
  Revision  Changes    Path
  1.3       +2 -2      jakarta-commons-sandbox/configuration/maven.xml
  
  Index: maven.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/configuration/maven.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- maven.xml	14 Dec 2002 11:43:32 -0000	1.2
  +++ maven.xml	7 Feb 2003 00:21:50 -0000	1.3
  @@ -9,7 +9,7 @@
   <project default="java:jar">
   
     <postGoal name="test:compile">
  -    <copy todir="${maven.test.dest}/org/apache/commons/configuration">
  +    <copy todir="${maven.build.dest}/org/apache/commons/configuration">
         <fileset dir="${maven.conf.dir}" includes="*.properties"/>
       </copy>
     </postGoal>
  
  
  
  1.21      +6 -4      jakarta-commons-sandbox/configuration/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/configuration/project.xml,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- project.xml	14 Jan 2003 03:53:12 -0000	1.20
  +++ project.xml	7 Feb 2003 00:21:50 -0000	1.21
  @@ -117,7 +117,7 @@
         <version>3.8.1</version>
         <url>http://www.junit.org</url>
       </dependency>
  -
  +<!--
       <dependency>
         <id>xerces</id>
         <version>2.2.1</version>
  @@ -129,7 +129,7 @@
         <version>1.0.b2</version>
         <url>http://xml.apache.org/commons/</url>
       </dependency>
  -
  +-->
     </dependencies>
   
     <build>
  @@ -144,8 +144,10 @@
       <unitTest>
         <includes>
           <include>**/*Test*.java</include>
  -        <exclude>**/TestBasePropertiesConfiguration.java</exclude>
         </includes>
  +      <excludes>
  +        <exclude>**/TestBasePropertiesConfiguration.java</exclude>
  +      </excludes>
         <resources>
           <resource>
             <directory>conf</directory>
  
  
  
  1.10      +63 -4     jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/BaseConfiguration.java
  
  Index: BaseConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/BaseConfiguration.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- BaseConfiguration.java	22 Jan 2003 03:00:21 -0000	1.9
  +++ BaseConfiguration.java	7 Feb 2003 00:21:50 -0000	1.10
  @@ -256,16 +256,41 @@
           store.put(key, obj);
       }
   
  +	/**
  +	 * interpolate key names to handle ${key} stuff
  +	 */
  +	protected String interpolate(String base )
  +	{
  +		return( interpolateHelper( base, null ) );
  +	}
  +
       /**
  -     * interpolate key names to handle ${key} stuff
  +     * 
  +     * Recursive handler for multple levels of interpolation.  
  +     * 
  +     * When called the first time, priorVariables should be null.
  +     * 
  +     * priorVariables serves two purposes: to allow checking for loops, and
  +     * creating a meaningful exception message should a loop occur.  It's 0'th
  +     * element will be set to the value of base from the first call.  All
  +     * subsequent interpolated variables are added afterward.
  +     * 
        */
  -    protected String interpolate(String base)
  +    protected String interpolateHelper(String base, List priorVariables )
       {
           if (base == null)
           {
               return null;
           }
   
  +        // on the first call initialize priorVariables 
  +        // and add base as the first element
  +        if ( priorVariables == null)
  +        {
  +        	priorVariables = new ArrayList();
  +        	priorVariables.add( base );
  +        }
  +        
           int begin = -1;
           int end = -1;
           int prec = 0 - END_TOKEN.length();
  @@ -278,9 +303,43 @@
           {
               result.append(base.substring(prec + END_TOKEN.length(), begin));
               variable = base.substring(begin + START_TOKEN.length(), end);
  +            
  +            // if we've got a loop, create a useful exception message and throw
  +            if (priorVariables.contains(variable))
  +            {
  +            	String initialBase = priorVariables.remove( 0 ).toString();
  +            	priorVariables.add( variable );
  +            	StringBuffer priorVariableSb = new StringBuffer();
  +            	
  +            	// create a nice trace of interpolated variables like so:
  +            	// var1->var2->var3
  +            	for( Iterator it = priorVariables.iterator(); it.hasNext(); )
  +            	{
  +                    priorVariableSb.append( it.next() );
  +                    if ( it.hasNext() )
  +                    {
  +                    	priorVariableSb.append( "->" );
  +                    }
  +            	}
  +            
  +            	throw new IllegalStateException( "infinite loop in property interpolation
of " +
  +                                                  initialBase + ": " + priorVariableSb.toString()
);
  +            }
  +            // otherwise, add this variable to the interpolation list.
  +            else 
  +            {
  +            	priorVariables.add( variable );	
  +            }
  +            
               if (store.get(variable) != null)
               {
  -                result.append(store.get(variable));
  +                result.append(interpolateHelper( store.get(variable).toString(), priorVariables));
  +                
  +                // pop the interpolated variable off the stack
  +                // this maintains priorVariables correctness for 
  +                // properties with multiple interpolations, e.g.
  +                // prop.name=${some.other.prop1}/blahblah/${some.other.prop2}
  +                priorVariables.remove( priorVariables.size() - 1 );
               }
               else if (defaults != null && defaults.getString(variable) != null)
               {
  
  
  
  1.5       +6 -1      jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java
  
  Index: BasePropertiesConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- BasePropertiesConfiguration.java	13 Jan 2003 19:29:38 -0000	1.4
  +++ BasePropertiesConfiguration.java	7 Feb 2003 00:21:50 -0000	1.5
  @@ -138,6 +138,11 @@
    *
    *      # commas may be escaped in tokens
    *      commas.excaped = Hi\, what'up?
  + * 
  + *      # properties can reference other properties
  + *      base.prop = /base
  + *      first.prop = ${base.prop}/first
  + *      second.prop = ${first.prop}/second		
    * </pre>
    *
    * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
  
  
  
  1.5       +31 -1     jakarta-commons-sandbox/configuration/src/test/org/apache/commons/configuration/TestBaseConfiguration.java
  
  Index: TestBaseConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/configuration/src/test/org/apache/commons/configuration/TestBaseConfiguration.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestBaseConfiguration.java	13 Jan 2003 23:08:13 -0000	1.4
  +++ TestBaseConfiguration.java	7 Feb 2003 00:21:50 -0000	1.5
  @@ -267,4 +267,34 @@
                        "/home/applicationRoot/1",
                        arrayInt[0]);
       }
  +
  +    public void testMultipleInterpolation() throws Exception
  +    {
  +        eprop.setProperty( "test.base-level", "/base-level" );
  +        eprop.setProperty( "test.first-level", "${test.base-level}/first-level" );
  +        eprop.setProperty( "test.second-level", "${test.first-level}/second-level" );
  +        eprop.setProperty( "test.third-level", "${test.second-level}/third-level" );
  +        
  +        String expectedValue = "/base-level/first-level/second-level/third-level";
  +        
  +        assertEquals( eprop.getString( "test.third-level" ),  expectedValue );
  +    }
  +
  +    public void testInterpolationLoop() throws Exception
  +    {
  +        eprop.setProperty( "test.a", "${test.b}" );
  +        eprop.setProperty( "test.b", "${test.a}" );
  +        
  +        try {
  +            eprop.getString( "test.a" );
  +        }
  +        catch( IllegalStateException e ) {
  +            e.printStackTrace();
  +            return;
  +        }
  +        
  +        fail( "IllegalStateException should have been thrown for looped property references"
);
  +    }
  +
  +
   }
  
  
  

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


Mime
View raw message