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.anyList;
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.junit.Before;
63 import org.junit.Rule;
64 import org.junit.Test;
65 import org.junit.rules.TemporaryFolder;
66 import org.junit.runner.RunWith;
67 import org.mockito.junit.MockitoJUnitRunner;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70
71 @RunWith( MockitoJUnitRunner.class )
72 public class ModuleSetAssemblyPhaseTest
73 {
74 private final Logger logger = LoggerFactory.getLogger( getClass() );
75
76 @Rule
77 public TemporaryFolder temporaryFolder = new TemporaryFolder();
78
79 private ModuleSetAssemblyPhase phase;
80
81 private DependencyResolver dependencyResolver;
82
83 private ProjectBuilder projectBuilder;
84
85 @Before
86 public void setUp()
87 {
88 this.projectBuilder = mock( ProjectBuilder.class );
89 this.dependencyResolver = mock( DependencyResolver.class );
90
91 this.phase = new ModuleSetAssemblyPhase( projectBuilder, dependencyResolver );
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 anyList() ) ).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 anyList() );
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(),
400 isNull(),
401 eq( configSource ),
402 anyList() ) ).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(),
420 isNull(),
421 eq( configSource ),
422 anyList() );
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(),
490 isNull(),
491 any( AssemblerConfigurationSource.class ),
492 anyList() ) ).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(),
503 isNull(),
504 any( AssemblerConfigurationSource.class ),
505 anyList() );
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 this.phase.addModuleSourceFileSets( sources, projects, archiver,
612 configSource );
613
614
615 verify( configSource ).getArchiveBaseDirectory();
616 verify( configSource, atLeastOnce() ).getFinalName();
617 verify( configSource, atLeastOnce() ).getProject();
618 verify( configSource, atLeastOnce() ).getMavenSession();
619
620 verify( archiver ).getOverrideDirectoryMode();
621 verify( archiver ).getOverrideFileMode();
622 }
623
624 @Test
625 public void testGetModuleProjects_ShouldReturnNothingWhenReactorContainsOnlyCurrentProject()
626 throws Exception
627 {
628 final MavenProject project = createProject( "group", "artifact", "version", null );
629
630 final List<MavenProject> projects = Collections.singletonList( project );
631
632 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
633 when( configSource.getProject() ).thenReturn( project );
634 when( configSource.getReactorProjects() ).thenReturn( projects );
635
636 final ModuleSet moduleSet = new ModuleSet();
637 moduleSet.setIncludeSubModules( true );
638
639 final Set<MavenProject> moduleProjects =
640 ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger );
641
642 assertTrue( moduleProjects.isEmpty() );
643
644
645 verify( configSource ).getReactorProjects();
646 verify( configSource, atLeastOnce() ).getProject();
647 }
648
649 @Test
650 public void testGetModuleProjects_ShouldReturnNothingWhenReactorContainsTwoSiblingProjects()
651 throws Exception
652 {
653 final MavenProject project = createProject( "group", "artifact", "version", null );
654 final MavenProject project2 = createProject( "group", "artifact2", "version", null );
655
656 final List<MavenProject> projects = new ArrayList<>();
657 projects.add( project );
658 projects.add( project2 );
659
660 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
661 when( configSource.getReactorProjects() ).thenReturn( projects );
662 when( configSource.getProject() ).thenReturn( project );
663
664 final ModuleSet moduleSet = new ModuleSet();
665 moduleSet.setIncludeSubModules( true );
666
667 final Set<MavenProject> moduleProjects =
668 ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger );
669
670 assertTrue( moduleProjects.isEmpty() );
671
672
673 verify( configSource ).getReactorProjects();
674 verify( configSource, atLeastOnce() ).getProject();
675 }
676
677 @Test
678 public void testGetModuleProjects_ShouldReturnModuleOfCurrentProject()
679 throws Exception
680 {
681 final MavenProject project = createProject( "group", "artifact", "version", null );
682 final MavenProject project2 = createProject( "group", "artifact2", "version", project );
683
684 final List<MavenProject> projects = new ArrayList<>();
685 projects.add( project );
686 projects.add( project2 );
687
688 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
689 when( configSource.getReactorProjects() ).thenReturn( projects );
690 when( configSource.getProject() ).thenReturn( project );
691
692 final ModuleSet moduleSet = new ModuleSet();
693 moduleSet.setIncludeSubModules( true );
694
695 final Set<MavenProject> moduleProjects =
696 ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger );
697
698 assertFalse( moduleProjects.isEmpty() );
699
700 final MavenProject result = moduleProjects.iterator().next();
701
702 assertEquals( "artifact2", result.getArtifactId() );
703
704
705 verify( configSource ).getReactorProjects();
706 verify( configSource, atLeastOnce() ).getProject();
707 }
708
709 @Test
710 public void testGetModuleProjects_ShouldReturnDescendentModulesOfCurrentProject()
711 throws Exception
712 {
713 final MavenProject project = createProject( "group", "artifact", "version", null );
714 final MavenProject project2 = createProject( "group", "artifact2", "version", project );
715 final MavenProject project3 = createProject( "group", "artifact3", "version", project2 );
716
717 final List<MavenProject> projects = new ArrayList<>();
718 projects.add( project );
719 projects.add( project2 );
720 projects.add( project3 );
721
722 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
723 when( configSource.getReactorProjects() ).thenReturn( projects );
724 when( configSource.getProject() ).thenReturn( project );
725
726 final ModuleSet moduleSet = new ModuleSet();
727 moduleSet.setIncludeSubModules( true );
728
729 final Set<MavenProject> moduleProjects =
730 ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger );
731
732 assertEquals( 2, moduleProjects.size() );
733
734 final List<MavenProject> check = new ArrayList<>();
735 check.add( project2 );
736 check.add( project3 );
737
738 verifyResultIs( check, moduleProjects );
739
740
741 verify( configSource ).getReactorProjects();
742 verify( configSource, atLeastOnce() ).getProject();
743 }
744
745 @Test
746 public void testGetModuleProjects_ShouldExcludeModuleAndDescendentsTransitively()
747 throws Exception
748 {
749 final MavenProject project = createProject( "group", "artifact", "version", null );
750
751 Artifact artifact1 = mock( Artifact.class );
752 project.setArtifact( artifact1 );
753
754 final MavenProject project2 = createProject( "group", "artifact2", "version", project );
755 Artifact artifact2 = mock( Artifact.class );
756 when( artifact2.getGroupId() ).thenReturn( "group" );
757 when( artifact2.getArtifactId() ).thenReturn( "artifact2" );
758 when( artifact2.getId() ).thenReturn( "group:artifact2:version:jar" );
759 when( artifact2.getDependencyConflictId() ).thenReturn( "group:artifact2:jar" );
760 project2.setArtifact( artifact2 );
761
762 final MavenProject project3 = createProject( "group", "artifact3", "version", project2 );
763 Artifact artifact3 = mock( Artifact.class );
764 when( artifact3.getGroupId() ).thenReturn( "group" );
765 when( artifact3.getArtifactId() ).thenReturn( "artifact3" );
766 when( artifact3.getId() ).thenReturn( "group:artifact3:version:jar" );
767 when( artifact3.getDependencyConflictId() ).thenReturn( "group:artifact3:jar" );
768 when( artifact3.getDependencyTrail() ).thenReturn( Arrays.asList( project2.getId(), project.getId() ) );
769 project3.setArtifact( artifact3 );
770
771 final List<MavenProject> projects = new ArrayList<>();
772 projects.add( project );
773 projects.add( project2 );
774 projects.add( project3 );
775
776 final AssemblerConfigurationSource configSource = mock( AssemblerConfigurationSource.class );
777 when( configSource.getReactorProjects() ).thenReturn( projects );
778 when( configSource.getProject() ).thenReturn( project );
779
780 final ModuleSet moduleSet = new ModuleSet();
781 moduleSet.setIncludeSubModules( true );
782
783 moduleSet.addExclude( "group:artifact2" );
784
785 final Set<MavenProject> moduleProjects =
786 ModuleSetAssemblyPhase.getModuleProjects( moduleSet, configSource, logger );
787
788 assertTrue( moduleProjects.isEmpty() );
789
790
791 verify( configSource ).getReactorProjects();
792 verify( configSource, atLeastOnce() ).getProject();
793 }
794
795 private void verifyResultIs( final List<MavenProject> check, final Set<MavenProject> moduleProjects )
796 {
797 boolean failed = false;
798
799 final Set<MavenProject> checkTooMany = new HashSet<>( moduleProjects );
800 checkTooMany.removeAll( check );
801
802 if ( !checkTooMany.isEmpty() )
803 {
804 failed = true;
805
806 System.out.println( "Unexpected projects in output: " );
807
808 for ( final MavenProject project : checkTooMany )
809 {
810 System.out.println( project.getId() );
811 }
812 }
813
814 final Set<MavenProject> checkTooFew = new HashSet<>( check );
815 checkTooFew.removeAll( moduleProjects );
816
817 if ( !checkTooFew.isEmpty() )
818 {
819 failed = true;
820
821 System.out.println( "Expected projects missing from output: " );
822
823 for ( final MavenProject project : checkTooMany )
824 {
825 System.out.println( project.getId() );
826 }
827 }
828
829 if ( failed )
830 {
831 fail( "See system output for more information." );
832 }
833 }
834
835 private MavenProject createProject( final String groupId, final String artifactId, final String version,
836 final MavenProject parentProject )
837 {
838 final Model model = new Model();
839 model.setArtifactId( artifactId );
840 model.setGroupId( groupId );
841 model.setVersion( version );
842
843 final MavenProject project = new MavenProject( model );
844
845 File pomFile;
846 if ( parentProject == null )
847 {
848 final File basedir = temporaryFolder.getRoot();
849 pomFile = new File( basedir, "pom.xml" );
850 }
851 else
852 {
853 final File parentBase = parentProject.getBasedir();
854 pomFile = new File( parentBase, artifactId + "/pom.xml" );
855
856 parentProject.getModel().addModule( artifactId );
857 project.setParent( parentProject );
858 }
859
860 project.setFile( pomFile );
861
862 return project;
863 }
864 }