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