View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package org.apache.maven.lifecycle.internal.stub;
17  
18  import org.apache.maven.execution.AbstractExecutionListener;
19  import org.apache.maven.execution.DefaultMavenExecutionRequest;
20  import org.apache.maven.execution.DefaultMavenExecutionResult;
21  import org.apache.maven.execution.MavenExecutionRequest;
22  import org.apache.maven.execution.MavenSession;
23  import org.apache.maven.execution.ProjectDependencyGraph;
24  import org.apache.maven.lifecycle.LifecycleNotFoundException;
25  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
26  import org.apache.maven.lifecycle.internal.GoalTask;
27  import org.apache.maven.lifecycle.internal.ProjectBuildList;
28  import org.apache.maven.lifecycle.internal.ProjectSegment;
29  import org.apache.maven.lifecycle.internal.TaskSegment;
30  import org.apache.maven.plugin.InvalidPluginDescriptorException;
31  import org.apache.maven.plugin.MojoNotFoundException;
32  import org.apache.maven.plugin.PluginDescriptorParsingException;
33  import org.apache.maven.plugin.PluginNotFoundException;
34  import org.apache.maven.plugin.PluginResolutionException;
35  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
36  import org.apache.maven.plugin.version.PluginVersionResolutionException;
37  import org.apache.maven.project.MavenProject;
38  
39  import java.util.ArrayList;
40  import java.util.Arrays;
41  import java.util.List;
42  
43  /**
44   * A stub dependency graph that is custom made for testing concurrent build graph evaluations.
45   * <p>
46   * Implements a graph as follows:
47   * A has no dependencies
48   * B depends on A
49   * C depends on A
50   * X depends on B &amp; C
51   * Y depends on B
52   * Z depends on C
53   * </p>
54   *
55   * @author Kristian Rosenvold
56   */
57  public class ProjectDependencyGraphStub
58      implements ProjectDependencyGraph
59  {
60      public static final MavenProject A = new MavenProject();
61  
62      public static final MavenProject B = new MavenProject();
63  
64      public static final MavenProject C = new MavenProject();
65  
66      public static final MavenProject X = new MavenProject();
67  
68      public static final MavenProject Y = new MavenProject();
69  
70      public static final MavenProject Z = new MavenProject();
71  
72      public static final MavenProject UNKNOWN = new MavenProject();
73  
74      static
75      {
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, PluginResolutionException,
89          LifecyclePhaseNotFoundException, LifecycleNotFoundException
90      {
91          final List<ProjectSegment> list = getProjectBuilds( session );
92          return new ProjectBuildList( list );
93  
94      }
95  
96      public static List<ProjectSegment> getProjectBuilds( MavenSession session )
97          throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
98          NoPluginFoundForPrefixException, PluginNotFoundException, MojoNotFoundException, PluginResolutionException,
99          LifecyclePhaseNotFoundException, LifecycleNotFoundException
100     {
101         List<ProjectSegment> projectBuilds = new ArrayList<>();
102 
103         TaskSegment segment = createTaskSegment();
104         projectBuilds.add( createProjectBuild( A, session, segment ) );
105         projectBuilds.add( createProjectBuild( B, session, segment ) );
106         projectBuilds.add( createProjectBuild( C, session, segment ) );
107         projectBuilds.add( createProjectBuild( X, session, segment ) );
108         projectBuilds.add( createProjectBuild( Y, session, segment ) );
109         projectBuilds.add( createProjectBuild( Z, session, segment ) );
110         return projectBuilds;
111     }
112 
113     private static ProjectSegment createProjectBuild( MavenProject project, MavenSession session,
114                                                       TaskSegment taskSegment )
115         throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
116         NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
117         LifecyclePhaseNotFoundException, LifecycleNotFoundException
118     {
119         final MavenSession session1 = session.clone();
120         return new ProjectSegment( project, taskSegment, session1 );
121     }
122 
123 
124     private static TaskSegment createTaskSegment()
125     {
126         TaskSegment result = new TaskSegment( false );
127         result.getTasks().add( new GoalTask( "t1" ) );
128         result.getTasks().add( new GoalTask( "t2" ) );
129         return result;
130     }
131 
132     class Dependency
133     {
134         MavenProject dependant;
135 
136         MavenProject dependency;
137 
138         Dependency( MavenProject dependant, MavenProject dependency )
139         {
140             this.dependant = dependant;
141             this.dependency = dependency;
142         }
143 
144         void addIfDownstream( MavenProject mavenProject, List<MavenProject> result )
145         {
146             if ( dependency == mavenProject )
147             {
148                 result.add( dependant );
149             }
150         }
151 
152         void addIfUpstreamOf( MavenProject mavenProject, List<MavenProject> result )
153         {
154             if ( dependant == mavenProject )
155             {
156                 result.add( dependency ); // All projects are the statics from this class
157             }
158         }
159     }
160 
161     private List<Dependency> getDependencies()
162     {
163         List<Dependency> dependencies = new ArrayList<>();
164         dependencies.add( new Dependency( B, A ) );
165         dependencies.add( new Dependency( C, A ) );
166         dependencies.add( new Dependency( X, B ) );
167         dependencies.add( new Dependency( X, C ) );
168         dependencies.add( new Dependency( Y, B ) );
169         dependencies.add( new Dependency( Z, C ) );
170         return dependencies;
171     }
172 
173     public List<MavenProject> getAllProjects()
174     {
175         return Arrays.asList( A, B, C, X, Y, Z, UNKNOWN );
176     }
177 
178     public List<MavenProject> getSortedProjects()
179     {
180         return Arrays.asList( A, B, C, X, Y, Z ); // I'm not entirely sure about the order but this should do...
181     }
182 
183     public List<MavenProject> getDownstreamProjects( MavenProject project, boolean transitive )
184     {
185         if ( transitive )
186         {
187             throw new RuntimeException( "Not implemented yet" );
188         }
189         List<MavenProject> result = new ArrayList<>();
190         for ( Dependency dependency : getDependencies() )
191         {
192             dependency.addIfDownstream( project, result );
193         }
194         return result;
195     }
196 
197     public List<MavenProject> getUpstreamProjects( MavenProject project, boolean transitive )
198     {
199         /*  if ( transitive )
200         {
201             throw new RuntimeException( "Not implemented yet" );
202         }*/
203         List<MavenProject> result = new ArrayList<>();
204         final List<Dependency> dependencies = getDependencies();
205         for ( Dependency dependency : dependencies )
206         {
207             dependency.addIfUpstreamOf( project, result );
208         }
209         return result;
210     }
211 
212     public static MavenSession getMavenSession( MavenProject mavenProject )
213     {
214         final MavenSession session = getMavenSession();
215         session.setCurrentProject( mavenProject );
216         return session;
217     }
218 
219     public static MavenSession getMavenSession()
220     {
221         final DefaultMavenExecutionResult defaultMavenExecutionResult = new DefaultMavenExecutionResult();
222         MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
223         mavenExecutionRequest.setExecutionListener( new AbstractExecutionListener() );
224         mavenExecutionRequest.setGoals( Arrays.asList( "clean", "aggr", "install" ) );
225         mavenExecutionRequest.setDegreeOfConcurrency( 1 );
226         final MavenSession session = new MavenSession( null, null, mavenExecutionRequest, defaultMavenExecutionResult );
227         final ProjectDependencyGraphStub dependencyGraphStub = new ProjectDependencyGraphStub();
228         session.setProjectDependencyGraph( dependencyGraphStub );
229         session.setProjects( dependencyGraphStub.getSortedProjects() );
230         return session;
231     }
232 
233 }