View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.io;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.OutputStreamWriter;
24  import java.io.StringReader;
25  import java.io.StringWriter;
26  import java.io.Writer;
27  import java.nio.charset.StandardCharsets;
28  import java.nio.file.Files;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.Iterator;
32  import java.util.List;
33  
34  import org.apache.maven.artifact.Artifact;
35  import org.apache.maven.model.Model;
36  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
37  import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
38  import org.apache.maven.plugins.assembly.archive.DefaultAssemblyArchiverTest;
39  import org.apache.maven.plugins.assembly.interpolation.AssemblyInterpolator;
40  import org.apache.maven.plugins.assembly.model.Assembly;
41  import org.apache.maven.plugins.assembly.model.Component;
42  import org.apache.maven.plugins.assembly.model.ContainerDescriptorHandlerConfig;
43  import org.apache.maven.plugins.assembly.model.DependencySet;
44  import org.apache.maven.plugins.assembly.model.FileItem;
45  import org.apache.maven.plugins.assembly.model.FileSet;
46  import org.apache.maven.plugins.assembly.model.io.xpp3.AssemblyXpp3Writer;
47  import org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Reader;
48  import org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Writer;
49  import org.apache.maven.project.MavenProject;
50  import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
51  import org.codehaus.plexus.interpolation.fixed.InterpolationState;
52  import org.junit.jupiter.api.Test;
53  import org.junit.jupiter.api.extension.ExtendWith;
54  import org.junit.jupiter.api.io.TempDir;
55  import org.mockito.Mock;
56  import org.mockito.junit.jupiter.MockitoExtension;
57  import org.mockito.junit.jupiter.MockitoSettings;
58  import org.mockito.quality.Strictness;
59  import org.slf4j.LoggerFactory;
60  
61  import static org.junit.jupiter.api.Assertions.assertEquals;
62  import static org.junit.jupiter.api.Assertions.assertNotNull;
63  import static org.junit.jupiter.api.Assertions.assertSame;
64  import static org.junit.jupiter.api.Assertions.fail;
65  import static org.mockito.Mockito.when;
66  
67  @MockitoSettings(strictness = Strictness.WARN)
68  @ExtendWith(MockitoExtension.class)
69  public class DefaultAssemblyReaderTest {
70  
71      @TempDir
72      private File temporaryFolder;
73  
74      @Mock
75      private AssemblerConfigurationSource configSource;
76  
77      public static StringReader writeToStringReader(Assembly assembly) throws IOException {
78          final StringWriter sw = new StringWriter();
79          final AssemblyXpp3Writer assemblyWriter = new AssemblyXpp3Writer();
80  
81          assemblyWriter.write(sw, assembly);
82  
83          return new StringReader(sw.toString());
84      }
85  
86      @Test
87      public void testIncludeSiteInAssemblyShouldFailIfSiteDirectoryNonExistent() throws Exception {
88          final File siteDir = Files.createTempFile("assembly-reader.", ".test").toFile();
89          siteDir.delete();
90  
91          when(configSource.getSiteDirectory()).thenReturn(siteDir);
92  
93          final Assembly assembly = new Assembly();
94  
95          try {
96              new DefaultAssemblyReader().includeSiteInAssembly(assembly, configSource);
97  
98              fail("Should fail when site directory is non-existent.");
99          } catch (final InvalidAssemblerConfigurationException e) {
100             // this should happen.
101         }
102     }
103 
104     @Test
105     public void testIncludeSiteInAssemblyShouldAddSiteDirFileSetWhenDirExists() throws Exception {
106         final File siteDir = temporaryFolder;
107 
108         when(configSource.getSiteDirectory()).thenReturn(siteDir);
109 
110         final Assembly assembly = new Assembly();
111 
112         new DefaultAssemblyReader().includeSiteInAssembly(assembly, configSource);
113 
114         final List<FileSet> fileSets = assembly.getFileSets();
115 
116         assertNotNull(fileSets);
117         assertEquals(1, fileSets.size());
118 
119         final FileSet fs = fileSets.get(0);
120 
121         assertEquals(siteDir.getPath(), fs.getDirectory());
122     }
123 
124     @Test
125     public void testMergeComponentWithAssemblyShouldAddOneFileSetToExistingListOfTwo() {
126         final Assembly assembly = new Assembly();
127 
128         FileSet fs = new FileSet();
129         fs.setDirectory("/dir");
130 
131         assembly.addFileSet(fs);
132 
133         fs = new FileSet();
134         fs.setDirectory("/other-dir");
135         assembly.addFileSet(fs);
136 
137         fs = new FileSet();
138         fs.setDirectory("/third-dir");
139 
140         final Component component = new Component();
141 
142         component.addFileSet(fs);
143 
144         new DefaultAssemblyReader().mergeComponentWithAssembly(component, assembly);
145 
146         final List<FileSet> fileSets = assembly.getFileSets();
147 
148         assertNotNull(fileSets);
149         assertEquals(3, fileSets.size());
150 
151         final FileSet rfs1 = fileSets.get(0);
152         assertEquals("/dir", rfs1.getDirectory());
153 
154         final FileSet rfs2 = fileSets.get(1);
155         assertEquals("/other-dir", rfs2.getDirectory());
156 
157         final FileSet rfs3 = fileSets.get(2);
158         assertEquals("/third-dir", rfs3.getDirectory());
159     }
160 
161     @Test
162     public void testMergeComponentWithAssemblyShouldAddOneFileItemToExistingListOfTwo() {
163         final Assembly assembly = new Assembly();
164 
165         FileItem fi = new FileItem();
166         fi.setSource("file");
167 
168         assembly.addFile(fi);
169 
170         fi = new FileItem();
171         fi.setSource("file2");
172 
173         assembly.addFile(fi);
174 
175         fi = new FileItem();
176         fi.setSource("file3");
177 
178         final Component component = new Component();
179 
180         component.addFile(fi);
181 
182         new DefaultAssemblyReader().mergeComponentWithAssembly(component, assembly);
183 
184         final List<FileItem> fileItems = assembly.getFiles();
185 
186         assertNotNull(fileItems);
187         assertEquals(3, fileItems.size());
188 
189         final FileItem rf1 = fileItems.get(0);
190         assertEquals("file", rf1.getSource());
191 
192         final FileItem rf2 = fileItems.get(1);
193         assertEquals("file2", rf2.getSource());
194 
195         final FileItem rf3 = fileItems.get(2);
196         assertEquals("file3", rf3.getSource());
197     }
198 
199     @Test
200     public void testMergeComponentWithAssemblyShouldAddOneDependencySetToExistingListOfTwo() {
201         final Assembly assembly = new Assembly();
202 
203         DependencySet ds = new DependencySet();
204         ds.setScope(Artifact.SCOPE_RUNTIME);
205 
206         assembly.addDependencySet(ds);
207 
208         ds = new DependencySet();
209         ds.setScope(Artifact.SCOPE_COMPILE);
210 
211         assembly.addDependencySet(ds);
212 
213         final Component component = new Component();
214 
215         ds = new DependencySet();
216         ds.setScope(Artifact.SCOPE_SYSTEM);
217 
218         component.addDependencySet(ds);
219 
220         new DefaultAssemblyReader().mergeComponentWithAssembly(component, assembly);
221 
222         final List<DependencySet> depSets = assembly.getDependencySets();
223 
224         assertNotNull(depSets);
225         assertEquals(3, depSets.size());
226 
227         assertEquals(Artifact.SCOPE_RUNTIME, depSets.get(0).getScope());
228         assertEquals(Artifact.SCOPE_COMPILE, depSets.get(1).getScope());
229         assertEquals(Artifact.SCOPE_SYSTEM, depSets.get(2).getScope());
230     }
231 
232     @Test
233     public void testMergeComponentWithAssemblyShouldAddOneContainerDescriptorHandlerToExistingListOfTwo() {
234         final Assembly assembly = new Assembly();
235 
236         ContainerDescriptorHandlerConfig cfg = new ContainerDescriptorHandlerConfig();
237         cfg.setHandlerName("one");
238 
239         assembly.addContainerDescriptorHandler(cfg);
240 
241         cfg = new ContainerDescriptorHandlerConfig();
242         cfg.setHandlerName("two");
243 
244         assembly.addContainerDescriptorHandler(cfg);
245 
246         final Component component = new Component();
247 
248         cfg = new ContainerDescriptorHandlerConfig();
249         cfg.setHandlerName("three");
250 
251         component.addContainerDescriptorHandler(cfg);
252 
253         new DefaultAssemblyReader().mergeComponentWithAssembly(component, assembly);
254 
255         final List<ContainerDescriptorHandlerConfig> result = assembly.getContainerDescriptorHandlers();
256 
257         assertNotNull(result);
258         assertEquals(3, result.size());
259 
260         final Iterator<ContainerDescriptorHandlerConfig> it = result.iterator();
261         assertEquals("one", it.next().getHandlerName());
262         assertEquals("two", it.next().getHandlerName());
263         assertEquals("three", it.next().getHandlerName());
264     }
265 
266     @Test
267     public void testMergeComponentsWithMainAssemblyShouldAddOneFileSetToAssembly() throws Exception {
268         final Component component = new Component();
269 
270         final FileSet fileSet = new FileSet();
271         fileSet.setDirectory("/dir");
272 
273         component.addFileSet(fileSet);
274 
275         final File componentFile = File.createTempFile("junit", null, temporaryFolder);
276 
277         try (Writer writer =
278                 new OutputStreamWriter(Files.newOutputStream(componentFile.toPath()), StandardCharsets.UTF_8)) {
279             final ComponentXpp3Writer componentWriter = new ComponentXpp3Writer();
280 
281             componentWriter.write(writer, component);
282         }
283 
284         final String filename = componentFile.getName();
285 
286         final Assembly assembly = new Assembly();
287         assembly.addComponentDescriptor(filename);
288 
289         final File basedir = componentFile.getParentFile();
290 
291         final MavenProject project = new MavenProject();
292 
293         when(configSource.getProject()).thenReturn(project);
294         when(configSource.getBasedir()).thenReturn(basedir);
295         DefaultAssemblyArchiverTest.setupInterpolators(configSource);
296         InterpolationState is = new InterpolationState();
297         ComponentXpp3Reader.ContentTransformer componentIp = AssemblyInterpolator.componentInterpolator(
298                 FixedStringSearchInterpolator.create(), is, LoggerFactory.getLogger(getClass()));
299 
300         new DefaultAssemblyReader().mergeComponentsWithMainAssembly(assembly, null, configSource, componentIp);
301 
302         final List<FileSet> fileSets = assembly.getFileSets();
303 
304         assertNotNull(fileSets);
305         assertEquals(1, fileSets.size());
306 
307         final FileSet fs = fileSets.get(0);
308 
309         assertEquals("/dir", fs.getDirectory());
310     }
311 
312     @Test
313     public void testReadAssemblyShouldReadAssemblyWithoutComponentsInterpolationOrSiteDirInclusion() throws Exception {
314         final Assembly assembly = new Assembly();
315         assembly.setId("test");
316 
317         final Assembly result = doReadAssembly(assembly);
318 
319         assertEquals(assembly.getId(), result.getId());
320     }
321 
322     @Test
323     public void testReadAssemblyShouldReadAssemblyWithSiteDirInclusionFromAssemblyWithoutComponentsOrInterpolation()
324             throws Exception {
325         final Assembly assembly = new Assembly();
326         assembly.setId("test");
327 
328         assembly.setIncludeSiteDirectory(true);
329 
330         final StringReader sr = writeToStringReader(assembly);
331 
332         final File siteDir = newFolder(temporaryFolder, "site");
333 
334         when(configSource.getSiteDirectory()).thenReturn(siteDir);
335 
336         final File basedir = temporaryFolder;
337 
338         when(configSource.getBasedir()).thenReturn(basedir);
339 
340         final Model model = new Model();
341         model.setGroupId("group");
342         model.setArtifactId("artifact");
343         model.setVersion("version");
344 
345         final MavenProject project = new MavenProject(model);
346 
347         when(configSource.getProject()).thenReturn(project);
348 
349         DefaultAssemblyArchiverTest.setupInterpolators(configSource);
350 
351         final Assembly result = new DefaultAssemblyReader().readAssembly(sr, "testLocation", null, configSource);
352 
353         assertEquals(assembly.getId(), result.getId());
354 
355         final List<FileSet> fileSets = result.getFileSets();
356 
357         assertEquals(1, fileSets.size());
358 
359         assertEquals("/site", fileSets.get(0).getOutputDirectory());
360     }
361 
362     @Test
363     public void testReadAssemblyShouldReadAssemblyWithComponentWithoutSiteDirInclusionOrInterpolation()
364             throws Exception {
365         final File componentsFile = File.createTempFile("junit", null, temporaryFolder);
366 
367         final File basedir = componentsFile.getParentFile();
368         final String componentsFilename = componentsFile.getName();
369 
370         final Component component = new Component();
371 
372         final FileSet fs = new FileSet();
373         fs.setDirectory("/dir");
374 
375         component.addFileSet(fs);
376 
377         try (Writer fw =
378                 new OutputStreamWriter(Files.newOutputStream(componentsFile.toPath()), StandardCharsets.UTF_8)) {
379             new ComponentXpp3Writer().write(fw, component);
380         }
381 
382         final Assembly assembly = new Assembly();
383         assembly.setId("test");
384 
385         assembly.addComponentDescriptor(componentsFilename);
386 
387         final StringReader sr = writeToStringReader(assembly);
388 
389         when(configSource.getBasedir()).thenReturn(basedir);
390 
391         final Model model = new Model();
392         model.setGroupId("group");
393         model.setArtifactId("artifact");
394         model.setVersion("version");
395 
396         final MavenProject project = new MavenProject(model);
397         when(configSource.getProject()).thenReturn(project);
398 
399         DefaultAssemblyArchiverTest.setupInterpolators(configSource);
400 
401         final Assembly result = new DefaultAssemblyReader().readAssembly(sr, "testLocation", null, configSource);
402 
403         assertEquals(assembly.getId(), result.getId());
404 
405         final List<FileSet> fileSets = result.getFileSets();
406 
407         assertEquals(1, fileSets.size());
408 
409         assertEquals("/dir", fileSets.get(0).getDirectory());
410     }
411 
412     @Test
413     public void
414             testReadAssemblyShouldReadAssemblyWithComponentInterpolationWithoutSiteDirInclusionOrAssemblyInterpolation()
415                     throws Exception {
416         final File componentsFile = File.createTempFile("junit", null, temporaryFolder);
417 
418         final File basedir = componentsFile.getParentFile();
419         final String componentsFilename = componentsFile.getName();
420 
421         final Component component = new Component();
422 
423         final FileSet fs = new FileSet();
424         fs.setDirectory("${groupId}-dir");
425 
426         component.addFileSet(fs);
427 
428         try (Writer fw =
429                 new OutputStreamWriter(Files.newOutputStream(componentsFile.toPath()), StandardCharsets.UTF_8)) {
430             new ComponentXpp3Writer().write(fw, component);
431         }
432 
433         final Assembly assembly = new Assembly();
434         assembly.setId("test");
435 
436         assembly.addComponentDescriptor(componentsFilename);
437 
438         final StringReader sr = writeToStringReader(assembly);
439 
440         when(configSource.getBasedir()).thenReturn(basedir);
441 
442         final Model model = new Model();
443         model.setGroupId("group");
444         model.setArtifactId("artifact");
445         model.setVersion("version");
446 
447         final MavenProject project = new MavenProject(model);
448 
449         when(configSource.getProject()).thenReturn(project);
450 
451         DefaultAssemblyArchiverTest.setupInterpolators(configSource);
452 
453         final Assembly result = new DefaultAssemblyReader().readAssembly(sr, "testLocation", null, configSource);
454 
455         assertEquals(assembly.getId(), result.getId());
456 
457         final List<FileSet> fileSets = result.getFileSets();
458 
459         assertEquals(1, fileSets.size());
460 
461         assertEquals("group-dir", fileSets.get(0).getDirectory());
462     }
463 
464     @Test
465     public void testReadAssemblyShouldReadAssemblyWithInterpolationWithoutComponentsOrSiteDirInclusion()
466             throws Exception {
467         final Assembly assembly = new Assembly();
468         assembly.setId("${groupId}-assembly");
469 
470         final Assembly result = doReadAssembly(assembly);
471 
472         assertEquals("group-assembly", result.getId());
473     }
474 
475     private Assembly doReadAssembly(Assembly assembly)
476             throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException {
477         final StringReader sr = writeToStringReader(assembly);
478 
479         final File basedir = temporaryFolder;
480 
481         when(configSource.getBasedir()).thenReturn(basedir);
482 
483         final Model model = new Model();
484         model.setGroupId("group");
485         model.setArtifactId("artifact");
486         model.setVersion("version");
487 
488         final MavenProject project = new MavenProject(model);
489 
490         when(configSource.getProject()).thenReturn(project);
491 
492         DefaultAssemblyArchiverTest.setupInterpolators(configSource);
493 
494         return new DefaultAssemblyReader().readAssembly(sr, "testLocation", null, configSource);
495     }
496 
497     @Test
498     public void testGetAssemblyFromDescriptorFileShouldReadAssembly() throws Exception {
499         final Assembly assembly = new Assembly();
500         assembly.setId("test");
501 
502         final FileSet fs = new FileSet();
503         fs.setDirectory("/dir");
504 
505         assembly.addFileSet(fs);
506 
507         final File assemblyFile = File.createTempFile("junit", null, temporaryFolder);
508 
509         final File basedir = assemblyFile.getParentFile();
510 
511         when(configSource.getBasedir()).thenReturn(basedir);
512 
513         when(configSource.getProject()).thenReturn(new MavenProject(new Model()));
514 
515         DefaultAssemblyArchiverTest.setupInterpolators(configSource);
516 
517         try (Writer writer =
518                 new OutputStreamWriter(Files.newOutputStream(assemblyFile.toPath()), StandardCharsets.UTF_8)) {
519             new AssemblyXpp3Writer().write(writer, assembly);
520         }
521 
522         final Assembly result = new DefaultAssemblyReader().getAssemblyFromDescriptorFile(assemblyFile, configSource);
523 
524         assertEquals(assembly.getId(), result.getId());
525     }
526 
527     @Test
528     public void testGetAssemblyForDescriptorReferenceShouldReadBinaryAssemblyRef() throws Exception {
529         final File basedir = temporaryFolder;
530 
531         when(configSource.getBasedir()).thenReturn(basedir);
532 
533         when(configSource.getProject()).thenReturn(new MavenProject(new Model()));
534 
535         DefaultAssemblyArchiverTest.setupInterpolators(configSource);
536 
537         final Assembly result = new DefaultAssemblyReader().getAssemblyForDescriptorReference("bin", configSource);
538 
539         assertEquals("bin", result.getId());
540     }
541 
542     @Test
543     public void testReadAssembliesShouldGetAssemblyDescriptorFromSingleFile() throws Exception {
544         final Assembly assembly = new Assembly();
545         assembly.setId("test");
546 
547         final FileSet fs = new FileSet();
548         fs.setDirectory("/dir");
549 
550         assembly.addFileSet(fs);
551 
552         final File basedir = temporaryFolder;
553 
554         final List<String> files = writeAssembliesToFile(Collections.singletonList(assembly), basedir);
555 
556         final String assemblyFile = files.get(0);
557 
558         final List<Assembly> assemblies = performReadAssemblies(basedir, new String[] {assemblyFile}, null, null);
559 
560         assertNotNull(assemblies);
561         assertEquals(1, assemblies.size());
562 
563         final Assembly result = assemblies.get(0);
564 
565         assertEquals(assembly.getId(), result.getId());
566     }
567 
568     @Test
569     public void testReadAssembliesShouldFailWhenSingleDescriptorFileMissing() throws Exception {
570         final File basedir = temporaryFolder;
571 
572         try {
573             performReadAssemblies(basedir, null, null, null, false);
574 
575             fail("Should fail when descriptor file is missing and ignoreDescriptors == false");
576         } catch (final AssemblyReadException e) {
577             // expected.
578         }
579     }
580 
581     @Test
582     public void testReadAssembliesShouldIgnoreMissingSingleDescriptorFileWhenIgnoreIsConfigured() throws Exception {
583         final File basedir = temporaryFolder;
584 
585         try {
586             performReadAssemblies(basedir, null, null, null, true);
587         } catch (final AssemblyReadException e) {
588             fail("Setting ignoreMissingDescriptor == true (true flag in performReadAssemblies, above) should NOT "
589                     + "produce an exception.");
590         }
591     }
592 
593     @Test
594     public void testReadAssembliesShouldGetAssemblyDescriptorFromFileArray() throws Exception {
595         final Assembly assembly1 = new Assembly();
596         assembly1.setId("test");
597 
598         final Assembly assembly2 = new Assembly();
599         assembly2.setId("test2");
600 
601         final List<Assembly> assemblies = new ArrayList<>();
602         assemblies.add(assembly1);
603         assemblies.add(assembly2);
604 
605         final File basedir = temporaryFolder;
606 
607         final List<String> files = writeAssembliesToFile(assemblies, basedir);
608 
609         final List<Assembly> results = performReadAssemblies(basedir, files.toArray(new String[0]), null, null);
610 
611         assertNotNull(results);
612         assertEquals(2, results.size());
613 
614         final Assembly result1 = assemblies.get(0);
615 
616         assertEquals(assembly1.getId(), result1.getId());
617 
618         final Assembly result2 = assemblies.get(1);
619 
620         assertEquals(assembly2.getId(), result2.getId());
621     }
622 
623     @Test
624     public void testReadAssembliesShouldGetAssemblyDescriptorFromMultipleRefs() throws Exception {
625         final File basedir = temporaryFolder;
626 
627         final List<Assembly> assemblies = performReadAssemblies(basedir, null, new String[] {"bin", "src"}, null);
628 
629         assertNotNull(assemblies);
630         assertEquals(2, assemblies.size());
631 
632         final Assembly result = assemblies.get(0);
633 
634         assertEquals("bin", result.getId());
635 
636         final Assembly result2 = assemblies.get(1);
637 
638         assertEquals("src", result2.getId());
639     }
640 
641     @Test
642     public void testReadAssembliesShouldGetAssemblyDescriptorFromDirectory() throws Exception {
643         final Assembly assembly1 = new Assembly();
644         assembly1.setId("test");
645 
646         final Assembly assembly2 = new Assembly();
647         assembly2.setId("test2");
648 
649         final List<Assembly> assemblies = new ArrayList<>();
650         assemblies.add(assembly1);
651         assemblies.add(assembly2);
652 
653         final File basedir = temporaryFolder;
654 
655         writeAssembliesToFile(assemblies, basedir);
656 
657         final List<Assembly> results = performReadAssemblies(basedir, null, null, basedir);
658 
659         assertNotNull(results);
660         assertEquals(2, results.size());
661 
662         final Assembly result1 = assemblies.get(0);
663 
664         assertEquals(assembly1.getId(), result1.getId());
665 
666         final Assembly result2 = assemblies.get(1);
667 
668         assertEquals(assembly2.getId(), result2.getId());
669     }
670 
671     @Test
672     public void testReadAssembliesShouldGetTwoAssemblyDescriptorsFromDirectoryWithThreeFiles() throws Exception {
673         final Assembly assembly1 = new Assembly();
674         assembly1.setId("test");
675 
676         final Assembly assembly2 = new Assembly();
677         assembly2.setId("test2");
678 
679         final List<Assembly> assemblies = new ArrayList<>();
680         assemblies.add(assembly1);
681         assemblies.add(assembly2);
682 
683         final File basedir = temporaryFolder;
684 
685         writeAssembliesToFile(assemblies, basedir);
686 
687         Files.write(
688                 basedir.toPath().resolve("readme.txt"),
689                 Collections.singletonList("This is just a readme file, not a descriptor."),
690                 StandardCharsets.UTF_8);
691 
692         final List<Assembly> results = performReadAssemblies(basedir, null, null, basedir);
693 
694         assertNotNull(results);
695         assertEquals(2, results.size());
696 
697         final Assembly result1 = assemblies.get(0);
698 
699         assertEquals(assembly1.getId(), result1.getId());
700 
701         final Assembly result2 = assemblies.get(1);
702 
703         assertEquals(assembly2.getId(), result2.getId());
704     }
705 
706     @Test
707     public void inlineAssemblyShouldBeReturned() throws Exception {
708         // given
709         Assembly assemby = new Assembly();
710         assemby.setId("test-id");
711         when(configSource.getInlineDescriptors()).thenReturn(Collections.singletonList(assemby));
712 
713         // when
714         List<Assembly> assemblies = new DefaultAssemblyReader().readAssemblies(configSource);
715 
716         // then
717         assertEquals(1, assemblies.size());
718         assertSame(assemby, assemblies.get(0));
719     }
720 
721     private List<String> writeAssembliesToFile(final List<Assembly> assemblies, final File dir) throws IOException {
722         final List<String> files = new ArrayList<>();
723 
724         for (final Assembly assembly : assemblies) {
725             final File assemblyFile = new File(dir, assembly.getId() + ".xml");
726 
727             try (Writer writer =
728                     new OutputStreamWriter(Files.newOutputStream(assemblyFile.toPath()), StandardCharsets.UTF_8)) {
729                 new AssemblyXpp3Writer().write(writer, assembly);
730             }
731 
732             files.add(assemblyFile.getAbsolutePath());
733         }
734 
735         return files;
736     }
737 
738     private List<Assembly> performReadAssemblies(
739             final File basedir, final String[] descriptors, final String[] descriptorRefs, final File descriptorDir)
740             throws AssemblyReadException, InvalidAssemblerConfigurationException {
741         return performReadAssemblies(basedir, descriptors, descriptorRefs, descriptorDir, false);
742     }
743 
744     private List<Assembly> performReadAssemblies(
745             final File basedir,
746             final String[] descriptors,
747             final String[] descriptorRefs,
748             final File descriptorDir,
749             final boolean ignoreMissing)
750             throws AssemblyReadException, InvalidAssemblerConfigurationException {
751         when(configSource.getDescriptorReferences()).thenReturn(descriptorRefs);
752 
753         when(configSource.getDescriptors()).thenReturn(descriptors);
754 
755         when(configSource.getDescriptorSourceDirectory()).thenReturn(descriptorDir);
756 
757         when(configSource.getBasedir()).thenReturn(basedir); // .atLeastOnce();
758 
759         if (descriptors == null && descriptorRefs == null && descriptorDir == null) {
760             when(configSource.isIgnoreMissingDescriptor()).thenReturn(ignoreMissing); // .atLeastOnce();
761         }
762 
763         if (!ignoreMissing) {
764             when(configSource.getProject()).thenReturn(new MavenProject(new Model())); // .atLeastOnce();
765 
766             DefaultAssemblyArchiverTest.setupInterpolators(configSource);
767         }
768 
769         return new DefaultAssemblyReader().readAssemblies(configSource);
770     }
771 
772     private static File newFolder(File root, String... subDirs) throws IOException {
773         String subFolder = String.join("/", subDirs);
774         File result = new File(root, subFolder);
775         if (!result.mkdirs()) {
776             throw new IOException("Couldn't create folders " + root);
777         }
778         return result;
779     }
780 }