View Javadoc

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