View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.exception;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import java.io.IOException;
24  import java.net.ConnectException;
25  import org.apache.maven.model.Plugin;
26  import org.apache.maven.plugin.MojoExecution;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.PluginContainerException;
29  import org.apache.maven.plugin.PluginExecutionException;
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.descriptor.PluginDescriptor;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * @author <a href="mailto:baerrach@apache.org">Barrie Treloar</a>
36   */
37  public class DefaultExceptionHandlerTest {
38      /**
39       * Running Maven under JDK7 may cause connection issues because IPv6 is used by default.
40       * <p>
41       * e.g running mvn site:run will cause Jetty to fail.
42       * </p>
43       * <p>
44       * The resolution is to add -Djava.net.preferIPv4Stack=true to the command line as documented in
45       * http://cwiki.apache.org/confluence/display/MAVEN/ConnectException
46       * </p>
47       */
48      @Test
49      public void testJdk7ipv6() {
50          ConnectException connEx = new ConnectException("Connection refused: connect");
51          IOException ioEx = new IOException("Unable to establish loopback connection", connEx);
52          MojoExecutionException mojoEx =
53                  new MojoExecutionException("Error executing Jetty: Unable to establish loopback connection", ioEx);
54  
55          ExceptionHandler exceptionHandler = new DefaultExceptionHandler();
56          ExceptionSummary exceptionSummary = exceptionHandler.handleException(mojoEx);
57  
58          String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/ConnectException";
59          assertEquals(expectedReference, exceptionSummary.getReference());
60      }
61  
62      @Test
63      public void testHandleExceptionAetherClassNotFound() {
64          Throwable cause2 = new NoClassDefFoundError("org/sonatype/aether/RepositorySystem");
65          Plugin plugin = new Plugin();
66          Exception cause = new PluginContainerException(plugin, null, null, cause2);
67          PluginDescriptor pluginDescriptor = new PluginDescriptor();
68          MojoDescriptor mojoDescriptor = new MojoDescriptor();
69          mojoDescriptor.setPluginDescriptor(pluginDescriptor);
70          MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);
71          Throwable exception = new PluginExecutionException(mojoExecution, null, cause);
72  
73          DefaultExceptionHandler handler = new DefaultExceptionHandler();
74          ExceptionSummary summary = handler.handleException(exception);
75  
76          String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/AetherClassNotFound";
77          assertEquals(expectedReference, summary.getReference());
78      }
79  
80      @Test
81      public void testHandleExceptionNoClassDefFoundErrorNull() {
82          Throwable cause2 = new NoClassDefFoundError();
83          Plugin plugin = new Plugin();
84          Exception cause = new PluginContainerException(plugin, null, null, cause2);
85          PluginDescriptor pluginDescriptor = new PluginDescriptor();
86          MojoDescriptor mojoDescriptor = new MojoDescriptor();
87          mojoDescriptor.setPluginDescriptor(pluginDescriptor);
88          MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);
89          Throwable exception = new PluginExecutionException(mojoExecution, null, cause);
90  
91          DefaultExceptionHandler handler = new DefaultExceptionHandler();
92          ExceptionSummary summary = handler.handleException(exception);
93  
94          String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException";
95          assertEquals(expectedReference, summary.getReference());
96      }
97  }