Author: jdcasey
Date: Mon Jul 17 16:55:57 2006
New Revision: 422918

URL: http://svn.apache.org/viewvc?rev=422918&view=rev
Log:
Started implementing unit tests for refactored utilities.

Added:
    
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSource.java
   (with props)
    
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/interpolation/AssemblyInterpolatorTest.java
   (with props)
    
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/stubs/
    
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/
    
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSourceTest.java
   (with props)
    
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertyUtilsTest.java
   (with props)
    maven/plugins/branches/MASSEMBLY-124/src/test/plugin-configs/
    maven/plugins/branches/MASSEMBLY-124/src/test/plugin-configs/assembly/
    maven/plugins/branches/MASSEMBLY-124/src/test/plugin-configs/attached/
    
maven/plugins/branches/MASSEMBLY-124/src/test/plugin-configs/basicAbstractAssemblyMojoFeaturesTest/
    maven/plugins/branches/MASSEMBLY-124/src/test/plugin-configs/directory/
    
maven/plugins/branches/MASSEMBLY-124/src/test/plugin-configs/directory-inline/
    maven/plugins/branches/MASSEMBLY-124/src/test/plugin-configs/unpack/
    maven/plugins/branches/MASSEMBLY-124/src/test/remote-repo/
    maven/plugins/branches/MASSEMBLY-124/src/test/remote-repo/assembly/
    
maven/plugins/branches/MASSEMBLY-124/src/test/remote-repo/assembly/dependency-artifact/
    
maven/plugins/branches/MASSEMBLY-124/src/test/remote-repo/assembly/dependency-artifact/1.0/
    
maven/plugins/branches/MASSEMBLY-124/src/test/remote-repo/assembly/dependency-artifact/1.1/
    maven/plugins/branches/MASSEMBLY-124/src/test/resources/
    maven/plugins/branches/MASSEMBLY-124/src/test/resources/assemblies/
    
maven/plugins/branches/MASSEMBLY-124/src/test/resources/assemblies/components/
    
maven/plugins/branches/MASSEMBLY-124/src/test/resources/assemblies/descriptorSourceDirectory/
    
maven/plugins/branches/MASSEMBLY-124/src/test/resources/basicAbstractAssemblyMojoFeaturesTest/
Modified:
    
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java
    
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertyUtils.java

Modified: 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java?rev=422918&r1=422917&r2=422918&view=diff
==============================================================================
--- 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java
 (original)
+++ 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java
 Mon Jul 17 16:55:57 2006
@@ -123,7 +123,7 @@
 
                     try
                     {
-                        Properties properties = 
PropertyUtils.loadPropertyFile( new File( filtersfile ), true, true );
+                        Properties properties = 
PropertyUtils.getInterpolatedPropertiesFromFile( new File( filtersfile ), true, 
true );
 
                         filterProperties.putAll( properties );
                     }

Added: 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSource.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSource.java?rev=422918&view=auto
==============================================================================
--- 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSource.java
 (added)
+++ 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSource.java
 Mon Jul 17 16:55:57 2006
@@ -0,0 +1,24 @@
+package org.apache.maven.plugin.assembly.utils;
+
+import org.codehaus.plexus.util.interpolation.ValueSource;
+
+import java.util.Properties;
+
+
+public class PropertiesInterpolationValueSource
+    implements ValueSource
+{
+    
+    private final Properties properties;
+
+    public PropertiesInterpolationValueSource( Properties properties )
+    {
+        this.properties = properties;
+    }
+
+    public Object getValue( String key )
+    {
+        return properties.getProperty( key );
+    }
+
+}

Propchange: 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSource.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertyUtils.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertyUtils.java?rev=422918&r1=422917&r2=422918&view=diff
==============================================================================
--- 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertyUtils.java
 (original)
+++ 
maven/plugins/branches/MASSEMBLY-124/src/main/java/org/apache/maven/plugin/assembly/utils/PropertyUtils.java
 Mon Jul 17 16:55:57 2006
@@ -17,6 +17,7 @@
  */
 
 import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -45,15 +46,19 @@
      * @param useSystemProps wheter to incorporate System.getProperties 
settings into the returned Properties object.
      * @return the loaded and fully resolved Properties object
      */
