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 org.apache.maven.AbstractCoreMavenComponentTestCase;
22  import org.apache.maven.execution.MavenSession;
23  import org.apache.maven.lifecycle.MavenExecutionPlan;
24  import org.apache.maven.lifecycle.internal.stub.BuildPluginManagerStub;
25  import org.apache.maven.lifecycle.internal.stub.DefaultLifecyclesStub;
26  import org.apache.maven.lifecycle.internal.stub.PluginPrefixResolverStub;
27  import org.apache.maven.lifecycle.internal.stub.PluginVersionResolverStub;
28  import org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub;
29  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  
34  /**
35   */
36  class LifecycleExecutionPlanCalculatorTest extends AbstractCoreMavenComponentTestCase {
37  
38      @Test
39      void testCalculateExecutionPlanWithGoalTasks() throws Exception {
40          MojoDescriptorCreator mojoDescriptorCreator = createMojoDescriptorCreator();
41          LifecycleExecutionPlanCalculator lifecycleExecutionPlanCalculator =
42                  createExecutionPlaceCalculator(mojoDescriptorCreator);
43  
44          final GoalTask goalTask1 = new GoalTask("compiler:compile");
45          final GoalTask goalTask2 = new GoalTask("surefire:test");
46          final TaskSegment taskSegment1 = new TaskSegment(false, goalTask1, goalTask2);
47          final MavenSession session1 = ProjectDependencyGraphStub.getMavenSession(ProjectDependencyGraphStub.A);
48  
49          MavenExecutionPlan executionPlan = lifecycleExecutionPlanCalculator.calculateExecutionPlan(
50                  session1, ProjectDependencyGraphStub.A, taskSegment1.getTasks());
51          assertEquals(2, executionPlan.size());
52  
53          final GoalTask goalTask3 = new GoalTask("surefire:test");
54          final TaskSegment taskSegment2 = new TaskSegment(false, goalTask1, goalTask2, goalTask3);
55          MavenExecutionPlan executionPlan2 = lifecycleExecutionPlanCalculator.calculateExecutionPlan(
56                  session1, ProjectDependencyGraphStub.A, taskSegment2.getTasks());
57          assertEquals(3, executionPlan2.size());
58      }
59  
60      // Maybe also make one with LifeCycleTasks
61  
62      public static LifecycleExecutionPlanCalculator createExecutionPlaceCalculator(
63              MojoDescriptorCreator mojoDescriptorCreator) throws ComponentLookupException {
64          LifecyclePluginResolver lifecyclePluginResolver = new LifecyclePluginResolver(new PluginVersionResolverStub());
65          return new DefaultLifecycleExecutionPlanCalculator(
66                  new BuildPluginManagerStub(),
67                  DefaultLifecyclesStub.createDefaultLifecycles(),
68                  mojoDescriptorCreator,
69                  lifecyclePluginResolver);
70      }
71  
72      public static MojoDescriptorCreator createMojoDescriptorCreator() {
73          return new MojoDescriptorCreator(
74                  new PluginVersionResolverStub(),
75                  new BuildPluginManagerStub(),
76                  new PluginPrefixResolverStub(),
77                  new LifecyclePluginResolver(new PluginVersionResolverStub()));
78      }
79  
80      @Override
81      protected String getProjectsDirectory() {
82          return "src/test/projects/lifecycle-executor";
83      }
84  }