|
| 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.lifecycle.internal.concurrent; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.apache.maven.execution.DefaultMavenExecutionRequest; |
| 26 | +import org.apache.maven.execution.DefaultMavenExecutionResult; |
| 27 | +import org.apache.maven.execution.MavenExecutionRequest; |
| 28 | +import org.apache.maven.execution.MavenSession; |
| 29 | +import org.apache.maven.execution.ProjectDependencyGraph; |
| 30 | +import org.apache.maven.execution.ProjectExecutionEvent; |
| 31 | +import org.apache.maven.execution.ProjectExecutionListener; |
| 32 | +import org.apache.maven.internal.impl.DefaultLifecycleRegistry; |
| 33 | +import org.apache.maven.internal.transformation.TransformerManager; |
| 34 | +import org.apache.maven.lifecycle.internal.LifecycleTask; |
| 35 | +import org.apache.maven.lifecycle.internal.ReactorBuildStatus; |
| 36 | +import org.apache.maven.lifecycle.internal.ReactorContext; |
| 37 | +import org.apache.maven.lifecycle.internal.TaskSegment; |
| 38 | +import org.apache.maven.lifecycle.internal.stub.ExecutionEventCatapultStub; |
| 39 | +import org.apache.maven.project.MavenProject; |
| 40 | +import org.eclipse.aether.DefaultRepositorySystemSession; |
| 41 | +import org.eclipse.aether.RepositorySystemSession; |
| 42 | +import org.eclipse.aether.deployment.DeployRequest; |
| 43 | +import org.eclipse.aether.installation.InstallRequest; |
| 44 | +import org.junit.jupiter.api.Test; |
| 45 | + |
| 46 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 48 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 49 | + |
| 50 | +class BuildPlanExecutorTest { |
| 51 | + |
| 52 | + /** |
| 53 | + * A build step that throws an {@link Error} must be reported as a build failure, the same way the |
| 54 | + * single threaded builder reports it. Otherwise the build ends with no exception at all and Maven |
| 55 | + * prints BUILD SUCCESS while nothing was built. |
| 56 | + */ |
| 57 | + @Test |
| 58 | + void errorThrownByBuildStepIsRecordedAsBuildFailure() throws Exception { |
| 59 | + Error thrown = new NoClassDefFoundError("some/Class"); |
| 60 | + MavenProject project = newProject(); |
| 61 | + MavenSession session = newSession(project); |
| 62 | + |
| 63 | + execute(session, project, event -> { |
| 64 | + throw thrown; |
| 65 | + }); |
| 66 | + |
| 67 | + List<Throwable> exceptions = session.getResult().getExceptions(); |
| 68 | + assertEquals(1, exceptions.size(), "expected the error to be recorded, but got: " + exceptions); |
| 69 | + assertSame(thrown, exceptions.get(0)); |
| 70 | + assertTrue(session.getResult().getBuildSummary(project) instanceof org.apache.maven.execution.BuildFailure); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * The same for an exception, which already worked. This pins the existing behaviour so the widened |
| 75 | + * catch does not change it. |
| 76 | + */ |
| 77 | + @Test |
| 78 | + void exceptionThrownByBuildStepIsRecordedAsBuildFailure() throws Exception { |
| 79 | + RuntimeException thrown = new IllegalStateException("nope"); |
| 80 | + MavenProject project = newProject(); |
| 81 | + MavenSession session = newSession(project); |
| 82 | + |
| 83 | + execute(session, project, event -> { |
| 84 | + throw thrown; |
| 85 | + }); |
| 86 | + |
| 87 | + List<Throwable> exceptions = session.getResult().getExceptions(); |
| 88 | + assertEquals(1, exceptions.size(), "expected the exception to be recorded, but got: " + exceptions); |
| 89 | + assertSame(thrown, exceptions.get(0)); |
| 90 | + assertTrue(session.getResult().getBuildSummary(project) instanceof org.apache.maven.execution.BuildFailure); |
| 91 | + } |
| 92 | + |
| 93 | + private void execute(MavenSession session, MavenProject project, BeforeProjectExecution listener) throws Exception { |
| 94 | + ReactorContext reactorContext = new ReactorContext( |
| 95 | + session.getResult(), |
| 96 | + Thread.currentThread().getContextClassLoader(), |
| 97 | + new ReactorBuildStatus(session.getProjectDependencyGraph())); |
| 98 | + TaskSegment taskSegment = new TaskSegment(false); |
| 99 | + taskSegment.getTasks().add(new LifecycleTask("validate")); |
| 100 | + newExecutor(listener).execute(session, reactorContext, List.of(taskSegment)); |
| 101 | + } |
| 102 | + |
| 103 | + private BuildPlanExecutor newExecutor(ProjectExecutionListener listener) { |
| 104 | + return new BuildPlanExecutor( |
| 105 | + null, |
| 106 | + new ExecutionEventCatapultStub(), |
| 107 | + List.of(listener), |
| 108 | + new NoopTransformerManager(), |
| 109 | + new BuildPlanLogger(), |
| 110 | + Map.of(), |
| 111 | + null, |
| 112 | + null, |
| 113 | + new DefaultLifecycleRegistry(Collections.emptyList())); |
| 114 | + } |
| 115 | + |
| 116 | + private MavenProject newProject() { |
| 117 | + MavenProject result = new MavenProject(); |
| 118 | + result.setArtifactId("a"); |
| 119 | + result.setCollectedProjects(List.of()); |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + private MavenSession newSession(MavenProject project) { |
| 124 | + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); |
| 125 | + request.setGoals(List.of("validate")); |
| 126 | + MavenSession result = new MavenSession( |
| 127 | + null, new DefaultRepositorySystemSession(h -> false), request, new DefaultMavenExecutionResult()); |
| 128 | + result.setProjectDependencyGraph(new SingleProjectDependencyGraph(project)); |
| 129 | + result.setProjects(List.of(project)); |
| 130 | + return result; |
| 131 | + } |
| 132 | + |
| 133 | + private static final class SingleProjectDependencyGraph implements ProjectDependencyGraph { |
| 134 | + |
| 135 | + private final List<MavenProject> projects; |
| 136 | + |
| 137 | + private SingleProjectDependencyGraph(MavenProject project) { |
| 138 | + this.projects = List.of(project); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public List<MavenProject> getAllProjects() { |
| 143 | + return projects; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public List<MavenProject> getSortedProjects() { |
| 148 | + return projects; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public List<MavenProject> getDownstreamProjects(MavenProject project, boolean transitive) { |
| 153 | + return List.of(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive) { |
| 158 | + return List.of(); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private static final class NoopTransformerManager implements TransformerManager { |
| 163 | + |
| 164 | + @Override |
| 165 | + public InstallRequest remapInstallArtifacts(RepositorySystemSession session, InstallRequest request) { |
| 166 | + return request; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public DeployRequest remapDeployArtifacts(RepositorySystemSession session, DeployRequest request) { |
| 171 | + return request; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void injectTransformedArtifacts(RepositorySystemSession repositorySession, MavenProject project) {} |
| 176 | + } |
| 177 | + |
| 178 | + @FunctionalInterface |
| 179 | + private interface BeforeProjectExecution extends ProjectExecutionListener { |
| 180 | + |
| 181 | + @Override |
| 182 | + void beforeProjectExecution(ProjectExecutionEvent event); |
| 183 | + |
| 184 | + @Override |
| 185 | + default void beforeProjectLifecycleExecution(ProjectExecutionEvent event) {} |
| 186 | + |
| 187 | + @Override |
| 188 | + default void afterProjectExecutionSuccess(ProjectExecutionEvent event) {} |
| 189 | + |
| 190 | + @Override |
| 191 | + default void afterProjectExecutionFailure(ProjectExecutionEvent event) {} |
| 192 | + } |
| 193 | +} |
0 commit comments