o Added runtime exception to construction of DefaultArtifact, when one or more of the four attributes required for object identity are null

o Created corresponding runtime exception: InvalidArtifactRTException
o Added error diagnoser for InvalidArtifactRTException
o Changed logError() in DefaultMaven to use error diagnosers (even the devs could use a hand!)
o Added unit test for InvalidArtifactDiagnoser.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@179265 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-05-31 19:31:58 +00:00
parent f1f5758d8a
commit 23f82d0199
6 changed files with 302 additions and 32 deletions

View File

@ -69,29 +69,6 @@ public class DefaultArtifact
String type,
String classifier )
{
// These should help us catch coding errors until this code gets a whole lot clearer
if( groupId == null )
{
throw new NullPointerException( "Artifact groupId cannot be null." );
}
if( artifactId == null )
{
throw new NullPointerException( "Artifact artifactId cannot be null." );
}
// From here at least we can report the g:a
if ( type == null )
{
throw new NullPointerException( "Artifact type cannot be null for " + groupId + ":" + artifactId );
}
if( version == null )
{
throw new NullPointerException( "Artifact version cannot be null for " + groupId + ":" + artifactId );
}
this.groupId = groupId;
this.artifactId = artifactId;
@ -103,6 +80,36 @@ public class DefaultArtifact
this.scope = scope;
this.classifier = classifier;
validateIdentity();
}
private void validateIdentity()
{
if( empty( groupId ) )
{
throw new InvalidArtifactRTException( groupId, artifactId, version, type, "The groupId cannot be empty." );
}
if( artifactId == null )
{
throw new InvalidArtifactRTException( groupId, artifactId, version, type, "The artifactId cannot be empty." );
}
if ( type == null )
{
throw new InvalidArtifactRTException( groupId, artifactId, version, type, "The type cannot be empty." );
}
if( version == null )
{
throw new InvalidArtifactRTException( groupId, artifactId, version, type, "The version cannot be empty." );
}
}
private boolean empty( String value )
{
return value == null || value.trim().length() < 1;
}
public DefaultArtifact( String groupId, String artifactId, String version, String scope, String type )

View File

