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.builder.multithreaded;
20  
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.apache.maven.execution.ProjectDependencyGraph;
25  import org.apache.maven.lifecycle.internal.ProjectBuildList;
26  import org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub;
27  import org.apache.maven.project.MavenProject;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  
32  class ConcurrencyDependencyGraphTest {
33  
34      @Test
35      void testGraph() throws Exception {
36  
37          ProjectBuildList projectBuildList =
38                  ProjectDependencyGraphStub.getProjectBuildList(ProjectDependencyGraphStub.getMavenSession());
39  
40          ProjectDependencyGraph projectDependencyGraph = new ProjectDependencyGraphStub();
41  
42          ConcurrencyDependencyGraph graph = new ConcurrencyDependencyGraph(projectBuildList, projectDependencyGraph);
43  
44          // start
45          assertEquals(0, graph.getFinishedProjects().size());
46          assertEquals(6, graph.getNumberOfBuilds());
47  
48          List<MavenProject> rootSchedulableBuilds = graph.getRootSchedulableBuilds();
49          // only Project.A has no dependencies
50          assertEquals(1, rootSchedulableBuilds.size());
51          assertEquals(
52                  ProjectDependencyGraphStub.A, rootSchedulableBuilds.iterator().next());
53          // double check A deps
54          List<MavenProject> dependenciesA = graph.getDependencies(ProjectDependencyGraphStub.A);
55          assertEquals(0, dependenciesA.size());
56  
57          assertEquals(6, graph.getUnfinishedProjects().size());
58  
59          List<MavenProject> schedulableNewProcesses = graph.markAsFinished(ProjectDependencyGraphStub.A);
60          // expect Project B, C
61          assertEquals(2, schedulableNewProcesses.size());
62          assertEquals(1, graph.getFinishedProjects().size());
63  
64          graph.markAsFinished(ProjectDependencyGraphStub.A);
65          // still only  A
66          assertEquals(1, graph.getFinishedProjects().size());
67  
68          Set<MavenProject> unfinishedProjects = graph.getUnfinishedProjects();
69          assertEquals(5, unfinishedProjects.size());
70  
71          graph.markAsFinished(schedulableNewProcesses.get(0));
72          assertEquals(2, graph.getFinishedProjects().size());
73          assertEquals(4, graph.getUnfinishedProjects().size());
74  
75          List<MavenProject> dependenciesC = graph.getDependencies(ProjectDependencyGraphStub.C);
76          // C depends only on A
77          assertEquals(1, dependenciesC.size());
78  
79          List<MavenProject> dependenciesX = graph.getDependencies(ProjectDependencyGraphStub.X);
80          // X depends only on B and C
81          assertEquals(2, dependenciesX.size());
82  
83          List<MavenProject> activeDependenciesC = graph.getActiveDependencies(ProjectDependencyGraphStub.C);
84          // A already finished
85          assertEquals(0, activeDependenciesC.size());
86  
87          List<MavenProject> activeDependenciesX = graph.getActiveDependencies(ProjectDependencyGraphStub.X);
88          // waiting for C
89          assertEquals(1, activeDependenciesX.size());
90      }
91  }