You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2013/02/16 14:49:55 UTC

git commit: MNG-3131: Error message is misleading if a missing plugin parameter is of a type like List

Updated Branches:
  refs/heads/master b1b526ac9 -> 56cd921fb


MNG-3131: Error message is misleading if a missing plugin parameter is of a type like List


Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/56cd921f
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/56cd921f
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/56cd921f

Branch: refs/heads/master
Commit: 56cd921fbd7e027457ad6f45f53d57ee1e4a90bf
Parents: b1b526a
Author: rfscholte <rf...@apache.org>
Authored: Sat Feb 16 14:49:19 2013 +0100
Committer: rfscholte <rf...@apache.org>
Committed: Sat Feb 16 14:49:19 2013 +0100

----------------------------------------------------------------------
 .../maven/plugin/PluginParameterException.java     |   51 +++++-
 .../maven/plugin/PluginParameterExceptionTest.java |  131 +++++++++++++++
 2 files changed, 178 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/56cd921f/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterException.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterException.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterException.java
index 07302e5..350349d 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterException.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterException.java
@@ -19,8 +19,10 @@ package org.apache.maven.plugin;
  * under the License.
  */
 
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 import org.apache.maven.plugin.descriptor.Parameter;
@@ -75,12 +77,53 @@ public class PluginParameterException
                                                                 StringBuilder messageBuffer )
     {
         String expression = param.getExpression();
-
+        
         if ( param.isEditable() )
         {
-            messageBuffer.append( "Inside the definition for plugin \'" + mojo.getPluginDescriptor().getArtifactId()
-                + "\', specify the following:\n\n<configuration>\n  ...\n  <" + param.getName() + ">VALUE</"
-                + param.getName() + ">\n</configuration>" );
+            boolean isArray = param.getType().endsWith( "[]" );
+            boolean isCollection = false;
+            boolean isMap = false;
+            if ( !isArray )
+            {
+                try
+                {
+                    //assuming Type is available in current ClassLoader
+                    isCollection = Collection.class.isAssignableFrom( Class.forName( param.getType() ) );
+                    isMap = Map.class.isAssignableFrom( Class.forName( param.getType() ) );
+                }
+                catch ( ClassNotFoundException e )
+                {
+                    // assume it is not assignable from Collection or Map
+                }
+            }
+            
+            messageBuffer.append( "Inside the definition for plugin \'");
+            messageBuffer.append( mojo.getPluginDescriptor().getArtifactId() );
+            messageBuffer.append( "\', specify the following:\n\n<configuration>\n  ...\n" );
+            messageBuffer.append( "  <" ).append( param.getName() ).append( '>' );
+            if( isArray || isCollection )
+            {
+                messageBuffer.append(  '\n' );
+                messageBuffer.append( "    <item>" );
+            }
+            else if ( isMap )
+            {
+                messageBuffer.append(  '\n' );
+                messageBuffer.append( "    <KEY>" );
+            }
+            messageBuffer.append( "VALUE" );
+            if( isArray || isCollection )
+            {
+                messageBuffer.append( "</item>\n" );
+                messageBuffer.append( "  " );
+            }    
+            else if ( isMap )
+            {
+                messageBuffer.append( "</KEY>\n" );
+                messageBuffer.append( "  " );
+            }    
+            messageBuffer.append( "</" ).append( param.getName() ).append( ">\n" );
+            messageBuffer.append( "</configuration>" );
 
             String alias = param.getAlias();
             if ( StringUtils.isNotEmpty( alias ) && !alias.equals( param.getName() ) )

http://git-wip-us.apache.org/repos/asf/maven/blob/56cd921f/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExceptionTest.java
----------------------------------------------------------------------
diff --git a/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExceptionTest.java b/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExceptionTest.java
new file mode 100644
index 0000000..4108b5c
--- /dev/null
+++ b/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExceptionTest.java
@@ -0,0 +1,131 @@
+package org.apache.maven.plugin;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Collections;
+
+import org.apache.maven.plugin.descriptor.MojoDescriptor;
+import org.apache.maven.plugin.descriptor.Parameter;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
+
+import junit.framework.TestCase;
+
+/**
+ * MNG-3131
+ * 
+ * @author Robert Scholte
+ *
+ */
+public class PluginParameterExceptionTest
+    extends TestCase
+{
+
+    public void testMissingRequiredStringArrayTypeParameter()
+    {
+        MojoDescriptor mojoDescriptor = new MojoDescriptor();
+        mojoDescriptor.setGoal( "goal" );
+        PluginDescriptor pluginDescriptor = new PluginDescriptor();
+        pluginDescriptor.setGoalPrefix( "goalPrefix" );
+        pluginDescriptor.setArtifactId( "artifactId" );
+        mojoDescriptor.setPluginDescriptor( pluginDescriptor );
+
+        Parameter parameter = new Parameter();
+        parameter.setType( "java.lang.String[]" );
+        parameter.setName( "toAddresses" );
+        
+        parameter.setRequired( true );
+
+        PluginParameterException exception =
+            new PluginParameterException( mojoDescriptor, Collections.singletonList( parameter ) );
+
+        assertEquals( "One or more required plugin parameters are invalid/missing for 'goalPrefix:goal'\n" + 
+        		"\n" + 
+        		"[0] Inside the definition for plugin 'artifactId', specify the following:\n" + 
+        		"\n" + 
+        		"<configuration>\n" + 
+        		"  ...\n" + 
+        		"  <toAddresses>\n" +
+        		"    <item>VALUE</item>\n" +
+        		"  </toAddresses>\n" + 
+        		"</configuration>.\n", exception.buildDiagnosticMessage() );
+    }
+    
+    public void testMissingRequiredCollectionTypeParameter()
+    {
+        MojoDescriptor mojoDescriptor = new MojoDescriptor();
+        mojoDescriptor.setGoal( "goal" );
+        PluginDescriptor pluginDescriptor = new PluginDescriptor();
+        pluginDescriptor.setGoalPrefix( "goalPrefix" );
+        pluginDescriptor.setArtifactId( "artifactId" );
+        mojoDescriptor.setPluginDescriptor( pluginDescriptor );
+
+        Parameter parameter = new Parameter();
+        parameter.setType( "java.util.List" );
+        parameter.setName( "toAddresses" );
+        
+        parameter.setRequired( true );
+
+        PluginParameterException exception =
+            new PluginParameterException( mojoDescriptor, Collections.singletonList( parameter ) );
+
+        assertEquals( "One or more required plugin parameters are invalid/missing for 'goalPrefix:goal'\n" + 
+                "\n" + 
+                "[0] Inside the definition for plugin 'artifactId', specify the following:\n" + 
+                "\n" + 
+                "<configuration>\n" + 
+                "  ...\n" + 
+                "  <toAddresses>\n" +
+                "    <item>VALUE</item>\n" +
+                "  </toAddresses>\n" + 
+                "</configuration>.\n", exception.buildDiagnosticMessage() );
+    }
+
+    public void testMissingRequiredMapTypeParameter()
+    {
+        MojoDescriptor mojoDescriptor = new MojoDescriptor();
+        mojoDescriptor.setGoal( "goal" );
+        PluginDescriptor pluginDescriptor = new PluginDescriptor();
+        pluginDescriptor.setGoalPrefix( "goalPrefix" );
+        pluginDescriptor.setArtifactId( "artifactId" );
+        mojoDescriptor.setPluginDescriptor( pluginDescriptor );
+
+        Parameter parameter = new Parameter();
+        parameter.setType( "java.util.Map" );
+        parameter.setName( "toAddresses" );
+        
+        parameter.setRequired( true );
+
+        PluginParameterException exception =
+            new PluginParameterException( mojoDescriptor, Collections.singletonList( parameter ) );
+
+        assertEquals( "One or more required plugin parameters are invalid/missing for 'goalPrefix:goal'\n" + 
+                "\n" + 
+                "[0] Inside the definition for plugin 'artifactId', specify the following:\n" + 
+                "\n" + 
+                "<configuration>\n" + 
+                "  ...\n" + 
+                "  <toAddresses>\n" +
+                "    <KEY>VALUE</KEY>\n" +
+                "  </toAddresses>\n" + 
+                "</configuration>.\n", exception.buildDiagnosticMessage() );
+    }
+
+
+}