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