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.File;
22 import java.io.IOException;
23 import java.net.ConnectException;
24 import java.util.List;
25 import java.util.concurrent.atomic.AtomicReference;
26
27 import org.apache.maven.model.Plugin;
28 import org.apache.maven.model.building.DefaultModelProblem;
29 import org.apache.maven.model.building.ModelProblem;
30 import org.apache.maven.plugin.MojoExecution;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.PluginContainerException;
33 import org.apache.maven.plugin.PluginExecutionException;
34 import org.apache.maven.plugin.descriptor.MojoDescriptor;
35 import org.apache.maven.plugin.descriptor.PluginDescriptor;
36 import org.apache.maven.project.ProjectBuildingException;
37 import org.apache.maven.project.ProjectBuildingResult;
38 import org.eclipse.aether.artifact.DefaultArtifact;
39 import org.eclipse.aether.repository.RemoteRepository;
40 import org.eclipse.aether.resolution.ArtifactResolutionException;
41 import org.eclipse.aether.resolution.ArtifactResult;
42 import org.eclipse.aether.transfer.ArtifactFilteredOutException;
43 import org.junit.jupiter.api.Test;
44
45 import static org.junit.jupiter.api.Assertions.assertEquals;
46 import static org.junit.jupiter.api.Assertions.assertTrue;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.when;
49
50
51
52 class DefaultExceptionHandlerTest {
53
54
55
56
57
58
59
60
61
62
63 @Test
64 void testJdk7ipv6() {
65 ConnectException connEx = new ConnectException("Connection refused: connect");
66 IOException ioEx = new IOException("Unable to establish loopback connection", connEx);
67 MojoExecutionException mojoEx =
68 new MojoExecutionException("Error executing Jetty: Unable to establish loopback connection", ioEx);
69
70 ExceptionHandler exceptionHandler = new DefaultExceptionHandler();
71 ExceptionSummary exceptionSummary = exceptionHandler.handleException(mojoEx);
72
73 String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/ConnectException";
74 assertEquals(expectedReference, exceptionSummary.getReference());
75 }
76
77 @Test
78 void testHandleExceptionAetherClassNotFound() {
79 Throwable cause2 = new NoClassDefFoundError("org/sonatype/aether/RepositorySystem");
80 Plugin plugin = new Plugin();
81 Exception cause = new PluginContainerException(plugin, null, null, cause2);
82 PluginDescriptor pluginDescriptor = new PluginDescriptor();
83 MojoDescriptor mojoDescriptor = new MojoDescriptor();
84 mojoDescriptor.setPluginDescriptor(pluginDescriptor);
85 MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);
86 Throwable exception = new PluginExecutionException(mojoExecution, null, cause);
87
88 DefaultExceptionHandler handler = new DefaultExceptionHandler();
89 ExceptionSummary summary = handler.handleException(exception);
90
91 String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/AetherClassNotFound";
92 assertEquals(expectedReference, summary.getReference());
93 }
94
95 @Test
96 void testHandleExceptionNoClassDefFoundErrorNull() {
97 Throwable cause2 = new NoClassDefFoundError();
98 Plugin plugin = new Plugin();
99 Exception cause = new PluginContainerException(plugin, null, null, cause2);
100 PluginDescriptor pluginDescriptor = new PluginDescriptor();
101 MojoDescriptor mojoDescriptor = new MojoDescriptor();
102 mojoDescriptor.setPluginDescriptor(pluginDescriptor);
103 MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);
104 Throwable exception = new PluginExecutionException(mojoExecution, null, cause);
105
106 DefaultExceptionHandler handler = new DefaultExceptionHandler();
107 ExceptionSummary summary = handler.handleException(exception);
108
109 String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException";
110 assertEquals(expectedReference, summary.getReference());
111 }
112
113 @Test
114 void testHandleExceptionLoopInCause() {
115
116 AtomicReference<Throwable> causeRef = new AtomicReference<>(null);
117 Exception cause2 = new RuntimeException("loop") {
118 @Override
119 public synchronized Throwable getCause() {
120 return causeRef.get();
121 }
122 };
123 causeRef.set(cause2);
124
125 Plugin plugin = new Plugin();
126 Exception cause = new PluginContainerException(plugin, null, null, cause2);
127 cause2.initCause(cause);
128 PluginDescriptor pluginDescriptor = new PluginDescriptor();
129 MojoDescriptor mojoDescriptor = new MojoDescriptor();
130 mojoDescriptor.setPluginDescriptor(pluginDescriptor);
131 MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);
132 Throwable exception = new PluginExecutionException(mojoExecution, null, cause);
133
134 DefaultExceptionHandler handler = new DefaultExceptionHandler();
135 ExceptionSummary summary = handler.handleException(exception);
136
137 String expectedReference = "http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException";
138 assertEquals(expectedReference, summary.getReference());
139 }
140
141 @Test
142 void testArtifactFilteredOutException() {
143 RemoteRepository repo =
144 new RemoteRepository.Builder("my-repo", "default", "https://repo.example.com/maven").build();
145 ArtifactFilteredOutException filterEx = new ArtifactFilteredOutException(
146 new DefaultArtifact("com.example:my-lib:jar:1.0"),
147 repo,
148 "Prefix com/example/my-lib/1.0/my-lib-1.0.jar NOT allowed from my-repo"
149 + " (https://repo.example.com/maven, default, releases)");
150 ArtifactResult artifactResult = new ArtifactResult(new org.eclipse.aether.resolution.ArtifactRequest(
151 new DefaultArtifact("com.example:my-lib:jar:1.0"), java.util.List.of(repo), null));
152 artifactResult.addException(filterEx);
153 ArtifactResolutionException resolutionEx =
154 new ArtifactResolutionException(java.util.List.of(artifactResult), "Could not resolve artifact");
155 MojoExecutionException mojoEx = new MojoExecutionException("Resolution failed", resolutionEx);
156
157 DefaultExceptionHandler handler = new DefaultExceptionHandler();
158 ExceptionSummary summary = handler.handleException(mojoEx);
159
160 assertTrue(
161 summary.getMessage().contains("-Daether.remoteRepositoryFilter.prefixes=false"),
162 "Message should contain the prefixes workaround property");
163 assertTrue(
164 summary.getMessage().contains("-Daether.remoteRepositoryFilter.groupId=false"),
165 "Message should contain the groupId workaround property");
166 assertTrue(
167 summary.getMessage().contains("remote repository filter"),
168 "Message should explain the filtering cause");
169 assertEquals(
170 "https://maven.apache.org/resolver/remote-repository-filtering.html",
171 summary.getReference(),
172 "Reference should point to the RRF documentation");
173 }
174
175 @Test
176 void testHandleExceptionSelfReferencing() {
177 RuntimeException boom3 = new RuntimeException("BOOM3");
178 RuntimeException boom2 = new RuntimeException("BOOM2", boom3);
179 RuntimeException boom1 = new RuntimeException("BOOM1", boom2);
180 boom3.initCause(boom1);
181
182 DefaultExceptionHandler handler = new DefaultExceptionHandler();
183 ExceptionSummary summary = handler.handleException(boom1);
184
185 assertEquals("BOOM1: BOOM2: BOOM3: [CIRCULAR REFERENCE]", summary.getMessage());
186 assertEquals("", summary.getReference());
187 assertEquals(0, summary.getChildren().size());
188 assertEquals(boom1, summary.getException());
189 }
190
191 @Test
192 void testProjectBuildingExceptionNotRenderedTwice() {
193 ModelProblem problem1 = new DefaultModelProblem(
194 "Malformed POM test.xml: unexpected element",
195 ModelProblem.Severity.FATAL,
196 null,
197 "test.xml",
198 8,
199 3,
200 null,
201 null);
202
203 ModelProblem problem2 = new DefaultModelProblem(
204 "Malformed POM test.xml: missing artifactId",
205 ModelProblem.Severity.ERROR,
206 null,
207 "test.xml",
208 12,
209 5,
210 null,
211 null);
212
213 ProjectBuildingResult result = mock(ProjectBuildingResult.class);
214 when(result.getProjectId()).thenReturn("test:fail-build:0.1-SNAPSHOT");
215 when(result.getPomFile()).thenReturn(new File("test.xml"));
216 when(result.getProblems()).thenReturn(List.of(problem1, problem2));
217
218 ProjectBuildingException exception = new ProjectBuildingException(List.of(result));
219
220 assertEquals(
221 "Encountered 2 problem(s) while processing the POMs",
222 exception.getMessage(),
223 "exception message must report the total number of problems");
224
225 ExceptionSummary summary = new DefaultExceptionHandler().handleException(exception);
226
227 ExceptionSummary projectSummary = summary.getChildren().get(0);
228 ExceptionSummary problemSummary = projectSummary.getChildren().get(0);
229
230 assertTrue(
231 problemSummary.getMessage().contains("Malformed POM test.xml"),
232 "the handler's own tree should still render the problem detail exactly once");
233
234 assertEquals(1, countOccurrences(flatten(summary), "Malformed POM test.xml: unexpected element"));
235 }
236
237 private static String flatten(ExceptionSummary summary) {
238 if (summary == null) {
239 return "";
240 }
241
242 StringBuilder sb = new StringBuilder();
243 if (summary.getMessage() != null) {
244 sb.append(summary.getMessage());
245 }
246
247 for (ExceptionSummary child : summary.getChildren()) {
248 if (!sb.isEmpty()) {
249 sb.append('\n');
250 }
251 sb.append(flatten(child));
252 }
253
254 return sb.toString();
255 }
256
257 private static int countOccurrences(String text, String substring) {
258 int count = 0;
259 int index = 0;
260
261 while ((index = text.indexOf(substring, index)) != -1) {
262 count++;
263 index += substring.length();
264 }
265
266 return count;
267 }
268 }