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