1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39 public class DefaultExceptionHandlerTest {
40
41
42
43
44
45
46
47
48
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
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 }