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 static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.io.File;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStreamWriter;
32 import java.io.StringReader;
33 import java.io.StringWriter;
34 import java.io.Writer;
35 import java.nio.charset.StandardCharsets;
36 import java.nio.file.Files;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.Iterator;
41 import java.util.List;
42
43 import org.apache.maven.artifact.Artifact;
44 import org.apache.maven.model.Model;
45 import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
46 import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
47 import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest;
48 import org.apache.maven.plugins.assembly.interpolation.AssemblyInterpolator;
49 import org.apache.maven.plugins.assembly.model.Assembly;
50 import org.apache.maven.plugins.assembly.model.Component;
51 import org.apache.maven.plugins.assembly.model.ContainerDescriptorHandlerConfig;
52 import org.apache.maven.plugins.assembly.model.DependencySet;
53 import org.apache.maven.plugins.assembly.model.FileItem;
54 import org.apache.maven.plugins.assembly.model.FileSet;
55 import org.apache.maven.plugins.assembly.model.Repository;
56 import org.apache.maven.plugins.assembly.model.io.xpp3.AssemblyXpp3Writer;
57 import org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Reader;
58 import org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Writer;
59 import org.apache.maven.project.MavenProject;
60 import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
61 import org.codehaus.plexus.interpolation.fixed.InterpolationState;
62 import org.junit.Before;
63 import org.junit.Rule;
64 import org.junit.Test;
65 import org.junit.rules.TemporaryFolder;
66 import org.junit.runner.RunWith;
67 import org.mockito.junit.MockitoJUnitRunner;
68 import org.slf4j.LoggerFactory;
69
70 @RunWith( MockitoJUnitRunner.class )
71 public class DefaultAssemblyReaderTest
72 {
73 @Rule
74 public TemporaryFolder temporaryFolder = new TemporaryFolder();
75
76 private AssemblerConfigurationSource configSource;
77
78 public static StringReader writeToStringReader( Assembly assembly )
79 throws IOException
80 {
81 final StringWriter sw = new StringWriter();
82 final AssemblyXpp3Writer assemblyWriter = new AssemblyXpp3Writer();
83
84 assemblyWriter.write( sw, assembly );
85
86 return new StringReader( sw.toString() );
87 }
88
89 @Before
90 public void setUp()
91 {
92 configSource = mock( AssemblerConfigurationSource.class );
93 }
94
95 @Test
96 public void testIncludeSiteInAssembly_ShouldFailIfSiteDirectoryNonExistent()
97 throws Exception
98 {
99 final File siteDir = Files.createTempFile( "assembly-reader.", ".test" ).toFile();
100 siteDir.delete();
101
102 when( configSource.getSiteDirectory() ).thenReturn( siteDir );
103
104 final Assembly assembly = new Assembly();
105
106 try
107 {
108 new DefaultAssemblyReader().includeSiteInAssembly( assembly, configSource );
109
110 fail( "Should fail when site directory is non-existent." );
111 }
112 catch ( final InvalidAssemblerConfigurationException e )
113 {
114
115 }
116 }
117
118 @Test
119 public void testIncludeSiteInAssembly_ShouldAddSiteDirFileSetWhenDirExists()
120 throws Exception
121 {
122 final File siteDir = temporaryFolder.getRoot();
123
124 when( configSource.getSiteDirectory() ).thenReturn( siteDir );
125
126 final Assembly assembly = new Assembly();
127
128 new DefaultAssemblyReader().includeSiteInAssembly( assembly, configSource );
129
130 final List<FileSet> fileSets = assembly.getFileSets();
131
132 assertNotNull( fileSets );
133 assertEquals( 1, fileSets.size() );
134
135 final FileSet fs = fileSets.get( 0 );
136
137 assertEquals( siteDir.getPath(), fs.getDirectory() );
138 }
139
140 @Test
141 public void testMergeComponentWithAssembly_ShouldAddOneFileSetToExistingListOfTwo()
142 {
143 final Assembly assembly = new Assembly();
144
145 FileSet fs = new FileSet();
146 fs.setDirectory( "/dir" );
147
148 assembly.addFileSet( fs );
149
150 fs = new FileSet();
151 fs.setDirectory( "/other-dir" );
152 assembly.addFileSet( fs );
153
154 fs = new FileSet();
155 fs.setDirectory( "/third-dir" );
156
157 final Component component = new Component();
158
159 component.addFileSet( fs );
160
161 new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
162
163 final List<FileSet> fileSets = assembly.getFileSets();
164
165 assertNotNull( fileSets );
166 assertEquals( 3, fileSets.size() );
167
168 final FileSet rfs1 = fileSets.get( 0 );
169 assertEquals( "/dir", rfs1.getDirectory() );
170
171 final FileSet rfs2 = fileSets.get( 1 );
172 assertEquals( "/other-dir", rfs2.getDirectory() );
173
174 final FileSet rfs3 = fileSets.get( 2 );
175 assertEquals( "/third-dir", rfs3.getDirectory() );
176
177 }
178
179 @Test
180 public void testMergeComponentWithAssembly_ShouldAddOneFileItemToExistingListOfTwo()
181 {
182 final Assembly assembly = new Assembly();
183
184 FileItem fi = new FileItem();
185 fi.setSource( "file" );
186
187 assembly.addFile( fi );
188
189 fi = new FileItem();
190 fi.setSource( "file2" );
191
192 assembly.addFile( fi );
193
194 fi = new FileItem();
195 fi.setSource( "file3" );
196
197 final Component component = new Component();
198
199 component.addFile( fi );
200
201 new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
202
203 final List<FileItem> fileItems = assembly.getFiles();
204
205 assertNotNull( fileItems );
206 assertEquals( 3, fileItems.size() );
207
208 final FileItem rf1 = fileItems.get( 0 );
209 assertEquals( "file", rf1.getSource() );
210
211 final FileItem rf2 = fileItems.get( 1 );
212 assertEquals( "file2", rf2.getSource() );
213
214 final FileItem rf3 = fileItems.get( 2 );
215 assertEquals( "file3", rf3.getSource() );
216
217 }
218
219 @Test
220 public void testMergeComponentWithAssembly_ShouldAddOneDependencySetToExistingListOfTwo()
221 {
222 final Assembly assembly = new Assembly();
223
224 DependencySet ds = new DependencySet();
225 ds.setScope( Artifact.SCOPE_RUNTIME );
226
227 assembly.addDependencySet( ds );
228
229 ds = new DependencySet();
230 ds.setScope( Artifact.SCOPE_COMPILE );
231
232 assembly.addDependencySet( ds );
233
234 final Component component = new Component();
235
236 ds = new DependencySet();
237 ds.setScope( Artifact.SCOPE_SYSTEM );
238
239 component.addDependencySet( ds );
240
241 new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
242
243 final List<DependencySet> depSets = assembly.getDependencySets();
244
245 assertNotNull( depSets );
246 assertEquals( 3, depSets.size() );
247
248 assertEquals( Artifact.SCOPE_RUNTIME, depSets.get( 0 ).getScope() );
249 assertEquals( Artifact.SCOPE_COMPILE, depSets.get( 1 ).getScope() );
250 assertEquals( Artifact.SCOPE_SYSTEM, depSets.get( 2 ).getScope() );
251 }
252
253 @Test
254 public void testMergeComponentWithAssembly_ShouldAddOneRepositoryToExistingListOfTwo()
255 {
256 final Assembly assembly = new Assembly();
257
258 Repository repo = new Repository();
259 repo.setScope( Artifact.SCOPE_RUNTIME );
260
261 assembly.addRepository( repo );
262
263 repo = new Repository();
264 repo.setScope( Artifact.SCOPE_COMPILE );
265
266 assembly.addRepository( repo );
267
268 final Component component = new Component();
269
270 repo = new Repository();
271 repo.setScope( Artifact.SCOPE_SYSTEM );
272
273 component.addRepository( repo );
274
275 new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
276
277 final List<Repository> depSets = assembly.getRepositories();
278
279 assertNotNull( depSets );
280 assertEquals( 3, depSets.size() );
281
282 assertEquals( Artifact.SCOPE_RUNTIME, depSets.get( 0 ).getScope() );
283 assertEquals( Artifact.SCOPE_COMPILE, depSets.get( 1 ).getScope() );
284 assertEquals( Artifact.SCOPE_SYSTEM, depSets.get( 2 ).getScope() );
285 }
286
287 @Test
288 public void testMergeComponentWithAssembly_ShouldAddOneContainerDescriptorHandlerToExistingListOfTwo()
289 {
290 final Assembly assembly = new Assembly();
291
292 ContainerDescriptorHandlerConfig cfg = new ContainerDescriptorHandlerConfig();
293 cfg.setHandlerName( "one" );
294
295 assembly.addContainerDescriptorHandler( cfg );
296
297 cfg = new ContainerDescriptorHandlerConfig();
298 cfg.setHandlerName( "two" );
299
300 assembly.addContainerDescriptorHandler( cfg );
301
302 final Component component = new Component();
303
304 cfg = new ContainerDescriptorHandlerConfig();
305 cfg.setHandlerName( "three" );
306
307 component.addContainerDescriptorHandler( cfg );
308
309 new DefaultAssemblyReader().mergeComponentWithAssembly( component, assembly );
310
311 final List<ContainerDescriptorHandlerConfig> result = assembly.getContainerDescriptorHandlers();
312
313 assertNotNull( result );
314 assertEquals( 3, result.size() );
315
316 final Iterator<ContainerDescriptorHandlerConfig> it = result.iterator();
317 assertEquals( "one", it.next().getHandlerName() );
318 assertEquals( "two", it.next().getHandlerName() );
319 assertEquals( "three", it.next().getHandlerName() );
320 }
321
322 @Test
323 public void testMergeComponentsWithMainAssembly_ShouldAddOneFileSetToAssembly()
324 throws Exception
325 {
326 final Component component = new Component();
327
328 final FileSet fileSet = new FileSet();
329 fileSet.setDirectory( "/dir" );
330
331 component.addFileSet( fileSet );
332
333 final File componentFile = temporaryFolder.newFile();
334
335 try ( Writer writer = new OutputStreamWriter( new FileOutputStream( componentFile ), "UTF-8" ) )
336 {
337 final ComponentXpp3Writer componentWriter = new ComponentXpp3Writer();
338
339 componentWriter.write( writer, component );
340 }
341
342 final String filename = componentFile.getName();
343
344 final Assembly assembly = new Assembly();
345 assembly.addComponentDescriptor( filename );
346
347 final File basedir = componentFile.getParentFile();
348
349 final MavenProject project = new MavenProject();
350
351 when( configSource.getProject() ).thenReturn( project );
352 when( configSource.getBasedir() ).thenReturn( basedir );
353 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
354 InterpolationState is = new InterpolationState();
355 ComponentXpp3Reader.ContentTransformer componentIp =
356 AssemblyInterpolator.componentInterpolator( FixedStringSearchInterpolator.create(), is,
357 LoggerFactory.getLogger( getClass() ) );
358
359 new DefaultAssemblyReader().mergeComponentsWithMainAssembly( assembly, null, configSource, componentIp );
360
361 final List<FileSet> fileSets = assembly.getFileSets();
362
363 assertNotNull( fileSets );
364 assertEquals( 1, fileSets.size() );
365
366 final FileSet fs = fileSets.get( 0 );
367
368 assertEquals( "/dir", fs.getDirectory() );
369 }
370
371 @Test
372 public void testReadAssembly_ShouldReadAssemblyWithoutComponentsInterpolationOrSiteDirInclusion()
373 throws Exception
374 {
375 final Assembly assembly = new Assembly();
376 assembly.setId( "test" );
377
378 final Assembly result = doReadAssembly( assembly );
379
380 assertEquals( assembly.getId(), result.getId() );
381 }
382
383 @Test
384 public void testReadAssembly_ShouldReadAssemblyWithSiteDirInclusionFromAssemblyWithoutComponentsOrInterpolation()
385 throws Exception
386 {
387 final Assembly assembly = new Assembly();
388 assembly.setId( "test" );
389
390 assembly.setIncludeSiteDirectory( true );
391
392 final StringReader sr = writeToStringReader( assembly );
393
394 final File siteDir = temporaryFolder.newFolder( "site" );
395
396 when( configSource.getSiteDirectory() ).thenReturn( siteDir );
397
398 final File basedir = temporaryFolder.getRoot();
399
400 when( configSource.getBasedir() ).thenReturn( basedir );
401
402 final Model model = new Model();
403 model.setGroupId( "group" );
404 model.setArtifactId( "artifact" );
405 model.setVersion( "version" );
406
407 final MavenProject project = new MavenProject( model );
408
409 when( configSource.getProject() ).thenReturn( project );
410
411 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
412
413
414 final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
415
416 assertEquals( assembly.getId(), result.getId() );
417
418 final List<FileSet> fileSets = result.getFileSets();
419
420 assertEquals( 1, fileSets.size() );
421
422 assertEquals( "/site", fileSets.get( 0 ).getOutputDirectory() );
423 }
424
425 @Test
426 public void testReadAssembly_ShouldReadAssemblyWithComponentWithoutSiteDirInclusionOrInterpolation()
427 throws Exception
428 {
429 final File componentsFile = temporaryFolder.newFile();
430
431 final File basedir = componentsFile.getParentFile();
432 final String componentsFilename = componentsFile.getName();
433
434 final Component component = new Component();
435
436 final FileSet fs = new FileSet();
437 fs.setDirectory( "/dir" );
438
439 component.addFileSet( fs );
440
441 try ( Writer fw = new OutputStreamWriter( new FileOutputStream( componentsFile ), "UTF-8" ) )
442 {
443 new ComponentXpp3Writer().write( fw, component );
444 }
445
446 final Assembly assembly = new Assembly();
447 assembly.setId( "test" );
448
449 assembly.addComponentDescriptor( componentsFilename );
450
451 final StringReader sr = writeToStringReader( assembly );
452
453 when( configSource.getBasedir() ).thenReturn( basedir );
454
455 final Model model = new Model();
456 model.setGroupId( "group" );
457 model.setArtifactId( "artifact" );
458 model.setVersion( "version" );
459
460 final MavenProject project = new MavenProject( model );
461 when( configSource.getProject() ).thenReturn( project );
462
463 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
464
465 final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
466
467 assertEquals( assembly.getId(), result.getId() );
468
469 final List<FileSet> fileSets = result.getFileSets();
470
471 assertEquals( 1, fileSets.size() );
472
473 assertEquals( "/dir", fileSets.get( 0 ).getDirectory() );
474 }
475
476 @Test
477 public void testReadAssembly_ShouldReadAssemblyWithComponentInterpolationWithoutSiteDirInclusionOrAssemblyInterpolation()
478 throws Exception
479 {
480 final File componentsFile = temporaryFolder.newFile();
481
482 final File basedir = componentsFile.getParentFile();
483 final String componentsFilename = componentsFile.getName();
484
485 final Component component = new Component();
486
487 final FileSet fs = new FileSet();
488 fs.setDirectory( "${groupId}-dir" );
489
490 component.addFileSet( fs );
491
492 try( Writer fw = new OutputStreamWriter( new FileOutputStream( componentsFile ), "UTF-8" ) )
493 {
494 new ComponentXpp3Writer().write( fw, component );
495 }
496
497 final Assembly assembly = new Assembly();
498 assembly.setId( "test" );
499
500 assembly.addComponentDescriptor( componentsFilename );
501
502 final StringReader sr = writeToStringReader( assembly );
503
504 when( configSource.getBasedir() ).thenReturn( basedir );
505
506 final Model model = new Model();
507 model.setGroupId( "group" );
508 model.setArtifactId( "artifact" );
509 model.setVersion( "version" );
510
511 final MavenProject project = new MavenProject( model );
512
513 when( configSource.getProject() ).thenReturn( project );
514
515 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
516
517 final Assembly result = new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
518
519 assertEquals( assembly.getId(), result.getId() );
520
521 final List<FileSet> fileSets = result.getFileSets();
522
523 assertEquals( 1, fileSets.size() );
524
525 assertEquals( "group-dir", fileSets.get( 0 ).getDirectory() );
526 }
527
528 @Test
529 public void testReadAssembly_ShouldReadAssemblyWithInterpolationWithoutComponentsOrSiteDirInclusion()
530 throws Exception
531 {
532 final Assembly assembly = new Assembly();
533 assembly.setId( "${groupId}-assembly" );
534
535 final Assembly result = doReadAssembly( assembly );
536
537 assertEquals( "group-assembly", result.getId() );
538 }
539
540 private Assembly doReadAssembly( Assembly assembly )
541 throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException
542 {
543 final StringReader sr = writeToStringReader( assembly );
544
545 final File basedir = temporaryFolder.getRoot();
546
547 when( configSource.getBasedir() ).thenReturn( basedir );
548
549 final Model model = new Model();
550 model.setGroupId( "group" );
551 model.setArtifactId( "artifact" );
552 model.setVersion( "version" );
553
554 final MavenProject project = new MavenProject( model );
555
556 when( configSource.getProject() ).thenReturn( project );
557
558 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
559
560 return new DefaultAssemblyReader().readAssembly( sr, "testLocation", null, configSource );
561 }
562
563 @Test
564 public void testGetAssemblyFromDescriptorFile_ShouldReadAssembly()
565 throws Exception
566 {
567 final Assembly assembly = new Assembly();
568 assembly.setId( "test" );
569
570 final FileSet fs = new FileSet();
571 fs.setDirectory( "/dir" );
572
573 assembly.addFileSet( fs );
574
575 final File assemblyFile = temporaryFolder.newFile();
576
577 final File basedir = assemblyFile.getParentFile();
578
579 when( configSource.getBasedir() ).thenReturn( basedir );
580
581 when( configSource.getProject() ).thenReturn( new MavenProject( new Model() ) );
582
583 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
584
585 try ( Writer writer = new OutputStreamWriter( new FileOutputStream( assemblyFile ), "UTF-8" ) )
586 {
587 new AssemblyXpp3Writer().write( writer, assembly );
588 }
589
590 final Assembly result = new DefaultAssemblyReader().getAssemblyFromDescriptorFile( assemblyFile, configSource );
591
592 assertEquals( assembly.getId(), result.getId() );
593 }
594
595 @Test
596 public void testGetAssemblyForDescriptorReference_ShouldReadBinaryAssemblyRef()
597 throws Exception
598 {
599 final File basedir = temporaryFolder.getRoot();
600
601 when( configSource.getBasedir() ).thenReturn( basedir );
602
603 when( configSource.getProject() ).thenReturn( new MavenProject( new Model() ) );
604
605 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
606
607 final Assembly result = new DefaultAssemblyReader().getAssemblyForDescriptorReference( "bin", configSource );
608
609 assertEquals( "bin", result.getId() );
610 }
611
612 @Test
613 public void testReadAssemblies_ShouldGetAssemblyDescriptorFromSingleFile()
614 throws Exception
615 {
616 final Assembly assembly = new Assembly();
617 assembly.setId( "test" );
618
619 final FileSet fs = new FileSet();
620 fs.setDirectory( "/dir" );
621
622 assembly.addFileSet( fs );
623
624 final File basedir = temporaryFolder.getRoot();
625
626 final List<String> files = writeAssembliesToFile( Collections.singletonList( assembly ), basedir );
627
628 final String assemblyFile = files.get( 0 );
629
630 final List<Assembly> assemblies = performReadAssemblies( basedir, new String[] { assemblyFile }, null, null );
631
632 assertNotNull( assemblies );
633 assertEquals( 1, assemblies.size() );
634
635 final Assembly result = assemblies.get( 0 );
636
637 assertEquals( assembly.getId(), result.getId() );
638 }
639
640 @Test
641 public void testReadAssemblies_ShouldFailWhenSingleDescriptorFileMissing()
642 throws Exception
643 {
644 final File basedir = temporaryFolder.getRoot();
645
646 try
647 {
648 performReadAssemblies( basedir, null, null, null, false );
649
650 fail( "Should fail when descriptor file is missing and ignoreDescriptors == false" );
651 }
652 catch ( final AssemblyReadException e )
653 {
654
655 }
656 }
657
658 @Test
659 public void testReadAssemblies_ShouldIgnoreMissingSingleDescriptorFileWhenIgnoreIsConfigured()
660 throws Exception
661 {
662 final File basedir = temporaryFolder.getRoot();
663
664 try
665 {
666 performReadAssemblies( basedir, null, null, null, true );
667 }
668 catch ( final AssemblyReadException e )
669 {
670 fail( "Setting ignoreMissingDescriptor == true (true flag in performReadAssemblies, above) should NOT "
671 + "produce an exception." );
672 }
673 }
674
675 @Test
676 public void testReadAssemblies_ShouldGetAssemblyDescriptorFromFileArray()
677 throws Exception
678 {
679 final Assembly assembly1 = new Assembly();
680 assembly1.setId( "test" );
681
682 final Assembly assembly2 = new Assembly();
683 assembly2.setId( "test2" );
684
685 final List<Assembly> assemblies = new ArrayList<>();
686 assemblies.add( assembly1 );
687 assemblies.add( assembly2 );
688
689 final File basedir = temporaryFolder.getRoot();
690
691 final List<String> files = writeAssembliesToFile( assemblies, basedir );
692
693 final List<Assembly> results =
694 performReadAssemblies( basedir, files.toArray( new String[0] ), null, null );
695
696 assertNotNull( results );
697 assertEquals( 2, results.size() );
698
699 final Assembly result1 = assemblies.get( 0 );
700
701 assertEquals( assembly1.getId(), result1.getId() );
702
703 final Assembly result2 = assemblies.get( 1 );
704
705 assertEquals( assembly2.getId(), result2.getId() );
706 }
707
708 @Test
709 public void testReadAssemblies_ShouldGetAssemblyDescriptorFromMultipleRefs()
710 throws Exception
711 {
712 final File basedir = temporaryFolder.getRoot();
713
714 final List<Assembly> assemblies =
715 performReadAssemblies( basedir, null, new String[]{ "bin", "src" }, null );
716
717 assertNotNull( assemblies );
718 assertEquals( 2, assemblies.size() );
719
720 final Assembly result = assemblies.get( 0 );
721
722 assertEquals( "bin", result.getId() );
723
724 final Assembly result2 = assemblies.get( 1 );
725
726 assertEquals( "src", result2.getId() );
727 }
728
729 @Test
730 public void testReadAssemblies_ShouldGetAssemblyDescriptorFromDirectory()
731 throws Exception
732 {
733 final Assembly assembly1 = new Assembly();
734 assembly1.setId( "test" );
735
736 final Assembly assembly2 = new Assembly();
737 assembly2.setId( "test2" );
738
739 final List<Assembly> assemblies = new ArrayList<>();
740 assemblies.add( assembly1 );
741 assemblies.add( assembly2 );
742
743 final File basedir = temporaryFolder.getRoot();
744
745 writeAssembliesToFile( assemblies, basedir );
746
747 final List<Assembly> results = performReadAssemblies( basedir, null, null, basedir );
748
749 assertNotNull( results );
750 assertEquals( 2, results.size() );
751
752 final Assembly result1 = assemblies.get( 0 );
753
754 assertEquals( assembly1.getId(), result1.getId() );
755
756 final Assembly result2 = assemblies.get( 1 );
757
758 assertEquals( assembly2.getId(), result2.getId() );
759 }
760
761 @Test
762 public void testReadAssemblies_ShouldGetTwoAssemblyDescriptorsFromDirectoryWithThreeFiles()
763 throws Exception
764 {
765 final Assembly assembly1 = new Assembly();
766 assembly1.setId( "test" );
767
768 final Assembly assembly2 = new Assembly();
769 assembly2.setId( "test2" );
770
771 final List<Assembly> assemblies = new ArrayList<>();
772 assemblies.add( assembly1 );
773 assemblies.add( assembly2 );
774
775 final File basedir = temporaryFolder.getRoot();
776
777 writeAssembliesToFile( assemblies, basedir );
778
779 Files.write( basedir.toPath().resolve( "readme.txt" ),
780 Arrays.asList( "This is just a readme file, not a descriptor." ),
781 StandardCharsets.UTF_8 );
782
783 final List<Assembly> results = performReadAssemblies( basedir, null, null, basedir );
784
785 assertNotNull( results );
786 assertEquals( 2, results.size() );
787
788 final Assembly result1 = assemblies.get( 0 );
789
790 assertEquals( assembly1.getId(), result1.getId() );
791
792 final Assembly result2 = assemblies.get( 1 );
793
794 assertEquals( assembly2.getId(), result2.getId() );
795 }
796
797 private List<String> writeAssembliesToFile( final List<Assembly> assemblies, final File dir )
798 throws IOException
799 {
800 final List<String> files = new ArrayList<>();
801
802 for ( final Assembly assembly : assemblies )
803 {
804 final File assemblyFile = new File( dir, assembly.getId() + ".xml" );
805
806 try ( Writer writer = new OutputStreamWriter( new FileOutputStream( assemblyFile ), "UTF-8" ) )
807 {
808 new AssemblyXpp3Writer().write( writer, assembly );
809 }
810
811 files.add( assemblyFile.getAbsolutePath() );
812 }
813
814 return files;
815 }
816
817 private List<Assembly> performReadAssemblies( final File basedir, final String[] descriptors,
818 final String[] descriptorRefs, final File descriptorDir )
819 throws AssemblyReadException, InvalidAssemblerConfigurationException
820 {
821 return performReadAssemblies( basedir, descriptors, descriptorRefs, descriptorDir, false );
822 }
823
824 private List<Assembly> performReadAssemblies( final File basedir, final String[] descriptors,
825 final String[] descriptorRefs, final File descriptorDir,
826 final boolean ignoreMissing )
827 throws AssemblyReadException, InvalidAssemblerConfigurationException
828 {
829 when( configSource.getDescriptorReferences() ).thenReturn( descriptorRefs );
830
831 when( configSource.getDescriptors() ).thenReturn( descriptors );
832
833 when( configSource.getDescriptorSourceDirectory() ).thenReturn( descriptorDir );
834
835 when( configSource.getBasedir() ).thenReturn( basedir );
836
837 if ( descriptors == null && descriptorRefs == null && descriptorDir == null )
838 {
839 when( configSource.isIgnoreMissingDescriptor() ).thenReturn( ignoreMissing );
840 }
841
842 if ( !ignoreMissing )
843 {
844 when( configSource.getProject() ).thenReturn( new MavenProject( new Model() ) );
845
846 DefaultAssemblyArchiverTest.setupInterpolators( configSource );
847 }
848
849 return new DefaultAssemblyReader().readAssemblies( configSource );
850 }
851
852 }