Author: brett
Date: Fri Oct  8 15:31:05 2010
New Revision: 1005860

URL: http://svn.apache.org/viewvc?rev=1005860&view=rev
Log:
[MNG-4858] correct NullPointerException if goal name is malformed

Modified:
    
maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoDescriptorCreator.java
    
maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java
    
maven/maven-3/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/MojoNotFoundException.java

Modified: 
maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoDescriptorCreator.java
URL: 
http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoDescriptorCreator.java?rev=1005860&r1=1005859&r2=1005860&view=diff
==============================================================================
--- 
maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoDescriptorCreator.java
 (original)
+++ 
maven/maven-3/trunk/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoDescriptorCreator.java
 Fri Oct  8 15:31:05 2010
@@ -153,7 +153,7 @@ public class MojoDescriptorCreator
 
         int numTokens = tok.countTokens();
 
-        if ( numTokens == 4 )
+        if ( numTokens >= 4 )
         {
             // We have everything that we need
             //
@@ -170,6 +170,11 @@ public class MojoDescriptorCreator
             plugin.setVersion( tok.nextToken() );
             goal = tok.nextToken();
 
+            // This won't be valid, but it constructs something easy to read 
in the error message
+            while ( tok.hasMoreTokens() )
+            {
+                goal += ":" + tok.nextToken();
+            }
         }
         else if ( numTokens == 3 )
         {
@@ -187,14 +192,23 @@ public class MojoDescriptorCreator
             plugin.setArtifactId( tok.nextToken() );
             goal = tok.nextToken();
         }
-        else if ( numTokens == 2 )
+        else if ( numTokens <= 2 )
         {
             // We have a prefix and goal
             //
             // idea:idea
             //
             String prefix = tok.nextToken();
-            goal = tok.nextToken();
+
+            if ( numTokens == 2 )
+            {
+                goal = tok.nextToken();
+            }
+            else
+            {
+                // goal was missing - pass through to MojoNotFoundException
+                goal = "";
+            }
 
             // This is the case where someone has executed a single goal from 
the command line
             // of the form:

Modified: 
maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java
URL: 
http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java?rev=1005860&r1=1005859&r2=1005860&view=diff
==============================================================================
--- 
maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java
 (original)
+++ 
maven/maven-3/trunk/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java
 Fri Oct  8 15:31:05 2010
@@ -27,6 +27,7 @@ import org.apache.maven.lifecycle.intern
 import org.apache.maven.lifecycle.internal.TaskSegment;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.plugin.MojoNotFoundException;
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 import org.codehaus.plexus.component.annotations.Requirement;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
@@ -310,6 +311,32 @@ public class LifecycleExecutorTest
                                                                         
mergedSegment.getTasks() );
     }
 
+    public void testInvalidGoalName()
+        throws Exception
+    {
+        File pom = getProject( "project-basic" );
+        MavenSession session = createMavenSession( pom );
+        try
+        {
+            getExecutions( calculateExecutionPlan( session, "resources:" ) );
+            fail( "expected a MojoNotFoundException" );
+        }
+        catch ( MojoNotFoundException e )
+        {
+            assertEquals( "", e.getGoal() );
+        }
+
+        try
+        {
+            getExecutions( calculateExecutionPlan( session, 
"org.apache.maven.plugins:maven-resources-plugin:0.1:resources:toomany" ) );
+            fail( "expected a MojoNotFoundException" );
+        }
+        catch ( MojoNotFoundException e )
+        {
+            assertEquals( "resources:toomany", e.getGoal() );
+        }
+    }
+
 
     public void testPluginPrefixRetrieval()
         throws Exception

Modified: 
maven/maven-3/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/MojoNotFoundException.java
URL: 
http://svn.apache.org/viewvc/maven/maven-3/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/MojoNotFoundException.java?rev=1005860&r1=1005859&r2=1005860&view=diff
==============================================================================
--- 
maven/maven-3/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/MojoNotFoundException.java
 (original)
+++ 
maven/maven-3/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/MojoNotFoundException.java
 Fri Oct  8 15:31:05 2010
@@ -54,7 +54,7 @@ public class MojoNotFoundException
     {
         StringBuilder buffer = new StringBuilder( 256 );
 
-        buffer.append( "Could not find goal " ).append( goal );
+        buffer.append( "Could not find goal '" ).append( goal ).append( "'" );
 
         if ( pluginDescriptor != null )
         {


Reply via email to