You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2019/08/05 14:47:37 UTC

[maven] branch removed-java-tools-3 updated: interpolate non java classes

This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch removed-java-tools-3
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/removed-java-tools-3 by this push:
     new caee9f0  interpolate non java classes
caee9f0 is described below

commit caee9f03a43364fa145642c490f67d44a94d6bd3
Author: tibordigana <ti...@apache.org>
AuthorDate: Mon Aug 5 16:47:27 2019 +0200

    interpolate non java classes
---
 .../StringSearchModelInterpolator.java             | 48 ++++++++++++----------
 .../StringSearchModelInterpolatorTest.java         | 35 ++++++++++++++++
 2 files changed, 61 insertions(+), 22 deletions(-)

diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java
index 3c2ea13..ae43aa4 100644
--- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java
+++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java
@@ -278,33 +278,37 @@ public class StringSearchModelInterpolator
                 this.isQualifiedForInterpolation = isQualifiedForInterpolation( clazz );
                 this.isArray = clazz.isArray();
                 List<CacheField> fields = new ArrayList<>();
-                for ( Field currentField : clazz.getDeclaredFields() )
+                if ( isQualifiedForInterpolation )
                 {
-                    Class<?> type = currentField.getType();
-                    if ( isQualifiedForInterpolation( currentField, type ) )
+                    for ( Field currentField : clazz.getDeclaredFields() )
                     {
-                        if ( String.class == type )
+                        Class<?> type = currentField.getType();
+                        if ( isQualifiedForInterpolation( currentField, type ) )
                         {
-                            if ( !Modifier.isFinal( currentField.getModifiers() ) )
+                            if ( String.class == type )
                             {
-                                fields.add( new StringField( currentField ) );
+                                if ( !Modifier.isFinal( currentField.getModifiers() ) )
+                                {
+                                    fields.add( new StringField( currentField ) );
+                                }
+                            }
+                            else if ( List.class.isAssignableFrom( type ) )
+                            {
+                                fields.add( new ListField( currentField ) );
+                            }
+                            else if ( Collection.class.isAssignableFrom( type ) )
+                            {
+                                throw new RuntimeException(
+                                        "We dont interpolate into collections, use a list instead" );
+                            }
+                            else if ( Map.class.isAssignableFrom( type ) )
+                            {
+                                fields.add( new MapField( currentField ) );
+                            }
+                            else
+                            {
+                                fields.add( new ObjectField( currentField ) );
                             }
-                        }
-                        else if ( List.class.isAssignableFrom( type ) )
-                        {
-                            fields.add( new ListField( currentField ) );
-                        }
-                        else if ( Collection.class.isAssignableFrom( type ) )
-                        {
-                            throw new RuntimeException( "We dont interpolate into collections, use a list instead" );
-                        }
-                        else if ( Map.class.isAssignableFrom( type ) )
-                        {
-                            fields.add( new MapField( currentField ) );
-                        }
-                        else
-                        {
-                            fields.add( new ObjectField( currentField ) );
                         }
                     }
                 }
diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java
index 71ebf51..907a0b5 100644
--- a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java
+++ b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java
@@ -33,6 +33,10 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Future;
 import java.util.concurrent.FutureTask;
 
+import static org.hamcrest.CoreMatchers.anyOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
 /**
  * @author jdcasey
  * @author Benjamin Bentmann
@@ -337,6 +341,37 @@ public class StringSearchModelInterpolatorTest
         assertEquals( "value4", ( (String[]) obj.values.get( "key2" ) )[1] );
     }
 
+    public void testInterpolateObjectWithFile()
+            throws Exception
+    {
+        Model model = new Model();
+        model.setPomFile( new File( System.getProperty( "user.dir" ), "pom.xml" ) );
+        File baseDir = model.getProjectDirectory();
+
+        Properties p = new Properties();
+
+        Map<String, String> values = new HashMap<>();
+        values.put( "key", "${project.basedir}" + File.separator + "target" );
+
+        ObjectWithMapField obj = new ObjectWithMapField( values );
+
+        StringSearchModelInterpolator interpolator = (StringSearchModelInterpolator) createInterpolator();
+
+        ModelBuildingRequest config = createModelBuildingRequest( p );
+
+        SimpleProblemCollector collector = new SimpleProblemCollector();
+        interpolator.interpolateObject( obj, model, new File( "." ), config, collector );
+        assertProblemFree( collector );
+
+        assertThat( baseDir.getCanonicalPath(), is( System.getProperty( "user.dir" ) ) );
+        assertThat( obj.values.size(), is( 1 ) );
+        assertThat( (String) obj.values.get( "key" ), is( anyOf(
+                is( System.getProperty( "user.dir" ) + File.separator + "target" ),
+                // TODO why MVN adds dot /./ in paths???
+                is( System.getProperty( "user.dir" ) + File.separator + '.' + File.separator + "target" )
+        ) ) );
+    }
+
 
     public void testConcurrentInterpolation()
         throws Exception