You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2021/08/17 23:16:16 UTC

[maven-archetype] branch master updated: * [ARCHETYPE-618] Improve default value interdependency calculation

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

olamy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-archetype.git


The following commit(s) were added to refs/heads/master by this push:
     new 54b5fb4  * [ARCHETYPE-618] Improve default value interdependency calculation
54b5fb4 is described below

commit 54b5fb4bf16d70fce0ab3493d2e178b317a2f5b7
Author: Matt Benson <mb...@apache.org>
AuthorDate: Tue Aug 17 18:16:12 2021 -0500

    * [ARCHETYPE-618] Improve default value interdependency calculation
    
    * add failing test
---
 .../DefaultArchetypeGenerationConfigurator.java    | 19 ++++++--
 ...efaultArchetypeGenerationConfigurator2Test.java | 54 ++++++++++++++++++++++
 2 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java b/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java
index 69a7ae1..c28e307 100644
--- a/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java
+++ b/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator.java
@@ -37,6 +37,8 @@ import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.Velocity;
 import org.apache.velocity.context.Context;
+import org.apache.velocity.context.InternalContextAdapterImpl;
+import org.apache.velocity.runtime.RuntimeServices;
 import org.apache.velocity.runtime.RuntimeSingleton;
 import org.apache.velocity.runtime.parser.ParseException;
 import org.apache.velocity.runtime.parser.node.ASTReference;
@@ -381,6 +383,11 @@ public class DefaultArchetypeGenerationConfigurator
 
             List<String> requiredProperties = archetypeConfiguration.getRequiredProperties();
 
+            final InternalContextAdapterImpl velocityContextAdapter =
+                            new InternalContextAdapterImpl( new VelocityContext() );
+
+            final RuntimeServices velocityRuntime = RuntimeSingleton.getRuntimeServices();
+
             for ( String propertyName : requiredProperties )
             {
                 final Set<String> referencedPropertyNames = new LinkedHashSet<>();
@@ -393,16 +400,18 @@ public class DefaultArchetypeGenerationConfigurator
                         final boolean dumpNamespace = false;
                         SimpleNode node = RuntimeSingleton.parse(
                                         new StringReader( defaultValue ), propertyName + ".default", dumpNamespace );
+
+                        node.init( velocityContextAdapter, velocityRuntime );
+
                         node.jjtAccept( new BaseVisitor()
                         {
-                            @SuppressWarnings( "unchecked" )
                             @Override
                             public Object visit( ASTReference node, Object data )
                             {
-                                ( ( Set<String> ) data ).add( node.getFirstToken().next.image );
+                                referencedPropertyNames.add( node.getRootString() );
                                 return super.visit( node, data );
                             }
-                        }, referencedPropertyNames );
+                        }, velocityRuntime );
                     }
                     catch ( ParseException e )
                     {
@@ -410,6 +419,10 @@ public class DefaultArchetypeGenerationConfigurator
                     }
                 }
 
+                referencedPropertyNames.retainAll( archetypeConfiguration.getRequiredProperties() );
+
+                // handle the case that a property expression #set()s itself:
+                referencedPropertyNames.remove( propertyName );
                 result.put( propertyName, referencedPropertyNames );
             }
 
diff --git a/maven-archetype-plugin/src/test/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator2Test.java b/maven-archetype-plugin/src/test/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator2Test.java
index 319b697..60eeca5 100644
--- a/maven-archetype-plugin/src/test/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator2Test.java
+++ b/maven-archetype-plugin/src/test/java/org/apache/maven/archetype/ui/generation/DefaultArchetypeGenerationConfigurator2Test.java
@@ -186,4 +186,58 @@ public class DefaultArchetypeGenerationConfigurator2Test
 
         assertEquals( "MYSERVICENAME", request.getProperties().get( "serviceUpper" ) );
     }
+
+    public void testArchetype618() throws Exception
+    {
+        RequiredProperty custom = getRequiredProperty( "serviceName" );
+        custom.setKey( "camelArtifact" );
+        custom.setDefaultValue( "${artifactId.class.forName('org.codehaus.plexus.util.StringUtils').capitaliseAllWords($artifactId.replaceAll('[^A-Za-z_\\$0-9]', ' ').replaceFirst('^(\\d)', '_$1').replaceAll('\\d', '$0 ').replaceAll('[A-Z](?=[^A-Z])', ' $0').toLowerCase()).replaceAll('\\s', '')}" );
+        descriptor.addRequiredProperty( custom );
+
+        getRequiredProperty( "artifactId" ).setDefaultValue( null );
+
+        ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
+        request.setArchetypeGroupId( "archetypeGroupId" );
+        request.setArchetypeArtifactId( "archetypeArtifactId" );
+        request.setArchetypeVersion( "archetypeVersion" );
+        Properties properties = new Properties();
+
+        EasyMock.expect( queryer.getPropertyValue( EasyMock.eq("groupName"), EasyMock.anyString(),
+                        EasyMock.<Pattern> isNull() ) ).andReturn( "myGroupName" );
+
+        EasyMock.expect( queryer.getPropertyValue( EasyMock.eq("artifactId"), EasyMock.anyString(),
+                        EasyMock.<Pattern> isNull() ) ).andReturn( "my-service-name" );
+
+        EasyMock.expect( queryer.getPropertyValue( EasyMock.anyString(), EasyMock.anyString(),
+                        EasyMock.<Pattern> anyObject())).andAnswer( new IAnswer<String>() {
+
+                            @Override
+                            public String answer() throws Throwable {
+                                return (String) EasyMock.getCurrentArguments()[1];
+                            }}
+                        ).anyTimes();
+
+        EasyMock.expect( queryer.confirmConfiguration( EasyMock.<ArchetypeConfiguration> anyObject() ) )
+                        .andReturn( Boolean.TRUE );
+
+        EasyMock.replay( queryer );
+        configurator.configureArchetype( request, Boolean.TRUE, properties );
+
+        assertEquals( "MyServiceName", request.getProperties().get( "camelArtifact" ) );
+    }
+
+    private RequiredProperty getRequiredProperty( String propertyName )
+    {
+        if ( propertyName != null )
+        {
+            for ( RequiredProperty candidate : descriptor.getRequiredProperties() )
+            {
+                if ( propertyName.equals( candidate.getKey() ) )
+                {
+                    return candidate;
+                }
+            }
+        }
+        return null;
+    }
 }
\ No newline at end of file