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.lifecycle.internal;
20  
21  import javax.inject.Inject;
22  
23  import java.lang.reflect.Field;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.apache.maven.execution.AbstractExecutionListener;
29  import org.apache.maven.execution.DefaultMavenExecutionRequest;
30  import org.apache.maven.execution.DefaultMavenExecutionResult;
31  import org.apache.maven.execution.MavenExecutionRequest;
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.lifecycle.LifecycleExecutionException;
34  import org.apache.maven.lifecycle.internal.stub.MojoExecutorStub;
35  import org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub;
36  import org.apache.maven.plugin.MojoExecution;
37  import org.apache.maven.project.MavenProject;
38  import org.codehaus.plexus.PlexusContainer;
39  import org.codehaus.plexus.testing.PlexusTest;
40  import org.eclipse.aether.DefaultRepositorySystemSession;
41  import org.junit.jupiter.api.Test;
42  
43  import static org.junit.jupiter.api.Assertions.assertEquals;
44  import static org.junit.jupiter.api.Assertions.assertNull;
45  
46  @PlexusTest
47  class LifecycleModuleBuilderTest {
48      @Inject
49      PlexusContainer container;
50  
51      @Test
52      void testCurrentProject() throws Exception {
53          List<MavenProject> currentProjects = new ArrayList<>();
54          MojoExecutorStub mojoExecutor = new MojoExecutorStub() {
55              @Override
56              public void execute(MavenSession session, List<MojoExecution> mojoExecutions)
57                      throws LifecycleExecutionException {
58                  super.execute(session, mojoExecutions);
59                  currentProjects.add(session.getCurrentProject());
60              }
61          };
62  
63          final DefaultMavenExecutionResult defaultMavenExecutionResult = new DefaultMavenExecutionResult();
64          MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
65          mavenExecutionRequest.setExecutionListener(new AbstractExecutionListener());
66          mavenExecutionRequest.setGoals(Arrays.asList("clean"));
67          final MavenSession session = new MavenSession(
68                  null,
69                  new DefaultRepositorySystemSession(h -> false),
70                  mavenExecutionRequest,
71                  defaultMavenExecutionResult);
72          final ProjectDependencyGraphStub dependencyGraphStub = new ProjectDependencyGraphStub();
73          session.setProjectDependencyGraph(dependencyGraphStub);
74          session.setProjects(dependencyGraphStub.getSortedProjects());
75  
76          LifecycleModuleBuilder moduleBuilder = container.lookup(LifecycleModuleBuilder.class);
77          set(moduleBuilder, "mojoExecutor", mojoExecutor);
78  
79          LifecycleStarter ls = container.lookup(LifecycleStarter.class);
80          ls.execute(session);
81  
82          assertNull(session.getCurrentProject());
83          assertEquals(
84                  Arrays.asList(
85                          ProjectDependencyGraphStub.A,
86                          ProjectDependencyGraphStub.B,
87                          ProjectDependencyGraphStub.C,
88                          ProjectDependencyGraphStub.X,
89                          ProjectDependencyGraphStub.Y,
90                          ProjectDependencyGraphStub.Z),
91                  currentProjects);
92      }
93  
94      static void set(Object obj, String field, Object v) throws NoSuchFieldException, IllegalAccessException {
95          Field f = obj.getClass().getDeclaredField(field);
96          f.setAccessible(true);
97          f.set(obj, v);
98      }
99  }