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 junit.framework.TestCase;
18  import org.apache.maven.execution.DefaultMavenExecutionResult;
19  import org.apache.maven.execution.MavenExecutionResult;
20  import org.apache.maven.execution.MavenSession;
21  import org.apache.maven.lifecycle.LifecycleNotFoundException;
22  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
23  import org.apache.maven.lifecycle.internal.stub.ExecutionEventCatapultStub;
24  import org.apache.maven.lifecycle.internal.stub.LifecycleExecutionPlanCalculatorStub;
25  import org.apache.maven.lifecycle.internal.stub.LifecycleTaskSegmentCalculatorStub;
26  import org.apache.maven.lifecycle.internal.stub.LoggerStub;
27  import org.apache.maven.lifecycle.internal.stub.MojoExecutorStub;
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  
37  import java.util.List;
38  import java.util.concurrent.ExecutionException;
39  import java.util.concurrent.ExecutorCompletionService;
40  import java.util.concurrent.ExecutorService;
41  import java.util.concurrent.Executors;
42  
43  /**
44   * @author Kristian Rosenvold>
45   */
46  public class LifecycleWeaveBuilderTest
47      extends TestCase
48  {
49  
50  /*    public void testBuildProjectSynchronously()
51          throws Exception
52      {
53          final CompletionService<ProjectSegment> service = new CompletionServiceStub( true );
54          final ProjectBuildList projectBuildList = runWithCompletionService( service );
55          assertEquals( "Expect all tasks to be scheduled", projectBuildList.size(),
56                        ( (CompletionServiceStub) service ).size() );
57      }
58    */
59  
60      public void testBuildProjectThreaded()
61          throws Exception
62      {
63          ExecutorService executor = Executors.newFixedThreadPool( 10 );
64          ExecutorCompletionService<ProjectSegment> service = new ExecutorCompletionService<ProjectSegment>( executor );
65          runWithCompletionService( executor );
66          executor.shutdown();
67      }
68  
69      public void testBuildProjectThreadedAggressive()
70          throws Exception
71      {
72          ExecutorService executor = Executors.newFixedThreadPool( 10 );
73          ExecutorCompletionService<ProjectSegment> service = new ExecutorCompletionService<ProjectSegment>( executor );
74          runWithCompletionService( executor );
75          executor.shutdown();
76      }
77  
78      private ProjectBuildList runWithCompletionService( ExecutorService service )
79          throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
80          MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
81          PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
82          ExecutionException, InterruptedException
83      {
84          final ClassLoader loader = Thread.currentThread().getContextClassLoader();
85          try
86          {
87              BuildListCalculator buildListCalculator = new BuildListCalculator();
88              final MavenSession session = ProjectDependencyGraphStub.getMavenSession();
89              List<TaskSegment> taskSegments = getTaskSegmentCalculator().calculateTaskSegments( session );
90              ProjectBuildList projectBuildList = buildListCalculator.calculateProjectBuilds( session, taskSegments );
91  
92              final MojoExecutorStub mojoExecutorStub = new MojoExecutorStub();
93              final LifecycleWeaveBuilder builder = getWeaveBuilder( mojoExecutorStub );
94              final ReactorContext buildContext = createBuildContext( session );
95              ReactorBuildStatus reactorBuildStatus = new ReactorBuildStatus( session.getProjectDependencyGraph() );
96              builder.build( projectBuildList, buildContext, taskSegments, session, service, reactorBuildStatus );
97  
98              LifecycleExecutionPlanCalculatorStub lifecycleExecutionPlanCalculatorStub =
99                  new LifecycleExecutionPlanCalculatorStub();
100             final int expected = lifecycleExecutionPlanCalculatorStub.getNumberOfExceutions( projectBuildList );
101             assertEquals( "All executions should be scheduled", expected, mojoExecutorStub.executions.size() );
102             return projectBuildList;
103         }
104         finally
105         {
106             Thread.currentThread().setContextClassLoader( loader );
107         }
108     }
109 
110 
111     private static LifecycleTaskSegmentCalculator getTaskSegmentCalculator()
112     {
113         return new LifecycleTaskSegmentCalculatorStub();
114     }
115 
116     private ReactorContext createBuildContext( MavenSession session )
117     {
118         MavenExecutionResult mavenExecutionResult = new DefaultMavenExecutionResult();
119         ReactorBuildStatus reactorBuildStatus = new ReactorBuildStatus( session.getProjectDependencyGraph() );
120         return new ReactorContext( mavenExecutionResult, null, null, reactorBuildStatus );
121     }
122 
123     private LifecycleWeaveBuilder getWeaveBuilder( MojoExecutor mojoExecutor )
124     {
125         final BuilderCommon builderCommon = getBuilderCommon();
126         final LoggerStub loggerStub = new LoggerStub();
127         return new LifecycleWeaveBuilder( mojoExecutor, builderCommon, loggerStub, new ExecutionEventCatapultStub() );
128     }
129 
130     private BuilderCommon getBuilderCommon()
131     {
132         final LifecycleDebugLogger logger = new LifecycleDebugLogger( new LoggerStub() );
133         return new BuilderCommon( logger, new LifecycleExecutionPlanCalculatorStub(),
134                                   new LoggerStub() );
135     }
136 }