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.stub;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import org.apache.maven.execution.AbstractExecutionListener;
26  import org.apache.maven.execution.DefaultMavenExecutionRequest;
27  import org.apache.maven.execution.DefaultMavenExecutionResult;
28  import org.apache.maven.execution.MavenExecutionRequest;
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.execution.ProjectDependencyGraph;
31  import org.apache.maven.lifecycle.LifecycleNotFoundException;
32  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
33  import org.apache.maven.lifecycle.internal.GoalTask;
34  import org.apache.maven.lifecycle.internal.ProjectBuildList;
35  import org.apache.maven.lifecycle.internal.ProjectSegment;
36  import org.apache.maven.lifecycle.internal.TaskSegment;
37  import org.apache.maven.plugin.InvalidPluginDescriptorException;
38  import org.apache.maven.plugin.MojoNotFoundException;
39  import org.apache.maven.plugin.PluginDescriptorParsingException;
40  import org.apache.maven.plugin.PluginNotFoundException;
41  import org.apache.maven.plugin.PluginResolutionException;
42  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
43  import org.apache.maven.plugin.version.PluginVersionResolutionException;
44  import org.apache.maven.project.MavenProject;
45  
46  /**
47   * A stub dependency graph that is custom made for testing concurrent build graph evaluations.
48   * <p>
49   * Implements a graph as follows:
50   * A has no dependencies
51   * B depends on A
52   * C depends on A
53   * X depends on B &amp; C
54   * Y depends on B
55   * Z depends on C
56   * </p>
57   *
58   * @author Kristian Rosenvold
59   */
60  public class ProjectDependencyGraphStub implements ProjectDependencyGraph {
61      public static final MavenProject A = new MavenProject();
62  
63      public static final MavenProject B = new MavenProject();
64  
65      public static final MavenProject C = new MavenProject();
66  
67      public static final MavenProject X = new MavenProject();
68  
69      public static final MavenProject Y = new MavenProject();
70  
71      public static final MavenProject Z = new MavenProject();
72  
73      public static final MavenProject UNKNOWN = new MavenProject();
74  
75      static {
76          A.setArtifactId("A");
77          B.setArtifactId("B");
78          C.setArtifactId("C");
79          X.setArtifactId("X");
80          Y.setArtifactId("Y");
81          Z.setArtifactId("Z");
82      }
83  
84      // This should probably be moved to a separate stub
85  
86      public static ProjectBuildList getProjectBuildList(MavenSession session)
87              throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
88                      NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException,
89                      PluginResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
90          final List<ProjectSegment> list = getProjectBuilds(session);
91          return new ProjectBuildList(list);
92      }
93  
94      public static List<ProjectSegment> getProjectBuilds(MavenSession session)
95              throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
96                      NoPluginFoundForPrefixException, PluginNotFoundException, MojoNotFoundException,
97                      PluginResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
98          List<ProjectSegment> projectBuilds = new ArrayList<>();
99  
100         TaskSegment segment = createTaskSegment();
101         projectBuilds.add(createProjectBuild(A, session, segment));
102         projectBuilds.add(createProjectBuild(B, session, segment));
103         projectBuilds.add(createProjectBuild(C, session, segment));
104         projectBuilds.add(createProjectBuild(X, session, segment));
105         projectBuilds.add(createProjectBuild(Y, session, segment));
106         projectBuilds.add(createProjectBuild(Z, session, segment));
107         return projectBuilds;
108     }
109 
110     private static ProjectSegment createProjectBuild(
111             MavenProject project, MavenSession session, TaskSegment taskSegment)
112             throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
113                     NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException,
114                     PluginResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
115         final MavenSession session1 = session.clone();
116         return new ProjectSegment(project, taskSegment, session1);
117     }
118 
119     private static TaskSegment createTaskSegment() {
120         TaskSegment result = new TaskSegment(false);
121         result.getTasks().add(new GoalTask("t1"));
122         result.getTasks().add(new GoalTask("t2"));
123         return result;
124     }
125 
126     class Dependency {
127         MavenProject dependant;
128 
129         MavenProject dependency;
130 
131         Dependency(MavenProject dependant, MavenProject dependency) {
132             this.dependant = dependant;
133             this.dependency = dependency;
134         }
135 
136         void addIfDownstream(MavenProject mavenProject, List<MavenProject> result) {
137             if (dependency == mavenProject) {
138                 result.add(dependant);
139             }
140         }
141 
142         void addIfUpstreamOf(MavenProject mavenProject, List<MavenProject> result) {
143             if (dependant == mavenProject) {
144                 result.add(dependency); // All projects are the statics from this class
145             }
146         }
147     }
148 
149     private List<Dependency> getDependencies() {
150         List<Dependency> dependencies = new ArrayList<>();
151         dependencies.add(new Dependency(B, A));
152         dependencies.add(new Dependency(C, A));
153         dependencies.add(new Dependency(X, B));
154         dependencies.add(new Dependency(X, C));
155         dependencies.add(new Dependency(Y, B));
156         dependencies.add(new Dependency(Z, C));
157         return dependencies;
158     }
159 
160     public List<MavenProject> getAllProjects() {
161         return Arrays.asList(A, B, C, X, Y, Z, UNKNOWN);
162     }
163 
164     public List<MavenProject> getSortedProjects() {
165         return Arrays.asList(A, B, C, X, Y, Z); // I'm not entirely sure about the order but this should do...
166     }
167 
168     public List<MavenProject> getDownstreamProjects(MavenProject project, boolean transitive) {
169         if (transitive) {
170             throw new RuntimeException("Not implemented yet");
171         }
172         List<MavenProject> result = new ArrayList<>();
173         for (Dependency dependency : getDependencies()) {
174             dependency.addIfDownstream(project, result);
175         }
176         return result;
177     }
178 
179     public List<MavenProject> getUpstreamProjects(MavenProject project, boolean transitive) {
180         /*  if ( transitive )
181         {
182             throw new RuntimeException( "Not implemented yet" );
183         }*/
184         List<MavenProject> result = new ArrayList<>();
185         final List<Dependency> dependencies = getDependencies();
186         for (Dependency dependency : dependencies) {
187             dependency.addIfUpstreamOf(project, result);
188         }
189         return result;
190     }
191 
192     public static MavenSession getMavenSession(MavenProject mavenProject) {
193         final MavenSession session = getMavenSession();
194         session.setCurrentProject(mavenProject);
195         return session;
196     }
197 
198     public static MavenSession getMavenSession() {
199         final DefaultMavenExecutionResult defaultMavenExecutionResult = new DefaultMavenExecutionResult();
200         MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
201         mavenExecutionRequest.setExecutionListener(new AbstractExecutionListener());
202         mavenExecutionRequest.setGoals(Arrays.asList("clean", "aggr", "install"));
203         mavenExecutionRequest.setDegreeOfConcurrency(1);
204         final MavenSession session = new MavenSession(null, null, mavenExecutionRequest, defaultMavenExecutionResult);
205         final ProjectDependencyGraphStub dependencyGraphStub = new ProjectDependencyGraphStub();
206         session.setProjectDependencyGraph(dependencyGraphStub);
207         session.setProjects(dependencyGraphStub.getSortedProjects());
208         return session;
209     }
210 }