View Javadoc

1   package org.apache.maven.plugin.assembly.io;
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.artifact.Artifact;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.model.Model;
25  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
26  import org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException;
27  import org.apache.maven.plugin.assembly.model.Assembly;
28  import org.apache.maven.plugin.assembly.model.Component;
29  import org.apache.maven.plugin.assembly.model.ContainerDescriptorHandlerConfig;
30  import org.apache.maven.plugin.assembly.model.DependencySet;
31  import org.apache.maven.plugin.assembly.model.FileItem;
32  import org.apache.maven.plugin.assembly.model.FileSet;
33  import org.apache.maven.plugin.assembly.model.Repository;
34  import org.apache.maven.plugin.assembly.model.io.xpp3.AssemblyXpp3Writer;
35  import org.apache.maven.plugin.assembly.model.io.xpp3.ComponentXpp3Writer;
36  import org.apache.maven.plugin.assembly.testutils.MockManager;
37  import org.apache.maven.plugin.assembly.testutils.TestFileManager;
38  import org.apache.maven.project.MavenProject;
39  import org.codehaus.plexus.util.IOUtil;
40  import org.easymock.MockControl;
41  
42  import java.io.File;
43  import java.io.FileOutputStream;
44  import java.io.IOException;
45  import java.io.OutputStreamWriter;
46  import java.io.StringReader;
47  import java.io.StringWriter;
48  import java.io.Writer;
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.Iterator;
52  import java.util.List;
53  
54  import junit.framework.TestCase;
55  
56  public class DefaultAssemblyReaderTest
57      extends TestCase
58  {
59  
60      private TestFileManager fileManager;
61  
62      private MockManager mockManager;
63  
64      private MockControl configSourceControl;
65  
66      private AssemblerConfigurationSource configSource;
67  
68      private ArtifactRepository localRepo;
69  
70      private MockControl localRepoControl;
71  
72      @Override
73      public void setUp()
74      {
75          fileManager = new TestFileManager( "assembly-reader.test.", ".xml" );
76          mockManager = new MockManager();
77  
78          configSourceControl = MockControl.createControl( AssemblerConfigurationSource.class );
79          mockManager.add( configSourceControl );
80  
81          configSource = (AssemblerConfigurationSource) configSourceControl.getMock();
82  
83          localRepoControl = MockControl.createControl( ArtifactRepository.class );
84          localRepo = (ArtifactRepository) localRepoControl.getMock();
85          mockManager.add( localRepoControl );
86  
87          localRepo.getBasedir();
88          localRepoControl.setReturnValue( "/path/to/local/repo", MockControl.ZERO_OR_MORE );
89  
90          configSource.getLocalRepository();
91          configSourceControl.setReturnValue( localRepo, MockControl.ZERO_OR_MORE );
92  
93          configSource.getRemoteRepositories();
94          configSourceControl.setReturnValue( Collections.EMPTY_LIST, MockControl.ZERO_OR_MORE );
95  
96          configSource.getMavenSession();
97          configSourceControl.setReturnValue( null, MockControl.ZERO_OR_MORE );
98      }
99  
100     @Override
101     public void tearDown()
102         throws IOException
103     {
104         fileManager.cleanUp();
105     }
106 
107     public void testIncludeSiteInAssembly_ShouldFailIfSiteDirectoryNonExistent()
108         throws IOException
109     {
110         final File siteDir = File.createTempFile( "assembly-reader.", ".test" );
111         siteDir.delete();
112 
113         configSource.getSiteDirectory();
114         configSourceControl.setReturnValue( siteDir, MockControl.ZERO_OR_MORE );
115 
116         final Assembly assembly = new Assembly();
117 
118         mockManager.replayAll();
119 
120         try
121         {
122             new DefaultAssemblyReader().includeSiteInAssembly( assembly, configSource );
123 
124             fail( "Should fail when site directory is non-existent." );
125         }
126         catch ( final InvalidAssemblerConfigurationException e )
127         {
128             // this should happen.
129         }
130 
131         mockManager.verifyAll();
132     }
133 
134     public void testIncludeSiteInAssembly_ShouldAddSiteDirFileSetWhenDirExists()
135         throws IOException, InvalidAssemblerConfigurationException
136     {
137         final File siteDir = fileManager.createTempDir();
138 
139         configSource.getSiteDirectory();
140         configSourceControl.setReturnValue( siteDir, MockControl.ZERO_OR_MORE );
141 
142         final Assembly assembly = new Assembly();
143 
144         mockManager.replayAll();
145 
146         new DefaultAssemblyReader().includeSiteInAssembly( assembly, configSource );
147 
148         final List<FileSet> fileSets = assembly.getFileSets();
149 
150         assertNotNull( fileSets );
151         assertEquals( 1, fileSets.size() );
152 
153         final FileSet fs = fileSets.get( 0 );
154 
155         assertEquals( siteDir.getPath(), fs.getDirectory() );
156 
157         mockManager.verifyAll();
158     }
159 
160     // public void testReadComponent_ShouldReadComponentFromXml()
161     // throws IOException, AssemblyReadException
162     // {
163     // Component component = new Component();
164     //
165     // FileSet fileSet = new FileSet();
166     // fileSet.setDirectory( "/dir" );
167     //
168     // component.addFileSet( fileSet );
169     //
170     // StringWriter sw = new StringWriter();
171     //
172     // ComponentXpp3Writer componentWriter = new ComponentXpp3Writer();
173     //
174     // componentWriter.write( sw, component );
175     //
176     // Component result = new DefaultAssemblyReader().readComponent( new StringReader( sw.toString() ) );
177     //
178     // List<FileSet> fileSets = result.getFileSets();
179     //
180     // assertNotNull( fileSets );
181     // assertEquals( 1, fileSets.size() );
182     //
183     // FileSet fs = (FileSet) fileSets.get( 0 );
184     //
185     // assertEquals( "/dir", fs.getDirectory() );
186     // }
187     //
188     // public void testGetComponentFromFile_ShouldReadComponent()
189     // throws IOException, AssemblyReadException
190     // {
191     // Component component = new Component();
192     //
193     // FileSet fileSet = new FileSet();
194     // fileSet.setDirectory( "/dir" );
195     //
196     // component.addFileSet( fileSet );
197     //
198     // File componentFile = fileManager.createTempFile();
199     //
200     // FileWriter writer = null;
201     //
202     // try
203     // {
204     // writer = new FileWriter( componentFile );
205     //
206     // ComponentXpp3Writer componentWriter = new ComponentXpp3Writer();
207     //
208     // componentWriter.write( writer, component );
209     // }
210     // finally
211     // {
212     // IOUtil.close( writer );
213     // }
214     //
215     // File basedir = componentFile.getParentFile();
216     // String filename = componentFile.getName();
217     //
218     // configSource.getBasedir();
219     // configSourceControl.setReturnValue( basedir );
220     //
221     // mockManager.replayAll();
222     //
223     // Component result = new DefaultAssemblyReader().getComponentFromFile( filename, configSource );
224     //
225     // List<FileSet> fileSets = result.getFileSets();
226     //
227     // assertNotNull( fileSets );
228     // assertEquals( 1, fileSets.size() );
229     //
230     // FileSet fs = (FileSet) fileSets.get( 0 );
231     //
232     // assertEquals( "/dir", fs.getDirectory() );
233     //
234     // mockManager.verifyAll();
235     // }
236 
237     public void testMergeComponentWithAssembly_ShouldAddOneFileSetToExistingListOfTwo()
238     {
239         final Assembly assembly = new Assembly();
240 
241         FileSet fs = new FileSet();
242         fs.setDirectory( "/dir" );
243 
244         assembly.addFileSet( fs );
245 
246         fs = new FileSet();
247         fs.setDirectory( "/other-dir" );
248         assembly.addFileSet( fs );
249 
250         fs = new FileSet();
251         fs.setDirectory( "/third-dir" );
252 
253         final Component component = new Component();
254 
255         component.addFileSet( fs );
256 
257         new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
258 
259         final List<FileSet> fileSets = assembly.getFileSets();
260 
261         assertNotNull( fileSets );
262         assertEquals( 3, fileSets.size() );
263 
264         final FileSet rfs1 = fileSets.get( 0 );
265         assertEquals( "/dir", rfs1.getDirectory() );
266 
267         final FileSet rfs2 = fileSets.get( 1 );
268         assertEquals( "/other-dir", rfs2.getDirectory() );
269 
270         final FileSet rfs3 = fileSets.get( 2 );
271         assertEquals( "/third-dir", rfs3.getDirectory() );
272 
273     }
274 
275     public void testMergeComponentWithAssembly_ShouldAddOneFileItemToExistingListOfTwo()
276     {
277         final Assembly assembly = new Assembly();
278 
279         FileItem fi = new FileItem();
280         fi.setSource( "file" );
281 
282         assembly.addFile( fi );
283 
284         fi = new FileItem();
285         fi.setSource( "file2" );
286 
287         assembly.addFile( fi );
288 
289         fi = new FileItem();
290         fi.setSource( "file3" );
291 
292         final Component component = new Component();
293 
294         component.addFile( fi );
295 
296         new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
297 
298         final List<FileItem> fileItems = assembly.getFiles();
299 
300         assertNotNull( fileItems );
301         assertEquals( 3, fileItems.size() );
302 
303         final FileItem rf1 = fileItems.get( 0 );
304         assertEquals( "file", rf1.getSource() );
305 
306         final FileItem rf2 = fileItems.get( 1 );
307         assertEquals( "file2", rf2.getSource() );
308 
309         final FileItem rf3 = fileItems.get( 2 );
310         assertEquals( "file3", rf3.getSource() );
311 
312     }
313 
314     public void testMergeComponentWithAssembly_ShouldAddOneDependencySetToExistingListOfTwo()
315     {
316         final Assembly assembly = new Assembly();
317 
318         DependencySet ds = new DependencySet();
319         ds.setScope( Artifact.SCOPE_RUNTIME );
320 
321         assembly.addDependencySet( ds );
322 
323         ds = new DependencySet();
324         ds.setScope( Artifact.SCOPE_COMPILE );
325 
326         assembly.addDependencySet( ds );
327 
328         final Component component = new Component();
329 
330         ds = new DependencySet();
331         ds.setScope( Artifact.SCOPE_SYSTEM );
332 
333         component.addDependencySet( ds );
334 
335         new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
336 
337         final List<DependencySet> depSets = assembly.getDependencySets();
338 
339         assertNotNull( depSets );
340         assertEquals( 3, depSets.size() );
341 
342         assertEquals( Artifact.SCOPE_RUNTIME, depSets.get( 0 )
343                                                      .getScope() );
344         assertEquals( Artifact.SCOPE_COMPILE, depSets.get( 1 )
345                                                      .getScope() );
346         assertEquals( Artifact.SCOPE_SYSTEM, depSets.get( 2 )
347                                                     .getScope() );
348     }
349 
350     public void testMergeComponentWithAssembly_ShouldAddOneRepositoryToExistingListOfTwo()
351     {
352         final Assembly assembly = new Assembly();
353 
354         Repository repo = new Repository();
355         repo.setScope( Artifact.SCOPE_RUNTIME );
356 
357         assembly.addRepository( repo );
358 
359         repo = new Repository();
360         repo.setScope( Artifact.SCOPE_COMPILE );
361 
362         assembly.addRepository( repo );
363 
364         final Component component = new Component();
365 
366         repo = new Repository();
367         repo.setScope( Artifact.SCOPE_SYSTEM );
368 
369         component.addRepository( repo );
370 
371         new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
372 
373         final List<Repository> depSets = assembly.getRepositories();
374 
375         assertNotNull( depSets );
376         assertEquals( 3, depSets.size() );
377 
378         assertEquals( Artifact.SCOPE_RUNTIME, depSets.get( 0 )
379                                                      .getScope() );
380         assertEquals( Artifact.SCOPE_COMPILE, depSets.get( 1 )
381                                                      .getScope() );
382         assertEquals( Artifact.SCOPE_SYSTEM, depSets.get( 2 )
383                                                     .getScope() );
384     }
385 
386     public void testMergeComponentWithAssembly_ShouldAddOneContainerDescriptorHandlerToExistingListOfTwo()
387     {
388         final Assembly assembly = new Assembly();
389 
390         ContainerDescriptorHandlerConfig cfg = new ContainerDescriptorHandlerConfig();
391         cfg.setHandlerName( "one" );
392 
393         assembly.addContainerDescriptorHandler( cfg );
394 
395         cfg = new ContainerDescriptorHandlerConfig();
396         cfg.setHandlerName( "two" );
397 
398         assembly.addContainerDescriptorHandler( cfg );
399 
400         final Component component = new Component();
401 
402         cfg = new ContainerDescriptorHandlerConfig();
403         cfg.setHandlerName( "three" );
404 
405         component.addContainerDescriptorHandler( cfg );
406 
407         new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
408 
409         final List<ContainerDescriptorHandlerConfig> result = assembly.getContainerDescriptorHandlers();
410 
411         assertNotNull( result );
412         assertEquals( 3, result.size() );
413 
414         final Iterator<ContainerDescriptorHandlerConfig> it = result.iterator();
415         assertEquals( "one", it.next()
416                                .getHandlerName() );
417         assertEquals( "two", it.next()
418                                .getHandlerName() );
419         assertEquals( "three", it.next()
420                                  .getHandlerName() );
421     }
422 
423     // FIXME: Deep merging should take place...
424     // public void
425     // testMergeComponentWithAssembly_ShouldMergeOneFileSetToOneOfExistingTwo()
426     // {
427     // Assembly assembly = new Assembly();
428     //
429     // FileSet fs = new FileSet();
430     // fs.setDirectory( "/dir" );
431     // fs.addInclude( "**/test.txt" );
432     //
433     // assembly.addFileSet( fs );
434     //
435     // fs = new FileSet();
436     // fs.setDirectory( "/other-dir" );
437     // assembly.addFileSet( fs );
438     //
439     // fs = new FileSet();
440     // fs.setDirectory( "/dir" );
441     // fs.addInclude( "**/components.txt" );
442     //
443     // Component component = new Component();
444     //
445     // component.addFileSet( fs );
446     //
447     // new DefaultAssemblyReader().mergeComponentWithAssembly( component,
448     // assembly );
449     //
450     // List<FileSet> fileSets = assembly.getFileSets();
451     //
452     // assertNotNull( fileSets );
453     // assertEquals( 2, fileSets.size() );
454     //
455     // FileSet rfs1 = (FileSet) fileSets.get( 0 );
456     // assertEquals( "/dir", rfs1.getDirectory() );
457     //
458     // List includes = rfs1.getIncludes();
459     //
460     // assertNotNull( includes );
461     // assertEquals( 2, includes.size() );
462     // assertTrue( includes.contains( "**/test.txt" ) );
463     // assertTrue( includes.contains( "**/components.txt" ) );
464     //
465     // FileSet rfs2 = (FileSet) fileSets.get( 1 );
466     // assertEquals( "/other-dir", rfs2.getDirectory() );
467     //
468     // }
469 
470     public void testMergeComponentsWithMainAssembly_ShouldAddOneFileSetToAssembly()
471         throws IOException, AssemblyReadException
472     {
473         final Component component = new Component();
474 
475         final FileSet fileSet = new FileSet();
476         fileSet.setDirectory( "/dir" );
477 
478         component.addFileSet( fileSet );
479 
480         final File componentFile = fileManager.createTempFile();
481 
482         Writer writer = null;
483 
484         try
485         {
486             writer = new OutputStreamWriter( new FileOutputStream( componentFile ), "UTF-8" );
487 
488             final ComponentXpp3Writer componentWriter = new ComponentXpp3Writer();
489 
490             componentWriter.write( writer, component );
491             writer.flush();
492         }
493         finally
494         {
495             IOUtil.close( writer );
496         }
497 
498         final String filename = componentFile.getName();
499 
500         final Assembly assembly = new Assembly();
501         assembly.addComponentDescriptor( filename );
502 
503         final File basedir = componentFile.getParentFile();
504 
505         configSource.getBasedir();
506         configSourceControl.setReturnValue( basedir, MockControl.ZERO_OR_MORE );
507 
508         final MavenProject project = new MavenProject();
509 
510         configSource.getProject();
511         configSourceControl.setReturnValue( project, MockControl.ZERO_OR_MORE );
512 
513         mockManager.replayAll();
514 
515         new DefaultAssemblyReader().mergeComponentsWithMainAssembly( assembly, null, configSource );
516 
517         final List<FileSet> fileSets = assembly.getFileSets();
518 
519         assertNotNull( fileSets );
520         assertEquals( 1, fileSets.size() );
521 
522         final FileSet fs = fileSets.get( 0 );
523 
524         assertEquals( "/dir", fs.getDirectory() );
525 
526         mockManager.verifyAll();
527     }
528 
529     public void testReadAssembly_ShouldReadAssemblyWithoutComponentsInterpolationOrSiteDirInclusion()
530         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
531     {
532         final Assembly assembly = new Assembly();
533         assembly.setId( "test" );
534 
535         final StringWriter sw = new StringWriter();
536         final AssemblyXpp3Writer assemblyWriter = new AssemblyXpp3Writer();
537 
538         assemblyWriter.write( sw, assembly );
539 
540         final StringReader sr = new StringReader( sw.toString() );
541 
542         final File basedir = fileManager.createTempDir();
543 
544         configSource.getBasedir();
545         configSourceControl.setReturnValue( basedir, MockControl.ZERO_OR_MORE );
546 
547         final Model model = new Model();
548         model.setGroupId( "group" );
549         model.setArtifactId( "artifact" );
550         model.setVersion( "version" );
551 
552         final MavenProject project = new MavenProject( model );
553 
554         configSource.getProject();
555         configSourceControl.setReturnValue( project, MockControl.ZERO_OR_MORE );
556 
557         configSource.isSiteIncluded();
558         configSourceControl.setReturnValue( false, MockControl.ZERO_OR_MORE );
559 
560         mockManager.replayAll();
561 
562         final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
563 
564         assertEquals( assembly.getId(), result.getId() );
565 
566         mockManager.verifyAll();
567     }
568 
569     public void testReadAssembly_ShouldReadAssemblyWithSiteDirInclusionFromAssemblyWithoutComponentsOrInterpolation()
570         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
571     {
572         final Assembly assembly = new Assembly();
573         assembly.setId( "test" );
574 
575         assembly.setIncludeSiteDirectory( true );
576 
577         final StringWriter sw = new StringWriter();
578         final AssemblyXpp3Writer assemblyWriter = new AssemblyXpp3Writer();
579 
580         assemblyWriter.write( sw, assembly );
581 
582         final StringReader sr = new StringReader( sw.toString() );
583 
584         final File siteDir = fileManager.createTempDir();
585 
586         configSource.getSiteDirectory();
587         configSourceControl.setReturnValue( siteDir, MockControl.ZERO_OR_MORE );
588 
589         final File basedir = fileManager.createTempDir();
590 
591         configSource.getBasedir();
592         configSourceControl.setReturnValue( basedir, MockControl.ZERO_OR_MORE );
593 
594         final Model model = new Model();
595         model.setGroupId( "group" );
596         model.setArtifactId( "artifact" );
597         model.setVersion( "version" );
598 
599         final MavenProject project = new MavenProject( model );
600 
601         configSource.getProject();
602         configSourceControl.setReturnValue( project, MockControl.ZERO_OR_MORE );
603 
604         configSource.isSiteIncluded();
605         configSourceControl.setReturnValue( false, MockControl.ZERO_OR_MORE );
606 
607         mockManager.replayAll();
608 
609         final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
610 
611         assertEquals( assembly.getId(), result.getId() );
612 
613         final List<FileSet> fileSets = result.getFileSets();
614 
615         assertEquals( 1, fileSets.size() );
616 
617         assertEquals( "/site", fileSets.get( 0 )
618                                        .getOutputDirectory() );
619 
620         mockManager.verifyAll();
621     }
622 
623     public void testReadAssembly_ShouldReadAssemblyWithSiteDirInclusionFromConfigWithoutComponentsOrInterpolation()
624         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
625     {
626         final Assembly assembly = new Assembly();
627         assembly.setId( "test" );
628 
629         final StringWriter sw = new StringWriter();
630         final AssemblyXpp3Writer assemblyWriter = new AssemblyXpp3Writer();
631 
632         assemblyWriter.write( sw, assembly );
633 
634         final StringReader sr = new StringReader( sw.toString() );
635 
636         final File siteDir = fileManager.createTempDir();
637 
638         configSource.getSiteDirectory();
639         configSourceControl.setReturnValue( siteDir, MockControl.ZERO_OR_MORE );
640 
641         final File basedir = fileManager.createTempDir();
642 
643         configSource.getBasedir();
644         configSourceControl.setReturnValue( basedir, MockControl.ZERO_OR_MORE );
645 
646         final Model model = new Model();
647         model.setGroupId( "group" );
648         model.setArtifactId( "artifact" );
649         model.setVersion( "version" );
650 
651         final MavenProject project = new MavenProject( model );
652 
653         configSource.getProject();
654         configSourceControl.setReturnValue( project, MockControl.ZERO_OR_MORE );
655 
656         configSource.isSiteIncluded();
657         configSourceControl.setReturnValue( true, MockControl.ZERO_OR_MORE );
658 
659         mockManager.replayAll();
660 
661         final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
662 
663         assertEquals( assembly.getId(), result.getId() );
664 
665         final List<FileSet> fileSets = result.getFileSets();
666 
667         assertEquals( 1, fileSets.size() );
668 
669         assertEquals( "/site", fileSets.get( 0 )
670                                        .getOutputDirectory() );
671 
672         mockManager.verifyAll();
673     }
674 
675     public void testReadAssembly_ShouldReadAssemblyWithComponentWithoutSiteDirInclusionOrInterpolation()
676         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
677     {
678         final File componentsFile = fileManager.createTempFile();
679 
680         final File basedir = componentsFile.getParentFile();
681         final String componentsFilename = componentsFile.getName();
682 
683         final Component component = new Component();
684 
685         final FileSet fs = new FileSet();
686         fs.setDirectory( "/dir" );
687 
688         component.addFileSet( fs );
689 
690         Writer fw = null;
691 
692         try
693         {
694             fw = new OutputStreamWriter( new FileOutputStream( componentsFile ), "UTF-8" );
695             new ComponentXpp3Writer().write( fw, component );
696         }
697         finally
698         {
699             IOUtil.close( fw );
700         }
701 
702         final Assembly assembly = new Assembly();
703         assembly.setId( "test" );
704 
705         assembly.addComponentDescriptor( componentsFilename );
706 
707         final StringWriter sw = new StringWriter();
708         final AssemblyXpp3Writer assemblyWriter = new AssemblyXpp3Writer();
709 
710         assemblyWriter.write( sw, assembly );
711 
712         final StringReader sr = new StringReader( sw.toString() );
713 
714         configSource.getBasedir();
715         configSourceControl.setReturnValue( basedir, MockControl.ZERO_OR_MORE );
716 
717         final Model model = new Model();
718         model.setGroupId( "group" );
719         model.setArtifactId( "artifact" );
720         model.setVersion( "version" );
721 
722         final MavenProject project = new MavenProject( model );
723         configSource.getProject();
724         configSourceControl.setReturnValue( project, MockControl.ZERO_OR_MORE );
725 
726         configSource.isSiteIncluded();
727         configSourceControl.setReturnValue( false, MockControl.ZERO_OR_MORE );
728 
729         mockManager.replayAll();
730 
731         final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
732 
733         assertEquals( assembly.getId(), result.getId() );
734 
735         final List<FileSet> fileSets = result.getFileSets();
736 
737         assertEquals( 1, fileSets.size() );
738 
739         assertEquals( "/dir", fileSets.get( 0 )
740                                       .getDirectory() );
741 
742         mockManager.verifyAll();
743     }
744 
745     public void testReadAssembly_ShouldReadAssemblyWithComponentInterpolationWithoutSiteDirInclusionOrAssemblyInterpolation()
746         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
747     {
748         final File componentsFile = fileManager.createTempFile();
749 
750         final File basedir = componentsFile.getParentFile();
751         final String componentsFilename = componentsFile.getName();
752 
753         final Component component = new Component();
754 
755         final FileSet fs = new FileSet();
756         fs.setDirectory( "${groupId}-dir" );
757 
758         component.addFileSet( fs );
759 
760         Writer fw = null;
761 
762         try
763         {
764             fw = new OutputStreamWriter( new FileOutputStream( componentsFile ), "UTF-8" );
765             new ComponentXpp3Writer().write( fw, component );
766         }
767         finally
768         {
769             IOUtil.close( fw );
770         }
771 
772         final Assembly assembly = new Assembly();
773         assembly.setId( "test" );
774 
775         assembly.addComponentDescriptor( componentsFilename );
776 
777         final StringWriter sw = new StringWriter();
778         final AssemblyXpp3Writer assemblyWriter = new AssemblyXpp3Writer();
779 
780         assemblyWriter.write( sw, assembly );
781 
782         final StringReader sr = new StringReader( sw.toString() );
783 
784         configSource.getBasedir();
785         configSourceControl.setReturnValue( basedir, MockControl.ONE_OR_MORE );
786 
787         final Model model = new Model();
788         model.setGroupId( "group" );
789         model.setArtifactId( "artifact" );
790         model.setVersion( "version" );
791 
792         final MavenProject project = new MavenProject( model );
793 
794         configSource.getProject();
795         configSourceControl.setReturnValue( project, MockControl.ONE_OR_MORE );
796 
797         configSource.isSiteIncluded();
798         configSourceControl.setReturnValue( false );
799 
800         mockManager.replayAll();
801 
802         final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
803 
804         assertEquals( assembly.getId(), result.getId() );
805 
806         final List<FileSet> fileSets = result.getFileSets();
807 
808         assertEquals( 1, fileSets.size() );
809 
810         assertEquals( "group-dir", fileSets.get( 0 )
811                                            .getDirectory() );
812 
813         mockManager.verifyAll();
814     }
815 
816     public void testReadAssembly_ShouldReadAssemblyWithInterpolationWithoutComponentsOrSiteDirInclusion()
817         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
818     {
819         final Assembly assembly = new Assembly();
820         assembly.setId( "${groupId}-assembly" );
821 
822         final StringWriter sw = new StringWriter();
823         final AssemblyXpp3Writer assemblyWriter = new AssemblyXpp3Writer();
824 
825         assemblyWriter.write( sw, assembly );
826 
827         final StringReader sr = new StringReader( sw.toString() );
828 
829         final File basedir = fileManager.createTempDir();
830 
831         configSource.getBasedir();
832         configSourceControl.setReturnValue( basedir, MockControl.ZERO_OR_MORE );
833 
834         final Model model = new Model();
835         model.setGroupId( "group" );
836         model.setArtifactId( "artifact" );
837         model.setVersion( "version" );
838 
839         final MavenProject project = new MavenProject( model );
840 
841         configSource.getProject();
842         configSourceControl.setReturnValue( project, MockControl.ZERO_OR_MORE );
843 
844         configSource.isSiteIncluded();
845         configSourceControl.setReturnValue( false, MockControl.ZERO_OR_MORE );
846 
847         mockManager.replayAll();
848 
849         final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
850 
851         assertEquals( "group-assembly", result.getId() );
852 
853         mockManager.verifyAll();
854     }
855 
856     public void testGetAssemblyFromDescriptorFile_ShouldReadAssembly()
857         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
858     {
859         final Assembly assembly = new Assembly();
860         assembly.setId( "test" );
861 
862         final FileSet fs = new FileSet();
863         fs.setDirectory( "/dir" );
864 
865         assembly.addFileSet( fs );
866 
867         final File assemblyFile = fileManager.createTempFile();
868 
869         final File basedir = assemblyFile.getParentFile();
870 
871         configSource.getBasedir();
872         configSourceControl.setReturnValue( basedir, MockControl.ZERO_OR_MORE );
873 
874         configSource.getProject();
875         configSourceControl.setReturnValue( new MavenProject( new Model() ), MockControl.ZERO_OR_MORE );
876 
877         configSource.isSiteIncluded();
878         configSourceControl.setReturnValue( false, MockControl.ZERO_OR_MORE );
879 
880         Writer writer = null;
881         try
882         {
883             writer = new OutputStreamWriter( new FileOutputStream( assemblyFile ), "UTF-8" );
884             new AssemblyXpp3Writer().write( writer, assembly );
885         }
886         finally
887         {
888             IOUtil.close( writer );
889         }
890 
891         mockManager.replayAll();
892 
893         final Assembly result = new DefaultAssemblyReader().getAssemblyFromDescriptorFile( assemblyFile, configSource );
894 
895         assertEquals( assembly.getId(), result.getId() );
896 
897         mockManager.verifyAll();
898     }
899 
900     public void testGetAssemblyForDescriptorReference_ShouldReadBinaryAssemblyRef()
901         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
902     {
903         final File basedir = fileManager.createTempDir();
904 
905         configSource.getBasedir();
906         configSourceControl.setReturnValue( basedir, MockControl.ZERO_OR_MORE );
907 
908         configSource.getProject();
909         configSourceControl.setReturnValue( new MavenProject( new Model() ), MockControl.ZERO_OR_MORE );
910 
911         configSource.isSiteIncluded();
912         configSourceControl.setReturnValue( false, MockControl.ZERO_OR_MORE );
913 
914         configSource.isIgnoreMissingDescriptor();
915         configSourceControl.setReturnValue( false, MockControl.ZERO_OR_MORE );
916 
917         mockManager.replayAll();
918 
919         final Assembly result = new DefaultAssemblyReader().getAssemblyForDescriptorReference( "bin", configSource );
920 
921         assertEquals( "bin", result.getId() );
922 
923         mockManager.verifyAll();
924     }
925 
926     public void testReadAssemblies_ShouldGetAssemblyDescriptorFromSingleFile()
927         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
928     {
929         final Assembly assembly = new Assembly();
930         assembly.setId( "test" );
931 
932         final FileSet fs = new FileSet();
933         fs.setDirectory( "/dir" );
934 
935         assembly.addFileSet( fs );
936 
937         final File basedir = fileManager.createTempDir();
938 
939         final List<String> files = writeAssembliesToFile( Collections.singletonList( assembly ), basedir );
940 
941         final String assemblyFile = files.get( 0 );
942 
943         final List<Assembly> assemblies = performReadAssemblies( basedir, assemblyFile, null, null, null, null );
944 
945         assertNotNull( assemblies );
946         assertEquals( 1, assemblies.size() );
947 
948         final Assembly result = assemblies.get( 0 );
949 
950         assertEquals( assembly.getId(), result.getId() );
951     }
952 
953     public void testReadAssemblies_ShouldFailWhenSingleDescriptorFileMissing()
954         throws IOException, InvalidAssemblerConfigurationException
955     {
956         final File basedir = fileManager.createTempDir();
957 
958         final File assemblyFile = new File( basedir, "test.xml" );
959         assemblyFile.delete();
960 
961         try
962         {
963             performReadAssemblies( basedir, assemblyFile.getAbsolutePath(), null, null, null, null, false );
964 
965             fail( "Should fail when descriptor file is missing and ignoreDescriptors == false" );
966         }
967         catch ( final AssemblyReadException e )
968         {
969             // expected.
970         }
971     }
972 
973     public void testReadAssemblies_ShouldIgnoreMissingSingleDescriptorFileWhenIgnoreIsConfigured()
974         throws IOException, InvalidAssemblerConfigurationException
975     {
976         final File basedir = fileManager.createTempDir();
977 
978         final File assemblyFile = new File( basedir, "test.xml" );
979         assemblyFile.delete();
980 
981         try
982         {
983             performReadAssemblies( basedir, assemblyFile.getAbsolutePath(), null, null, null, null, true );
984         }
985         catch ( final AssemblyReadException e )
986         {
987             e.printStackTrace();
988             fail( "Setting ignoreMissingDescriptor == true (true flag in performReadAssemblies, above) should NOT produce an exception." );
989         }
990     }
991 
992     public void testReadAssemblies_ShouldGetAssemblyDescriptorFromSingleRef()
993         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
994     {
995         final File basedir = fileManager.createTempDir();
996 
997         final List<Assembly> assemblies = performReadAssemblies( basedir, null, "bin", null, null, null );
998 
999         assertNotNull( assemblies );
1000         assertEquals( 1, assemblies.size() );
1001 
1002         final Assembly result = assemblies.get( 0 );
1003 
1004         assertEquals( "bin", result.getId() );
1005     }
1006 
1007     public void testReadAssemblies_ShouldGetAssemblyDescriptorFromFileArray()
1008         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
1009     {
1010         final Assembly assembly1 = new Assembly();
1011         assembly1.setId( "test" );
1012 
1013         final Assembly assembly2 = new Assembly();
1014         assembly2.setId( "test2" );
1015 
1016         final List<Assembly> assemblies = new ArrayList<Assembly>();
1017         assemblies.add( assembly1 );
1018         assemblies.add( assembly2 );
1019 
1020         final File basedir = fileManager.createTempDir();
1021 
1022         final List<String> files = writeAssembliesToFile( assemblies, basedir );
1023 
1024         final List<Assembly> results =
1025             performReadAssemblies( basedir, null, null, files.toArray( new String[0] ), null, null );
1026 
1027         assertNotNull( results );
1028         assertEquals( 2, results.size() );
1029 
1030         final Assembly result1 = assemblies.get( 0 );
1031 
1032         assertEquals( assembly1.getId(), result1.getId() );
1033 
1034         final Assembly result2 = assemblies.get( 1 );
1035 
1036         assertEquals( assembly2.getId(), result2.getId() );
1037     }
1038 
1039     public void testReadAssemblies_ShouldGetAssemblyDescriptorFromMultipleRefs()
1040         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
1041     {
1042         final File basedir = fileManager.createTempDir();
1043 
1044         final List<Assembly> assemblies =
1045             performReadAssemblies( basedir, null, null, null, new String[] { "bin", "src" }, null );
1046 
1047         assertNotNull( assemblies );
1048         assertEquals( 2, assemblies.size() );
1049 
1050         final Assembly result = assemblies.get( 0 );
1051 
1052         assertEquals( "bin", result.getId() );
1053 
1054         final Assembly result2 = assemblies.get( 1 );
1055 
1056         assertEquals( "src", result2.getId() );
1057     }
1058 
1059     public void testReadAssemblies_ShouldGetAssemblyDescriptorFromDirectory()
1060         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
1061     {
1062         final Assembly assembly1 = new Assembly();
1063         assembly1.setId( "test" );
1064 
1065         final Assembly assembly2 = new Assembly();
1066         assembly2.setId( "test2" );
1067 
1068         final List<Assembly> assemblies = new ArrayList<Assembly>();
1069         assemblies.add( assembly1 );
1070         assemblies.add( assembly2 );
1071 
1072         final File basedir = fileManager.createTempDir();
1073 
1074         writeAssembliesToFile( assemblies, basedir );
1075 
1076         final List<Assembly> results = performReadAssemblies( basedir, null, null, null, null, basedir );
1077 
1078         assertNotNull( results );
1079         assertEquals( 2, results.size() );
1080 
1081         final Assembly result1 = assemblies.get( 0 );
1082 
1083         assertEquals( assembly1.getId(), result1.getId() );
1084 
1085         final Assembly result2 = assemblies.get( 1 );
1086 
1087         assertEquals( assembly2.getId(), result2.getId() );
1088     }
1089 
1090     public void testReadAssemblies_ShouldGetTwoAssemblyDescriptorsFromDirectoryWithThreeFiles()
1091         throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
1092     {
1093         final Assembly assembly1 = new Assembly();
1094         assembly1.setId( "test" );
1095 
1096         final Assembly assembly2 = new Assembly();
1097         assembly2.setId( "test2" );
1098 
1099         final List<Assembly> assemblies = new ArrayList<Assembly>();
1100         assemblies.add( assembly1 );
1101         assemblies.add( assembly2 );
1102 
1103         final File basedir = fileManager.createTempDir();
1104 
1105         writeAssembliesToFile( assemblies, basedir );
1106 
1107         fileManager.createFile( basedir, "readme.txt", "This is just a readme file, not a descriptor." );
1108 
1109         final List<Assembly> results = performReadAssemblies( basedir, null, null, null, null, basedir );
1110 
1111         assertNotNull( results );
1112         assertEquals( 2, results.size() );
1113 
1114         final Assembly result1 = assemblies.get( 0 );
1115 
1116         assertEquals( assembly1.getId(), result1.getId() );
1117 
1118         final Assembly result2 = assemblies.get( 1 );
1119 
1120         assertEquals( assembly2.getId(), result2.getId() );
1121     }
1122 
1123     private List<String> writeAssembliesToFile( final List<Assembly> assemblies, final File dir )
1124         throws IOException
1125     {
1126         final List<String> files = new ArrayList<String>();
1127 
1128         for ( final Iterator<Assembly> it = assemblies.iterator(); it.hasNext(); )
1129         {
1130             final Assembly assembly = it.next();
1131 
1132             final File assemblyFile = new File( dir, assembly.getId() + ".xml" );
1133 
1134             Writer writer = null;
1135             try
1136             {
1137                 writer = new OutputStreamWriter( new FileOutputStream( assemblyFile ), "UTF-8" );
1138                 new AssemblyXpp3Writer().write( writer, assembly );
1139             }
1140             finally
1141             {
1142                 IOUtil.close( writer );
1143             }
1144 
1145             files.add( assemblyFile.getAbsolutePath() );
1146         }
1147 
1148         return files;
1149     }
1150 
1151     private List<Assembly> performReadAssemblies( final File basedir, final String descriptor,
1152                                                   final String descriptorRef, final String[] descriptors,
1153                                                   final String[] descriptorRefs, final File descriptorDir )
1154         throws AssemblyReadException, InvalidAssemblerConfigurationException
1155     {
1156         return performReadAssemblies( basedir, descriptor, descriptorRef, descriptors, descriptorRefs, descriptorDir,
1157                                       false );
1158     }
1159 
1160     private List<Assembly> performReadAssemblies( final File basedir, final String descriptor,
1161                                                   final String descriptorRef, final String[] descriptors,
1162                                                   final String[] descriptorRefs, final File descriptorDir,
1163                                                   final boolean ignoreMissing )
1164         throws AssemblyReadException, InvalidAssemblerConfigurationException
1165     {
1166         configSource.getDescriptor();
1167         configSourceControl.setReturnValue( descriptor );
1168 
1169         configSource.getDescriptorId();
1170         configSourceControl.setReturnValue( descriptorRef );
1171 
1172         configSource.getDescriptorReferences();
1173         configSourceControl.setReturnValue( descriptorRefs );
1174 
1175         configSource.getDescriptors();
1176         configSourceControl.setReturnValue( descriptors );
1177 
1178         configSource.getDescriptorSourceDirectory();
1179         configSourceControl.setReturnValue( descriptorDir );
1180 
1181         configSource.getBasedir();
1182         configSourceControl.setReturnValue( basedir, MockControl.ZERO_OR_MORE );
1183 
1184         configSource.getProject();
1185         configSourceControl.setReturnValue( new MavenProject( new Model() ), MockControl.ZERO_OR_MORE );
1186 
1187         configSource.isSiteIncluded();
1188         configSourceControl.setReturnValue( false, MockControl.ZERO_OR_MORE );
1189 
1190         configSource.isIgnoreMissingDescriptor();
1191         configSourceControl.setReturnValue( ignoreMissing, MockControl.ZERO_OR_MORE );
1192 
1193         mockManager.replayAll();
1194 
1195         final List<Assembly> assemblies = new DefaultAssemblyReader().readAssemblies( configSource );
1196 
1197         mockManager.verifyAll();
1198 
1199         return assemblies;
1200     }
1201 
1202 }