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