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 java.io.IOException;
22  import java.net.ConnectException;
23  import java.util.concurrent.atomic.AtomicReference;
24  
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.Test;
33  
34  import static org.junit.Assert.assertEquals;
35  
36  /**
37   * @author <a href="mailto:baerrach@apache.org">Barrie Treloar</a>
38   */
39  public class DefaultExceptionHandlerTest {
40      /**
41       * Running Maven under JDK7 may cause connection issues because IPv6 is used by default.
42       * <p>
43       * e.g running mvn site:run will cause Jetty to fail.
44       * </p>
45       * <p>
46       * The resolution is to add -Djava.net.preferIPv4Stack=true to the command line as documented in
47       * http://cwiki.apache.org/confluence/display/MAVEN/ConnectException
48       * </p>
49       */
50      @Test
51      public void testJdk7ipv6() {
52          ConnectException connEx = new ConnectException("Connection refused: connect");
53          IOException ioEx = new IOException("Unable to establish loopback connection", connEx);
54          MojoExecutionException mojoEx =
55                  new MojoExecutionException("Error executing Jetty: Unable to establish loopback connection", ioEx);
56  
57          ExceptionHandler exceptionHandler = new DefaultExceptionHandler();
58          ExceptionSummary exceptionSummary = exceptionHandler.handleException(mojoEx);
59  
60          String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/ConnectException";
61          assertEquals(expectedReference, exceptionSummary.getReference());
62      }
63  
64      @Test
65      public void testHandleExceptionAetherClassNotFound() {
66          Throwable cause2 = new NoClassDefFoundError("org/sonatype/aether/RepositorySystem");
67          Plugin plugin = new Plugin();
68          Exception cause = new PluginContainerException(plugin, null, null, cause2);
69          PluginDescriptor pluginDescriptor = new PluginDescriptor();
70          MojoDescriptor mojoDescriptor = new MojoDescriptor();
71          mojoDescriptor.setPluginDescriptor(pluginDescriptor);
72          MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);
73          Throwable exception = new PluginExecutionException(mojoExecution, null, cause);
74  
75          DefaultExceptionHandler handler = new DefaultExceptionHandler();
76          ExceptionSummary summary = handler.handleException(exception);
77  
78          String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/AetherClassNotFound";
79          assertEquals(expectedReference, summary.getReference());
80      }
81  
82      @Test
83      public void testHandleExceptionNoClassDefFoundErrorNull() {
84          Throwable cause2 = new NoClassDefFoundError();
85          Plugin plugin = new Plugin();
86          Exception cause = new PluginContainerException(plugin, null, null, cause2);
87          PluginDescriptor pluginDescriptor = new PluginDescriptor();
88          MojoDescriptor mojoDescriptor = new MojoDescriptor();
89          mojoDescriptor.setPluginDescriptor(pluginDescriptor);
90          MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);
91          Throwable exception = new PluginExecutionException(mojoExecution, null, cause);
92  
93          DefaultExceptionHandler handler = new DefaultExceptionHandler();
94          ExceptionSummary summary = handler.handleException(exception);
95  
96          String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException";
97          assertEquals(expectedReference, summary.getReference());
98      }
99  
100     @Test
101     public void testHandleExceptionLoopInCause() {
102         // Some broken exception that does return "this" as getCause
103         AtomicReference<Throwable> causeRef = new AtomicReference<>(null);
104         Exception cause2 = new RuntimeException("loop") {
105             @Override
106             public synchronized Throwable getCause() {
107                 return causeRef.get();
108             }
109         };
110         causeRef.set(cause2);
111 
112         Plugin plugin = new Plugin();
113         Exception cause = new PluginContainerException(plugin, null, null, cause2);
114         cause2.initCause(cause);
115         PluginDescriptor pluginDescriptor = new PluginDescriptor();
116         MojoDescriptor mojoDescriptor = new MojoDescriptor();
117         mojoDescriptor.setPluginDescriptor(pluginDescriptor);
118         MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);
119         Throwable exception = new PluginExecutionException(mojoExecution, null, cause);
120 
121         DefaultExceptionHandler handler = new DefaultExceptionHandler();
122         ExceptionSummary summary = handler.handleException(exception);
123 
124         String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException";
125         assertEquals(expectedReference, summary.getReference());
126     }
127 }