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