001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
003     * agreements. See the NOTICE file distributed with this work for additional information regarding
004     * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
005     * "License"); you may not use this file except in compliance with the License. You may obtain a
006     * copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software distributed under the License
011     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing permissions and limitations under
013     * the License.
014     */
015    package org.apache.maven.lifecycle.internal;
016    
017    import junit.framework.TestCase;
018    import org.apache.maven.execution.DefaultMavenExecutionResult;
019    import org.apache.maven.execution.MavenExecutionResult;
020    import org.apache.maven.execution.MavenSession;
021    import org.apache.maven.lifecycle.LifecycleNotFoundException;
022    import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
023    import org.apache.maven.lifecycle.internal.stub.ExecutionEventCatapultStub;
024    import org.apache.maven.lifecycle.internal.stub.LifecycleExecutionPlanCalculatorStub;
025    import org.apache.maven.lifecycle.internal.stub.LifecycleTaskSegmentCalculatorStub;
026    import org.apache.maven.lifecycle.internal.stub.LoggerStub;
027    import org.apache.maven.lifecycle.internal.stub.MojoExecutorStub;
028    import org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub;
029    import org.apache.maven.plugin.InvalidPluginDescriptorException;
030    import org.apache.maven.plugin.MojoNotFoundException;
031    import org.apache.maven.plugin.PluginDescriptorParsingException;
032    import org.apache.maven.plugin.PluginNotFoundException;
033    import org.apache.maven.plugin.PluginResolutionException;
034    import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
035    import org.apache.maven.plugin.version.PluginVersionResolutionException;
036    
037    import java.util.List;
038    import java.util.concurrent.ExecutionException;
039    import java.util.concurrent.ExecutorCompletionService;
040    import java.util.concurrent.ExecutorService;
041    import java.util.concurrent.Executors;
042    
043    /**
044     * @author Kristian Rosenvold>
045     */
046    public class LifecycleWeaveBuilderTest
047        extends TestCase
048    {
049    
050    /*    public void testBuildProjectSynchronously()
051            throws Exception
052        {
053            final CompletionService<ProjectSegment> service = new CompletionServiceStub( true );
054            final ProjectBuildList projectBuildList = runWithCompletionService( service );
055            assertEquals( "Expect all tasks to be scheduled", projectBuildList.size(),
056                          ( (CompletionServiceStub) service ).size() );
057        }
058      */
059    
060        public void testBuildProjectThreaded()
061            throws Exception
062        {
063            ExecutorService executor = Executors.newFixedThreadPool( 10 );
064            ExecutorCompletionService<ProjectSegment> service = new ExecutorCompletionService<ProjectSegment>( executor );
065            runWithCompletionService( executor );
066            executor.shutdown();
067        }
068    
069        public void testBuildProjectThreadedAggressive()
070            throws Exception
071        {
072            ExecutorService executor = Executors.newFixedThreadPool( 10 );
073            ExecutorCompletionService<ProjectSegment> service = new ExecutorCompletionService<ProjectSegment>( executor );
074            runWithCompletionService( executor );
075            executor.shutdown();
076        }
077    
078        private ProjectBuildList runWithCompletionService( ExecutorService service )
079            throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
080            MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
081            PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
082            ExecutionException, InterruptedException
083        {
084            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
085            try
086            {
087                BuildListCalculator buildListCalculator = new BuildListCalculator();
088                final MavenSession session = ProjectDependencyGraphStub.getMavenSession();
089                List<TaskSegment> taskSegments = getTaskSegmentCalculator().calculateTaskSegments( session );
090                ProjectBuildList projectBuildList = buildListCalculator.calculateProjectBuilds( session, taskSegments );
091    
092                final MojoExecutorStub mojoExecutorStub = new MojoExecutorStub();
093                final LifecycleWeaveBuilder builder = getWeaveBuilder( mojoExecutorStub );
094                final ReactorContext buildContext = createBuildContext( session );
095                ReactorBuildStatus reactorBuildStatus = new ReactorBuildStatus( session.getProjectDependencyGraph() );
096                builder.build( projectBuildList, buildContext, taskSegments, session, service, reactorBuildStatus );
097    
098                LifecycleExecutionPlanCalculatorStub lifecycleExecutionPlanCalculatorStub =
099                    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    }