@ -0,0 +1,85 @@
package org.apache.maven.artifact;
/*
* 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 class InvalidArtifactRTException
extends RuntimeException
{
private final String groupId;
private final String artifactId;
private final String version;
private final String type;
private final String baseMessage;
public InvalidArtifactRTException( String groupId, String artifactId, String version, String type, String message )
{
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.type = type;
this.baseMessage = message;
}
public InvalidArtifactRTException( String groupId, String artifactId, String version, String type, String message, Throwable cause )
{
super( cause );
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.type = type;
this.baseMessage = message;
}
public String getMessage()
{
return "For artifact {" + getArtifactKey() + "}: " + getBaseMessage();
}
public String getBaseMessage()
{
return baseMessage;
}
public String getArtifactId()
{
return artifactId;
}
public String getGroupId()
{
return groupId;
}
public String getType()
{
return type;
}
public String getVersion()
{
return version;
}
public String getArtifactKey()
{
return groupId + ":" + artifactId + ":" + version + ":" + type;
}
}

View File

@ -380,7 +380,32 @@ public class DefaultMaven
getLogger().error( "BUILD ERROR" );
line();
Throwable error = r.getException();
String message = null;
if ( errorDiagnosers != null )
{
for ( Iterator it = errorDiagnosers.values().iterator(); it.hasNext(); )
{
ErrorDiagnoser diagnoser = (ErrorDiagnoser) it.next();
if ( diagnoser.canDiagnose( error ) )
{
message = diagnoser.diagnose( error );
}
}
}
if ( message == null )
{
message = error.getMessage();
}
getLogger().info( "Diagnosis: " + message );
line();
getLogger().error( "Cause: ", r.getException() );
line();
@ -390,7 +415,7 @@ public class DefaultMaven
line();
}
protected void logFailure( MavenExecutionResponse r, Throwable e, String longMessage )
protected void logFailure( MavenExecutionResponse r, Throwable error, String longMessage )
{
line();
@ -405,16 +430,16 @@ public class DefaultMaven
{
ErrorDiagnoser diagnoser = (ErrorDiagnoser) it.next();
if ( diagnoser.canDiagnose( e ) )
if ( diagnoser.canDiagnose( error ) )
{
message = diagnoser.diagnose( e );
message = diagnoser.diagnose( error );
}
}
}
if ( message == null )
{
message = "Reason: " + e.getMessage();
message = "Reason: " + error.getMessage();
}
getLogger().info( message );
@ -431,7 +456,7 @@ public class DefaultMaven
// TODO: needs to honour -e
if ( getLogger().isDebugEnabled() )
{
getLogger().debug( "Trace", e );
getLogger().debug( "Trace", error );
line();
}

View File

@ -0,0 +1,61 @@
package org.apache.maven.usability;
import org.apache.maven.artifact.InvalidArtifactRTException;
/*
* 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 class InvalidArtifactDiagnoser
implements ErrorDiagnoser
{
public boolean canDiagnose( Throwable error )
{
return error instanceof InvalidArtifactRTException;
}
public String diagnose( Throwable error )
{
StringBuffer diagnosis = new StringBuffer();
InvalidArtifactRTException e = (InvalidArtifactRTException) error;
diagnosis.append( "An invalid artifact was detected.\n\n" )
.append( "This artifact might be in your project's POM, ")
.append( "or it might have been included transitively during the resolution process. ")
.append( "Here is the information we do have for this artifact:\n")
.append( "\n o GroupID: ").append( maybeFlag( e.getGroupId() ) )
.append( "\n o ArtifactID: ").append( maybeFlag( e.getArtifactId() ) )
.append( "\n o Version: ").append( maybeFlag( e.getVersion() ) )
.append( "\n o Type: ").append( maybeFlag( e.getType() ) )
.append( "\n" );
return diagnosis.toString();
}
private String maybeFlag( String value )
{
if( value == null || value.trim().length() < 1 )
{
return "<<< MISSING >>>";
}
else
{
return value;
}
}
}

View File

@ -26,24 +26,34 @@
</component>
<!--
|
|
|PluginConfigurationDiagnoser
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role-hint>plugin-configuration</role-hint>
<role-hint>PluginConfigurationDiagnoser</role-hint>
<implementation>org.apache.maven.usability.PluginConfigurationDiagnoser</implementation>
</component>
<!--
|
|
|ArtifactResolverDiagnoser
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role-hint>artifact-resolution</role-hint>
<role-hint>ArtifactResolverDiagnoser</role-hint>
<implementation>org.apache.maven.usability.ArtifactResolverDiagnoser</implementation>
</component>
<!--
|
|InvalidArtifactDiagnoser
|
-->
<component>
<role>org.apache.maven.usability.ErrorDiagnoser</role>
<role-hint>InvalidArtifactDiagnoser</role-hint>
<implementation>org.apache.maven.usability.InvalidArtifactDiagnoser</implementation>
</component>
<!--
|
|

View File

@ -0,0 +1,82 @@
package org.apache.maven.usability;
import org.apache.maven.artifact.DefaultArtifact;
import junit.framework.TestCase;
/*
* 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 class InvalidArtifactDiagnoserTest
extends TestCase
{
private InvalidArtifactDiagnoser diagnoser = new InvalidArtifactDiagnoser();
public void testShouldDiagnoseArtifactWithMissingGroupId() throws Throwable
{
testDiagnosis( "Test diagnosis for missing groupId", null, "test-artifact", "1.0", "jar" );
}
public void testShouldDiagnoseArtifactWithMissingArtifactId() throws Throwable
{
testDiagnosis( "Test diagnosis for missing artifactId", "test.group.id", null, "1.0", "jar" );
}
public void testShouldDiagnoseArtifactWithMissingVersion() throws Throwable
{
testDiagnosis( "Test diagnosis for missing version", "test.group.id", "test-artifact", null, "jar" );
}
public void testShouldDiagnoseArtifactWithMissingType() throws Throwable
{
testDiagnosis( "Test diagnosis for missing type", "test.group.id", "test-artifact", "1.0", null );
}
public void testShouldDiagnoseArtifactWithMissingGroupIdAndArtifactId() throws Throwable
{
testDiagnosis( "Test diagnosis for missing groupId and artifactId", null, null, "1.0", "jar" );
}
private void testDiagnosis( String testHeader, String groupId, String artifactId, String version, String type )
throws Throwable
{
System.out.println( "------------------------------------------------------------" );
System.out.println( "| " + testHeader );
System.out.println( "------------------------------------------------------------" );
System.out.println();
try
{
new DefaultArtifact( groupId, artifactId, version, type );
fail( "artifact creation did not fail; nothing to diagnose." );
}
catch ( Throwable error )
{
assertTrue( "Unexpected error while constructing artifact: " + error, diagnoser.canDiagnose( error ) );
if ( diagnoser.canDiagnose( error ) )
{
System.out.println( diagnoser.diagnose( error ) );
}
else
{
throw error;
}
}
}
}