1 package org.apache.maven.plugin.dependency.its;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.net.MalformedURLException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Properties;
28
29 import org.apache.maven.project.MavenProject;
30 import org.apache.maven.shared.invoker.InvocationRequest;
31 import org.apache.maven.shared.invoker.InvocationResult;
32 import org.apache.maven.shared.test.plugin.BuildTool;
33 import org.apache.maven.shared.test.plugin.PluginTestTool;
34 import org.apache.maven.shared.test.plugin.ProjectTool;
35 import org.apache.maven.shared.test.plugin.TestToolsException;
36 import org.codehaus.classworlds.ClassRealm;
37 import org.codehaus.plexus.PlexusContainer;
38 import org.codehaus.plexus.PlexusTestCase;
39 import org.codehaus.plexus.util.StringUtils;
40
41
42
43
44
45
46
47
48 public abstract class AbstractDependencyPluginITCase
49 extends PlexusTestCase
50 {
51
52 private BuildTool buildTool;
53
54 private ProjectTool projectTool;
55
56
57
58
59 protected static File localRepositoryDirectory = getTestFile( "target/test-classes/m2repo" );
60
61
62
63
64 protected static File PomFile = new File( getBasedir(), "pom.xml" );
65
66
67
68
69 protected static final String GROUP_ID = "org.apache.maven.plugins";
70
71
72
73
74 protected static final String ARTIFACT_ID = "maven-dependency-plugin";
75
76
77
78
79
80 protected static final String VERSION = "test";
81
82 private static final String BUILD_OUTPUT_DIRECTORY = "target/surefire-reports/build-output";
83
84 private static boolean installed = false;
85
86
87
88
89 protected void setUp()
90 throws Exception
91 {
92 if ( !installed )
93 {
94 System.out
95 .println( "*** Running test builds; output will be directed to: " + BUILD_OUTPUT_DIRECTORY + "\n" );
96 }
97
98 super.setUp();
99
100 buildTool = (BuildTool) lookup( BuildTool.ROLE, "default" );
101
102 projectTool = (ProjectTool) lookup( ProjectTool.ROLE, "default" );
103
104 String mavenHome = System.getProperty( "maven.home" );
105
106
107
108
109
110 if ( mavenHome == null )
111 {
112 String path = System.getProperty( "java.library.path" );
113 String[] paths = StringUtils.split( path, System.getProperty( "path.separator" ) );
114 for ( int j = 0; j < paths.length; j++ )
115 {
116 String pt = paths[j];
117 if ( new File( pt, "mvn" ).exists() )
118 {
119 System.setProperty( "maven.home", new File( pt ).getAbsoluteFile().getParent() );
120 break;
121 }
122
123 }
124 }
125
126 System.setProperty( "MAVEN_TERMINATE_CMD", "on" );
127
128 synchronized ( AbstractDependencyPluginITCase.class )
129 {
130 if ( !installed )
131 {
132 PluginTestTool pluginTestTool = (PluginTestTool) lookup( PluginTestTool.ROLE, "default" );
133
134 localRepositoryDirectory = pluginTestTool
135 .preparePluginForUnitTestingWithMavenBuilds( PomFile, "test", localRepositoryDirectory );
136
137 System.out.println( "*** Installed test-version of the Dependency plugin to: "
138 + localRepositoryDirectory + "\n" );
139
140 installed = true;
141 }
142 }
143
144 }
145
146
147
148
149 protected void tearDown()
150 throws Exception
151 {
152 super.tearDown();
153
154 List<PlexusContainer> containers = new ArrayList<PlexusContainer>();
155
156 containers.add( getContainer() );
157
158 for ( PlexusContainer container : containers )
159 {
160 if ( container != null )
161 {
162 container.dispose();
163
164 ClassRealm realm = container.getContainerRealm();
165
166 if ( realm != null )
167 {
168 realm.getWorld().disposeRealm( realm.getId() );
169 }
170 }
171 }
172 }
173
174
175
176
177
178
179
180
181
182 protected void testProject( String projectName, String goalList )
183 throws Exception
184 {
185 Properties props = new Properties();
186 testProject( projectName, props, goalList );
187 }
188
189
190
191
192
193
194
195
196
197
198 protected void testProject( String projectName, Properties properties, String goalList )
199 throws Exception
200 {
201 File theBasedir = getTestFile( "target/test-classes/its/" + projectName );
202
203 File pom = new File( theBasedir, "pom.xml" );
204
205 List<String> goals = Arrays.asList( goalList.split( "," ) );
206
207 executeMaven( pom, properties, goals );
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 }
224
225 protected File getOutputDirectory( String projectName )
226 {
227 return getTestFile( "target/test-classes/projects/" + projectName );
228 }
229
230 protected void executeMaven( File pom, Properties properties, List<String> goals )
231 throws TestToolsException, ExecutionFailedException
232 {
233 executeMaven( pom, properties, goals, true );
234 }
235
236 protected void executeMaven( File pom, Properties properties, List<String> goals, boolean switchLocalRepo )
237 throws TestToolsException, ExecutionFailedException
238 {
239
240
241 properties.setProperty( "test", "true" );
242 new File( BUILD_OUTPUT_DIRECTORY ).mkdirs();
243
244 File buildLog = null;
245
246 for ( StackTraceElement element : new NullPointerException().getStackTrace() )
247 {
248 String methodName = element.getMethodName();
249
250 if ( methodName.startsWith( "test" ) && !methodName.equals( "testProject" ) )
251 {
252 String classname = element.getClassName();
253
254 buildLog = new File( BUILD_OUTPUT_DIRECTORY, classname + "_" + element.getMethodName() + ".build.log" );
255
256 break;
257 }
258 }
259
260 if ( buildLog == null )
261 {
262 buildLog = new File( BUILD_OUTPUT_DIRECTORY, "unknown.build.log" );
263 }
264
265 InvocationRequest request = buildTool.createBasicInvocationRequest( pom, properties, goals, buildLog );
266 request.setUpdateSnapshots( false );
267 request.setShowErrors( true );
268
269 request.setDebug( true );
270
271 if ( switchLocalRepo )
272 {
273 request.setLocalRepositoryDirectory( localRepositoryDirectory );
274 }
275
276 InvocationResult result = buildTool.executeMaven( request );
277
278 if ( result.getExitCode() != 0 )
279 {
280 String buildLogUrl = buildLog.getAbsolutePath();
281
282 try
283 {
284
285 buildLogUrl = buildLog.toURI().toURL().toExternalForm();
286 }
287 catch ( MalformedURLException e )
288 {
289 }
290
291 throw new ExecutionFailedException( "Failed to execute build.\nPOM: " + pom + "\nGoals: "
292 + StringUtils.join( goals.iterator(), ", " ) + "\nExit Code: " + result.getExitCode() + "\nError: "
293 + result.getExecutionException() + "\nBuild Log: " + buildLogUrl + "\n", result );
294 }
295 }
296
297 protected MavenProject readProject( File pom )
298 throws TestToolsException
299 {
300 return projectTool.readProject( pom, localRepositoryDirectory );
301 }
302
303 protected String getPluginCLISpecification()
304 {
305 String pluginSpec = GROUP_ID + ":" + ARTIFACT_ID + ":";
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320 pluginSpec += VERSION + ":";
321
322 return pluginSpec;
323 }
324 }