1 package org.apache.maven.plugins.assembly.io;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.plugins.assembly.AssemblerConfigurationSource;
27 import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
28 import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest;
29 import org.apache.maven.plugins.assembly.interpolation.AssemblyInterpolator;
30 import org.apache.maven.plugins.assembly.model.Assembly;
31 import org.apache.maven.plugins.assembly.model.Component;
32 import org.apache.maven.plugins.assembly.model.ContainerDescriptorHandlerConfig;
33 import org.apache.maven.plugins.assembly.model.DependencySet;
34 import org.apache.maven.plugins.assembly.model.FileItem;
35 import org.apache.maven.plugins.assembly.model.FileSet;
36 import org.apache.maven.plugins.assembly.model.Repository;
37 import org.apache.maven.plugins.assembly.model.io.xpp3.AssemblyXpp3Writer;
38 import org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Reader;
39 import org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Writer;
40 import org.apache.maven.plugins.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 public class DefaultAssemblyReaderTest
64 extends TestCase
65 {
66
67 private TestFileManager fileManager;
68
69 private EasyMockSupport mockManager;
70
71
72 private AssemblerConfigurationSource configSource;
73
74 public static StringReader writeToStringReader( Assembly assembly )
75 throws IOException
76 {
77 final StringWriter sw = new StringWriter();
78 final AssemblyXpp3Writer assemblyWriter = new AssemblyXpp3Writer();
79
80 assemblyWriter.write( sw, assembly );
81
82 return new StringReader( sw.toString() );
83 }
84
85 @Override
86 public void setUp()
87 {
88 fileManager = new TestFileManager( "assembly-reader.test.", ".xml" );
89 mockManager = new EasyMockSupport();
90
91 configSource = mockManager.createMock( AssemblerConfigurationSource.class );
92
93 ArtifactRepository localRepo = mockManager.createMock( ArtifactRepository.class );
94
95 expect( localRepo.getBasedir() ).andReturn( "/path/to/local/repo" ).anyTimes();
96 expect( configSource.getLocalRepository() ).andReturn( localRepo ).anyTimes();
97 expect( configSource.getRemoteRepositories() ).andReturn(
98 Collections.<ArtifactRepository>emptyList() ).anyTimes();
99 expect( configSource.getMavenSession() ).andReturn( null ).anyTimes();
100 }
101
102 @Override
103 public void tearDown()
104 throws IOException
105 {
106 fileManager.cleanUp();
107 }
108
109 public void testIncludeSiteInAssembly_ShouldFailIfSiteDirectoryNonExistent()
110 throws IOException
111 {
112 final File siteDir = File.createTempFile( "assembly-reader.", ".test" );
113 siteDir.delete();
114
115 expect( configSource.getSiteDirectory() ).andReturn( siteDir ).anyTimes();
116
117 final Assembly assembly = new Assembly();
118
119 mockManager.replayAll();
120
121 try
122 {
123 new DefaultAssemblyReader().includeSiteInAssembly( assembly, configSource );
124
125 fail( "Should fail when site directory is non-existent." );
126 }
127 catch ( final InvalidAssemblerConfigurationException e )
128 {
129
130 }
131
132 mockManager.verifyAll();
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 public void testIncludeSiteInAssembly_ShouldAddSiteDirFileSetWhenDirExists()
213 throws IOException, InvalidAssemblerConfigurationException
214 {
215 final File siteDir = fileManager.createTempDir();
216
217 expect( configSource.getSiteDirectory() ).andReturn( siteDir ).anyTimes();
218
219 final Assembly assembly = new Assembly();
220
221 mockManager.replayAll();
222
223 new DefaultAssemblyReader().includeSiteInAssembly( assembly, configSource );
224
225 final List<FileSet> fileSets = assembly.getFileSets();
226
227 assertNotNull( fileSets );
228 assertEquals( 1, fileSets.size() );
229
230 final FileSet fs = fileSets.get( 0 );
231
232 assertEquals( siteDir.getPath(), 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 ).getScope() );
343 assertEquals( Artifact.SCOPE_COMPILE, depSets.get( 1 ).getScope() );
344 assertEquals( Artifact.SCOPE_SYSTEM, depSets.get( 2 ).getScope() );
345 }
346
347 public void testMergeComponentWithAssembly_ShouldAddOneRepositoryToExistingListOfTwo()
348 {
349 final Assembly assembly = new Assembly();
350
351 Repository repo = new Repository();
352 repo.setScope( Artifact.SCOPE_RUNTIME );
353
354 assembly.addRepository( repo );
355
356 repo = new Repository();
357 repo.setScope( Artifact.SCOPE_COMPILE );
358
359 assembly.addRepository( repo );
360
361 final Component component = new Component();
362
363 repo = new Repository();
364 repo.setScope( Artifact.SCOPE_SYSTEM );
365
366 component.addRepository( repo );
367
368 new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
369
370 final List<Repository> depSets = assembly.getRepositories();
371
372 assertNotNull( depSets );
373 assertEquals( 3, depSets.size() );
374
375 assertEquals( Artifact.SCOPE_RUNTIME, depSets.get( 0 ).getScope() );
376 assertEquals( Artifact.SCOPE_COMPILE, depSets.get( 1 ).getScope() );
377 assertEquals( Artifact.SCOPE_SYSTEM, depSets.get( 2 ).getScope() );
378 }
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427 public void testMergeComponentWithAssembly_ShouldAddOneContainerDescriptorHandlerToExistingListOfTwo()
428 {
429 final Assembly assembly = new Assembly();
430
431 ContainerDescriptorHandlerConfig cfg = new ContainerDescriptorHandlerConfig();
432 cfg.setHandlerName( "one" );
433
434 assembly.addContainerDescriptorHandler( cfg );
435
436 cfg = new ContainerDescriptorHandlerConfig();
437 cfg.setHandlerName( "two" );
438
439 assembly.addContainerDescriptorHandler( cfg );
440
441 final Component component = new Component();
442
443 cfg = new ContainerDescriptorHandlerConfig();
444 cfg.setHandlerName( "three" );
445
446 component.addContainerDescriptorHandler( cfg );
447
448 new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
449
450 final List<ContainerDescriptorHandlerConfig> result = assembly.getContainerDescriptorHandlers();
451
452 assertNotNull( result );
453 assertEquals( 3, result.size() );
454
455 final Iterator<ContainerDescriptorHandlerConfig> it = result.iterator();
456 assertEquals( "one", it.next().getHandlerName() );
457 assertEquals( "two", it.next().getHandlerName() );
458 assertEquals( "three", it.next().getHandlerName() );
459 }
460
461 public void testMergeComponentsWithMainAssembly_ShouldAddOneFileSetToAssembly()
462 throws IOException, AssemblyReadException
463 {
464 final Component component = new Component();
465
466 final FileSet fileSet = new FileSet();
467 fileSet.setDirectory( "/dir" );
468
469 component.addFileSet( fileSet );
470
471 final File componentFile = fileManager.createTempFile();
472
473 Writer writer = null;
474
475 try
476 {
477 writer = new OutputStreamWriter( new FileOutputStream( componentFile ), "UTF-8" );
478
479 final ComponentXpp3Writer componentWriter = new ComponentXpp3Writer();
480
481 componentWriter.write( writer, component );
482 writer.close();
483 writer = null;
484 }
485 finally
486 {
487 IOUtil.close( writer );
488 }
489
490 final String filename = componentFile.getName();
491
492 final Assembly assembly = new Assembly();
493 assembly.addComponentDescriptor( filename );
494
495 final File basedir = componentFile.getParentFile();
496
497 final MavenProject project = new MavenProject();
498
499 expect( configSource.getProject() ).andReturn( project ).anyTimes();
500 expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes();
501 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
502 InterpolationState is = new InterpolationState();
503 ComponentXpp3Reader.ContentTransformer componentIp =
504 AssemblyInterpolator.componentInterpolator( FixedStringSearchInterpolator.create(), is,
505 new ConsoleLogger( Logger.LEVEL_DEBUG, "console" ) );
506
507 mockManager.replayAll();
508
509 new DefaultAssemblyReader().mergeComponentsWithMainAssembly( assembly, null, configSource, componentIp );
510
511 final List<FileSet> fileSets = assembly.getFileSets();
512
513 assertNotNull( fileSets );
514 assertEquals( 1, fileSets.size() );
515
516 final FileSet fs = fileSets.get( 0 );
517
518 assertEquals( "/dir", fs.getDirectory() );
519
520 mockManager.verifyAll();
521 }
522
523 public void testReadAssembly_ShouldReadAssemblyWithoutComponentsInterpolationOrSiteDirInclusion()
524 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
525 {
526 final Assembly assembly = new Assembly();
527 assembly.setId( "test" );
528
529 final Assembly result = doReadAssembly( assembly );
530
531 assertEquals( assembly.getId(), result.getId() );
532
533 mockManager.verifyAll();
534 }
535
536 public void testReadAssembly_ShouldReadAssemblyWithSiteDirInclusionFromAssemblyWithoutComponentsOrInterpolation()
537 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
538 {
539 final Assembly assembly = new Assembly();
540 assembly.setId( "test" );
541
542 assembly.setIncludeSiteDirectory( true );
543
544 final StringReader sr = writeToStringReader( assembly );
545
546 final File siteDir = fileManager.createTempDir();
547
548 expect( configSource.getSiteDirectory() ).andReturn( siteDir ).anyTimes();
549
550 final File basedir = fileManager.createTempDir();
551
552 expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes();
553
554 final Model model = new Model();
555 model.setGroupId( "group" );
556 model.setArtifactId( "artifact" );
557 model.setVersion( "version" );
558
559 final MavenProject project = new MavenProject( model );
560
561 expect( configSource.getProject() ).andReturn( project ).anyTimes();
562
563 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
564
565 mockManager.replayAll();
566
567 final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
568
569 assertEquals( assembly.getId(), result.getId() );
570
571 final List<FileSet> fileSets = result.getFileSets();
572
573 assertEquals( 1, fileSets.size() );
574
575 assertEquals( "/site", fileSets.get( 0 ).getOutputDirectory() );
576
577 mockManager.verifyAll();
578 }
579
580 public void testReadAssembly_ShouldReadAssemblyWithComponentWithoutSiteDirInclusionOrInterpolation()
581 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
582 {
583 final File componentsFile = fileManager.createTempFile();
584
585 final File basedir = componentsFile.getParentFile();
586 final String componentsFilename = componentsFile.getName();
587
588 final Component component = new Component();
589
590 final FileSet fs = new FileSet();
591 fs.setDirectory( "/dir" );
592
593 component.addFileSet( fs );
594
595 Writer fw = null;
596
597 try
598 {
599 fw = new OutputStreamWriter( new FileOutputStream( componentsFile ), "UTF-8" );
600 new ComponentXpp3Writer().write( fw, component );
601 fw.close();
602 fw = null;
603 }
604 finally
605 {
606 IOUtil.close( fw );
607 }
608
609 final Assembly assembly = new Assembly();
610 assembly.setId( "test" );
611
612 assembly.addComponentDescriptor( componentsFilename );
613
614 final StringReader sr = writeToStringReader( assembly );
615
616 expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes();
617
618 final Model model = new Model();
619 model.setGroupId( "group" );
620 model.setArtifactId( "artifact" );
621 model.setVersion( "version" );
622
623 final MavenProject project = new MavenProject( model );
624 expect( configSource.getProject() ).andReturn( project ).anyTimes();
625
626 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
627
628 mockManager.replayAll();
629
630 final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
631
632 assertEquals( assembly.getId(), result.getId() );
633
634 final List<FileSet> fileSets = result.getFileSets();
635
636 assertEquals( 1, fileSets.size() );
637
638 assertEquals( "/dir", fileSets.get( 0 ).getDirectory() );
639
640 mockManager.verifyAll();
641 }
642
643 public void
644 testReadAssembly_ShouldReadAssemblyWithComponentInterpolationWithoutSiteDirInclusionOrAssemblyInterpolation()
645 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
646 {
647 final File componentsFile = fileManager.createTempFile();
648
649 final File basedir = componentsFile.getParentFile();
650 final String componentsFilename = componentsFile.getName();
651
652 final Component component = new Component();
653
654 final FileSet fs = new FileSet();
655 fs.setDirectory( "${groupId}-dir" );
656
657 component.addFileSet( fs );
658
659 Writer fw = null;
660
661 try
662 {
663 fw = new OutputStreamWriter( new FileOutputStream( componentsFile ), "UTF-8" );
664 new ComponentXpp3Writer().write( fw, component );
665 fw.close();
666 fw = null;
667 }
668 finally
669 {
670 IOUtil.close( fw );
671 }
672
673 final Assembly assembly = new Assembly();
674 assembly.setId( "test" );
675
676 assembly.addComponentDescriptor( componentsFilename );
677
678 final StringReader sr = writeToStringReader( assembly );
679
680 expect( configSource.getBasedir() ).andReturn( basedir ).atLeastOnce();
681
682 final Model model = new Model();
683 model.setGroupId( "group" );
684 model.setArtifactId( "artifact" );
685 model.setVersion( "version" );
686
687 final MavenProject project = new MavenProject( model );
688
689 expect( configSource.getProject() ).andReturn( project ).atLeastOnce();
690
691 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
692
693 mockManager.replayAll();
694
695 final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
696
697 assertEquals( assembly.getId(), result.getId() );
698
699 final List<FileSet> fileSets = result.getFileSets();
700
701 assertEquals( 1, fileSets.size() );
702
703 assertEquals( "group-dir", fileSets.get( 0 ).getDirectory() );
704
705 mockManager.verifyAll();
706 }
707
708 public void testReadAssembly_ShouldReadAssemblyWithInterpolationWithoutComponentsOrSiteDirInclusion()
709 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
710 {
711 final Assembly assembly = new Assembly();
712 assembly.setId( "${groupId}-assembly" );
713
714 final Assembly result = doReadAssembly( assembly );
715
716 assertEquals( "group-assembly", result.getId() );
717
718 mockManager.verifyAll();
719 }
720
721 private Assembly doReadAssembly( Assembly assembly )
722 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
723 {
724 final StringReader sr = writeToStringReader( assembly );
725
726 final File basedir = fileManager.createTempDir();
727
728 expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes();
729
730 final Model model = new Model();
731 model.setGroupId( "group" );
732 model.setArtifactId( "artifact" );
733 model.setVersion( "version" );
734
735 final MavenProject project = new MavenProject( model );
736
737 expect( configSource.getProject() ).andReturn( project ).anyTimes();
738
739 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
740
741 mockManager.replayAll();
742
743 return new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
744 }
745
746 public void testGetAssemblyFromDescriptorFile_ShouldReadAssembly()
747 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
748 {
749 final Assembly assembly = new Assembly();
750 assembly.setId( "test" );
751
752 final FileSet fs = new FileSet();
753 fs.setDirectory( "/dir" );
754
755 assembly.addFileSet( fs );
756
757 final File assemblyFile = fileManager.createTempFile();
758
759 final File basedir = assemblyFile.getParentFile();
760
761 expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes();
762
763 expect( configSource.getProject() ).andReturn( new MavenProject( new Model() ) ).anyTimes();
764
765 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
766
767 Writer writer = null;
768 try
769 {
770 writer = new OutputStreamWriter( new FileOutputStream( assemblyFile ), "UTF-8" );
771 new AssemblyXpp3Writer().write( writer, assembly );
772 writer.close();
773 writer = null;
774 }
775 finally
776 {
777 IOUtil.close( writer );
778 }
779
780 mockManager.replayAll();
781
782 final Assembly result = new DefaultAssemblyReader().getAssemblyFromDescriptorFile( assemblyFile, configSource );
783
784 assertEquals( assembly.getId(), result.getId() );
785
786 mockManager.verifyAll();
787 }
788
789 public void testGetAssemblyForDescriptorReference_ShouldReadBinaryAssemblyRef()
790 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
791 {
792 final File basedir = fileManager.createTempDir();
793
794 expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes();
795
796 expect( configSource.getProject() ).andReturn( new MavenProject( new Model() ) ).anyTimes();
797
798 expect( configSource.isIgnoreMissingDescriptor() ).andReturn( false ).anyTimes();
799
800 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
801
802 mockManager.replayAll();
803
804 final Assembly result = new DefaultAssemblyReader().getAssemblyForDescriptorReference( "bin", configSource );
805
806 assertEquals( "bin", result.getId() );
807
808 mockManager.verifyAll();
809 }
810
811 public void testReadAssemblies_ShouldGetAssemblyDescriptorFromSingleFile()
812 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
813 {
814 final Assembly assembly = new Assembly();
815 assembly.setId( "test" );
816
817 final FileSet fs = new FileSet();
818 fs.setDirectory( "/dir" );
819
820 assembly.addFileSet( fs );
821
822 final File basedir = fileManager.createTempDir();
823
824 final List<String> files = writeAssembliesToFile( Collections.singletonList( assembly ), basedir );
825
826 final String assemblyFile = files.get( 0 );
827
828 final List<Assembly> assemblies = performReadAssemblies( basedir, new String[] { assemblyFile }, null, null );
829
830 assertNotNull( assemblies );
831 assertEquals( 1, assemblies.size() );
832
833 final Assembly result = assemblies.get( 0 );
834
835 assertEquals( assembly.getId(), result.getId() );
836 }
837
838 public void testReadAssemblies_ShouldFailWhenSingleDescriptorFileMissing()
839 throws IOException, InvalidAssemblerConfigurationException
840 {
841 final File basedir = fileManager.createTempDir();
842
843 final File assemblyFile = new File( basedir, "test.xml" );
844 assemblyFile.delete();
845
846 try
847 {
848 performReadAssemblies( basedir, null, null, null, false );
849
850 fail( "Should fail when descriptor file is missing and ignoreDescriptors == false" );
851 }
852 catch ( final AssemblyReadException e )
853 {
854
855 }
856 }
857
858 public void testReadAssemblies_ShouldIgnoreMissingSingleDescriptorFileWhenIgnoreIsConfigured()
859 throws IOException, InvalidAssemblerConfigurationException
860 {
861 final File basedir = fileManager.createTempDir();
862
863 final File assemblyFile = new File( basedir, "test.xml" );
864 assemblyFile.delete();
865
866 try
867 {
868 performReadAssemblies( basedir, null, null, null, true );
869 }
870 catch ( final AssemblyReadException e )
871 {
872 fail( "Setting ignoreMissingDescriptor == true (true flag in performReadAssemblies, above) should NOT "
873 + "produce an exception." );
874 }
875 }
876
877 public void testReadAssemblies_ShouldGetAssemblyDescriptorFromFileArray()
878 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
879 {
880 final Assembly assembly1 = new Assembly();
881 assembly1.setId( "test" );
882
883 final Assembly assembly2 = new Assembly();
884 assembly2.setId( "test2" );
885
886 final List<Assembly> assemblies = new ArrayList<Assembly>();
887 assemblies.add( assembly1 );
888 assemblies.add( assembly2 );
889
890 final File basedir = fileManager.createTempDir();
891
892 final List<String> files = writeAssembliesToFile( assemblies, basedir );
893
894 final List<Assembly> results =
895 performReadAssemblies( basedir, files.toArray( new String[files.size()] ), null, null );
896
897 assertNotNull( results );
898 assertEquals( 2, results.size() );
899
900 final Assembly result1 = assemblies.get( 0 );
901
902 assertEquals( assembly1.getId(), result1.getId() );
903
904 final Assembly result2 = assemblies.get( 1 );
905
906 assertEquals( assembly2.getId(), result2.getId() );
907 }
908
909 public void testReadAssemblies_ShouldGetAssemblyDescriptorFromMultipleRefs()
910 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
911 {
912 final File basedir = fileManager.createTempDir();
913
914 final List<Assembly> assemblies =
915 performReadAssemblies( basedir, null, new String[]{ "bin", "src" }, null );
916
917 assertNotNull( assemblies );
918 assertEquals( 2, assemblies.size() );
919
920 final Assembly result = assemblies.get( 0 );
921
922 assertEquals( "bin", result.getId() );
923
924 final Assembly result2 = assemblies.get( 1 );
925
926 assertEquals( "src", result2.getId() );
927 }
928
929 public void testReadAssemblies_ShouldGetAssemblyDescriptorFromDirectory()
930 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
931 {
932 final Assembly assembly1 = new Assembly();
933 assembly1.setId( "test" );
934
935 final Assembly assembly2 = new Assembly();
936 assembly2.setId( "test2" );
937
938 final List<Assembly> assemblies = new ArrayList<Assembly>();
939 assemblies.add( assembly1 );
940 assemblies.add( assembly2 );
941
942 final File basedir = fileManager.createTempDir();
943
944 writeAssembliesToFile( assemblies, basedir );
945
946 final List<Assembly> results = performReadAssemblies( basedir, null, null, basedir );
947
948 assertNotNull( results );
949 assertEquals( 2, results.size() );
950
951 final Assembly result1 = assemblies.get( 0 );
952
953 assertEquals( assembly1.getId(), result1.getId() );
954
955 final Assembly result2 = assemblies.get( 1 );
956
957 assertEquals( assembly2.getId(), result2.getId() );
958 }
959
960 public void testReadAssemblies_ShouldGetTwoAssemblyDescriptorsFromDirectoryWithThreeFiles()
961 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
962 {
963 final Assembly assembly1 = new Assembly();
964 assembly1.setId( "test" );
965
966 final Assembly assembly2 = new Assembly();
967 assembly2.setId( "test2" );
968
969 final List<Assembly> assemblies = new ArrayList<Assembly>();
970 assemblies.add( assembly1 );
971 assemblies.add( assembly2 );
972
973 final File basedir = fileManager.createTempDir();
974
975 writeAssembliesToFile( assemblies, basedir );
976
977 fileManager.createFile( basedir, "readme.txt", "This is just a readme file, not a descriptor." );
978
979 final List<Assembly> results = performReadAssemblies( basedir, null, null, basedir );
980
981 assertNotNull( results );
982 assertEquals( 2, results.size() );
983
984 final Assembly result1 = assemblies.get( 0 );
985
986 assertEquals( assembly1.getId(), result1.getId() );
987
988 final Assembly result2 = assemblies.get( 1 );
989
990 assertEquals( assembly2.getId(), result2.getId() );
991 }
992
993 private List<String> writeAssembliesToFile( final List<Assembly> assemblies, final File dir )
994 throws IOException
995 {
996 final List<String> files = new ArrayList<String>();
997
998 for ( final Assembly assembly : assemblies )
999 {
1000 final File assemblyFile = new File( dir, assembly.getId() + ".xml" );
1001
1002 Writer writer = null;
1003 try
1004 {
1005 writer = new OutputStreamWriter( new FileOutputStream( assemblyFile ), "UTF-8" );
1006 new AssemblyXpp3Writer().write( writer, assembly );
1007 writer.close();
1008 writer = null;
1009 }
1010 finally
1011 {
1012 IOUtil.close( writer );
1013 }
1014
1015 files.add( assemblyFile.getAbsolutePath() );
1016 }
1017
1018 return files;
1019 }
1020
1021 private List<Assembly> performReadAssemblies( final File basedir, final String[] descriptors,
1022 final String[] descriptorRefs, final File descriptorDir )
1023 throws AssemblyReadException, InvalidAssemblerConfigurationException
1024 {
1025 return performReadAssemblies( basedir, descriptors, descriptorRefs, descriptorDir, false );
1026 }
1027
1028 private List<Assembly> performReadAssemblies( final File basedir, final String[] descriptors,
1029 final String[] descriptorRefs, final File descriptorDir,
1030 final boolean ignoreMissing )
1031 throws AssemblyReadException, InvalidAssemblerConfigurationException
1032 {
1033 expect( configSource.getDescriptorReferences() ).andReturn( descriptorRefs );
1034
1035 expect( configSource.getDescriptors() ).andReturn( descriptors );
1036
1037 expect( configSource.getDescriptorSourceDirectory() ).andReturn( descriptorDir );
1038
1039 expect( configSource.getBasedir() ).andReturn( basedir ).anyTimes();
1040
1041 expect( configSource.getProject() ).andReturn( new MavenProject( new Model() ) ).anyTimes();
1042
1043 expect( configSource.isIgnoreMissingDescriptor() ).andReturn( ignoreMissing ).anyTimes();
1044 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
1045
1046 mockManager.replayAll();
1047
1048 final List<Assembly> assemblies = new DefaultAssemblyReader().readAssemblies( configSource );
1049
1050 mockManager.verifyAll();
1051
1052 return assemblies;
1053 }
1054
1055 }