-    public static Properties loadPropertyFile( File propfile, boolean fail, 
boolean useSystemProps )
+    public static Properties getInterpolatedPropertiesFromFile( File propfile, 
boolean fail, boolean useSystemProps )
         throws IOException
     {
-        Properties props = new Properties();
+        Properties props;
 
         if ( useSystemProps )
         {
             props = new Properties( System.getProperties() );
         }
+        else
+        {
+            props = new Properties();
+        }
 
         if ( propfile.exists() )
         {
@@ -72,76 +77,18 @@
             throw new FileNotFoundException( propfile.toString() );
         }
 
+        RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
+        interpolator.addValueSource( new PropertiesInterpolationValueSource( 
props ) );
+        
         for ( Enumeration n = props.propertyNames(); n.hasMoreElements(); )
         {
-            String k = (String) n.nextElement();
-            props.setProperty( k, getPropertyValue( k, props ) );
+            String key = (String) n.nextElement();
+            String value = interpolator.interpolate( props.getProperty( key ), 
"__properties" );
+            
+            props.setProperty( key, value );
         }
 
         return props;
     }
 
-
-    /**
-     * Retrieves a property value, replacing values like ${token}
-     * using the Properties to look them up.
-     * <p/>
-     * It will leave unresolved properties alone, trying for System
-     * properties, and implements reparsing (in the case that
-     * the value of a property contains a key), and will
-     * not loop endlessly on a pair like
-     * test = ${test}.
-     */
-    private static String getPropertyValue( String k, Properties p )
-    {
-        // This can also be done using InterpolationFilterReader,
-        // but it requires reparsing the file over and over until
-        // it doesn't change.
-
-        String v = p.getProperty( k );
-        String ret = "";
-        int idx, idx2;
-
-        while ( ( idx = v.indexOf( "${" ) ) >= 0 )
-        {
-            // append prefix to result
-            ret += v.substring( 0, idx );
-
-            // strip prefix from original
-            v = v.substring( idx + 2 );
-
-            // if no matching } then bail
-            if ( ( idx2 = v.indexOf( '}' ) ) < 0 )
-            {
-                break;
-            }
-
-            // strip out the key and resolve it
-            // resolve the key/value for the ${statement}
-            String nk = v.substring( 0, idx2 );
-            v = v.substring( idx2 + 1 );
-            String nv = p.getProperty( nk );
-
-            // try global environment..
-            if ( nv == null )
-            {
-                nv = System.getProperty( nk );
-            }
-
-            // if the key cannot be resolved,
-            // leave it alone ( and don't parse again )
-            // else prefix the original string with the
-            // resolved property ( so it can be parsed further )
-            // taking recursion into account.
-            if ( nv == null || nv.equals( k ) )
-            {
-                ret += "${" + nk + "}";
-            }
-            else
-            {
-                v = nv + v;
-            }
-        }
-        return ret + v;
-    }
 }

Added: 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/interpolation/AssemblyInterpolatorTest.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/interpolation/AssemblyInterpolatorTest.java?rev=422918&view=auto
==============================================================================
--- 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/interpolation/AssemblyInterpolatorTest.java
 (added)
+++ 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/interpolation/AssemblyInterpolatorTest.java
 Mon Jul 17 16:55:57 2006
@@ -0,0 +1,44 @@
+package org.apache.maven.plugin.assembly.interpolation;
+
+import org.apache.maven.model.Model;
+import org.apache.maven.plugins.assembly.model.Assembly;
+import org.apache.maven.plugins.assembly.model.DependencySet;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public class AssemblyInterpolatorTest
+    extends TestCase
+{
+
+    public void testDependencyOutputFileNameMappingsAreNotInterpolated() 
+        throws IOException, AssemblyInterpolationException
+    {
+        AssemblyInterpolator interpolator = new AssemblyInterpolator();
+
+        Model model = new Model();
+        model.setArtifactId( "artifact-id" );
+        model.setGroupId( "group.id" );
+        model.setVersion( "1" );
+
+        Assembly assembly = new Assembly();
+
+        DependencySet set = new DependencySet();
+        set.setOutputFileNameMapping( "${artifactId}.${extension}" );
+
+        assembly.addDependencySet( set );
+
+        Assembly outputAssembly = interpolator.interpolate( assembly, model, 
Collections.EMPTY_MAP );
+        
+        List outputDependencySets = outputAssembly.getDependencySets();
+        assertEquals( 1, outputDependencySets.size() );
+        
+        DependencySet outputSet = (DependencySet) outputDependencySets.get( 0 
);
+        
+        assertEquals( set.getOutputFileNameMapping(), 
outputSet.getOutputFileNameMapping() );
+    }
+
+}

