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