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.format;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.Reader;
26  import java.util.Arrays;
27  import java.util.Collections;
28  import java.util.HashSet;
29  import java.util.Properties;
30  import java.util.Set;
31  
32  import org.apache.commons.io.IOUtils;
33  import org.apache.maven.model.Model;
34  import org.apache.maven.plugins.assembly.testutils.PojoConfigSource;
35  import org.apache.maven.project.MavenProject;
36  import org.apache.maven.shared.filtering.DefaultMavenReaderFilter;
37  import org.apache.maven.shared.filtering.MavenReaderFilter;
38  import org.apache.maven.shared.filtering.MavenReaderFilterRequest;
39  import org.codehaus.plexus.archiver.resources.PlexusIoVirtualFileResource;
40  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
41  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
42  import org.junit.Test;
43  import org.mockito.ArgumentCaptor;
44  
45  import static org.hamcrest.MatcherAssert.assertThat;
46  import static org.hamcrest.Matchers.not;
47  import static org.hamcrest.Matchers.sameInstance;
48  import static org.junit.Assert.assertEquals;
49  import static org.mockito.ArgumentMatchers.any;
50  import static org.mockito.Mockito.mock;
51  import static org.mockito.Mockito.verify;
52  import static org.mockito.Mockito.when;
53  
54  public class ReaderFormatterTest {
55      @Test
56      public void lineDosFeed() throws IOException, AssemblyFormattingException {
57          final PojoConfigSource cfg = getPojoConfigSource();
58          InputStreamTransformer fileSetTransformers =
59                  ReaderFormatter.getFileSetTransformers(cfg, true, Collections.emptySet(), "dos");
60          InputStream fud = fileSetTransformers.transform(dummyResource(), payload("This is a\ntest."));
61          assertEquals("This is a\r\ntest.", readResultStream(fud));
62      }
63  
64      @Test
65      public void lineDosFeed_withoutFiltering() throws IOException, AssemblyFormattingException {
66          final PojoConfigSource cfg = getPojoConfigSource();
67          InputStreamTransformer fileSetTransformers =
68                  ReaderFormatter.getFileSetTransformers(cfg, false, Collections.emptySet(), "dos");
69          InputStream fud = fileSetTransformers.transform(dummyResource(), payload("This is a\ntest."));
70          assertEquals("This is a\r\ntest.", readResultStream(fud));
71      }
72  
73      @Test
74      public void lineUnixFeedWithInterpolation() throws IOException, AssemblyFormattingException {
75          final PojoConfigSource cfg = getPojoConfigSource();
76          InputStreamTransformer fileSetTransformers =
77                  ReaderFormatter.getFileSetTransformers(cfg, true, Collections.emptySet(), "unix");
78          InputStream fud = fileSetTransformers.transform(
79                  dummyResource(), payload("This is a test for project: ${artifactId} @artifactId@."));
80          assertEquals("This is a test for project: anArtifact anArtifact.", readResultStream(fud));
81      }
82  
83      @Test
84      public void nonFilteredFileExtensions() throws Exception {
85          final PojoConfigSource cfg = getPojoConfigSource();
86          Set<String> nonFilteredFileExtensions = new HashSet<>(Arrays.asList("jpg", "tar.gz"));
87          InputStreamTransformer transformer =
88                  ReaderFormatter.getFileSetTransformers(cfg, true, nonFilteredFileExtensions, "unix");
89  
90          final InputStream is = new ByteArrayInputStream(new byte[0]);
91          PlexusIoResource resource = mock(PlexusIoResource.class);
92  
93          when(resource.getName()).thenReturn("file.jpg", "file.tar.gz", "file.txt", "file.nojpg", "file.gz", "file");
94          assertThat(transformer.transform(resource, is), sameInstance(is));
95          assertThat(transformer.transform(resource, is), sameInstance(is));
96          assertThat(transformer.transform(resource, is), not(sameInstance(is)));
97          assertThat(transformer.transform(resource, is), not(sameInstance(is)));
98          assertThat(transformer.transform(resource, is), not(sameInstance(is)));
99          assertThat(transformer.transform(resource, is), not(sameInstance(is)));
100     }
101 
102     @Test
103     public void additionalProperties() throws Exception {
104         final MavenReaderFilter mavenReaderFilter = mock(MavenReaderFilter.class);
105         when(mavenReaderFilter.filter(any())).thenReturn(mock(Reader.class));
106 
107         final PojoConfigSource cfg = getPojoConfigSource();
108         cfg.setMavenReaderFilter(mavenReaderFilter);
109         Properties additionalProperties = new Properties();
110         cfg.setAdditionalProperties(additionalProperties);
111 
112         InputStreamTransformer transformer =
113                 ReaderFormatter.getFileSetTransformers(cfg, true, Collections.emptySet(), "unix");
114 
115         final InputStream inputStream = new ByteArrayInputStream(new byte[0]);
116         PlexusIoResource resource = mock(PlexusIoResource.class);
117         when(resource.getName()).thenReturn("file.txt");
118 
119         transformer.transform(resource, inputStream);
120 
121         ArgumentCaptor<MavenReaderFilterRequest> filteringRequest =
122                 ArgumentCaptor.forClass(MavenReaderFilterRequest.class);
123         verify(mavenReaderFilter).filter(filteringRequest.capture());
124         assertThat(filteringRequest.getValue().getAdditionalProperties(), sameInstance(additionalProperties));
125     }
126 
127     private MavenProject createBasicMavenProject() {
128         final Model model = new Model();
129         model.setArtifactId("anArtifact");
130         model.setGroupId("group");
131         model.setVersion("version");
132 
133         return new MavenProject(model);
134     }
135 
136     private String readResultStream(InputStream fud) throws IOException {
137         byte[] actual = new byte[100];
138         int read = IOUtils.read(fud, actual);
139         return new String(actual, 0, read);
140     }
141 
142     private ByteArrayInputStream payload(String payload) {
143         return new ByteArrayInputStream(payload.getBytes());
144     }
145 
146     private PojoConfigSource getPojoConfigSource() {
147         final PojoConfigSource cfg = new PojoConfigSource();
148         cfg.setEncoding("UTF-8");
149         DefaultMavenReaderFilter mavenReaderFilter = new DefaultMavenReaderFilter();
150         cfg.setMavenReaderFilter(mavenReaderFilter);
151         cfg.setEscapeString(null);
152         cfg.setMavenProject(createBasicMavenProject());
153         return cfg;
154     }
155 
156     private PlexusIoVirtualFileResource dummyResource() {
157         return new PlexusIoVirtualFileResource(new File("fud"), "fud") {};
158     }
159 }