improved the error diagnosis for invalid plugin parameters, as in MNG-345

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@179274 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-05-31 21:01:25 +00:00
parent 23f82d0199
commit 25fb7c646d
4 changed files with 130 additions and 44 deletions

View File

@ -31,7 +31,7 @@ public class ArtifactResolverDiagnoser
public String diagnose( Throwable error ) public String diagnose( Throwable error )
{ {
Throwable root = traverseToRoot( error ); Throwable root = DiagnosisUtils.getRootCause( error );
String message = null; String message = null;
@ -39,7 +39,7 @@ public class ArtifactResolverDiagnoser
{ {
StringBuffer messageBuffer = new StringBuffer(); StringBuffer messageBuffer = new StringBuffer();
if ( causalityChainContains( error, TransitiveArtifactResolutionException.class ) ) if ( DiagnosisUtils.containsInCausality( error, TransitiveArtifactResolutionException.class ) )
{ {
messageBuffer.append( messageBuffer.append(
"Error while transitively resolving artifacts (transitive path trace currently unavailable):\n\n" ); "Error while transitively resolving artifacts (transitive path trace currently unavailable):\n\n" );
@ -67,36 +67,4 @@ public class ArtifactResolverDiagnoser
return message; return message;
} }
private boolean causalityChainContains( Throwable error, Class errorClass )
{
Throwable cause = error;
boolean contains = false;
while ( cause != null )
{
if ( errorClass.isInstance( cause ) )
{
contains = true;
break;
}
cause = cause.getCause();
}
return contains;
}
private Throwable traverseToRoot( Throwable error )
{
Throwable potentialRoot = error;
while ( potentialRoot.getCause() != null )
{
potentialRoot = potentialRoot.getCause();
}
return potentialRoot;
}
} }

View File

@ -0,0 +1,80 @@
package org.apache.maven.usability;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed 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.
*/
public final class DiagnosisUtils
{
private DiagnosisUtils()
{
}
public static boolean containsInCausality( Throwable error, Class test )
{
Throwable cause = error;
while( cause != null )
{
if( test.isInstance( cause ) )
{
return true;
}
cause = cause.getCause();
}
return false;
}
public static Throwable getRootCause( Throwable error )
{
Throwable cause = error;
while( true )
{
Throwable nextCause = cause.getCause();
if( nextCause == null )
{
break;
}
else
{
cause = nextCause;
}
}
return cause;
}
public static Throwable getFromCausality( Throwable error, Class targetClass )
{
Throwable cause = error;
while( cause != null )
{
if( targetClass.isInstance( cause ) )
{
return cause;
}
cause = cause.getCause();
}
return null;
}
}

View File

@ -20,6 +20,7 @@ import org.apache.maven.plugin.PluginConfigurationException;
import org.apache.maven.plugin.PluginParameterException; import org.apache.maven.plugin.PluginParameterException;
import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter; import org.apache.maven.plugin.descriptor.Parameter;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -58,7 +59,12 @@ public class PluginConfigurationDiagnoser
{ {
PluginParameterException exception = (PluginParameterException) error; PluginParameterException exception = (PluginParameterException) error;
return buildDiagnosticMessage( exception ); return buildParameterDiagnosticMessage( exception );
}
else if( DiagnosisUtils.containsInCausality(error, ComponentConfigurationException.class ) )
{
ComponentConfigurationException cce = (ComponentConfigurationException) DiagnosisUtils.getFromCausality( error, ComponentConfigurationException.class );
return buildInvalidPluginConfigurationDiagnosisMessage( cce );
} }
else else
{ {
@ -66,7 +72,18 @@ public class PluginConfigurationDiagnoser
} }
} }
private String buildDiagnosticMessage( PluginParameterException exception ) private String buildInvalidPluginConfigurationDiagnosisMessage( ComponentConfigurationException cce )
{
StringBuffer message = new StringBuffer();
message.append( "Either your POM or one of its ancestors has declared an invalid plugin configuration.\n" )
.append( "The error message is:\n\n" )
.append( cce.getMessage() ).append( "\n" );
return message.toString();
}
private String buildParameterDiagnosticMessage( PluginParameterException exception )
{ {
StringBuffer messageBuffer = new StringBuffer(); StringBuffer messageBuffer = new StringBuffer();

View File

@ -17,11 +17,14 @@ package org.apache.maven.usability;
*/ */
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.maven.plugin.PluginConfigurationException;
import org.apache.maven.plugin.PluginParameterException; import org.apache.maven.plugin.PluginParameterException;
import org.apache.maven.plugin.descriptor.DuplicateParameterException; import org.apache.maven.plugin.descriptor.DuplicateParameterException;
import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter; import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -30,6 +33,8 @@ import java.util.List;
public class PluginErrorDiagnoserTest public class PluginErrorDiagnoserTest
extends TestCase extends TestCase
{ {
private PluginConfigurationDiagnoser diagnoser = new PluginConfigurationDiagnoser();
private PluginParameterException buildException( String prefix, String goal, List params ) private PluginParameterException buildException( String prefix, String goal, List params )
throws DuplicateParameterException throws DuplicateParameterException
@ -49,6 +54,22 @@ public class PluginErrorDiagnoserTest
return new PluginParameterException( mojoDescriptor, params ); return new PluginParameterException( mojoDescriptor, params );
} }
public void testShouldDiagnoseInvalidPluginConfiguration()
{
printMethodHeader();
ComponentConfigurationException cce = new ComponentConfigurationException( "Class \'org.apache.maven.plugin.jar.JarMojo\' does not contain a field named \'addClasspath\'" );
PluginConfigurationException pce = new PluginConfigurationException( "test", cce );
assertTrue( diagnoser.canDiagnose( pce ) );
String userMessage = diagnoser.diagnose( pce );
System.out.println( userMessage );
assertNotNull( userMessage );
}
public void testShouldBeAbleToDiagnosePluginParameterExceptions() public void testShouldBeAbleToDiagnosePluginParameterExceptions()
throws DuplicateParameterException throws DuplicateParameterException
@ -61,7 +82,7 @@ public class PluginErrorDiagnoserTest
PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
assertTrue( new PluginConfigurationDiagnoser().canDiagnose( error ) ); assertTrue( diagnoser.canDiagnose( error ) );
} }
public void testParamWithOneReportsExpressionAndOneProjectBasedExpression() public void testParamWithOneReportsExpressionAndOneProjectBasedExpression()
@ -93,7 +114,7 @@ public class PluginErrorDiagnoserTest
PluginParameterException error = buildException( "test", "test", params ); PluginParameterException error = buildException( "test", "test", params );
String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); String userMessage = diagnoser.diagnose( error );
System.out.println( userMessage ); System.out.println( userMessage );
@ -113,7 +134,7 @@ public class PluginErrorDiagnoserTest
PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); String userMessage = diagnoser.diagnose( error );
System.out.println( userMessage ); System.out.println( userMessage );
@ -132,7 +153,7 @@ public class PluginErrorDiagnoserTest
PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); String userMessage = diagnoser.diagnose( error );
System.out.println( userMessage ); System.out.println( userMessage );
@ -152,7 +173,7 @@ public class PluginErrorDiagnoserTest
PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); String userMessage = diagnoser.diagnose( error );
System.out.println( userMessage ); System.out.println( userMessage );
@ -172,7 +193,7 @@ public class PluginErrorDiagnoserTest
PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); String userMessage = diagnoser.diagnose( error );
System.out.println( userMessage ); System.out.println( userMessage );
@ -192,7 +213,7 @@ public class PluginErrorDiagnoserTest
PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); String userMessage = diagnoser.diagnose( error );
System.out.println( userMessage ); System.out.println( userMessage );
@ -212,7 +233,7 @@ public class PluginErrorDiagnoserTest
PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) ); PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
String userMessage = new PluginConfigurationDiagnoser().diagnose( error ); String userMessage = diagnoser.diagnose( error );
System.out.println( userMessage ); System.out.println( userMessage );