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 org.apache.maven.shared.utils.Os;
34 import org.apache.maven.shared.utils.cli.Commandline;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.TemporaryFolder;
40
41 import static org.hamcrest.CoreMatchers.is;
42 import static org.hamcrest.CoreMatchers.notNullValue;
43 import static org.junit.Assert.assertArrayEquals;
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertFalse;
46 import static org.junit.Assert.assertTrue;
47 import static org.junit.Assert.fail;
48 import static org.junit.Assume.assumeThat;
49
50 public class MavenCommandLineBuilderTest
51 {
52 @Rule
53 public TemporaryFolder temporaryFolder = new TemporaryFolder();
54
55 private Properties sysProps;
56 private File lrd;
57 private MavenCommandLineBuilder mclb = new MavenCommandLineBuilder();
58 private Commandline cli = new Commandline();
59
60 @Before
61 public void setUp() throws IOException
62 {
63 sysProps = System.getProperties();
64 Properties p = new Properties( sysProps );
65
66 System.setProperties( p );
67
68 lrd = temporaryFolder.newFile();
69
70 }
71
72 @After
73 public void tearDown()
74 {
75 System.setProperties( sysProps );
76 }
77
78
79 @Test
80 public void testShouldFailToSetLocalRepoLocationGloballyWhenItIsAFile()
81 {
82
83 mclb.setLocalRepositoryDirectory( lrd );
84
85 try
86 {
87 mclb.setLocalRepository( newRequest(), cli );
88 fail( "Should not set local repo location to point to a file." );
89 }
90 catch ( IllegalArgumentException expected )
91 {
92 }
93 }
94
95 @Test
96 public void testShouldFailToSetLocalRepoLocationFromRequestWhenItIsAFile()
97 {
98 InvocationRequest request = newRequest().setLocalRepositoryDirectory( lrd );
99 try
100 {
101 mclb.setLocalRepository( request, cli );
102 fail( "Should not set local repo location to point to a file." );
103 }
104 catch ( IllegalArgumentException expected )
105 {
106 }
107 }
108
109 @Test
110 public void testShouldSetLocalRepoLocationGlobally() throws IOException
111 {
112 File lrd = temporaryFolder.newFolder( "workdir" ).getCanonicalFile();
113 mclb.setLocalRepositoryDirectory( lrd );
114 mclb.setLocalRepository( newRequest(), cli );
115
116 assertArgumentsPresentInOrder( cli, "-D", "maven.repo.local=" + lrd.getPath() );
117 }
118
119 @Test
120 public void testShouldSetLocalRepoLocationFromRequest()
121 throws Exception
122 {
123 File lrd = temporaryFolder.newFolder( "workdir" ).getCanonicalFile();
124 mclb.setLocalRepository( newRequest().setLocalRepositoryDirectory( lrd ), cli );
125
126 assertArgumentsPresentInOrder( cli, "-D", "maven.repo.local=" + lrd.getPath() );
127 }
128
129 @Test
130 public void testRequestProvidedLocalRepoLocationShouldOverrideGlobal()
131 throws Exception
132 {
133 File lrd = temporaryFolder.newFolder( "workdir" ).getCanonicalFile();
134 File glrd = temporaryFolder.newFolder( "global-workdir" ).getCanonicalFile();
135
136 mclb.setLocalRepositoryDirectory( glrd );
137 mclb.setLocalRepository( newRequest().setLocalRepositoryDirectory( lrd ), cli );
138
139 assertArgumentsPresentInOrder( cli, "-D", "maven.repo.local=" + lrd.getPath() );
140 }
141
142 @Test
143 public void testShouldSetWorkingDirectoryGlobally()
144 throws Exception
145 {
146 File wd = temporaryFolder.newFolder( "workdir" );
147
148 mclb.setBaseDirectory( wd );
149 Commandline commandline = mclb.build( newRequest() );
150
151 assertEquals( commandline.getWorkingDirectory(), wd.getCanonicalFile() );
152 }
153
154 @Test
155 public void testShouldSetWorkingDirectoryFromRequest()
156 throws Exception
157 {
158 File wd = temporaryFolder.newFolder( "workdir" );
159
160 InvocationRequest req = newRequest();
161 req.setBaseDirectory( wd );
162
163 mclb.setupBaseDirectory( req );
164
165 assertEquals( mclb.getBaseDirectory(), wd.getCanonicalFile() );
166 }
167
168 @Test
169 public void testRequestProvidedWorkingDirectoryShouldOverrideGlobal()
170 throws Exception
171 {
172 File wd = temporaryFolder.newFolder( "workdir" );
173 File gwd = temporaryFolder.newFolder( "global-workdir" );
174
175 mclb.setBaseDirectory( gwd );
176
177 InvocationRequest req = newRequest();
178 req.setBaseDirectory( wd );
179
180 mclb.setupBaseDirectory( req );
181
182 assertEquals( mclb.getBaseDirectory(), wd.getCanonicalFile() );
183 }
184
185 @Test
186 public void testShouldUseSystemOutLoggerWhenNoneSpecified()
187 throws Exception
188 {
189 setupTempMavenHomeIfMissing( false );
190
191 mclb.checkRequiredState();
192 }
193
194 private File setupTempMavenHomeIfMissing( boolean forceDummy )
195 throws Exception
196 {
197 String mavenHome = System.getProperty( "maven.home" );
198
199 File appDir;
200
201 if ( forceDummy || ( mavenHome == null ) || !new File( mavenHome ).exists() )
202 {
203 appDir = temporaryFolder.newFolder( "invoker-tests", "maven-home" );
204
205 File binDir = new File( appDir, "bin" );
206 binDir.mkdirs();
207
208 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
209 {
210 createDummyFile( binDir, "mvn.bat" );
211 }
212 else
213 {
214 createDummyFile( binDir, "mvn" );
215 }
216
217 Properties props = System.getProperties();
218 props.setProperty( "maven.home", appDir.getCanonicalPath() );
219
220 System.setProperties( props );
221 }
222 else
223 {
224 appDir = new File( mavenHome );
225 }
226
227 return appDir;
228 }
229
230 @Test
231 public void testShouldFailIfLoggerSetToNull()
232 {
233 mclb.setLogger( null );
234
235 try
236 {
237 mclb.checkRequiredState();
238 fail( "Should not allow execution to proceed when logger is missing." );
239 }
240 catch ( IllegalStateException expected )
241 {
242 }
243 }
244
245 @Test
246 public void testShouldFindDummyMavenExecutable()
247 throws Exception
248 {
249 File dummyMavenHomeBin = temporaryFolder.newFolder( "invoker-tests", "dummy-maven-home", "bin" );
250
251 File check;
252 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
253 {
254 check = createDummyFile( dummyMavenHomeBin, "mvn.bat" );
255 }
256 else
257 {
258 check = createDummyFile( dummyMavenHomeBin, "mvn" );
259 }
260
261 mclb.setMavenHome( dummyMavenHomeBin.getParentFile() );
262 mclb.setupMavenExecutable( newRequest() );
263
264 assertEquals( check.getCanonicalPath(), mclb.getMavenExecutable().getCanonicalPath() );
265 }
266
267 @Test
268 public void testShouldFindDummyMavenExecutableWithMavenHomeFromRequest()
269 throws Exception
270 {
271 File dummyMavenHomeBin = temporaryFolder.newFolder( "invoker-tests", "dummy-maven-home", "bin" );
272
273 File check;
274 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
275 {
276 check = createDummyFile( dummyMavenHomeBin, "mvn.bat" );
277 }
278 else
279 {
280 check = createDummyFile( dummyMavenHomeBin, "mvn" );
281 }
282
283
284 mclb.setMavenHome( new File( "not-present-1234" ) );
285 mclb.build( newRequest().setMavenHome( dummyMavenHomeBin.getParentFile() ) );
286
287 assertEquals( check.getCanonicalPath(), mclb.getMavenExecutable().getCanonicalPath() );
288 }
289
290 @Test
291 public void testShouldSetBatchModeFlagFromRequest()
292 {
293
294 mclb.setFlags( newRequest().setBatchMode( true ), cli );
295
296 assertArgumentsPresent( cli, Collections.singleton( "-B" ) );
297 }
298
299 @Test
300 public void testShouldSetOfflineFlagFromRequest()
301 {
302
303 mclb.setFlags( newRequest().setOffline( true ), cli );
304
305 assertArgumentsPresent( cli, Collections.singleton( "-o" ) );
306 }
307
308 @Test
309 public void testShouldSetUpdateSnapshotsFlagFromRequest()
310 {
311
312 mclb.setFlags( newRequest().setUpdateSnapshots( true ), cli );
313
314 assertArgumentsPresent( cli, Collections.singleton( "-U" ) );
315 }
316
317 @Test
318 public void testShouldSetDebugFlagFromRequest()
319 {
320
321 mclb.setFlags( newRequest().setDebug( true ), cli );
322
323 assertArgumentsPresent( cli, Collections.singleton( "-X" ) );
324 }
325
326 @Test
327 public void testShouldSetErrorFlagFromRequest()
328 {
329
330 mclb.setFlags( newRequest().setShowErrors( true ), cli );
331
332 assertArgumentsPresent( cli, Collections.singleton( "-e" ) );
333 }
334
335 @Test
336 public void testShouldSetQuietFlagFromRequest()
337 {
338
339 mclb.setFlags( newRequest().setQuiet( true ), cli );
340
341 assertArgumentsPresent( cli, Collections.singleton( "-q" ) );
342 }
343
344 @Test
345 public void testShouldSetNonRecursiveFlagsFromRequest()
346 {
347 mclb.setFlags( newRequest().setRecursive( false ), cli );
348
349 assertArgumentsPresent( cli, Collections.singleton( "-N" ) );
350 }
351
352 @Test
353 public void testShouldSetShowVersionFlagsFromRequest()
354 {
355 mclb.setFlags( newRequest().setShowVersion( true ), cli );
356
357 assertArgumentsPresent( cli, Collections.singleton( "-V" ) );
358 }
359
360 @Test
361 public void testDebugOptionShouldMaskShowErrorsOption()
362 {
363
364 mclb.setFlags( newRequest().setDebug( true ).setShowErrors( true ), cli );
365
366 assertArgumentsPresent( cli, Collections.singleton( "-X" ) );
367 assertArgumentsNotPresent( cli, Collections.singleton( "-e" ) );
368 }
369
370 @Test
371 public void testShouldSetBuilderIdOptionsFromRequest()
372 {
373 mclb.setFlags( newRequest().setBuilder( "builder-id-123" ), cli );
374
375 assertArgumentsPresentInOrder( cli, "-b", "builder-id-123" );
376 }
377
378 @Test
379 public void testAlsoMake()
380 {
381
382 mclb.setReactorBehavior( newRequest().setAlsoMake( true ), cli );
383
384
385 assertArgumentsNotPresent( cli, Collections.singleton( "-am" ) );
386 }
387
388 @Test
389 public void testProjectsAndAlsoMake()
390 {
391
392 mclb.setReactorBehavior( newRequest().setProjects( Collections.singletonList( "proj1" ) ).setAlsoMake( true ),
393 cli );
394
395 assertArgumentsPresentInOrder( cli, "-pl", "proj1", "-am" );
396 }
397
398 @Test
399 public void testAlsoMakeDependents()
400 {
401
402 mclb.setReactorBehavior( newRequest().setAlsoMakeDependents( true ), cli );
403
404
405 assertArgumentsNotPresent( cli, Collections.singleton( "-amd" ) );
406 }
407
408 @Test
409 public void testProjectsAndAlsoMakeDependents()
410 {
411
412 mclb.setReactorBehavior( newRequest().setProjects( Collections.singletonList( "proj1" ) ).setAlsoMakeDependents( true ),
413 cli );
414
415 assertArgumentsPresentInOrder( cli, "-pl", "proj1", "-amd" );
416 }
417
418 @Test
419 public void testProjectsAndAlsoMakeAndAlsoMakeDependents()
420 {
421
422 mclb.setReactorBehavior( newRequest().setProjects( Collections.singletonList( "proj1" ) ).setAlsoMake( true ).setAlsoMakeDependents( true ),
423 cli );
424
425 assertArgumentsPresentInOrder( cli, "-pl", "proj1", "-am", "-amd" );
426 }
427
428 @Test
429 public void testShouldSetResumeFrom()
430 {
431
432 mclb.setReactorBehavior( newRequest().setResumeFrom( ":module3" ), cli );
433
434 assertArgumentsPresentInOrder( cli, "-rf", ":module3" );
435 }
436
437 @Test
438 public void testShouldSetStrictChecksumPolityFlagFromRequest()
439 {
440
441 mclb.setFlags( newRequest().setGlobalChecksumPolicy( InvocationRequest.CheckSumPolicy.Fail ), cli );
442
443 assertArgumentsPresent( cli, Collections.singleton( "-C" ) );
444 }
445
446 @Test
447 public void testShouldSetLaxChecksumPolicyFlagFromRequest()
448 {
449
450 mclb.setFlags( newRequest().setGlobalChecksumPolicy( InvocationRequest.CheckSumPolicy.Warn ), cli );
451
452 assertArgumentsPresent( cli, Collections.singleton( "-c" ) );
453 }
454
455 @Test
456 public void testShouldSetFailAtEndFlagFromRequest()
457 {
458
459 mclb.setReactorBehavior( newRequest().setReactorFailureBehavior( InvocationRequest.ReactorFailureBehavior.FailAtEnd ),
460 cli );
461
462 assertArgumentsPresent( cli, Collections.singleton( "-fae" ) );
463 }
464
465 @Test
466 public void testShouldSetFailNeverFlagFromRequest()
467 {
468
469 mclb.setReactorBehavior( newRequest().setReactorFailureBehavior( InvocationRequest.ReactorFailureBehavior.FailNever ),
470 cli );
471
472 assertArgumentsPresent( cli, Collections.singleton( "-fn" ) );
473 }
474
475
476 @Test
477 public void testShouldAddArg() throws CommandLineConfigurationException
478 {
479 InvocationRequest request = newRequest()
480 .addArg( "arg1" )
481 .addArg( "arg2" )
482 .setQuiet( true )
483 .setBuilder( "bId" );
484
485 Commandline commandline = mclb.build( request );
486
487 String[] arguments = commandline.getArguments();
488
489 assertArrayEquals( Arrays.asList( "-b", "bId", "-q", "arg1", "arg2" ).toArray(), arguments );
490 }
491
492 @Test
493 public void testShouldUseDefaultOfFailFastWhenSpecifiedInRequest()
494 {
495
496 mclb.setReactorBehavior( newRequest().setReactorFailureBehavior( InvocationRequest.ReactorFailureBehavior.FailFast ),
497 cli );
498
499 Set<String> banned = new HashSet<>();
500 banned.add( "-fae" );
501 banned.add( "-fn" );
502
503 assertArgumentsNotPresent( cli, banned );
504 }
505
506 @Test
507 public void testShouldSetNoTransferProgressFlagFromRequest()
508 {
509 mclb.setFlags( newRequest().setNoTransferProgress( true ), cli );
510 assertArgumentsPresent( cli, Collections.singleton( "-ntp" ) );
511 }
512
513 @Test
514 public void testShouldSpecifyFileOptionUsingNonStandardPomFileLocation()
515 throws Exception
516 {
517 File projectDir = temporaryFolder.newFolder( "invoker-tests", "file-option-nonstd-pom-file-location" );
518
519 File pomFile = createDummyFile( projectDir, "non-standard-pom.xml" ).getCanonicalFile();
520
521
522 InvocationRequest req = newRequest().setPomFile( pomFile );
523
524 Commandline commandline = mclb.build( req );
525
526 assertEquals( projectDir.getCanonicalFile(), commandline.getWorkingDirectory() );
527
528 Set<String> args = new HashSet<>();
529 args.add( "-f" );
530 args.add( "non-standard-pom.xml" );
531
532 assertArgumentsPresent( commandline, args );
533 }
534
535 @Test
536 public void testShouldNotSpecifyFileOptionUsingStandardPomFileLocation()
537 throws Exception
538 {
539 File projectDir = temporaryFolder.newFolder( "invoker-tests", "std-pom-file-location" );
540
541 File pomFile = createDummyFile( projectDir, "pom.xml" ).getCanonicalFile();
542
543
544 InvocationRequest req = newRequest().setPomFile( pomFile );
545
546 Commandline commandline = mclb.build( req );
547
548 assertEquals( projectDir.getCanonicalFile(), commandline.getWorkingDirectory() );
549
550 Set<String> args = new HashSet<>();
551 args.add( "-f" );
552 args.add( "pom.xml" );
553
554 assertArgumentsNotPresent( commandline, args );
555 }
556
557 @Test
558 public void testShouldSetPomForOutsideWorkspace()
559 throws Exception
560 {
561 File projectDir = temporaryFolder.newFolder( "invoker-tests", "std-pom-file-location" );
562
563 File outsidePom = temporaryFolder.newFile( "pom.xml" );
564
565
566 InvocationRequest req = newRequest()
567 .setBaseDirectory( projectDir )
568 .setPomFile( outsidePom );
569
570 Commandline commandline = mclb.build( req );
571
572 assertEquals( projectDir.getCanonicalFile(), commandline.getWorkingDirectory() );
573
574 Set<String> args = new HashSet<>();
575 args.add( "-f" );
576 args.add( outsidePom.getCanonicalPath() );
577
578 assertArgumentsPresent( commandline, args );
579 }
580
581 @Test
582 public void testShouldNotSpecifyFileOptionUsingStandardPomInBasedir()
583 throws Exception
584 {
585 File projectDir = temporaryFolder.newFolder( "invoker-tests", "std-basedir-is-pom-file" );
586
587 File basedir = createDummyFile( projectDir, "pom.xml" ).getCanonicalFile();
588
589
590 InvocationRequest req = newRequest().setBaseDirectory( basedir );
591
592 Commandline commandline = mclb.build( req );
593
594 assertEquals( projectDir.getCanonicalFile(), commandline.getWorkingDirectory() );
595
596 Set<String> args = new HashSet<>();
597 args.add( "-f" );
598 args.add( "pom.xml" );
599
600 assertArgumentsNotPresent( commandline, args );
601 }
602
603 @Test
604 public void testShouldUseDefaultPomFileWhenBasedirSpecifiedWithoutPomFileName()
605 throws Exception
606 {
607 File projectDir = temporaryFolder.newFolder( "invoker-tests", "std-basedir-no-pom-filename" );
608
609
610 InvocationRequest req = newRequest().setBaseDirectory( projectDir );
611
612 Commandline commandline = mclb.build( req );
613
614 assertEquals( projectDir.getCanonicalFile(), commandline.getWorkingDirectory() );
615
616 Set<String> args = new HashSet<>();
617 args.add( "-f" );
618 args.add( "pom.xml" );
619
620 assertArgumentsNotPresent( commandline, args );
621 }
622
623 @Test
624 public void testShouldSpecifyPomFileWhenBasedirSpecifiedWithPomFileName()
625 throws Exception
626 {
627 File projectDir = temporaryFolder.newFolder( "invoker-tests", "std-basedir-with-pom-filename" );
628
629 InvocationRequest req = newRequest().setBaseDirectory( projectDir ).setPomFileName( "non-standard-pom.xml" );
630
631 Commandline commandline = mclb.build( req );
632
633 assertEquals( projectDir.getCanonicalFile(), commandline.getWorkingDirectory() );
634
635 Set<String> args = new HashSet<>();
636 args.add( "-f" );
637 args.add( "non-standard-pom.xml" );
638
639 assertArgumentsPresent( commandline, args );
640 }
641
642 @Test
643 public void testShouldSpecifyCustomUserSettingsLocationFromRequest()
644 throws Exception
645 {
646 File projectDir = temporaryFolder.newFolder( "invoker-tests", "custom-settings" );
647
648 File settingsFile = createDummyFile( projectDir, "settings.xml" );
649
650
651 mclb.setSettingsLocation( newRequest().setUserSettingsFile( settingsFile ), cli );
652
653 Set<String> args = new HashSet<>();
654 args.add( "-s" );
655 args.add( settingsFile.getCanonicalPath() );
656
657 assertArgumentsPresent( cli, args );
658 }
659
660 @Test
661 public void testShouldSpecifyCustomGlobalSettingsLocationFromRequest()
662 throws Exception
663 {
664 File projectDir = temporaryFolder.newFolder( "invoker-tests", "custom-settings" ).getCanonicalFile();
665
666 File settingsFile = createDummyFile( projectDir, "settings.xml" );
667
668
669 mclb.setSettingsLocation( newRequest().setGlobalSettingsFile( settingsFile ), cli );
670
671 Set<String> args = new HashSet<>();
672 args.add( "-gs" );
673 args.add( settingsFile.getCanonicalPath() );
674
675 assertArgumentsPresent( cli, args );
676 }
677
678 @Test
679 public void testShouldSpecifyCustomToolchainsLocationFromRequest()
680 throws Exception
681 {
682 File projectDir = temporaryFolder.newFolder( "invoker-tests", "custom-toolchains" );
683
684 File toolchainsFile = createDummyFile( projectDir, "toolchains.xml" );
685
686
687 mclb.setToolchainsLocation( newRequest().setToolchainsFile( toolchainsFile ), cli );
688
689 Set<String> args = new HashSet<>();
690 args.add( "-t" );
691 args.add( toolchainsFile.getCanonicalPath() );
692
693 assertArgumentsPresent( cli, args );
694 }
695
696 @Test
697 public void testShouldSpecifyCustomPropertyFromRequest()
698 {
699
700 Properties properties = new Properties();
701 properties.setProperty( "key", "value" );
702
703 mclb.setProperties( newRequest().setProperties( properties ), cli );
704
705 assertArgumentsPresentInOrder( cli, "-D", "key=value" );
706 }
707
708 @Test
709 public void testShouldSpecifyCustomPropertyWithSpacesInValueFromRequest()
710 {
711
712 Properties properties = new Properties();
713 properties.setProperty( "key", "value with spaces" );
714
715 mclb.setProperties( newRequest().setProperties( properties ), cli );
716
717 assertArgumentsPresentInOrder( cli, "-D", "key=value with spaces" );
718 }
719
720 @Test
721 public void testShouldSpecifyCustomPropertyWithSpacesInKeyFromRequest()
722 {
723
724 Properties properties = new Properties();
725 properties.setProperty( "key with spaces", "value with spaces" );
726
727 mclb.setProperties( newRequest().setProperties( properties ), cli );
728
729 assertArgumentsPresentInOrder( cli, "-D", "key with spaces=value with spaces" );
730 }
731
732 @Test
733 public void testShouldSpecifySingleGoalFromRequest() throws CommandLineConfigurationException
734 {
735
736 List<String> goals = new ArrayList<>();
737 goals.add( "test" );
738
739 mclb.setGoals( newRequest().setGoals( goals ), cli );
740
741 assertArgumentsPresent( cli, Collections.singleton( "test" ) );
742 }
743
744 @Test
745 public void testShouldSpecifyTwoGoalsFromRequest() throws CommandLineConfigurationException
746 {
747 List<String> goals = new ArrayList<>();
748 goals.add( "test" );
749 goals.add( "clean" );
750
751 mclb.setGoals( newRequest().setGoals( goals ), cli );
752
753 assertArgumentsPresent( cli, new HashSet<>( goals ) );
754 assertArgumentsPresentInOrder( cli, goals );
755 }
756
757 @Test
758 public void testShouldSpecifyThreadsFromRequest()
759 {
760 mclb.setThreads( newRequest().setThreads( "2.0C" ), cli );
761
762 assertArgumentsPresentInOrder( cli, "-T", "2.0C" );
763 }
764
765 @Test
766 public void testBuildTypicalMavenInvocationEndToEnd()
767 throws Exception
768 {
769 File mavenDir = setupTempMavenHomeIfMissing( false );
770
771 InvocationRequest request = newRequest();
772
773 File projectDir = temporaryFolder.newFolder( "invoker-tests", "typical-end-to-end-cli-build" );
774
775 request.setBaseDirectory( projectDir );
776
777 Set<String> expectedArgs = new HashSet<>();
778 Set<String> bannedArgs = new HashSet<>();
779
780 createDummyFile( projectDir, "pom.xml" );
781
782 bannedArgs.add( "-f" );
783 bannedArgs.add( "pom.xml" );
784
785 Properties properties = new Properties();
786
787 properties.setProperty( "maven.tests.skip", "true" );
788
789 expectedArgs.add( "maven.tests.skip=true" );
790
791 request.setProperties( properties );
792
793 request.setOffline( true );
794
795 expectedArgs.add( "-o" );
796
797 List<String> goals = new ArrayList<>();
798
799 goals.add( "post-clean" );
800 goals.add( "deploy" );
801 goals.add( "site-deploy" );
802
803 request.setGoals( goals );
804
805 Commandline commandline = mclb.build( request );
806
807 assertArgumentsPresent( commandline, expectedArgs );
808 assertArgumentsNotPresent( commandline, bannedArgs );
809 assertArgumentsPresentInOrder( commandline, goals );
810
811 String executable = commandline.getExecutable();
812
813 assertTrue( executable.contains( new File( mavenDir, "bin/mvn" ).getCanonicalPath() ) );
814 assertEquals( projectDir.getCanonicalPath(), commandline.getWorkingDirectory().getCanonicalPath() );
815 }
816
817 @Test
818 public void testShouldInsertActivatedProfiles()
819 throws Exception
820 {
821 setupTempMavenHomeIfMissing( false );
822
823 String profile1 = "profile-1";
824 String profile2 = "profile-2";
825
826 InvocationRequest request = newRequest();
827
828 List<String> profiles = new ArrayList<>();
829 profiles.add( profile1 );
830 profiles.add( profile2 );
831
832 request.setProfiles( profiles );
833
834 Commandline commandline = mclb.build( request );
835
836 assertArgumentsPresentInOrder( commandline, "-P", profile1 + "," + profile2 );
837 }
838
839 @Test
840 public void testMvnExecutableFromInvoker()
841 throws Exception
842 {
843 assumeThat( "Test only works when maven.home is set",
844 System.getProperty( "maven.home" ), is(notNullValue()));
845
846 File mavenExecutable = new File( "mvnDebug" );
847
848 mclb.setMavenExecutable( mavenExecutable );
849 mclb.build( newRequest() );
850
851 assertTrue( "Expected executable to exist", mclb.getMavenExecutable().exists() );
852 assertTrue( "Expected executable to be absolute", mclb.getMavenExecutable().isAbsolute() );
853 assertTrue( "Expected mvnDebug as command mvnDebug", mclb.getMavenExecutable().getName().contains( "mvnDebug" ) );
854 }
855
856 @Test
857 public void testMvnExecutableFormRequest()
858 throws Exception
859 {
860 assumeThat( "Test only works when maven.home is set",
861 System.getProperty( "maven.home" ), is(notNullValue()));
862
863 File mavenExecutable = new File( "mvnDebug" );
864
865 mclb.build( newRequest().setMavenExecutable( mavenExecutable ) );
866
867 assertTrue( "Expected executable to exist", mclb.getMavenExecutable().exists() );
868 assertTrue( "Expected executable to be absolute", mclb.getMavenExecutable().isAbsolute() );
869 assertTrue( "Expected mvnDebug as command", mclb.getMavenExecutable().getName().contains( "mvnDebug" ) );
870 }
871
872 @Test
873 public void testDefaultMavenCommand()
874 throws Exception
875 {
876 assumeThat( "Test only works when maven.home is set",
877 System.getProperty( "maven.home" ), is(notNullValue()));
878
879 mclb.build( newRequest() );
880
881 assertTrue( "Expected executable to exist", mclb.getMavenExecutable().exists() );
882 assertTrue( "Expected executable to be absolute", mclb.getMavenExecutable().isAbsolute() );
883 }
884
885 @Test
886 public void testAddShellEnvironment()
887 throws Exception
888 {
889 setupTempMavenHomeIfMissing( false );
890
891 InvocationRequest request = newRequest();
892
893 String envVar1Name = "VAR-1";
894 String envVar1Value = "VAR-1-VALUE";
895
896 String envVar2Name = "VAR-2";
897 String envVar2Value = "VAR-2-VALUE";
898
899 request.addShellEnvironment( envVar1Name, envVar1Value );
900 request.addShellEnvironment( envVar2Name, envVar2Value );
901
902 Commandline commandline = mclb.build( request );
903
904 assertEnvironmentVariablePresent( commandline, envVar1Name, envVar1Value );
905 assertEnvironmentVariablePresent( commandline, envVar2Name, envVar2Value );
906 }
907
908 private void assertEnvironmentVariablePresent( Commandline cli, String varName, String varValue )
909 {
910 List<String> environmentVariables = Arrays.asList( cli.getEnvironmentVariables() );
911
912 String expectedDeclaration = varName + "=" + varValue;
913
914 assertTrue( "Environment variable setting: '" + expectedDeclaration + "' is mssing in "
915 + environmentVariables, environmentVariables.contains( expectedDeclaration ) );
916 }
917
918 private void assertArgumentsPresentInOrder( Commandline cli, String... expected )
919 {
920 assertArgumentsPresentInOrder( cli, Arrays.asList( expected ) );
921 }
922
923 private void assertArgumentsPresentInOrder( Commandline cli, List<String> expected )
924 {
925 String[] arguments = cli.getArguments();
926
927 int expectedCounter = 0;
928
929 for ( String argument : arguments )
930 {
931 if ( argument.equals( expected.get( expectedCounter ) ) )
932 {
933 expectedCounter++;
934 }
935 }
936
937 assertEquals( "Arguments: " + expected + " were not found or are in the wrong order: "
938 + Arrays.asList( arguments ), expected.size(), expectedCounter );
939 }
940
941 private void assertArgumentsPresent( Commandline cli, Set<String> requiredArgs )
942 {
943 String[] argv = cli.getArguments();
944 List<String> args = Arrays.asList( argv );
945
946 for ( String arg : requiredArgs )
947 {
948 assertTrue( "Command-line argument: '" + arg + "' is missing in " + args, args.contains( arg ) );
949 }
950 }
951
952 private void assertArgumentsNotPresent( Commandline cli, Set<String> bannedArgs )
953 {
954 String[] argv = cli.getArguments();
955 List<String> args = Arrays.asList( argv );
956
957 for ( String arg : bannedArgs )
958 {
959 assertFalse( "Command-line argument: '" + arg + "' should not be present.", args.contains( arg ) );
960 }
961 }
962
963 private File createDummyFile( File directory, String filename )
964 throws IOException
965 {
966 File dummyFile = new File( directory, filename );
967
968 try ( FileWriter writer = new FileWriter( dummyFile ) )
969 {
970 writer.write( "This is a dummy file." );
971 }
972
973 return dummyFile;
974 }
975
976 private InvocationRequest newRequest()
977 {
978 return new DefaultInvocationRequest();
979 }
980
981 }