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.util.List;
22  
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.execution.ProjectDependencyGraph;
25  import org.apache.maven.lifecycle.LifecycleNotFoundException;
26  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
27  import org.apache.maven.lifecycle.internal.builder.multithreaded.ConcurrencyDependencyGraph;
28  import org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub;
29  import org.apache.maven.plugin.InvalidPluginDescriptorException;
30  import org.apache.maven.plugin.MojoNotFoundException;
31  import org.apache.maven.plugin.PluginDescriptorParsingException;
32  import org.apache.maven.plugin.PluginNotFoundException;
33  import org.apache.maven.plugin.PluginResolutionException;
34  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
35  import org.apache.maven.plugin.version.PluginVersionResolutionException;
36  import org.apache.maven.project.MavenProject;
37  import org.junit.jupiter.api.Test;
38  
39  import static org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub.A;
40  import static org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub.B;
41  import static org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub.C;
42  import static org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub.X;
43  import static org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub.Y;
44  import static org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub.Z;
45  import static org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub.getProjectBuildList;
46  import static org.junit.jupiter.api.Assertions.assertEquals;
47  
48  /**
49   */
50  class ConcurrencyDependencyGraphTest {
51      @Test
52      void testConcurrencyGraphPrimaryVersion()
53              throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
54                      NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException,
55                      PluginResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
56          ProjectDependencyGraph dependencyGraph = new ProjectDependencyGraphStub();
57          final MavenSession session = ProjectDependencyGraphStub.getMavenSession();
58  
59          ConcurrencyDependencyGraph graph =
60                  new ConcurrencyDependencyGraph(getProjectBuildList(session), dependencyGraph);
61  
62          final List<MavenProject> projectBuilds = graph.getRootSchedulableBuilds();
63          assertEquals(1, projectBuilds.size());
64          assertEquals(A, projectBuilds.iterator().next());
65  
66          final List<MavenProject> subsequent = graph.markAsFinished(A);
67          assertEquals(2, subsequent.size());
68          assertEquals(ProjectDependencyGraphStub.B, subsequent.get(0));
69          assertEquals(C, subsequent.get(1));
70  
71          final List<MavenProject> bDescendants = graph.markAsFinished(B);
72          assertEquals(1, bDescendants.size());
73          assertEquals(Y, bDescendants.get(0));
74  
75          final List<MavenProject> cDescendants = graph.markAsFinished(C);
76          assertEquals(2, cDescendants.size());
77          assertEquals(X, cDescendants.get(0));
78          assertEquals(Z, cDescendants.get(1));
79      }
80  
81      @Test
82      void testConcurrencyGraphDifferentCompletionOrder()
83              throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
84                      NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException,
85                      PluginResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
86          ProjectDependencyGraph dependencyGraph = new ProjectDependencyGraphStub();
87          final MavenSession session = ProjectDependencyGraphStub.getMavenSession();
88          ConcurrencyDependencyGraph graph =
89                  new ConcurrencyDependencyGraph(getProjectBuildList(session), dependencyGraph);
90  
91          graph.markAsFinished(A);
92          final List<MavenProject> cDescendants = graph.markAsFinished(C);
93          assertEquals(1, cDescendants.size());
94          assertEquals(Z, cDescendants.get(0));
95  
96          final List<MavenProject> bDescendants = graph.markAsFinished(B);
97          assertEquals(2, bDescendants.size());
98          assertEquals(X, bDescendants.get(0));
99          assertEquals(Y, bDescendants.get(1));
100     }
101 }