Propchange: 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/interpolation/AssemblyInterpolatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/interpolation/AssemblyInterpolatorTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSourceTest.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSourceTest.java?rev=422918&view=auto
==============================================================================
--- 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSourceTest.java
 (added)
+++ 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSourceTest.java
 Mon Jul 17 16:55:57 2006
@@ -0,0 +1,27 @@
+package org.apache.maven.plugin.assembly.utils;
+
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+
+public class PropertiesInterpolationValueSourceTest
+    extends TestCase
+{
+    
+    public void testShouldRetrievePropertyValueForKey()
+    {
+        Properties props = new Properties();
+        props.setProperty( "key", "value" );
+        
+        assertEquals( "value", new PropertiesInterpolationValueSource( props 
).getValue( "key" ) );
+    }
+
+    public void testShouldRetrieveNullValueForMissingKey()
+    {
+        Properties props = new Properties();
+        
+        assertNull( new PropertiesInterpolationValueSource( props ).getValue( 
"key" ) );
+    }
+
+}

Propchange: 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSourceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertiesInterpolationValueSourceTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertyUtilsTest.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertyUtilsTest.java?rev=422918&view=auto
==============================================================================
--- 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertyUtilsTest.java
 (added)
+++ 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertyUtilsTest.java
 Mon Jul 17 16:55:57 2006
