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
51
52
53 public class ExecMojoTest
54 extends AbstractMojoTestCase
55 {
56 private MockExecMojo mojo;
57
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
69 protected int executeCommandLine( Executor exec, CommandLine commandLine, Map enviro, OutputStream out, OutputStream err )
70 throws IOException, ExecuteException
71 {
72 commandLines.add( commandLine );
73 if ( failureMsg != null )
74 {
75 throw new ExecuteException( failureMsg, executeResult );
76 }
77 return executeResult;
78 }
79
80 protected String getSystemProperty( String key )
81 {
82 return (String) systemProperties.get( key );
83 }
84
85 int getAmountExecutedCommandLines() {
86 return commandLines.size();
87 }
88
89 CommandLine getExecutedCommandline( int index ) {
90 return ((CommandLine) commandLines.get( index ));
91 }
92 }
93
94 public void setUp()
95 throws Exception
96 {
97 super.setUp();
98 mojo = new MockExecMojo();
99
100
101 mojo.setExecutable( "mvn" );
102 mojo.setArguments( Arrays.asList( new String[]{"--version"} ) );
103 mojo.executeResult = 0;
104 mojo.setBasedir( File.createTempFile( "mvn-temp", "txt" ).getParentFile() );
105 }
106
107
108
109 public void testRunOK()
110 throws MojoExecutionException
111 {
112 mojo.execute();
113
114 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
199
200 private String execute( File pom, String goal ) throws Exception {
201
202 ExecMojo mojo;
203 mojo = (ExecMojo) lookupMojo( goal, pom );
204
205 setUpProject( pom, mojo );
206
207 MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
208
209
210
211
212
213 assertNotNull( mojo );
214 assertNotNull( project );
215
216
217 PrintStream out = System.out;
218 StringOutputStream stringOutputStream = new StringOutputStream();
219 System.setOut( new PrintStream( stringOutputStream ) );
220
221 mojo.setLog( new DefaultLog( new ConsoleLogger( Logger.LEVEL_ERROR, "exec:exec" ) ) );
222
223 try
224 {
225 mojo.execute();
226 }
227 catch ( Throwable e )
228 {
229 e.printStackTrace( System.err );
230 fail( e.getMessage() );
231 }
232 finally
233 {
234 System.setOut( out );
235 }
236
237 return stringOutputStream.toString();
238 }
239
240
241 private void setUpProject( File pomFile, ExecMojo mojo )
242 throws Exception
243 {
244 MavenProjectBuilder builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
245
246 ArtifactRepositoryLayout localRepositoryLayout =
247 (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
248
249 String path = "src/test/repository";
250
251 ArtifactRepository localRepository = new DefaultArtifactRepository( "local", "file://" +
252 new File( path ).getAbsolutePath(), localRepositoryLayout );
253
254 mojo.setBasedir( File.createTempFile( "mvn-temp", "txt" ).getParentFile() );
255
256 MavenProject project = builder.buildWithDependencies( pomFile, localRepository, null );
257
258
259 project.getBuild().setOutputDirectory( new File( "target/test-classes" ).getAbsolutePath() );
260
261 mojo.setProject( project );
262
263 mojo.setLog( new SystemStreamLog()
264 {
265 public boolean isDebugEnabled()
266 {
267 return true;
268 }
269 } );
270 }
271
272
273 public void testGetExecutablePath() throws IOException
274 {
275
276 ExecMojo realMojo = new ExecMojo();
277
278 File workdir = new File( System.getProperty( "user.dir" ) );
279 Map enviro = new HashMap();
280
281 String myJavaPath = "target" + File.separator + "javax";
282 File f = new File( myJavaPath );
283 assertTrue( "file created...", f.createNewFile() );
284 assertTrue( "file exists...", f.exists() );
285
286 realMojo.setExecutable( myJavaPath );
287
288 CommandLine cmd = realMojo.getExecutablePath(enviro, workdir);
289 assertTrue( "File exists so path is absolute",
290 cmd.getExecutable().startsWith( System.getProperty( "user.dir" ) ) );
291
292 f.delete();
293 assertFalse( "file deleted...", f.exists() );
294 cmd = realMojo.getExecutablePath(enviro, workdir);
295 assertEquals( "File doesn't exist. Let the system find it (in that PATH?)",
296 myJavaPath, cmd.getExecutable() );
297
298
299 if ( OS.isFamilyWindows() )
300 {
301
302 myJavaPath = "target" + File.separator + "javax.bat";
303 f = new File( myJavaPath );
304 assertTrue( "file created...", f.createNewFile() );
305 assertTrue( "file exists...", f.exists() );
306
307
308 realMojo.setExecutable( "javax.bat" );
309 cmd = realMojo.getExecutablePath( enviro, workdir );
310 assertTrue( "is bat file on windows, execute using cmd.",
311 cmd.getExecutable().equals( "cmd" ) );
312
313 enviro.put( "PATH", workdir.getAbsolutePath() + File.separator + "target" );
314 cmd = realMojo.getExecutablePath( enviro, workdir );
315 assertTrue( "is bat file on windows' PATH, execute using cmd.",
316 cmd.getExecutable().equals( "cmd" ) );
317 f.delete();
318 assertFalse( "file deleted...", f.exists() );
319 }
320 }
321
322 public void testRunFailure()
323 {
324 mojo.executeResult = 1;
325
326 try
327 {
328 mojo.execute();
329 fail( "expected failure" );
330 }
331 catch ( MojoExecutionException e )
332 {
333 assertEquals( "Result of " + mojo.getExecutedCommandline( 0 ) + " execution is: '1'.",
334 e.getMessage() );
335 }
336
337 checkMojo( "mvn --version" );
338 }
339
340 public void testRunError()
341 {
342 mojo.failureMsg = "simulated failure";
343
344 try
345 {
346 mojo.execute();
347 fail( "expected failure" );
348 }
349 catch ( MojoExecutionException e )
350 {
351 assertEquals( "Command execution failed.", e.getMessage() );
352 }
353
354 checkMojo( "mvn --version" );
355 }
356
357 public void testOverrides()
358 throws MojoExecutionException
359 {
360 mojo.systemProperties.put( "exec.args", "-f pom.xml" );
361 mojo.execute();
362
363 checkMojo( "mvn -f pom.xml" );
364 }
365
366 public void testOverrides3()
367 throws MojoExecutionException
368 {
369 mojo.systemProperties.put( "exec.args", null );
370 mojo.execute();
371
372 checkMojo( "mvn --version" );
373
374 mojo.commandLines.clear();
375 mojo.systemProperties.put( "exec.args", "" );
376 mojo.execute();
377
378 checkMojo( "mvn --version" );
379 }
380
381 public void testIsResultCodeAFailure()
382 {
383 ExecMojo execMojo = new ExecMojo();
384 assertTrue(execMojo.isResultCodeAFailure(1));
385 assertFalse(execMojo.isResultCodeAFailure(0));
386
387 execMojo.setSuccessCodes(new ArrayList());
388 assertTrue(execMojo.isResultCodeAFailure(1));
389 assertFalse(execMojo.isResultCodeAFailure(0));
390
391 execMojo.setSuccessCodes(Arrays.asList(new String[] { "2", "5" }));
392 assertTrue(execMojo.isResultCodeAFailure(0));
393 assertTrue(execMojo.isResultCodeAFailure(10));
394 assertFalse(execMojo.isResultCodeAFailure(2));
395 assertFalse(execMojo.isResultCodeAFailure(5));
396 }
397
398
399 public void testParseCommandlineOSWin() throws Exception
400 {
401 ExecMojo execMojo = new ExecMojo();
402 final String javaHome = "C:\\Java\\jdk1.5.0_15";
403
404 setVariableValueToObject( execMojo, "commandlineArgs", javaHome );
405 String[] args = execMojo.parseCommandlineArgs();
406 assertEquals( javaHome, args[0] );
407 }
408
409 private void checkMojo( String expectedCommandLine )
410 {
411 assertEquals( 1, mojo.getAmountExecutedCommandLines() );
412 CommandLine commandline = mojo.getExecutedCommandline( 0 );
413
414 assertEquals(expectedCommandLine, getCommandLineAsString( commandline ));
415 }
416
417 private String getCommandLineAsString( CommandLine commandline ) {
418
419
420 String result = commandline.getExecutable();
421 boolean isCmd = false;
422 if (OS.isFamilyWindows() && result.equals("cmd")) {
423 result = "";
424 isCmd = true;
425 }
426 String[] arguments = commandline.getArguments();
427 for (int i = 0; i < arguments.length; i++)
428 {
429 String arg = arguments[i];
430 if (isCmd && i == 0 && "/c".equals(arg)) {
431 continue;
432 }
433 if (isCmd && i == 1 && arg.endsWith( ".bat")) {
434 arg = arg.substring( 0, arg.length() - ".bat".length());
435 }
436 result += (result.length() == 0 ? "" : " ") + arg;
437 }
438 return result;
439 }
440
441
442
443
444
445
446
447
448
449
450
451
452 private void assertFileEquals( String mavenRepo, File expectedFile, File actualFile )
453 throws IOException
454 {
455 List expectedLines = getLines( mavenRepo, expectedFile );
456
457 List actualLines = getLines( mavenRepo, actualFile );
458
459 for ( int i = 0; i < expectedLines.size(); i++ )
460 {
461 String expected = expectedLines.get( i ).toString();
462
463 if ( actualLines.size() < i )
464 {
465 fail( "Too few lines in the actual file. Was " + actualLines.size() + ", expected: " +
466 expectedLines.size() );
467 }
468
469 String actual = actualLines.get( i ).toString();
470
471 assertEquals( "Checking line #" + ( i + 1 ), expected, actual );
472 }
473
474 assertTrue( "Unequal number of lines.", expectedLines.size() == actualLines.size() );
475 }
476
477
478
479
480
481
482
483
484
485 private List getLines( String mavenRepo, File file )
486 throws IOException
487 {
488 List lines = new ArrayList();
489
490 BufferedReader reader = new BufferedReader( new FileReader( file ) );
491
492 String line;
493
494 while ( ( line = reader.readLine() ) != null )
495 {
496 lines.add(
497 line );
498 }
499
500 return lines;
501 }
502 }