1 package org.apache.maven.plugins.assembly.archive.phase;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static java.util.Collections.singleton;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.any;
28 import static org.mockito.Mockito.anyListOf;
29 import static org.mockito.Mockito.atLeastOnce;
30 import static org.mockito.Mockito.eq;
31 import static org.mockito.Mockito.isNull;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.times;
34 import static org.mockito.Mockito.when;
35 import static org.mockito.Mockito.verify;
36
37 import java.io.File;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.HashSet;
42 import java.util.LinkedHashMap;
43 import java.util.List;
44 import java.util.Set;
45
46 import org.apache.maven.artifact.Artifact;
47 import org.apache.maven.model.Model;
48 import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
49 import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
50 import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
51 import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest;
52 import org.apache.maven.plugins.assembly.artifact.DependencyResolver;
53 import org.apache.maven.plugins.assembly.model.Assembly;
54 import org.apache.maven.plugins.assembly.model.DependencySet;
55 import org.apache.maven.plugins.assembly.model.FileSet;
56 import org.apache.maven.plugins.assembly.model.ModuleBinaries;
57 import org.apache.maven.plugins.assembly.model.ModuleSet;
58 import org.apache.maven.plugins.assembly.model.ModuleSources;
59 import org.apache.maven.project.MavenProject;
60 import org.apache.maven.project.ProjectBuilder;
61 import org.codehaus.plexus.archiver.Archiver;
62 import org.codehaus.plexus.logging.Logger;
63 import org.junit.Before;
64 import org.junit.Rule;
65 import org.junit.Test;
66 import org.junit.rules.TemporaryFolder;
67 import org.junit.runner.RunWith;
68 import org.mockito.junit.MockitoJUnitRunner;
69
70 @RunWith( MockitoJUnitRunner.class )
71 public class ModuleSetAssemblyPhaseTest
72 {
73 @Rule
74 public TemporaryFolder temporaryFolder = new TemporaryFolder();
75
76 private ModuleSetAssemblyPhase phase;
77
78 private DependencyResolver dependencyResolver;
79
80 private ProjectBuilder projectBuilder;
81
82 private Logger logger;
83
84 @Before
85 public void setUp()
86 {
87 this.dependencyResolver = mock( DependencyResolver.class );
88
89 this.logger = mock( Logger.class );
90
91 this.phase = new ModuleSetAssemblyPhase( projectBuilder, dependencyResolver, logger );
92 }
93
94 @Test
95 public void testIsDeprecatedModuleSourcesConfigPresent_ShouldCatchOutputDir()
96 {
97 final ModuleSources sources = new ModuleSources();
98 sources.setOutputDirectory( "outdir" );
99
100 assertTrue( this.phase.isDeprecatedModuleSourcesConfigPresent( sources ) );
101 }
102
103 @Test
104 public void testIsDeprecatedModuleSourcesConfigPresent_ShouldCatchInclude()
105 {
106 final ModuleSources sources = new ModuleSources();
107 sources.addInclude( "**/included.txt" );
108
109 assertTrue( this.phase.isDeprecatedModuleSourcesConfigPresent( sources ) );
110 }
111
112 @Test
113 public void testIsDeprecatedModuleSourcesConfigPresent_ShouldCatchExclude()
114 {
115 final ModuleSources sources = new ModuleSources();
116 sources.addExclude( "**/excluded.txt" );
117
118 assertTrue( this.phase.isDeprecatedModuleSourcesConfigPresent( sources ) );
119 }
120
121 @Test
122 public void testIsDeprecatedModuleSourcesConfigPresent_ShouldNotCatchFileMode()
123 {
124 final ModuleSources sources = new ModuleSources();
125 sources.setFileMode( "777" );
126
127 assertFalse( this.phase.isDeprecatedModuleSourcesConfigPresent( sources ) );
128 }
129
130 @Test
131 public void testIsDeprecatedModuleSourcesConfigPresent_ShouldNotCatchDirMode()
132 {
133 final ModuleSources sources = new ModuleSources();
134 sources.setDirectoryMode( "777" );
135
136 assertFalse( this.phase.isDeprecatedModuleSourcesConfigPresent( sources ) );
137 }
138
139 @Test
140 public void testCreateFileSet_ShouldUseModuleDirOnlyWhenOutDirIsNull()
141 throws Exception
142 {
143 final Model model = new Model();
144 model.setArtifactId( "artifact" );
145
146 final MavenProject project = new MavenProject( model );
147
148 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
149 when( configSource.getProject() ).thenReturn( project );
150
151 final FileSet fs = new FileSet();
152
153 final ModuleSources sources = new ModuleSources();
154 sources.setIncludeModuleDirectory( true );
155
156 final File basedir = temporaryFolder.getRoot();
157
158 final MavenProject artifactProject = new MavenProject( new Model() );
159 artifactProject.setGroupId( "GROUPID" );
160 artifactProject.setFile( new File( basedir, "pom.xml" ) );
161
162 Artifact artifact = mock( Artifact.class );
163 when( artifact.getGroupId() ).thenReturn( "GROUPID" );
164 when( artifact.getArtifactId() ).thenReturn( "artifact" );
165
166 artifactProject.setArtifact( artifact );
167
168 DefaultAssemblyArchiverTest.setupInterpolators( configSource, project );
169
170 final FileSet result = this.phase.createFileSet( fs, sources, artifactProject, configSource );
171
172 assertEquals( "artifact/", result.getOutputDirectory() );
173
174
175 verify( configSource, atLeastOnce() ).getFinalName();
176 verify( configSource, atLeastOnce() ).getMavenSession();
177 verify( configSource, atLeastOnce() ).getProject();
178 }
179
180 @Test
181 public void testCreateFileSet_ShouldPrependModuleDirWhenOutDirIsProvided()
182 throws Exception
183 {
184 final Model model = new Model();
185 model.setArtifactId( "artifact" );
186
187 final MavenProject project = new MavenProject( model );
188
189 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
190 when( configSource.getProject() ).thenReturn( project );
191
192 final FileSet fs = new FileSet();
193 fs.setOutputDirectory( "out" );
194
195 final ModuleSources sources = new ModuleSources();
196 sources.setIncludeModuleDirectory( true );
197
198 final MavenProject artifactProject = new MavenProject( new Model() );
199 artifactProject.setGroupId( "GROUPID" );
200
201 final File basedir = temporaryFolder.getRoot();
202
203 artifactProject.setFile( new File( basedir, "pom.xml" ) );
204
205 Artifact artifact = mock( Artifact.class );
206 when( artifact.getGroupId() ).thenReturn( "GROUPID" );
207 when( artifact.getArtifactId() ).thenReturn( "artifact" );
208
209 artifactProject.setArtifact( artifact );
210 DefaultAssemblyArchiverTest.setupInterpolators( configSource, project );
211
212 final FileSet result = this.phase.createFileSet( fs, sources, artifactProject, configSource );
213
214 assertEquals( "artifact/out/", result.getOutputDirectory() );
215
216
217 verify( configSource, atLeastOnce() ).getFinalName();
218 verify( configSource, atLeastOnce() ).getMavenSession();
219 verify( configSource, atLeastOnce() ).getProject();
220 }
221
222 @Test
223 public void testCreateFileSet_ShouldAddExcludesForSubModulesWhenExcludeSubModDirsIsTrue()
224 throws Exception
225 {
226 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
227
228 final FileSet fs = new FileSet();
229
230 final ModuleSources sources = new ModuleSources();
231 sources.setExcludeSubModuleDirectories( true );
232
233 final Model model = new Model();
234 model.setArtifactId( "artifact" );
235
236 model.addModule( "submodule" );
237
238 final MavenProject project = new MavenProject( model );
239
240 final File basedir = temporaryFolder.getRoot();
241 project.setGroupId( "GROUPID" );
242 project.setFile( new File( basedir, "pom.xml" ) );
243
244 Artifact artifact = mock( Artifact.class );
245 when( artifact.getGroupId() ).thenReturn( "GROUPID" );
246
247 project.setArtifact( artifact );
248 DefaultAssemblyArchiverTest.setupInterpolators( configSource, project );
249
250 final FileSet result = this.phase.createFileSet( fs, sources, project, configSource );
251
252 assertEquals( 1, result.getExcludes().size() );
253 assertEquals( "submodule/**", result.getExcludes().get( 0 ) );
254
255
256 verify( configSource, atLeastOnce() ).getFinalName();
257 verify( configSource, atLeastOnce() ).getMavenSession();
258 verify( configSource, atLeastOnce() ).getProject();
259 }
260
261 @Test
262 public void testExecute_ShouldSkipIfNoModuleSetsFound()
263 throws Exception
264 {
265 final Assembly assembly = new Assembly();
266 assembly.setIncludeBaseDirectory( false );
267
268 this.phase.execute( assembly, null, null );
269 }
270
271 @Test
272 public void testExecute_ShouldAddOneModuleSetWithOneModuleInIt()
273 throws Exception
274 {
275 final MavenProject project = createProject( "group", "artifact", "version", null );
276
277 final MavenProject module = createProject( "group", "module", "version", project );
278
279 Artifact artifact = mock( Artifact.class );
280 final File moduleArtifactFile = temporaryFolder.newFile();
281 when( artifact.getGroupId() ).thenReturn( "GROUPID" );
282 when( artifact.getFile() ).thenReturn( moduleArtifactFile );
283 module.setArtifact( artifact );
284
285 final List<MavenProject> projects = new ArrayList<>();
286 projects.add( module );
287
288 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
289 when( configSource.getReactorProjects() ).thenReturn( projects );
290 when( configSource.getFinalName() ).thenReturn( "final-name" );
291 when( configSource.getProject() ).thenReturn( project );
292
293 final Archiver archiver = mock( Archiver.class );
294 when( archiver.getDestFile() ).thenReturn( new File( "junk" ) );
295 when( archiver.getOverrideDirectoryMode() ).thenReturn( 0777 );
296 when( archiver.getOverrideFileMode() ).thenReturn( 0777 );
297
298 final ModuleBinaries bin = new ModuleBinaries();
299 bin.setOutputFileNameMapping( "artifact" );
300 bin.setOutputDirectory( "out" );
301 bin.setFileMode( "777" );
302 bin.setUnpack( false );
303 bin.setIncludeDependencies( false );
304
305 final ModuleSet ms = new ModuleSet();
306 ms.setBinaries( bin );
307
308 final Assembly assembly = new Assembly();
309 assembly.setIncludeBaseDirectory( false );
310 assembly.addModuleSet( ms );
311
312 when( dependencyResolver.resolveDependencySets( eq( assembly ),
313 eq( ms ),
314 eq( configSource ),
315 anyListOf( DependencySet.class ) ) ).thenReturn( new LinkedHashMap<DependencySet, Set<Artifact>>() );
316 DefaultAssemblyArchiverTest.setupInterpolators( configSource, module );
317
318 this.phase.execute( assembly, archiver, configSource );
319
320
321 verify( configSource, atLeastOnce() ).getFinalName();
322 verify( configSource, atLeastOnce() ).getMavenSession();
323 verify( configSource, atLeastOnce() ).getProject();
324 verify( configSource, atLeastOnce() ).getReactorProjects();
325
326 verify( archiver ).addFile( moduleArtifactFile, "out/artifact", 511 );
327 verify( archiver, atLeastOnce() ).getDestFile();
328 verify( archiver ).getOverrideDirectoryMode();
329 verify( archiver ).getOverrideFileMode();
330 verify( archiver, times( 2 ) ).setFileMode( 511 );
331
332 verify( dependencyResolver ).resolveDependencySets( eq( assembly ),
333 eq( ms ),
334 eq( configSource ),
335 anyListOf( DependencySet.class ) );
336 }
337
338 @Test
339 public void testAddModuleBinaries_ShouldReturnImmediatelyWhenBinariesIsNull()
340 throws Exception
341 {
342 this.phase.addModuleBinaries( null, null, null, null, null, null );
343 }
344
345 @Test
346 public void testAddModuleBinaries_ShouldFilterPomModule()
347 throws Exception
348 {
349 final ModuleBinaries binaries = new ModuleBinaries();
350
351 binaries.setUnpack( false );
352 binaries.setFileMode( "777" );
353 binaries.setOutputDirectory( "out" );
354 binaries.setOutputFileNameMapping( "artifact" );
355
356 final MavenProject project = createProject( "group", "artifact", "version", null );
357 project.setPackaging( "pom" );
358
359 Artifact artifact = mock( Artifact.class );
360 project.setArtifact( artifact );
361
362 final Set<MavenProject> projects = singleton( project );
363
364 this.phase.addModuleBinaries( null, null, binaries, projects, null, null );
365 }
366
367 @Test
368 public void testAddModuleBinaries_ShouldAddOneModuleAttachmentArtifactAndNoDeps()
369 throws Exception
370 {
371 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
372 when( configSource.getFinalName() ).thenReturn( "final-name" );
373
374 Artifact artifact = mock( Artifact.class );
375 when( artifact.getGroupId() ).thenReturn( "GROUPID" );
376 when( artifact.getClassifier() ).thenReturn( "test" );
377 final File artifactFile = temporaryFolder.newFile();
378 when( artifact.getFile() ).thenReturn( artifactFile );
379
380 final Archiver archiver = mock( Archiver.class );
381 when( archiver.getDestFile() ).thenReturn( new File( "junk" ) );
382 when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 );
383 when( archiver.getOverrideFileMode() ).thenReturn( 0222 );
384
385 final ModuleBinaries binaries = new ModuleBinaries();
386
387 binaries.setIncludeDependencies( false );
388 binaries.setUnpack( false );
389 binaries.setFileMode( "777" );
390 binaries.setOutputDirectory( "out" );
391 binaries.setOutputFileNameMapping( "artifact" );
392 binaries.setAttachmentClassifier( "test" );
393
394 final MavenProject project = createProject( "group", "artifact", "version", null );
395 project.addAttachedArtifact( artifact );
396
397 final Set<MavenProject> projects = singleton( project );
398
399 when( dependencyResolver.resolveDependencySets( isNull( Assembly.class ),
400 isNull( ModuleSet.class ),
401 eq( configSource ),
402 anyListOf( DependencySet.class ) ) ).thenReturn( new LinkedHashMap<DependencySet, Set<Artifact>>() );
403 DefaultAssemblyArchiverTest.setupInterpolators( configSource, project );
404
405 this.phase.addModuleBinaries( null, null, binaries, projects, archiver, configSource );
406
407
408 verify( configSource, atLeastOnce() ).getFinalName();
409 verify( configSource, atLeastOnce() ).getMavenSession();
410 verify( configSource, atLeastOnce() ).getProject();
411
412 verify( archiver ).addFile( artifactFile, "out/artifact", 511 );
413 verify( archiver, atLeastOnce() ).getDestFile();
414 verify( archiver ).getOverrideDirectoryMode();
415 verify( archiver ).getOverrideFileMode();
416 verify( archiver ).setFileMode( 511 );
417 verify( archiver ).setFileMode( 146 );
418
419 verify( dependencyResolver ).resolveDependencySets( isNull( Assembly.class ),
420 isNull( ModuleSet.class ),
421 eq( configSource ),
422 anyListOf( DependencySet.class ) );
423 }
424
425 @Test
426 public void testAddModuleBinaries_ShouldFailWhenOneModuleDoesntHaveAttachmentWithMatchingClassifier()
427 throws Exception
428 {
429 Artifact artifact = mock( Artifact.class );
430
431 final ModuleBinaries binaries = new ModuleBinaries();
432
433 binaries.setUnpack( false );
434 binaries.setFileMode( "777" );
435 binaries.setOutputDirectory( "out" );
436 binaries.setOutputFileNameMapping( "artifact" );
437 binaries.setAttachmentClassifier( "test" );
438
439 final MavenProject project = createProject( "group", "artifact", "version", null );
440 project.setArtifact( artifact );
441
442 final Set<MavenProject> projects = singleton( project );
443
444 try
445 {
446
447 this.phase.addModuleBinaries( null, null, binaries, projects, null, null );
448
449 fail( "Should throw an invalid configuration exception because of module with missing attachment." );
450 }
451 catch ( final InvalidAssemblerConfigurationException e )
452 {
453 assertEquals( "Cannot find attachment with classifier: test in module project: group:artifact:jar:version. "
454 + "Please exclude this module from the module-set.", e.getMessage());
455
456 }
457 }
458
459 @Test
460 public void testAddModuleBinaries_ShouldAddOneModuleArtifactAndNoDeps()
461 throws Exception
462 {
463 Artifact artifact = mock( Artifact.class );
464 final File artifactFile = temporaryFolder.newFile();
465 when( artifact.getGroupId() ).thenReturn( "GROUPID" );
466 when( artifact.getFile() ).thenReturn( artifactFile );
467
468 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
469 when( configSource.getFinalName() ).thenReturn( "final-name" );
470
471 final Archiver archiver = mock( Archiver.class );
472 when( archiver.getDestFile() ).thenReturn( new File( "junk" ) );
473 when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 );
474 when( archiver.getOverrideFileMode() ).thenReturn( 0222 );
475
476 final ModuleBinaries binaries = new ModuleBinaries();
477
478 binaries.setIncludeDependencies( false );
479 binaries.setUnpack( false );
480 binaries.setFileMode( "777" );
481 binaries.setOutputDirectory( "out" );
482 binaries.setOutputFileNameMapping( "artifact" );
483
484 final MavenProject project = createProject( "group", "artifact", "version", null );
485 project.setArtifact( artifact );
486
487 final Set<MavenProject> projects = singleton( project );
488
489 when( dependencyResolver.resolveDependencySets( isNull( Assembly.class ),
490 isNull( ModuleSet.class ),
491 any( AssemblerConfigurationSource.class ),
492 anyListOf( DependencySet.class ) ) ).thenReturn( new LinkedHashMap<DependencySet, Set<Artifact>>() );
493 DefaultAssemblyArchiverTest.setupInterpolators( configSource, project );
494
495 this.phase.addModuleBinaries( null, null, binaries, projects, archiver, configSource );
496
497
498 verify( configSource, atLeastOnce() ).getFinalName();
499 verify( configSource, atLeastOnce() ).getMavenSession();
500 verify( configSource, atLeastOnce() ).getProject();
501
502 verify( dependencyResolver ).resolveDependencySets( isNull( Assembly.class ),
503 isNull( ModuleSet.class ),
504 any( AssemblerConfigurationSource.class ),
505 anyListOf( DependencySet.class ) );
506
507 verify( archiver ).addFile( artifactFile, "out/artifact", 511 );
508 verify( archiver, atLeastOnce() ).getDestFile();
509 verify( archiver ).getOverrideDirectoryMode();
510 verify( archiver ).getOverrideFileMode();
511 verify( archiver ).setFileMode( 511 );
512 verify( archiver ).setFileMode( 146);
513 }
514
515 @Test
516 public void testAddModuleArtifact_ShouldThrowExceptionWhenArtifactFileIsNull()
517 throws Exception
518 {
519 Artifact artifact = mock( Artifact.class );
520 try
521 {
522 this.phase.addModuleArtifact( artifact, null, null, null, null );
523
524 fail( "Expected ArchiveCreationException since artifact file is null." );
525 }
526 catch ( final ArchiveCreationException e )
527 {
528
529 }
530 }
531
532 @Test
533 public void testAddModuleArtifact_ShouldAddOneArtifact()
534 throws Exception
535 {
536 Artifact artifact = mock( Artifact.class );
537 when( artifact.getGroupId() ).thenReturn( "GROUPID" );
538 final File artifactFile = temporaryFolder.newFile();
539 when( artifact.getFile() ).thenReturn( artifactFile );
540
541 final MavenProject project = createProject( "group", "artifact", "version", null );
542 project.setArtifact( artifact );
543
544 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
545 when( configSource.getFinalName() ).thenReturn( "final-name" );
546
547 final Archiver archiver = mock( Archiver.class );
548 when( archiver.getDestFile() ).thenReturn( new File( "junk" ) );
549 when( archiver.getOverrideDirectoryMode() ).thenReturn( 0222 );
550 when( archiver.getOverrideFileMode() ).thenReturn( 0222 );
551
552 final ModuleBinaries binaries = new ModuleBinaries();
553 binaries.setOutputDirectory( "out" );
554 binaries.setOutputFileNameMapping( "artifact" );
555 binaries.setUnpack( false );
556 binaries.setFileMode( "777" );
557 DefaultAssemblyArchiverTest.setupInterpolators( configSource, project );
558
559 this.phase.addModuleArtifact( artifact, project, archiver, configSource, binaries );
560
561
562 verify( configSource, atLeastOnce() ).getFinalName();
563 verify( configSource, atLeastOnce() ).getMavenSession();
564 verify( configSource, atLeastOnce() ).getProject();
565
566 verify( archiver ).addFile( artifactFile, "out/artifact", 511 );
567 verify( archiver, atLeastOnce() ).getDestFile();
568 verify( archiver ).getOverrideDirectoryMode();
569 verify( archiver ).getOverrideFileMode();
570 verify( archiver ).setFileMode( 511 );
571 verify( archiver ).setFileMode( 146 );
572 }
573
574 @Test
575 public void testAddModuleSourceFileSets_ShouldReturnImmediatelyIfSourcesIsNull()
576 throws Exception
577 {
578 this.phase.addModuleSourceFileSets( null, null, null, null );
579 }
580
581 @Test
582 public void testAddModuleSourceFileSets_ShouldAddOneSourceDirectory()
583 throws Exception
584 {
585 final MavenProject project = createProject( "group", "artifact", "version", null );
586
587 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
588 when( configSource.getFinalName() ).thenReturn( "final-name" );
589 when( configSource.getProject() ).thenReturn( project );
590 Artifact artifact = mock( Artifact.class );
591 when( artifact.getGroupId() ).thenReturn( "GROUPID" );
592 project.setArtifact( artifact );
593
594 final Set<MavenProject> projects = singleton( project );
595
596 final FileSet fs = new FileSet();
597 fs.setDirectory( "/src" );
598 fs.setDirectoryMode( "777" );
599 fs.setFileMode( "777" );
600
601 final ModuleSources sources = new ModuleSources();
602 sources.addFileSet( fs );
603
604
605 final Archiver archiver = mock( Archiver.class );
606 when( archiver.getOverrideDirectoryMode() ).thenReturn( -1 );
607 when( archiver.getOverrideFileMode() ).thenReturn( -1 );
608
609 DefaultAssemblyArchiverTest.setupInterpolators( configSource, project );
610
611 when( logger.isDebugEnabled() ).thenReturn( true );
612
613 this.phase.addModuleSourceFileSets( sources, projects, archiver,
614 configSource );
615
616
617 verify( configSource ).getArchiveBaseDirectory();
618 verify( configSource, atLeastOnce() ).getFinalName();
619 verify( configSource, atLeastOnce() ).getProject();
620 verify( configSource, atLeastOnce() ).getMavenSession();
621
622 verify( archiver ).getOverrideDirectoryMode();
623 verify( archiver ).getOverrideFileMode();
624 }
625
626 @Test
627 public void testGetModuleProjects_ShouldReturnNothingWhenReactorContainsOnlyCurrentProject()
628 throws Exception
629 {
630 final MavenProject project = createProject( "group", "artifact", "version", null );
631
632 final List<MavenProject> projects = Collections.singletonList( project );
633
634 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
635 when( configSource.getProject() ).thenReturn( project );
636 when( configSource.getReactorProjects() ).thenReturn( projects );
637
638 final ModuleSet moduleSet = new ModuleSet();
639 moduleSet.setIncludeSubModules( true );
640
641 final Set<MavenProject> moduleProjects =
642 ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger );
643
644 assertTrue( moduleProjects.isEmpty() );
645
646
647 verify( configSource ).getReactorProjects();
648 verify( configSource, atLeastOnce() ).getProject();
649 }
650
651 @Test
652 public void testGetModuleProjects_ShouldReturnNothingWhenReactorContainsTwoSiblingProjects()
653 throws Exception
654 {
655 final MavenProject project = createProject( "group", "artifact", "version", null );
656 final MavenProject project2 = createProject( "group", "artifact2", "version", null );
657
658 final List<MavenProject> projects = new ArrayList<>();
659 projects.add( project );
660 projects.add( project2 );
661
662 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
663 when( configSource.getReactorProjects() ).thenReturn( projects );
664 when( configSource.getProject() ).thenReturn( project );
665
666 final ModuleSet moduleSet = new ModuleSet();
667 moduleSet.setIncludeSubModules( true );
668
669 final Set<MavenProject> moduleProjects =
670 ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger );
671
672 assertTrue( moduleProjects.isEmpty() );
673
674
675 verify( configSource ).getReactorProjects();
676 verify( configSource, atLeastOnce() ).getProject();
677 }
678
679 @Test
680 public void testGetModuleProjects_ShouldReturnModuleOfCurrentProject()
681 throws Exception
682 {
683 final MavenProject project = createProject( "group", "artifact", "version", null );
684 final MavenProject project2 = createProject( "group", "artifact2", "version", project );
685
686 final List<MavenProject> projects = new ArrayList<>();
687 projects.add( project );
688 projects.add( project2 );
689
690 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
691 when( configSource.getReactorProjects() ).thenReturn( projects );
692 when( configSource.getProject() ).thenReturn( project );
693
694 final ModuleSet moduleSet = new ModuleSet();
695 moduleSet.setIncludeSubModules( true );
696
697 final Set<MavenProject> moduleProjects =
698 ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger );
699
700 assertFalse( moduleProjects.isEmpty() );
701
702 final MavenProject result = moduleProjects.iterator().next();
703
704 assertEquals( "artifact2", result.getArtifactId() );
705
706
707 verify( configSource ).getReactorProjects();
708 verify( configSource, atLeastOnce() ).getProject();
709 }
710
711 @Test
712 public void testGetModuleProjects_ShouldReturnDescendentModulesOfCurrentProject()
713 throws Exception
714 {
715 final MavenProject project = createProject( "group", "artifact", "version", null );
716 final MavenProject project2 = createProject( "group", "artifact2", "version", project );
717 final MavenProject project3 = createProject( "group", "artifact3", "version", project2 );
718
719 final List<MavenProject> projects = new ArrayList<>();
720 projects.add( project );
721 projects.add( project2 );
722 projects.add( project3 );
723
724 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
725 when( configSource.getReactorProjects() ).thenReturn( projects );
726 when( configSource.getProject() ).thenReturn( project );
727
728 final ModuleSet moduleSet = new ModuleSet();
729 moduleSet.setIncludeSubModules( true );
730
731 final Set<MavenProject> moduleProjects =
732 ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger );
733
734 assertEquals( 2, moduleProjects.size() );
735
736 final List<MavenProject> check = new ArrayList<>();
737 check.add( project2 );
738 check.add( project3 );
739
740 verifyResultIs( check, moduleProjects );
741
742
743 verify( configSource ).getReactorProjects();
744 verify( configSource, atLeastOnce() ).getProject();
745 }
746
747 @Test
748 public void testGetModuleProjects_ShouldExcludeModuleAndDescendentsTransitively()
749 throws Exception
750 {
751 final MavenProject project = createProject( "group", "artifact", "version", null );
752
753 Artifact artifact1 = mock( Artifact.class );
754 project.setArtifact( artifact1 );
755
756 final MavenProject project2 = createProject( "group", "artifact2", "version", project );
757 Artifact artifact2 = mock( Artifact.class );
758 when( artifact2.getGroupId() ).thenReturn( "group" );
759 when( artifact2.getArtifactId() ).thenReturn( "artifact2" );
760 when( artifact2.getId() ).thenReturn( "group:artifact2:version:jar" );
761 when( artifact2.getDependencyConflictId() ).thenReturn( "group:artifact2:jar" );
762 project2.setArtifact( artifact2 );
763
764 final MavenProject project3 = createProject( "group", "artifact3", "version", project2 );
765 Artifact artifact3 = mock( Artifact.class );
766 when( artifact3.getGroupId() ).thenReturn( "group" );
767 when( artifact3.getArtifactId() ).thenReturn( "artifact3" );
768 when( artifact3.getId() ).thenReturn( "group:artifact3:version:jar" );
769 when( artifact3.getDependencyConflictId() ).thenReturn( "group:artifact3:jar" );
770 when( artifact3.getDependencyTrail() ).thenReturn( Arrays.asList( project2.getId(), project.getId() ) );
771 project3.setArtifact( artifact3 );
772
773 final List<MavenProject> projects = new ArrayList<>();
774 projects.add( project );
775 projects.add( project2 );
776 projects.add( project3 );
777
778 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
779 when( configSource.getReactorProjects() ).thenReturn( projects );
780 when( configSource.getProject() ).thenReturn( project );
781
782 final ModuleSet moduleSet = new ModuleSet();
783 moduleSet.setIncludeSubModules( true );
784
785 moduleSet.addExclude( "group:artifact2" );
786
787 final Set<MavenProject> moduleProjects =
788 ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger );
789
790 assertTrue( moduleProjects.isEmpty() );
791
792
793 verify( configSource ).getReactorProjects();
794 verify( configSource, atLeastOnce() ).getProject();
795 }
796
797 private void verifyResultIs( final List<MavenProject> check, final Set<MavenProject> moduleProjects )
798 {
799 boolean failed = false;
800
801 final Set<MavenProject> checkTooMany = new HashSet<>( moduleProjects );
802 checkTooMany.removeAll( check );
803
804 if ( !checkTooMany.isEmpty() )
805 {
806 failed = true;
807
808 System.out.println( "Unexpected projects in output: " );
809
810 for ( final MavenProject project : checkTooMany )
811 {
812 System.out.println( project.getId() );
813 }
814 }
815
816 final Set<MavenProject> checkTooFew = new HashSet<>( check );
817 checkTooFew.removeAll( moduleProjects );
818
819 if ( !checkTooFew.isEmpty() )
820 {
821 failed = true;
822
823 System.out.println( "Expected projects missing from output: " );
824
825 for ( final MavenProject project : checkTooMany )
826 {
827 System.out.println( project.getId() );
828 }
829 }
830
831 if ( failed )
832 {
833 fail( "See system output for more information." );
834 }
835 }
836
837 private MavenProject createProject( final String groupId, final String artifactId, final String version,
838 final MavenProject parentProject )
839 {
840 final Model model = new Model();
841 model.setArtifactId( artifactId );
842 model.setGroupId( groupId );
843 model.setVersion( version );
844
845 final MavenProject project = new MavenProject( model );
846
847 File pomFile;
848 if ( parentProject == null )
849 {
850 final File basedir = temporaryFolder.getRoot();
851 pomFile = new File( basedir, "pom.xml" );
852 }
853 else
854 {
855 final File parentBase = parentProject.getBasedir();
856 pomFile = new File( parentBase, artifactId + "/pom.xml" );
857
858 parentProject.getModel().addModule( artifactId );
859 project.setParent( parentProject );
860 }
861
862 project.setFile( pomFile );
863
864 return project;
865 }
866 }