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