1 |
|
package org.codehaus.mojo.exec; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
import org.apache.maven.artifact.repository.ArtifactRepository; |
20 |
|
import org.apache.maven.artifact.repository.DefaultArtifactRepository; |
21 |
|
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; |
22 |
|
import org.apache.maven.plugin.MojoExecutionException; |
23 |
|
import org.apache.maven.plugin.logging.SystemStreamLog; |
24 |
|
import org.apache.maven.project.MavenProject; |
25 |
|
import org.apache.maven.project.MavenProjectBuilder; |
26 |
|
|
27 |
|
import java.io.BufferedReader; |
28 |
|
import java.io.File; |
29 |
|
import java.io.FileReader; |
30 |
|
import java.io.IOException; |
31 |
|
import java.io.OutputStream; |
32 |
|
import java.io.PrintStream; |
33 |
|
import org.codehaus.plexus.util.StringOutputStream; |
34 |
|
import java.util.ArrayList; |
35 |
|
import java.util.Arrays; |
36 |
|
import java.util.Collections; |
37 |
|
import java.util.HashMap; |
38 |
|
import java.util.List; |
39 |
|
import java.util.Map; |
40 |
|
import org.apache.commons.exec.CommandLine; |
41 |
|
import org.apache.commons.exec.ExecuteException; |
42 |
|
import org.apache.commons.exec.Executor; |
43 |
|
import org.apache.commons.exec.OS; |
44 |
|
import org.codehaus.plexus.logging.Logger; |
45 |
|
import org.codehaus.plexus.logging.console.ConsoleLogger; |
46 |
|
import org.apache.maven.monitor.logging.DefaultLog; |
47 |
|
import org.apache.maven.plugin.testing.AbstractMojoTestCase; |
48 |
|
|
49 |
|
|
50 |
|
@author |
51 |
|
@version |
52 |
|
|
|
|
| 0% |
Uncovered Elements: 165 (165) |
Complexity: 33 |
Complexity Density: 0.25 |
|
53 |
|
public class ExecMojoTest |
54 |
|
extends AbstractMojoTestCase |
55 |
|
{ |
56 |
|
private MockExecMojo mojo; |
57 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 5 |
Complexity Density: 0.71 |
|
58 |
|
static class MockExecMojo |
59 |
|
extends ExecMojo |
60 |
|
{ |
61 |
|
public int executeResult; |
62 |
|
|
63 |
|
public List commandLines = new ArrayList(); |
64 |
|
|
65 |
|
public String failureMsg; |
66 |
|
|
67 |
|
public Map systemProperties = new HashMap(); |
68 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
69 |
0
|
protected int executeCommandLine( Executor exec, CommandLine commandLine, Map enviro, OutputStream out, OutputStream err )... |
70 |
|
throws IOException, ExecuteException |
71 |
|
{ |
72 |
0
|
commandLines.add( commandLine ); |
73 |
0
|
if ( failureMsg != null ) |
74 |
|
{ |
75 |
0
|
throw new ExecuteException( failureMsg, executeResult ); |
76 |
|
} |
77 |
0
|
return executeResult; |
78 |
|
} |
79 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
80 |
0
|
protected String getSystemProperty( String key )... |
81 |
|
{ |
82 |
0
|
return (String) systemProperties.get( key ); |
83 |
|
} |
84 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
0
|
int getAmountExecutedCommandLines() {... |
86 |
0
|
return commandLines.size(); |
87 |
|
} |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
0
|
CommandLine getExecutedCommandline( int index ) {... |
90 |
0
|
return ((CommandLine) commandLines.get( index )); |
91 |
|
} |
92 |
|
} |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
94 |
0
|
public void setUp()... |
95 |
|
throws Exception |
96 |
|
{ |
97 |
0
|
super.setUp(); |
98 |
0
|
mojo = new MockExecMojo(); |
99 |
|
|
100 |
|
|
101 |
0
|
mojo.setExecutable( "mvn" ); |
102 |
0
|
mojo.setArguments( Arrays.asList( new String[]{"--version"} ) ); |
103 |
0
|
mojo.executeResult = 0; |
104 |
0
|
mojo.setBasedir( File.createTempFile( "mvn-temp", "txt" ).getParentFile() ); |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
4
-
|
|
109 |
0
|
public void testRunOK()... |
110 |
|
throws MojoExecutionException |
111 |
|
{ |
112 |
0
|
mojo.execute(); |
113 |
|
|
114 |
0
|
checkMojo( "mvn --version" ); |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
@return |
199 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 2 |
Complexity Density: 0.12 |
|
200 |
0
|
private String execute( File pom, String goal ) throws Exception {... |
201 |
|
|
202 |
0
|
ExecMojo mojo; |
203 |
0
|
mojo = (ExecMojo) lookupMojo( goal, pom ); |
204 |
|
|
205 |
0
|
setUpProject( pom, mojo ); |
206 |
|
|
207 |
0
|
MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" ); |
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
0
|
assertNotNull( mojo ); |
214 |
0
|
assertNotNull( project ); |
215 |
|
|
216 |
|
|
217 |
0
|
PrintStream out = System.out; |
218 |
0
|
StringOutputStream stringOutputStream = new StringOutputStream(); |
219 |
0
|
System.setOut( new PrintStream( stringOutputStream ) ); |
220 |
|
|
221 |
0
|
mojo.setLog( new DefaultLog( new ConsoleLogger( Logger.LEVEL_ERROR, "exec:exec" ) ) ); |
222 |
|
|
223 |
0
|
try |
224 |
|
{ |
225 |
0
|
mojo.execute(); |
226 |
|
} |
227 |
|
catch ( Throwable e ) |
228 |
|
{ |
229 |
0
|
e.printStackTrace( System.err ); |
230 |
0
|
fail( e.getMessage() ); |
231 |
|
} |
232 |
|
finally |
233 |
|
{ |
234 |
0
|
System.setOut( out ); |
235 |
|
} |
236 |
|
|
237 |
0
|
return stringOutputStream.toString(); |
238 |
|
} |
239 |
|
|
240 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
241 |
0
|
private void setUpProject( File pomFile, ExecMojo mojo )... |
242 |
|
throws Exception |
243 |
|
{ |
244 |
0
|
MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE ); |
245 |
|
|
246 |
0
|
ArtifactRepositoryLayout localRepositoryLayout = |
247 |
|
(ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); |
248 |
|
|
249 |
0
|
String path = "src/test/repository"; |
250 |
|
|
251 |
0
|
ArtifactRepository localRepository = new DefaultArtifactRepository( "local", "file://" + |
252 |
|
new File( path ).getAbsolutePath(), localRepositoryLayout ); |
253 |
|
|
254 |
0
|
mojo.setBasedir( File.createTempFile( "mvn-temp", "txt" ).getParentFile() ); |
255 |
|
|
256 |
0
|
MavenProject project = builder.buildWithDependencies( pomFile, localRepository, null ); |
257 |
|
|
258 |
|
|
259 |
0
|
project.getBuild().setOutputDirectory( new File( "target/test-classes" ).getAbsolutePath() ); |
260 |
|
|
261 |
0
|
mojo.setProject( project ); |
262 |
|
|
263 |
0
|
mojo.setLog( new SystemStreamLog() |
264 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
265 |
0
|
public boolean isDebugEnabled()... |
266 |
|
{ |
267 |
0
|
return true; |
268 |
|
} |
269 |
|
} ); |
270 |
|
} |
271 |
|
|
272 |
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 2 |
Complexity Density: 0.07 |
4
-
|
|
273 |
0
|
public void testGetExecutablePath() throws IOException... |
274 |
|
{ |
275 |
|
|
276 |
0
|
ExecMojo realMojo = new ExecMojo(); |
277 |
|
|
278 |
0
|
File workdir = new File( System.getProperty( "user.dir" ) ); |
279 |
0
|
Map enviro = new HashMap(); |
280 |
|
|
281 |
0
|
String myJavaPath = "target" + File.separator + "javax"; |
282 |
0
|
File f = new File( myJavaPath ); |
283 |
0
|
assertTrue( "file created...", f.createNewFile() ); |
284 |
0
|
assertTrue( "file exists...", f.exists() ); |
285 |
|
|
286 |
0
|
realMojo.setExecutable( myJavaPath ); |
287 |
|
|
288 |
0
|
CommandLine cmd = realMojo.getExecutablePath(enviro, workdir); |
289 |
0
|
assertTrue( "File exists so path is absolute", |
290 |
|
cmd.getExecutable().startsWith( System.getProperty( "user.dir" ) ) ); |
291 |
|
|
292 |
0
|
f.delete(); |
293 |
0
|
assertFalse( "file deleted...", f.exists() ); |
294 |
0
|
cmd = realMojo.getExecutablePath(enviro, workdir); |
295 |
0
|
assertEquals( "File doesn't exist. Let the system find it (in that PATH?)", |
296 |
|
myJavaPath, cmd.getExecutable() ); |
297 |
|
|
298 |
|
|
299 |
0
|
if ( OS.isFamilyWindows() ) |
300 |
|
{ |
301 |
|
|
302 |
0
|
myJavaPath = "target" + File.separator + "javax.bat"; |
303 |
0
|
f = new File( myJavaPath ); |
304 |
0
|
assertTrue( "file created...", f.createNewFile() ); |
305 |
0
|
assertTrue( "file exists...", f.exists() ); |
306 |
|
|
307 |
|
|
308 |
0
|
realMojo.setExecutable( "javax.bat" ); |
309 |
0
|
cmd = realMojo.getExecutablePath( enviro, workdir ); |
310 |
0
|
assertTrue( "is bat file on windows, execute using cmd.", |
311 |
|
cmd.getExecutable().equals( "cmd" ) ); |
312 |
|
|
313 |
0
|
enviro.put( "PATH", workdir.getAbsolutePath() + File.separator + "target" ); |
314 |
0
|
cmd = realMojo.getExecutablePath( enviro, workdir ); |
315 |
0
|
assertTrue( "is bat file on windows' PATH, execute using cmd.", |
316 |
|
cmd.getExecutable().equals( "cmd" ) ); |
317 |
0
|
f.delete(); |
318 |
0
|
assertFalse( "file deleted...", f.exists() ); |
319 |
|
} |
320 |
|
} |
321 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
4
-
|
|
322 |
0
|
public void testRunFailure()... |
323 |
|
{ |
324 |
0
|
mojo.executeResult = 1; |
325 |
|
|
326 |
0
|
try |
327 |
|
{ |
328 |
0
|
mojo.execute(); |
329 |
0
|
fail( "expected failure" ); |
330 |
|
} |
331 |
|
catch ( MojoExecutionException e ) |
332 |
|
{ |
333 |
0
|
assertEquals( "Result of " + mojo.getExecutedCommandline( 0 ) + " execution is: '1'.", |
334 |
|
e.getMessage() ); |
335 |
|
} |
336 |
|
|
337 |
0
|
checkMojo( "mvn --version" ); |
338 |
|
} |
339 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
4
-
|
|
340 |
0
|
public void testRunError()... |
341 |
|
{ |
342 |
0
|
mojo.failureMsg = "simulated failure"; |
343 |
|
|
344 |
0
|
try |
345 |
|
{ |
346 |
0
|
mojo.execute(); |
347 |
0
|
fail( "expected failure" ); |
348 |
|
} |
349 |
|
catch ( MojoExecutionException e ) |
350 |
|
{ |
351 |
0
|
assertEquals( "Command execution failed.", e.getMessage() ); |
352 |
|
} |
353 |
|
|
354 |
0
|
checkMojo( "mvn --version" ); |
355 |
|
} |
356 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
4
-
|
|
357 |
0
|
public void testOverrides()... |
358 |
|
throws MojoExecutionException |
359 |
|
{ |
360 |
0
|
mojo.systemProperties.put( "exec.args", "-f pom.xml" ); |
361 |
0
|
mojo.execute(); |
362 |
|
|
363 |
0
|
checkMojo( "mvn -f pom.xml" ); |
364 |
|
} |
365 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
4
-
|
|
366 |
0
|
public void testOverrides3()... |
367 |
|
throws MojoExecutionException |
368 |
|
{ |
369 |
0
|
mojo.systemProperties.put( "exec.args", null ); |
370 |
0
|
mojo.execute(); |
371 |
|
|
372 |
0
|
checkMojo( "mvn --version" ); |
373 |
|
|
374 |
0
|
mojo.commandLines.clear(); |
375 |
0
|
mojo.systemProperties.put( "exec.args", "" ); |
376 |
0
|
mojo.execute(); |
377 |
|
|
378 |
0
|
checkMojo( "mvn --version" ); |
379 |
|
} |
380 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
4
-
|
|
381 |
0
|
public void testIsResultCodeAFailure() ... |
382 |
|
{ |
383 |
0
|
ExecMojo execMojo = new ExecMojo(); |
384 |
0
|
assertTrue(execMojo.isResultCodeAFailure(1)); |
385 |
0
|
assertFalse(execMojo.isResultCodeAFailure(0)); |
386 |
|
|
387 |
0
|
execMojo.setSuccessCodes(new ArrayList()); |
388 |
0
|
assertTrue(execMojo.isResultCodeAFailure(1)); |
389 |
0
|
assertFalse(execMojo.isResultCodeAFailure(0)); |
390 |
|
|
391 |
0
|
execMojo.setSuccessCodes(Arrays.asList(new String[] { "2", "5" })); |
392 |
0
|
assertTrue(execMojo.isResultCodeAFailure(0)); |
393 |
0
|
assertTrue(execMojo.isResultCodeAFailure(10)); |
394 |
0
|
assertFalse(execMojo.isResultCodeAFailure(2)); |
395 |
0
|
assertFalse(execMojo.isResultCodeAFailure(5)); |
396 |
|
} |
397 |
|
|
398 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4
-
|
|
399 |
0
|
public void testParseCommandlineOSWin() throws Exception ... |
400 |
|
{ |
401 |
0
|
ExecMojo execMojo = new ExecMojo(); |
402 |
0
|
final String javaHome = "C:\\Java\\jdk1.5.0_15"; |
403 |
|
|
404 |
0
|
setVariableValueToObject( execMojo, "commandlineArgs", javaHome ); |
405 |
0
|
String[] args = execMojo.parseCommandlineArgs(); |
406 |
0
|
assertEquals( javaHome, args[0] ); |
407 |
|
} |
408 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
409 |
0
|
private void checkMojo( String expectedCommandLine )... |
410 |
|
{ |
411 |
0
|
assertEquals( 1, mojo.getAmountExecutedCommandLines() ); |
412 |
0
|
CommandLine commandline = mojo.getExecutedCommandline( 0 ); |
413 |
|
|
414 |
0
|
assertEquals(expectedCommandLine, getCommandLineAsString( commandline )); |
415 |
|
} |
416 |
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 11 |
Complexity Density: 0.79 |
|
417 |
0
|
private String getCommandLineAsString( CommandLine commandline ) {... |
418 |
|
|
419 |
|
|
420 |
0
|
String result = commandline.getExecutable(); |
421 |
0
|
boolean isCmd = false; |
422 |
0
|
if (OS.isFamilyWindows() && result.equals("cmd")) { |
423 |
0
|
result = ""; |
424 |
0
|
isCmd = true; |
425 |
|
} |
426 |
0
|
String[] arguments = commandline.getArguments(); |
427 |
0
|
for (int i = 0; i < arguments.length; i++) |
428 |
|
{ |
429 |
0
|
String arg = arguments[i]; |
430 |
0
|
if (isCmd && i == 0 && "/c".equals(arg)) { |
431 |
0
|
continue; |
432 |
|
} |
433 |
0
|
if (isCmd && i == 1 && arg.endsWith( ".bat")) { |
434 |
0
|
arg = arg.substring( 0, arg.length() - ".bat".length()); |
435 |
|
} |
436 |
0
|
result += (result.length() == 0 ? "" : " ") + arg; |
437 |
|
} |
438 |
0
|
return result; |
439 |
|
} |
440 |
|
|
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
|
446 |
|
|
447 |
|
@param |
448 |
|
@param |
449 |
|
@param |
450 |
|
@throws |
451 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
452 |
0
|
private void assertFileEquals( String mavenRepo, File expectedFile, File actualFile )... |
453 |
|
throws IOException |
454 |
|
{ |
455 |
0
|
List expectedLines = getLines( mavenRepo, expectedFile ); |
456 |
|
|
457 |
0
|
List actualLines = getLines( mavenRepo, actualFile ); |
458 |
|
|
459 |
0
|
for ( int i = 0; i < expectedLines.size(); i++ ) |
460 |
|
{ |
461 |
0
|
String expected = expectedLines.get( i ).toString(); |
462 |
|
|
463 |
0
|
if ( actualLines.size() < i ) |
464 |
|
{ |
465 |
0
|
fail( "Too few lines in the actual file. Was " + actualLines.size() + ", expected: " + |
466 |
|
expectedLines.size() ); |
467 |
|
} |
468 |
|
|
469 |
0
|
String actual = actualLines.get( i ).toString(); |
470 |
|
|
471 |
0
|
assertEquals( "Checking line #" + ( i + 1 ), expected, actual ); |
472 |
|
} |
473 |
|
|
474 |
0
|
assertTrue( "Unequal number of lines.", expectedLines.size() == actualLines.size() ); |
475 |
|
} |
476 |
|
|
477 |
|
|
478 |
|
|
479 |
|
|
480 |
|
@param |
481 |
|
@param |
482 |
|
@return |
483 |
|
@throws |
484 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
485 |
0
|
private List getLines( String mavenRepo, File file )... |
486 |
|
throws IOException |
487 |
|
{ |
488 |
0
|
List lines = new ArrayList(); |
489 |
|
|
490 |
0
|
BufferedReader reader = new BufferedReader( new FileReader( file ) ); |
491 |
|
|
492 |
0
|
String line; |
493 |
|
|
494 |
0
|
while ( ( line = reader.readLine() ) != null ) |
495 |
|
{ |
496 |
0
|
lines.add( |
497 |
|
line ); |
498 |
|
} |
499 |
|
|
500 |
0
|
return lines; |
501 |
|
} |
502 |
|
} |