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