From 1a18eb6c9c7ac24adc9d57781e85fb3f60b6b65f Mon Sep 17 00:00:00 2001 From: Sergey Chernov Date: Fri, 5 Apr 2019 22:29:33 +0300 Subject: [PATCH] [MNG-6626] fix DefaultExceptionHandler NPE Closes #241 --- .../exception/DefaultExceptionHandler.java | 9 ++-- .../DefaultExceptionHandlerTest.java | 53 +++++++++++++++++-- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java b/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java index 2a86667f16..2193e61a35 100644 --- a/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java +++ b/maven-core/src/main/java/org/apache/maven/exception/DefaultExceptionHandler.java @@ -209,10 +209,13 @@ public class DefaultExceptionHandler { Throwable cause2 = cause.getCause(); - if ( cause2 instanceof NoClassDefFoundError - && cause2.getMessage().contains( "org/sonatype/aether/" ) ) + if ( cause2 instanceof NoClassDefFoundError ) { - reference = "AetherClassNotFound"; + String message = cause2.getMessage(); + if ( message != null && message.contains( "org/sonatype/aether/" ) ) + { + reference = "AetherClassNotFound"; + } } } diff --git a/maven-core/src/test/java/org/apache/maven/exception/DefaultExceptionHandlerTest.java b/maven-core/src/test/java/org/apache/maven/exception/DefaultExceptionHandlerTest.java index 9727bdf8a2..9fe6d4f30f 100644 --- a/maven-core/src/test/java/org/apache/maven/exception/DefaultExceptionHandlerTest.java +++ b/maven-core/src/test/java/org/apache/maven/exception/DefaultExceptionHandlerTest.java @@ -22,15 +22,22 @@ package org.apache.maven.exception; import java.io.IOException; import java.net.ConnectException; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.MojoExecution; import org.apache.maven.plugin.MojoExecutionException; -import junit.framework.TestCase; +import org.apache.maven.plugin.PluginContainerException; +import org.apache.maven.plugin.PluginExecutionException; +import org.apache.maven.plugin.descriptor.MojoDescriptor; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; /** * @author Barrie Treloar */ public class DefaultExceptionHandlerTest - extends TestCase { /** * Running Maven under JDK7 may cause connection issues because IPv6 is used by default. @@ -42,11 +49,11 @@ public class DefaultExceptionHandlerTest * http://cwiki.apache.org/confluence/display/MAVEN/ConnectException *

*/ + @Test public void testJdk7ipv6() { ConnectException connEx = new ConnectException( "Connection refused: connect" ); - IOException ioEx = new IOException( "Unable to establish loopback connection" ); - ioEx.initCause( connEx ); + IOException ioEx = new IOException( "Unable to establish loopback connection", connEx ); MojoExecutionException mojoEx = new MojoExecutionException( "Error executing Jetty: Unable to establish loopback connection", ioEx ); @@ -57,4 +64,42 @@ public class DefaultExceptionHandlerTest assertEquals( expectedReference, exceptionSummary.getReference() ); } + + @Test + public void testHandleExceptionAetherClassNotFound() + { + Throwable cause2 = new NoClassDefFoundError( "org/sonatype/aether/RepositorySystem" ); + Plugin plugin = new Plugin(); + Exception cause = new PluginContainerException( plugin, null, null, cause2 ); + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + MojoDescriptor mojoDescriptor = new MojoDescriptor(); + mojoDescriptor.setPluginDescriptor( pluginDescriptor ); + MojoExecution mojoExecution = new MojoExecution(mojoDescriptor); + Throwable exception = new PluginExecutionException( mojoExecution, null, cause ); + + DefaultExceptionHandler handler = new DefaultExceptionHandler(); + ExceptionSummary summary = handler.handleException( exception ); + + String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/AetherClassNotFound"; + assertEquals( expectedReference, summary.getReference() ); + } + + @Test + public void testHandleExceptionNoClassDefFoundErrorNull() + { + Throwable cause2 = new NoClassDefFoundError(); + Plugin plugin = new Plugin(); + Exception cause = new PluginContainerException( plugin, null, null, cause2 ); + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + MojoDescriptor mojoDescriptor = new MojoDescriptor(); + mojoDescriptor.setPluginDescriptor( pluginDescriptor ); + MojoExecution mojoExecution = new MojoExecution(mojoDescriptor); + Throwable exception = new PluginExecutionException( mojoExecution, null, cause ); + + DefaultExceptionHandler handler = new DefaultExceptionHandler(); + ExceptionSummary summary = handler.handleException( exception ); + + String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException"; + assertEquals( expectedReference, summary.getReference() ); + } }