1 package org.apache.maven.shared.invoker;
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.io.FileWriter;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Properties;
31 import java.util.Set;
32
33 import junit.framework.TestCase;
34
35 import org.codehaus.plexus.util.Os;
36 import org.codehaus.plexus.util.cli.Commandline;
37 import org.codehaus.plexus.util.FileUtils;
38 import org.codehaus.plexus.util.IOUtil;
39
40 public class MavenCommandLineBuilderTest
41 extends TestCase
42 {
43
44 private List<File> toDelete = new ArrayList<File>();
45
46 private Properties sysProps;
47
48 public void testWrapwithQuotes()
49 {
50 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
51 String test = "noSpacesInHere";
52
53 assertSame( test, tcb.wrapStringWithQuotes( test ) );
54 assertEquals( "noSpacesInHere", tcb.wrapStringWithQuotes( test ) );
55
56 test = "bunch of spaces in here";
57 assertNotSame( test, tcb.wrapStringWithQuotes( test ) );
58 assertEquals( "\"bunch of spaces in here\"", tcb.wrapStringWithQuotes( test ) );
59
60 }
61
62 public void testShouldFailToSetLocalRepoLocationGloballyWhenItIsAFile()
63 throws IOException
64 {
65 logTestStart();
66
67 File lrd = File.createTempFile( "workdir-test", "file" ).getCanonicalFile();
68
69 toDelete.add( lrd );
70
71 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
72 tcb.setLocalRepositoryDirectory( lrd );
73
74 Commandline cli = new Commandline();
75
76 try
77 {
78 tcb.setEnvironmentPaths( newRequest(), cli );
79 fail( "Should not set local repo location to point to a file." );
80 }
81 catch ( IllegalArgumentException e )
82 {
83 assertTrue( true );
84 }
85 }
86
87 public void testShouldFailToSetLocalRepoLocationFromRequestWhenItIsAFile()
88 throws IOException
89 {
90 logTestStart();
91
92 File lrd = File.createTempFile( "workdir-test", "file" ).getCanonicalFile();
93
94 toDelete.add( lrd );
95
96 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
97
98 Commandline cli = new Commandline();
99
100 try
101 {
102 tcb.setEnvironmentPaths( newRequest().setLocalRepositoryDirectory( lrd ), cli );
103 fail( "Should not set local repo location to point to a file." );
104 }
105 catch ( IllegalArgumentException e )
106 {
107 assertTrue( true );
108 }
109 }
110
111 public void testShouldSetLocalRepoLocationGlobally()
112 throws Exception
113 {
114 logTestStart();
115
116 File tmpDir = getTempDir();
117
118 File lrd = new File( tmpDir, "workdir" ).getCanonicalFile();
119
120 lrd.mkdirs();
121 toDelete.add( lrd );
122
123 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
124 tcb.setLocalRepositoryDirectory( lrd );
125
126 Commandline cli = new Commandline();
127
128 tcb.setEnvironmentPaths( newRequest(), cli );
129
130 assertArgumentsPresentInOrder( cli, "-D", "maven.repo.local=" + lrd.getPath() );
131 }
132
133 public void testShouldSetLocalRepoLocationFromRequest()
134 throws Exception
135 {
136 logTestStart();
137
138 File tmpDir = getTempDir();
139
140 File lrd = new File( tmpDir, "workdir" ).getCanonicalFile();
141
142 lrd.mkdirs();
143 toDelete.add( lrd );
144
145 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
146
147 Commandline cli = new Commandline();
148
149 tcb.setEnvironmentPaths( newRequest().setLocalRepositoryDirectory( lrd ), cli );
150
151 assertArgumentsPresentInOrder( cli, "-D", "maven.repo.local=" + lrd.getPath() );
152 }
153
154 public void testRequestProvidedLocalRepoLocationShouldOverrideGlobal()
155 throws Exception
156 {
157 logTestStart();
158
159 File tmpDir = getTempDir();
160
161 File lrd = new File( tmpDir, "workdir" ).getCanonicalFile();
162 File glrd = new File( tmpDir, "global-workdir" ).getCanonicalFile();
163
164 lrd.mkdirs();
165 glrd.mkdirs();
166
167 toDelete.add( lrd );
168 toDelete.add( glrd );
169
170 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
171 tcb.setLocalRepositoryDirectory( glrd );
172
173 Commandline cli = new Commandline();
174
175 tcb.setEnvironmentPaths( newRequest().setLocalRepositoryDirectory( lrd ), cli );
176
177 assertArgumentsPresentInOrder( cli, "-D", "maven.repo.local=" + lrd.getPath() );
178 }
179
180 public void testShouldSetWorkingDirectoryGlobally()
181 throws Exception
182 {
183 logTestStart();
184
185 File tmpDir = getTempDir();
186
187 File wd = new File( tmpDir, "workdir" );
188
189 wd.mkdirs();
190
191 toDelete.add( wd );
192
193 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
194 tcb.setWorkingDirectory( wd );
195
196 Commandline cli = new Commandline();
197
198 tcb.setEnvironmentPaths( newRequest(), cli );
199
200 assertEquals( cli.getWorkingDirectory(), wd );
201 }
202
203 public void testShouldSetWorkingDirectoryFromRequest()
204 throws Exception
205 {
206 logTestStart();
207
208 File tmpDir = getTempDir();
209
210 File wd = new File( tmpDir, "workdir" );
211
212 wd.mkdirs();
213
214 toDelete.add( wd );
215
216 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
217 InvocationRequest req = newRequest();
218 req.setBaseDirectory( wd );
219
220 Commandline cli = new Commandline();
221
222 tcb.setEnvironmentPaths( req, cli );
223
224 assertEquals( cli.getWorkingDirectory(), wd );
225 }
226
227 public void testRequestProvidedWorkingDirectoryShouldOverrideGlobal()
228 throws Exception
229 {
230 logTestStart();
231
232 File tmpDir = getTempDir();
233
234 File wd = new File( tmpDir, "workdir" );
235 File gwd = new File( tmpDir, "global-workdir" );
236
237 wd.mkdirs();
238 gwd.mkdirs();
239
240 toDelete.add( wd );
241 toDelete.add( gwd );
242
243 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
244 tcb.setWorkingDirectory( gwd );
245
246 InvocationRequest req = newRequest();
247 req.setBaseDirectory( wd );
248
249 Commandline cli = new Commandline();
250
251 tcb.setEnvironmentPaths( req, cli );
252
253 assertEquals( cli.getWorkingDirectory(), wd );
254 }
255
256 public void testShouldUseSystemOutLoggerWhenNoneSpecified()
257 throws Exception
258 {
259 logTestStart();
260 setupTempMavenHomeIfMissing();
261
262 TestCommandLineBuilder tclb = new TestCommandLineBuilder();
263 tclb.checkRequiredState();
264 }
265
266 private File setupTempMavenHomeIfMissing()
267 throws Exception
268 {
269 String mavenHome = System.getProperty( "maven.home" );
270
271 File appDir = null;
272
273 if ( ( mavenHome == null ) || !new File( mavenHome ).exists() )
274 {
275 File tmpDir = getTempDir();
276 appDir = new File( tmpDir, "invoker-tests/maven-home" );
277
278 File binDir = new File( appDir, "bin" );
279
280 binDir.mkdirs();
281 toDelete.add( appDir );
282
283 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
284 {
285 createDummyFile( binDir, "mvn.bat" );
286 }
287 else
288 {
289 createDummyFile( binDir, "mvn" );
290 }
291
292 Properties props = System.getProperties();
293 props.setProperty( "maven.home", appDir.getCanonicalPath() );
294
295 System.setProperties( props );
296 }
297 else
298 {
299 appDir = new File( mavenHome );
300 }
301
302 return appDir;
303 }
304
305 public void testShouldFailIfLoggerSetToNull()
306 {
307 logTestStart();
308
309 TestCommandLineBuilder tclb = new TestCommandLineBuilder();
310 tclb.setLogger( null );
311
312 try
313 {
314 tclb.checkRequiredState();
315 fail( "Should not allow execution to proceed when logger is missing." );
316 }
317 catch ( IllegalStateException e )
318 {
319 assertTrue( true );
320 }
321 catch ( IOException e )
322 {
323 fail( e.getMessage() );
324 }
325 }
326
327 public void testShouldFindDummyMavenExecutable()
328 throws Exception
329 {
330 logTestStart();
331
332 File tmpDir = getTempDir();
333
334 File base = new File( tmpDir, "invoker-tests" );
335
336 File dummyMavenHomeBin = new File( base, "dummy-maven-home/bin" );
337
338 dummyMavenHomeBin.mkdirs();
339
340 toDelete.add( base );
341
342 File check;
343 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
344 {
345 check = createDummyFile( dummyMavenHomeBin, "mvn.bat" );
346 }
347 else
348 {
349 check = createDummyFile( dummyMavenHomeBin, "mvn" );
350 }
351
352 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
353 tcb.setMavenHome( dummyMavenHomeBin.getParentFile() );
354
355 File mavenExe = tcb.findMavenExecutable();
356
357 assertEquals( check.getCanonicalPath(), mavenExe.getCanonicalPath() );
358 }
359
360 public void testShouldSetBatchModeFlagFromRequest()
361 {
362 logTestStart();
363
364 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
365 Commandline cli = new Commandline();
366
367 tcb.setFlags( newRequest().setInteractive( false ), cli );
368
369 assertArgumentsPresent( cli, Collections.singleton( "-B" ) );
370 }
371
372 public void testShouldSetOfflineFlagFromRequest()
373 {
374 logTestStart();
375
376 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
377 Commandline cli = new Commandline();
378
379 tcb.setFlags( newRequest().setOffline( true ), cli );
380
381 assertArgumentsPresent( cli, Collections.singleton( "-o" ) );
382 }
383
384 public void testShouldSetUpdateSnapshotsFlagFromRequest()
385 {
386 logTestStart();
387
388 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
389 Commandline cli = new Commandline();
390
391 tcb.setFlags( newRequest().setUpdateSnapshots( true ), cli );
392
393 assertArgumentsPresent( cli, Collections.singleton( "-U" ) );
394 }
395
396 public void testShouldSetDebugFlagFromRequest()
397 {
398 logTestStart();
399
400 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
401 Commandline cli = new Commandline();
402
403 tcb.setFlags( newRequest().setDebug( true ), cli );
404
405 assertArgumentsPresent( cli, Collections.singleton( "-X" ) );
406 }
407
408 public void testShouldSetErrorFlagFromRequest()
409 {
410 logTestStart();
411
412 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
413 Commandline cli = new Commandline();
414
415 tcb.setFlags( newRequest().setShowErrors( true ), cli );
416
417 assertArgumentsPresent( cli, Collections.singleton( "-e" ) );
418 }
419
420 public void testDebugOptionShouldMaskShowErrorsOption()
421 {
422 logTestStart();
423
424 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
425 Commandline cli = new Commandline();
426
427 tcb.setFlags( newRequest().setDebug( true ).setShowErrors( true ), cli );
428
429 assertArgumentsPresent( cli, Collections.singleton( "-X" ) );
430 assertArgumentsNotPresent( cli, Collections.singleton( "-e" ) );
431 }
432
433 public void testActivateReactor()
434 {
435 logTestStart();
436
437 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
438 Commandline cli = new Commandline();
439
440 tcb.setReactorBehavior( newRequest().activateReactor( null, null ), cli );
441
442 assertArgumentsPresent( cli, Collections.singleton( "-r" ) );
443 }
444
445 public void testActivateReactorIncludesExcludes()
446 {
447 logTestStart();
448
449 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
450 Commandline cli = new Commandline();
451
452 String[] includes = new String[] {"foo", "bar"};
453 String[] excludes = new String[] {"baz", "quz"};
454
455 tcb.setReactorBehavior( newRequest().activateReactor( includes, excludes ), cli );
456
457 Set<String> args = new HashSet<String>();
458 args.add( "-r" );
459 args.add( "-D" );
460 args.add( "maven.reactor.includes=foo,bar" );
461 args.add( "maven.reactor.excludes=baz,quz" );
462
463 assertArgumentsPresent( cli, args );
464 }
465
466 public void testAlsoMake()
467 {
468 logTestStart();
469
470 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
471 Commandline cli = new Commandline();
472
473 tcb.setReactorBehavior( newRequest().setAlsoMake( true ), cli );
474
475
476 assertArgumentsNotPresent( cli, Collections.singleton( "-am" ) );
477 }
478
479 public void testProjectsAndAlsoMake()
480 {
481 logTestStart();
482
483 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
484 Commandline cli = new Commandline();
485
486 tcb.setReactorBehavior( newRequest().setProjects( Collections.singletonList( "proj1" ) ).setAlsoMake( true ), cli );
487
488 assertArgumentsPresentInOrder( cli, "-pl", "proj1", "-am" );
489 }
490
491 public void testAlsoMakeDependents()
492 {
493 logTestStart();
494
495 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
496 Commandline cli = new Commandline();
497
498 tcb.setReactorBehavior( newRequest().setAlsoMakeDependents( true ), cli );
499
500
501 assertArgumentsNotPresent( cli, Collections.singleton( "-amd" ) );
502 }
503
504 public void testProjectsAndAlsoMakeDependents()
505 {
506 logTestStart();
507
508 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
509 Commandline cli = new Commandline();
510
511 tcb.setReactorBehavior( newRequest().setProjects( Collections.singletonList( "proj1" ) ).setAlsoMakeDependents( true ), cli );
512
513 assertArgumentsPresentInOrder( cli, "-pl", "proj1", "-amd" );
514 }
515
516 public void testProjectsAndAlsoMakeAndAlsoMakeDependents()
517 {
518 logTestStart();
519
520 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
521 Commandline cli = new Commandline();
522
523 tcb.setReactorBehavior( newRequest().setProjects( Collections.singletonList( "proj1" ) ).setAlsoMake( true ).setAlsoMakeDependents( true ), cli );
524
525 assertArgumentsPresentInOrder( cli, "-pl", "proj1", "-am", "-amd" );
526 }
527
528 public void testShouldSetResumeFrom()
529 {
530 logTestStart();
531
532 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
533 Commandline cli = new Commandline();
534
535 tcb.setReactorBehavior( newRequest().setResumeFrom( ":module3" ), cli );
536
537 assertArgumentsPresentInOrder( cli, "-rf", ":module3" );
538 }
539
540 public void testShouldSetStrictChecksumPolityFlagFromRequest()
541 {
542 logTestStart();
543
544 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
545 Commandline cli = new Commandline();
546
547 tcb.setFlags( newRequest().setGlobalChecksumPolicy( InvocationRequest.CHECKSUM_POLICY_FAIL ), cli );
548
549 assertArgumentsPresent( cli, Collections.singleton( "-C" ) );
550 }
551
552 public void testShouldSetLaxChecksumPolicyFlagFromRequest()
553 {
554 logTestStart();
555
556 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
557 Commandline cli = new Commandline();
558
559 tcb.setFlags( newRequest().setGlobalChecksumPolicy( InvocationRequest.CHECKSUM_POLICY_WARN ), cli );
560
561 assertArgumentsPresent( cli, Collections.singleton( "-c" ) );
562 }
563
564 public void testShouldSetFailAtEndFlagFromRequest()
565 {
566 logTestStart();
567
568 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
569 Commandline cli = new Commandline();
570
571 tcb.setReactorBehavior( newRequest().setFailureBehavior( InvocationRequest.REACTOR_FAIL_AT_END ), cli );
572
573 assertArgumentsPresent( cli, Collections.singleton( "-fae" ) );
574 }
575
576 public void testShouldSetFailNeverFlagFromRequest()
577 {
578 logTestStart();
579
580 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
581 Commandline cli = new Commandline();
582
583 tcb.setReactorBehavior( newRequest().setFailureBehavior( InvocationRequest.REACTOR_FAIL_NEVER ), cli );
584
585 assertArgumentsPresent( cli, Collections.singleton( "-fn" ) );
586 }
587
588 public void testShouldUseDefaultOfFailFastWhenSpecifiedInRequest()
589 {
590 logTestStart();
591
592 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
593 Commandline cli = new Commandline();
594
595 tcb.setReactorBehavior( newRequest().setFailureBehavior( InvocationRequest.REACTOR_FAIL_FAST ), cli );
596
597 Set<String> banned = new HashSet<String>();
598 banned.add( "-fae" );
599 banned.add( "-fn" );
600
601 assertArgumentsNotPresent( cli, banned );
602 }
603
604 public void testShouldSpecifyFileOptionUsingNonStandardPomFileLocation()
605 throws Exception
606 {
607 logTestStart();
608
609 File tmpDir = getTempDir();
610 File base = new File( tmpDir, "invoker-tests" );
611
612 toDelete.add( base );
613
614 File projectDir = new File( base, "file-option-nonstd-pom-file-location" ).getCanonicalFile();
615
616 projectDir.mkdirs();
617
618 File pomFile = createDummyFile( projectDir, "non-standard-pom.xml" ).getCanonicalFile();
619
620 Commandline cli = new Commandline();
621
622 InvocationRequest req = newRequest().setPomFile( pomFile );
623
624 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
625 tcb.setEnvironmentPaths( req, cli );
626 tcb.setPomLocation( req, cli );
627
628 assertEquals( projectDir, cli.getWorkingDirectory() );
629
630 Set<String> args = new HashSet<String>();
631 args.add( "-f" );
632 args.add( "non-standard-pom.xml" );
633
634 assertArgumentsPresent( cli, args );
635 }
636
637 public void testShouldSpecifyFileOptionUsingNonStandardPomInBasedir()
638 throws Exception
639 {
640 logTestStart();
641
642 File tmpDir = getTempDir();
643 File base = new File( tmpDir, "invoker-tests" );
644
645 toDelete.add( base );
646
647 File projectDir = new File( base, "file-option-nonstd-basedir" ).getCanonicalFile();
648
649 projectDir.mkdirs();
650
651 File basedir = createDummyFile( projectDir, "non-standard-pom.xml" ).getCanonicalFile();
652
653 Commandline cli = new Commandline();
654
655 InvocationRequest req = newRequest().setBaseDirectory( basedir );
656
657 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
658 tcb.setEnvironmentPaths( req, cli );
659 tcb.setPomLocation( req, cli );
660
661 assertEquals( projectDir, cli.getWorkingDirectory() );
662
663 Set<String> args = new HashSet<String>();
664 args.add( "-f" );
665 args.add( "non-standard-pom.xml" );
666
667 assertArgumentsPresent( cli, args );
668 }
669
670 public void testShouldNotSpecifyFileOptionUsingStandardPomFileLocation()
671 throws Exception
672 {
673 logTestStart();
674
675 File tmpDir = getTempDir();
676 File base = new File( tmpDir, "invoker-tests" );
677
678 toDelete.add( base );
679
680 File projectDir = new File( base, "std-pom-file-location" ).getCanonicalFile();
681
682 projectDir.mkdirs();
683
684 File pomFile = createDummyFile( projectDir, "pom.xml" ).getCanonicalFile();
685
686 Commandline cli = new Commandline();
687
688 InvocationRequest req = newRequest().setPomFile( pomFile );
689
690 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
691 tcb.setEnvironmentPaths( req, cli );
692 tcb.setPomLocation( req, cli );
693
694 assertEquals( projectDir, cli.getWorkingDirectory() );
695
696 Set<String> args = new HashSet<String>();
697 args.add( "-f" );
698 args.add( "pom.xml" );
699
700 assertArgumentsNotPresent( cli, args );
701 }
702
703 public void testShouldNotSpecifyFileOptionUsingStandardPomInBasedir()
704 throws Exception
705 {
706 logTestStart();
707
708 File tmpDir = getTempDir();
709 File base = new File( tmpDir, "invoker-tests" );
710
711 toDelete.add( base );
712
713 File projectDir = new File( base, "std-basedir-is-pom-file" ).getCanonicalFile();
714
715 projectDir.mkdirs();
716
717 File basedir = createDummyFile( projectDir, "pom.xml" ).getCanonicalFile();
718
719 Commandline cli = new Commandline();
720
721 InvocationRequest req = newRequest().setBaseDirectory( basedir );
722
723 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
724 tcb.setEnvironmentPaths( req, cli );
725 tcb.setPomLocation( req, cli );
726
727 assertEquals( projectDir, cli.getWorkingDirectory() );
728
729 Set<String> args = new HashSet<String>();
730 args.add( "-f" );
731 args.add( "pom.xml" );
732
733 assertArgumentsNotPresent( cli, args );
734 }
735
736 public void testShouldUseDefaultPomFileWhenBasedirSpecifiedWithoutPomFileName()
737 throws Exception
738 {
739 logTestStart();
740
741 File tmpDir = getTempDir();
742 File base = new File( tmpDir, "invoker-tests" );
743
744 toDelete.add( base );
745
746 File projectDir = new File( base, "std-basedir-no-pom-filename" ).getCanonicalFile();
747
748 projectDir.mkdirs();
749
750 Commandline cli = new Commandline();
751
752 InvocationRequest req = newRequest().setBaseDirectory( projectDir );
753
754 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
755 tcb.setEnvironmentPaths( req, cli );
756 tcb.setPomLocation( req, cli );
757
758 assertEquals( projectDir, cli.getWorkingDirectory() );
759
760 Set<String> args = new HashSet<String>();
761 args.add( "-f" );
762 args.add( "pom.xml" );
763
764 assertArgumentsNotPresent( cli, args );
765 }
766
767 public void testShouldSpecifyPomFileWhenBasedirSpecifiedWithPomFileName()
768 throws Exception
769 {
770 logTestStart();
771
772 File tmpDir = getTempDir();
773 File base = new File( tmpDir, "invoker-tests" );
774
775 toDelete.add( base );
776
777 File projectDir = new File( base, "std-basedir-with-pom-filename" ).getCanonicalFile();
778
779 projectDir.mkdirs();
780
781 Commandline cli = new Commandline();
782
783 InvocationRequest req = newRequest().setBaseDirectory( projectDir ).setPomFileName( "non-standard-pom.xml" );
784
785 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
786 tcb.setEnvironmentPaths( req, cli );
787 tcb.setPomLocation( req, cli );
788
789 assertEquals( projectDir, cli.getWorkingDirectory() );
790
791 Set<String> args = new HashSet<String>();
792 args.add( "-f" );
793 args.add( "non-standard-pom.xml" );
794
795 assertArgumentsPresent( cli, args );
796 }
797
798 public void testShouldSpecifyCustomUserSettingsLocationFromRequest()
799 throws Exception
800 {
801 logTestStart();
802
803 File tmpDir = getTempDir();
804 File base = new File( tmpDir, "invoker-tests" );
805
806 toDelete.add( base );
807
808 File projectDir = new File( base, "custom-settings" ).getCanonicalFile();
809
810 projectDir.mkdirs();
811
812 File settingsFile = createDummyFile( projectDir, "settings.xml" );
813
814 Commandline cli = new Commandline();
815
816 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
817 tcb.setSettingsLocation( newRequest().setUserSettingsFile( settingsFile ), cli );
818
819 Set<String> args = new HashSet<String>();
820 args.add( "-s" );
821 args.add( settingsFile.getCanonicalPath() );
822
823 assertArgumentsPresent( cli, args );
824 }
825
826 public void testShouldSpecifyCustomGlobalSettingsLocationFromRequest()
827 throws Exception
828 {
829 logTestStart();
830
831 File tmpDir = getTempDir();
832 File base = new File( tmpDir, "invoker-tests" );
833
834 toDelete.add( base );
835
836 File projectDir = new File( base, "custom-settings" ).getCanonicalFile();
837
838 projectDir.mkdirs();
839
840 File settingsFile = createDummyFile( projectDir, "settings.xml" );
841
842 Commandline cli = new Commandline();
843
844 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
845 tcb.setSettingsLocation( newRequest().setGlobalSettingsFile( settingsFile ), cli );
846
847 Set<String> args = new HashSet<String>();
848 args.add( "-gs" );
849 args.add( settingsFile.getCanonicalPath() );
850
851 assertArgumentsPresent( cli, args );
852 }
853
854 public void testShouldSpecifyCustomToolchainsLocationFromRequest()
855 throws Exception
856 {
857 logTestStart();
858
859 File tmpDir = getTempDir();
860 File base = new File( tmpDir, "invoker-tests" );
861
862 toDelete.add( base );
863
864 File projectDir = new File( base, "custom-toolchains" ).getCanonicalFile();
865
866 projectDir.mkdirs();
867
868 File toolchainsFile = createDummyFile( projectDir, "toolchains.xml" );
869
870 Commandline cli = new Commandline();
871
872 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
873 tcb.setToolchainsLocation( newRequest().setToolchainsFile( toolchainsFile ), cli );
874
875 Set<String> args = new HashSet<String>();
876 args.add( "-t" );
877 args.add( toolchainsFile.getCanonicalPath() );
878
879 assertArgumentsPresent( cli, args );
880 }
881 public void testShouldSpecifyCustomPropertyFromRequest()
882 throws IOException
883 {
884 logTestStart();
885
886 Commandline cli = new Commandline();
887
888 Properties properties = new Properties();
889 properties.setProperty( "key", "value" );
890
891 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
892 tcb.setProperties( newRequest().setProperties( properties ), cli );
893
894 assertArgumentsPresentInOrder( cli, "-D", "key=value" );
895 }
896
897 public void testShouldSpecifyCustomPropertyWithSpacesInValueFromRequest()
898 throws IOException
899 {
900 logTestStart();
901
902 Commandline cli = new Commandline();
903
904 Properties properties = new Properties();
905 properties.setProperty( "key", "value with spaces" );
906
907 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
908 tcb.setProperties( newRequest().setProperties( properties ), cli );
909
910 assertArgumentsPresentInOrder( cli, "-D", "key=value with spaces" );
911 }
912
913 public void testShouldSpecifyCustomPropertyWithSpacesInKeyFromRequest()
914 throws IOException
915 {
916 logTestStart();
917
918 Commandline cli = new Commandline();
919
920 Properties properties = new Properties();
921 properties.setProperty( "key with spaces", "value with spaces" );
922
923 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
924 tcb.setProperties( newRequest().setProperties( properties ), cli );
925
926 assertArgumentsPresentInOrder( cli, "-D", "key with spaces=value with spaces" );
927 }
928
929 public void testShouldSpecifySingleGoalFromRequest()
930 throws IOException
931 {
932 logTestStart();
933
934 Commandline cli = new Commandline();
935
936 List<String> goals = new ArrayList<String>();
937 goals.add( "test" );
938
939 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
940 tcb.setGoals( newRequest().setGoals( goals ), cli );
941
942 assertArgumentsPresent( cli, Collections.singleton( "test" ) );
943 }
944
945 public void testShouldSpecifyTwoGoalsFromRequest()
946 throws IOException
947 {
948 logTestStart();
949
950 Commandline cli = new Commandline();
951
952 List<String> goals = new ArrayList<String>();
953 goals.add( "test" );
954 goals.add( "clean" );
955
956 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
957 tcb.setGoals( newRequest().setGoals( goals ), cli );
958
959 assertArgumentsPresent( cli, new HashSet<String>( goals ) );
960 assertArgumentsPresentInOrder( cli, goals );
961 }
962
963 public void testShouldSpecifyThreadsFromRequest()
964 throws IOException
965 {
966 logTestStart();
967
968 Commandline cli = new Commandline();
969
970 TestCommandLineBuilder tcb = new TestCommandLineBuilder();
971 tcb.setThreads( newRequest().setThreads( "2.0C" ), cli );
972
973 assertArgumentsPresentInOrder( cli, "-T", "2.0C");
974 }
975 public void testBuildTypicalMavenInvocationEndToEnd()
976 throws Exception
977 {
978 logTestStart();
979 File mavenDir = setupTempMavenHomeIfMissing();
980
981 InvocationRequest request = newRequest();
982
983 File tmpDir = getTempDir();
984 File projectDir = new File( tmpDir, "invoker-tests/typical-end-to-end-cli-build" );
985
986 projectDir.mkdirs();
987 toDelete.add( projectDir.getParentFile() );
988
989 request.setBaseDirectory( projectDir );
990
991 Set<String> expectedArgs = new HashSet<String>();
992 Set<String> bannedArgs = new HashSet<String>();
993
994 createDummyFile( projectDir, "pom.xml" );
995
996 bannedArgs.add( "-f" );
997 bannedArgs.add( "pom.xml" );
998
999 Properties properties = new Properties();
1000
1001 properties.setProperty( "maven.tests.skip", "true" );
1002
1003 expectedArgs.add( "maven.tests.skip=true" );
1004
1005 request.setProperties( properties );
1006
1007 request.setOffline( true );
1008
1009 expectedArgs.add( "-o" );
1010
1011 List<String> goals = new ArrayList<String>();
1012
1013 goals.add( "post-clean" );
1014 goals.add( "deploy" );
1015 goals.add( "site-deploy" );
1016
1017 request.setGoals( goals );
1018
1019 MavenCommandLineBuilder commandLineBuilder = new MavenCommandLineBuilder();
1020
1021 Commandline commandline = commandLineBuilder.build( request );
1022
1023 assertArgumentsPresent( commandline, expectedArgs );
1024 assertArgumentsNotPresent( commandline, bannedArgs );
1025 assertArgumentsPresentInOrder( commandline, goals );
1026
1027 File mavenFile;
1028 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
1029 {
1030 mavenFile = new File( mavenDir, "bin/mvn.bat" );
1031 }
1032 else
1033 {
1034 mavenFile = new File( mavenDir, "bin/mvn" );
1035 }
1036
1037 String executable = commandline.getExecutable();
1038 System.out.println( "Executable is: " + executable );
1039
1040 assertTrue( executable.indexOf( mavenFile.getCanonicalPath() ) > -1 );
1041 assertEquals( projectDir.getCanonicalPath(), commandline.getWorkingDirectory().getCanonicalPath() );
1042 }
1043
1044 public void testShouldSetEnvVar_MAVEN_TERMINATE_CMD()
1045 throws Exception
1046 {
1047 logTestStart();
1048 setupTempMavenHomeIfMissing();
1049
1050 InvocationRequest request = newRequest();
1051
1052 File tmpDir = getTempDir();
1053 File projectDir = new File( tmpDir, "invoker-tests/maven-terminate-cmd-options-set" );
1054
1055 projectDir.mkdirs();
1056 toDelete.add( projectDir.getParentFile() );
1057
1058 request.setBaseDirectory( projectDir );
1059
1060 createDummyFile( projectDir, "pom.xml" );
1061
1062 List<String> goals = new ArrayList<String>();
1063
1064 goals.add( "clean" );
1065 request.setGoals( goals );
1066
1067 MavenCommandLineBuilder commandLineBuilder = new MavenCommandLineBuilder();
1068
1069 Commandline commandline = commandLineBuilder.build( request );
1070
1071 String[] environmentVariables = commandline.getEnvironmentVariables();
1072 String envVarMavenTerminateCmd = null;
1073 for ( int i = 0; i < environmentVariables.length; i++ )
1074 {
1075 String envVar = environmentVariables[i];
1076 if ( envVar.startsWith( "MAVEN_TERMINATE_CMD=" ) )
1077 {
1078 envVarMavenTerminateCmd = envVar;
1079 break;
1080 }
1081 }
1082 assertEquals( "MAVEN_TERMINATE_CMD=on", envVarMavenTerminateCmd );
1083
1084 }
1085
1086 public void testShouldInsertActivatedProfiles()
1087 throws Exception
1088 {
1089 setupTempMavenHomeIfMissing();
1090
1091 String profile1 = "profile-1";
1092 String profile2 = "profile-2";
1093
1094 InvocationRequest request = newRequest();
1095
1096 List<String> profiles = new ArrayList<String>();
1097 profiles.add( profile1 );
1098 profiles.add( profile2 );
1099
1100 request.setProfiles( profiles );
1101
1102 MavenCommandLineBuilder commandLineBuilder = new MavenCommandLineBuilder();
1103
1104 Commandline commandline = commandLineBuilder.build( request );
1105
1106 assertArgumentsPresentInOrder( commandline, "-P", profile1 + "," + profile2 );
1107 }
1108
1109 public void testMvnCommand() throws Exception
1110 {
1111 MavenCommandLineBuilder commandLineBuilder = new MavenCommandLineBuilder();
1112 File mavenExecutable = new File ( "mvnDebug" );
1113 commandLineBuilder.setMavenExecutable( mavenExecutable );
1114 File executable = commandLineBuilder.findMavenExecutable();
1115 assertTrue( "Expected executable to exist", executable.exists() );
1116 assertTrue( "Expected executable to be absolute", executable.isAbsolute() );
1117 }
1118
1119 public void setUp()
1120 {
1121 sysProps = System.getProperties();
1122
1123 Properties p = new Properties( sysProps );
1124
1125 System.setProperties( p );
1126 }
1127
1128 public void tearDown()
1129 throws IOException
1130 {
1131 System.setProperties( sysProps );
1132
1133 for ( File file : toDelete )
1134 {
1135 if ( file.exists() )
1136 {
1137 if ( file.isDirectory() )
1138 {
1139 FileUtils.deleteDirectory( file );
1140 }
1141 else
1142 {
1143 file.delete();
1144 }
1145 }
1146 }
1147 }
1148
1149
1150 private void logTestStart()
1151 {
1152 NullPointerException npe = new NullPointerException();
1153 StackTraceElement element = npe.getStackTrace()[1];
1154
1155 System.out.println( "Starting: " + element.getMethodName() );
1156 }
1157
1158 private void assertArgumentsPresentInOrder( Commandline cli, String... expected )
1159 {
1160 assertArgumentsPresentInOrder( cli, Arrays.asList( expected ) );
1161 }
1162
1163 private void assertArgumentsPresentInOrder( Commandline cli, List<String> expected )
1164 {
1165 String[] arguments = cli.getArguments();
1166
1167 int expectedCounter = 0;
1168
1169 for ( int i = 0; i < arguments.length; i++ )
1170 {
1171 if ( arguments[i].equals( expected.get( expectedCounter ) ) )
1172 {
1173 expectedCounter++;
1174 }
1175 }
1176
1177 assertEquals( "Arguments: " + expected + " were not found or are in the wrong order: "
1178 + Arrays.asList( arguments ), expected.size(), expectedCounter );
1179 }
1180
1181 private void assertArgumentsPresent( Commandline cli, Set<String> requiredArgs )
1182 {
1183 String[] argv = cli.getArguments();
1184 List<String> args = Arrays.asList( argv );
1185
1186 for ( String arg : requiredArgs )
1187 {
1188 assertTrue( "Command-line argument: \'" + arg + "\' is missing in " + args, args.contains( arg ) );
1189 }
1190 }
1191
1192 private void assertArgumentsNotPresent( Commandline cli, Set<String> bannedArgs )
1193 {
1194 String[] argv = cli.getArguments();
1195 List<String> args = Arrays.asList( argv );
1196
1197 for ( String arg : bannedArgs )
1198 {
1199 assertFalse( "Command-line argument: \'" + arg + "\' should not be present.", args.contains( arg ) );
1200 }
1201 }
1202
1203 private File createDummyFile( File directory, String filename )
1204 throws IOException
1205 {
1206 File dummyFile = new File( directory, filename );
1207
1208 FileWriter writer = null;
1209 try
1210 {
1211 writer = new FileWriter( dummyFile );
1212 writer.write( "This is a dummy file." );
1213 }
1214 finally
1215 {
1216 IOUtil.close( writer );
1217 }
1218
1219 toDelete.add( dummyFile );
1220
1221 return dummyFile;
1222 }
1223
1224 private static final class TestCommandLineBuilder
1225 extends MavenCommandLineBuilder
1226 {
1227 public void checkRequiredState()
1228 throws IOException
1229 {
1230 super.checkRequiredState();
1231 }
1232
1233 public File findMavenExecutable()
1234 throws CommandLineConfigurationException, IOException
1235 {
1236 return super.findMavenExecutable();
1237 }
1238
1239 public void setEnvironmentPaths( InvocationRequest request, Commandline cli )
1240 {
1241 super.setEnvironmentPaths( request, cli );
1242 }
1243
1244 public void setFlags( InvocationRequest request, Commandline cli )
1245 {
1246 super.setFlags( request, cli );
1247 }
1248
1249 public void setGoals( InvocationRequest request, Commandline cli )
1250 {
1251 super.setGoals( request, cli );
1252 }
1253
1254 public void setPomLocation( InvocationRequest request, Commandline cli )
1255 {
1256 super.setPomLocation( request, cli );
1257 }
1258
1259 public void setProperties( InvocationRequest request, Commandline cli )
1260 {
1261 super.setProperties( request, cli );
1262 }
1263
1264 public void setReactorBehavior( InvocationRequest request, Commandline cli )
1265 {
1266 super.setReactorBehavior( request, cli );
1267 }
1268
1269 public void setSettingsLocation( InvocationRequest request, Commandline cli )
1270 {
1271 super.setSettingsLocation( request, cli );
1272 }
1273
1274 public void setShellEnvironment( InvocationRequest request, Commandline cli )
1275 throws CommandLineConfigurationException
1276 {
1277 super.setShellEnvironment( request, cli );
1278 }
1279
1280 }
1281
1282 private File getTempDir()
1283 throws Exception
1284 {
1285 return new File( System.getProperty( "java.io.tmpdir" ) ).getCanonicalFile();
1286 }
1287
1288 private InvocationRequest newRequest()
1289 {
1290 return new DefaultInvocationRequest();
1291 }
1292
1293 }