1
2
3
4
5
6
7
8
9
10
11
12
13
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
45
46 public class LifecycleWeaveBuilderTest
47 extends TestCase
48 {
49
50
51
52
53
54
55
56
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 }