@@ -0,0 +1,203 @@
+package org.apache.maven.plugin.assembly.utils;
+
+import org.codehaus.plexus.util.IOUtil;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+public class PropertyUtilsTest
+    extends TestCase
+{
+
+    public void testShouldNotTouchPropertiesWithNoExpressions()
+        throws IOException
+    {
+        Properties props = new Properties();
+        props.setProperty( "key", "value" );
+        props.setProperty( "key2", "value2" );
+
+        File propsFile = getTempFile();
+
+        writePropertiesTo( propsFile, props );
+
+        Properties result = PropertyUtils.getInterpolatedPropertiesFromFile( 
propsFile, true, false );
+
+        assertEquals( "value", result.getProperty( "key" ) );
+        assertEquals( "value2", result.getProperty( "key2" ) );
+    }
+
+    public void 
testShouldResolveExpressionReferringToExistingKeyWithoutExpressionPrefix()
+        throws IOException
+    {
+        Properties props = new Properties();
+        props.setProperty( "key", "value" );
+        props.setProperty( "key2", "${key}" );
+
+        File propsFile = getTempFile();
+
+        writePropertiesTo( propsFile, props );
+
+        Properties result = PropertyUtils.getInterpolatedPropertiesFromFile( 
propsFile, true, false );
+
+        assertEquals( "value", result.getProperty( "key" ) );
+        assertEquals( "value", result.getProperty( "key2" ) );
+    }
+
+    public void 
testShouldResolveExpressionReferringToExistingKeyWithExpressionPrefix()
+        throws IOException
+    {
+        Properties props = new Properties();
+        props.setProperty( "key", "value" );
+        props.setProperty( "key2", "${__properties.key}" );
+
+        File propsFile = getTempFile();
+
+        writePropertiesTo( propsFile, props );
+
+        Properties result = PropertyUtils.getInterpolatedPropertiesFromFile( 
propsFile, true, false );
+
+        assertEquals( "value", result.getProperty( "key" ) );
+        assertEquals( "value", result.getProperty( "key2" ) );
+    }
+
+    public void 
testShouldResolveExpressionReferringToSysPropKeyWithoutExpressionPrefix()
+        throws IOException
+    {
+        Properties props = new Properties();
+        props.setProperty( "key", "value" );
+        props.setProperty( "key2", "${user.name}" );
+        
+        String userName = System.getProperty( "user.name" );
+
+        File propsFile = getTempFile();
+
+        writePropertiesTo( propsFile, props );
+
+        Properties result = PropertyUtils.getInterpolatedPropertiesFromFile( 
propsFile, true, true );
+
+        assertEquals( userName, result.getProperty( "key2" ) );
+    }
+
+    public void 
testShouldResolveExpressionReferringToSysPropKeyWithExpressionPrefix()
+        throws IOException
+    {
+        Properties props = new Properties();
+        props.setProperty( "key", "value" );
+        props.setProperty( "key2", "${__properties.user.name}" );
+        
+        String userName = System.getProperty( "user.name" );
+
+        File propsFile = getTempFile();
+
+        writePropertiesTo( propsFile, props );
+
+        Properties result = PropertyUtils.getInterpolatedPropertiesFromFile( 
propsFile, true, true );
+
+        assertEquals( userName, result.getProperty( "key2" ) );
+    }
+
+    public void 
testShouldNotTouchExpressionReferringToNonExistentKeyWithoutExpressionPrefix()
+        throws IOException
+    {
+        Properties props = new Properties();
+        props.setProperty( "key", "value" );
+        props.setProperty( "key2", "${foo.bar.gobbledy.gook}" );
+
+        File propsFile = getTempFile();
+
+        writePropertiesTo( propsFile, props );
+
+        Properties result = PropertyUtils.getInterpolatedPropertiesFromFile( 
propsFile, true, false );
+
+        assertEquals( "${foo.bar.gobbledy.gook}", result.getProperty( "key2" ) 
);
+    }
+
+    public void 
testShouldNotTouchExpressionReferringToNonExistentKeyWithExpressionPrefix()
+        throws IOException
+    {
+        Properties props = new Properties();
+        props.setProperty( "key", "value" );
+        props.setProperty( "key2", "${__properties.foo.bar.gobbledy.gook}" );
+
+        File propsFile = getTempFile();
+
+        writePropertiesTo( propsFile, props );
+
+        Properties result = PropertyUtils.getInterpolatedPropertiesFromFile( 
propsFile, true, false );
+
+        assertEquals( "${__properties.foo.bar.gobbledy.gook}", 
result.getProperty( "key2" ) );
+    }
+
+    public void testShouldNotIncludeSystemProperties()
+        throws IOException
+    {
+        Properties props = new Properties();
+
+        File propsFile = getTempFile();
+
+        writePropertiesTo( propsFile, props );
+
+        Properties result = PropertyUtils.getInterpolatedPropertiesFromFile( 
propsFile, true, false );
+
+        assertNull( result.getProperty( "user.name" ) );
+    }
+
+    public void testShouldFailWhenGivenNonExistentPropertiesFileLocation()
+        throws IOException
+    {
+        File propsFile = getTempFile();
+        propsFile.delete();
+
+        try
+        {
+            PropertyUtils.getInterpolatedPropertiesFromFile( propsFile, true, 
false );
+
+            fail( "Should throw FileNotFoundException when properties file 
doesn't exist, and fail flag is set." );
+        }
+        catch ( FileNotFoundException e )
+        {
+            // expected.
+        }
+    }
+
+    private File getTempFile()
+        throws IOException
+    {
+        File tempFile = File.createTempFile( "properties-test.", "" );
+        tempFile.deleteOnExit();
+
+        return tempFile;
+    }
+
+    private void writePropertiesTo( File propsFile, Properties properties )
+        throws IOException
+    {
+        FileOutputStream out = null;
+
+        try
+        {
+            propsFile.getParentFile().mkdirs();
+
+            out = new FileOutputStream( propsFile );
+            properties.store( out, "unit test properties file for: " + 
getMethodAndClass() );
+        }
+        finally
+        {
+            IOUtil.close( out );
+        }
+    }
+
+    private String getMethodAndClass()
+    {
+        NullPointerException npe = new NullPointerException();
+        StackTraceElement callerInfo = npe.getStackTrace()[2];
+
+        return callerInfo.getClassName() + ":" + callerInfo.getMethodName() + 
"@" + callerInfo.getLineNumber();
+    }
+
+}

Propchange: 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertyUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/plugins/branches/MASSEMBLY-124/src/test/java/org/apache/maven/plugin/assembly/utils/PropertyUtilsTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"


Reply via email to