You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by jd...@apache.org on 2005/05/31 21:31:59 UTC

svn commit: r179265 - /maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact /maven/components/trunk/maven-core/src/main/java/org/apache/maven /maven/components/trunk/maven-core/src/main/java/org/apache/maven/usability /maven/components/trunk/maven-core/src/main/resources/META-INF/plexus /maven/components/trunk/maven-core/src/test/java/org/apache/maven/usability

Author: jdcasey
Date: Tue May 31 12:31:58 2005
New Revision: 179265

URL: http://svn.apache.org/viewcvs?rev=179265&view=rev
Log:
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.

Added:
    maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/InvalidArtifactRTException.java   (with props)
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/usability/InvalidArtifactDiagnoser.java   (with props)
    maven/components/trunk/maven-core/src/test/java/org/apache/maven/usability/InvalidArtifactDiagnoserTest.java   (with props)
Modified:
    maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
    maven/components/trunk/maven-core/src/main/resources/META-INF/plexus/components.xml

Modified: maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java?rev=179265&r1=179264&r2=179265&view=diff
==============================================================================
--- maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java (original)
+++ maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java Tue May 31 12:31:58 2005
@@ -69,40 +69,47 @@
                             String type,
                             String classifier )
     {
-        // These should help us catch coding errors until this code gets a whole lot clearer
-        if( groupId == null )
+        this.groupId = groupId;
+
+        this.artifactId = artifactId;
+
+        this.version = version;
+
+        this.type = type;
+
+        this.scope = scope;
+
+        this.classifier = classifier;
+        
+        validateIdentity();
+    }
+
+    private void validateIdentity()
+    {
+        if( empty( groupId ) )
         {
-            throw new NullPointerException( "Artifact groupId cannot be null." );
+            throw new InvalidArtifactRTException( groupId, artifactId, version, type, "The groupId cannot be empty." );
         }
 
         if( artifactId == null )
         {
-            throw new NullPointerException( "Artifact artifactId cannot be null." );
+            throw new InvalidArtifactRTException( groupId, artifactId, version, type, "The artifactId cannot be empty." );
         }
 
-        // From here at least we can report the g:a
-
         if ( type == null )
         {
-            throw new NullPointerException( "Artifact type cannot be null for " + groupId + ":" + artifactId );
+            throw new InvalidArtifactRTException( groupId, artifactId, version, type, "The type cannot be empty." );
         }
 
         if( version == null )
         {
-            throw new NullPointerException( "Artifact version cannot be null for " + groupId + ":" + artifactId );
+            throw new InvalidArtifactRTException( groupId, artifactId, version, type, "The version cannot be empty." );
         }
+    }
 
-        this.groupId = groupId;
-
-        this.artifactId = artifactId;
-
-        this.version = version;
-
-        this.type = type;
-
-        this.scope = scope;
-
-        this.classifier = classifier;
+    private boolean empty( String value )
+    {
+        return value == null || value.trim().length() < 1;
     }
 
     public DefaultArtifact( String groupId, String artifactId, String version, String scope, String type )

Added: maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/InvalidArtifactRTException.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/InvalidArtifactRTException.java?rev=179265&view=auto
==============================================================================
--- maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/InvalidArtifactRTException.java (added)
+++ maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/InvalidArtifactRTException.java Tue May 31 12:31:58 2005
@@ -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;
+    }
+
+}

Propchange: maven/components/trunk/maven-artifact/src/main/java/org/apache/maven/artifact/InvalidArtifactRTException.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author"

Modified: maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java?rev=179265&r1=179264&r2=179265&view=diff
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java (original)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java Tue May 31 12:31:58 2005
@@ -380,7 +380,32 @@
         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 @@
         line();
     }
 
-    protected void logFailure( MavenExecutionResponse r, Throwable e, String longMessage )
+    protected void logFailure( MavenExecutionResponse r, Throwable error, String longMessage )
     {
         line();
 
@@ -405,16 +430,16 @@
             {
                 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 @@
         // TODO: needs to honour -e
         if ( getLogger().isDebugEnabled() )
         {
-            getLogger().debug( "Trace", e );
+            getLogger().debug( "Trace", error );
 
             line();
         }

Added: maven/components/trunk/maven-core/src/main/java/org/apache/maven/usability/InvalidArtifactDiagnoser.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core/src/main/java/org/apache/maven/usability/InvalidArtifactDiagnoser.java?rev=179265&view=auto
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/usability/InvalidArtifactDiagnoser.java (added)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/usability/InvalidArtifactDiagnoser.java Tue May 31 12:31:58 2005
@@ -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;
+        }
+    }
+
+}

Propchange: maven/components/trunk/maven-core/src/main/java/org/apache/maven/usability/InvalidArtifactDiagnoser.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author"

Modified: maven/components/trunk/maven-core/src/main/resources/META-INF/plexus/components.xml
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core/src/main/resources/META-INF/plexus/components.xml?rev=179265&r1=179264&r2=179265&view=diff
==============================================================================
--- maven/components/trunk/maven-core/src/main/resources/META-INF/plexus/components.xml (original)
+++ maven/components/trunk/maven-core/src/main/resources/META-INF/plexus/components.xml Tue May 31 12:31:58 2005
@@ -26,23 +26,33 @@
     </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>
     <!--
      |

Added: maven/components/trunk/maven-core/src/test/java/org/apache/maven/usability/InvalidArtifactDiagnoserTest.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core/src/test/java/org/apache/maven/usability/InvalidArtifactDiagnoserTest.java?rev=179265&view=auto
==============================================================================
--- maven/components/trunk/maven-core/src/test/java/org/apache/maven/usability/InvalidArtifactDiagnoserTest.java (added)
+++ maven/components/trunk/maven-core/src/test/java/org/apache/maven/usability/InvalidArtifactDiagnoserTest.java Tue May 31 12:31:58 2005
@@ -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;
+            }
+        }
+    }
+}

Propchange: maven/components/trunk/maven-core/src/test/java/org/apache/maven/usability/InvalidArtifactDiagnoserTest.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author"



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org