1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }