View Javadoc
1   package org.apache.maven.plugins.assembly.format;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.hamcrest.MatcherAssert.assertThat;
23  import static org.hamcrest.Matchers.not;
24  import static org.hamcrest.Matchers.sameInstance;
25  import static org.junit.Assert.assertEquals;
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.when;
28  import static org.mockito.Mockito.verify;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.File;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.util.Arrays;
35  import java.util.Collections;
36  import java.util.HashSet;
37  import java.util.Properties;
38  import java.util.Set;
39  
40  import org.apache.commons.io.IOUtils;
41  import org.apache.maven.model.Model;
42  import org.apache.maven.plugins.assembly.testutils.PojoConfigSource;
43  import org.apache.maven.project.MavenProject;
44  import org.apache.maven.shared.filtering.DefaultMavenReaderFilter;
45  import org.apache.maven.shared.filtering.MavenReaderFilter;
46  import org.apache.maven.shared.filtering.MavenReaderFilterRequest;
47  import org.codehaus.plexus.archiver.resources.PlexusIoVirtualFileResource;
48  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
49  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
50  import org.junit.Test;
51  import org.mockito.ArgumentCaptor;
52  
53  
54  public class ReaderFormatterTest
55  {
56      @Test
57      public void lineDosFeed()
58          throws IOException, AssemblyFormattingException
59      {
60          final PojoConfigSource cfg = getPojoConfigSource();
61          InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, true, Collections.<String>emptySet(), "dos" );
62          InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a\ntest." ) );
63          assertEquals( "This is a\r\ntest.", readResultStream( fud ) );
64      }
65  
66      @Test
67      public void lineDosFeed_withoutFiltering()
68          throws IOException, AssemblyFormattingException
69      {
70          final PojoConfigSource cfg = getPojoConfigSource();
71          InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, false, Collections.<String>emptySet(), "dos" );
72          InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a\ntest." ) );
73          assertEquals( "This is a\r\ntest.", readResultStream( fud ) );
74      }
75  
76      @Test
77      public void lineUnixFeedWithInterpolation()
78          throws IOException, AssemblyFormattingException
79      {
80          final PojoConfigSource cfg = getPojoConfigSource();
81          InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, true, Collections.<String>emptySet(), "unix" );
82          InputStream fud = fileSetTransformers.transform( dummyResource(), payload(
83              "This is a test for project: ${artifactId} @artifactId@." ) );
84          assertEquals( "This is a test for project: anArtifact anArtifact.", readResultStream( fud ) );
85      }
86  
87      @Test
88      public void nonFilteredFileExtensions() throws Exception
89      {
90          final PojoConfigSource cfg = getPojoConfigSource();
91          Set<String> nonFilteredFileExtensions = new HashSet<>( Arrays.asList( "jpg", "tar.gz" ) );
92          InputStreamTransformer transformer = ReaderFormatter.getFileSetTransformers( cfg, true, nonFilteredFileExtensions, "unix" );
93  
94          final InputStream is = new ByteArrayInputStream( new byte[0] );
95          PlexusIoResource resource = mock( PlexusIoResource.class );
96  
97          when( resource.getName() ).thenReturn( "file.jpg", "file.tar.gz", "file.txt", "file.nojpg", "file.gz", "file" );
98          assertThat( transformer.transform( resource, is ), sameInstance( is ) );
99          assertThat( transformer.transform( resource, is ), sameInstance( is ) );
100         assertThat( transformer.transform( resource, is ), not( sameInstance( is ) ) );
101         assertThat( transformer.transform( resource, is ), not( sameInstance( is ) ) );
102         assertThat( transformer.transform( resource, is ), not( sameInstance( is ) ) );
103         assertThat( transformer.transform( resource, is ), not( sameInstance( is ) ) );
104     }
105 
106     @Test
107     public void additionalProperties() throws Exception
108     {
109         final MavenReaderFilter mavenReaderFilter = mock( MavenReaderFilter.class );
110 
111         final PojoConfigSource cfg = getPojoConfigSource();
112         cfg.setMavenReaderFilter( mavenReaderFilter );
113         Properties additionalProperties = new Properties();
114         cfg.setAdditionalProperties( additionalProperties );
115         
116         InputStreamTransformer transformer =  ReaderFormatter.getFileSetTransformers( cfg, true, Collections.<String>emptySet(), "unix" );
117         
118         final InputStream inputStream = new ByteArrayInputStream( new byte[0] );
119         PlexusIoResource resource = mock( PlexusIoResource.class );
120         when( resource.getName() ).thenReturn( "file.txt" );
121 
122         transformer.transform( resource, inputStream );
123 
124         ArgumentCaptor<MavenReaderFilterRequest> filteringRequest = 
125                         ArgumentCaptor.forClass(MavenReaderFilterRequest.class);
126         verify( mavenReaderFilter ).filter( filteringRequest.capture() );
127         assertThat( filteringRequest.getValue().getAdditionalProperties(), sameInstance( additionalProperties ) );
128     }
129 
130     private MavenProject createBasicMavenProject()
131     {
132         final Model model = new Model();
133         model.setArtifactId( "anArtifact" );
134         model.setGroupId( "group" );
135         model.setVersion( "version" );
136 
137         return new MavenProject( model );
138     }
139 
140 
141     private String readResultStream( InputStream fud )
142         throws IOException
143     {
144         byte[] actual = new byte[100];
145         int read = IOUtils.read( fud, actual );
146         return new String( actual, 0, read );
147     }
148 
149     private ByteArrayInputStream payload( String payload )
150     {
151         return new ByteArrayInputStream( payload.getBytes() );
152     }
153 
154     private PojoConfigSource getPojoConfigSource()
155     {
156         final PojoConfigSource cfg = new PojoConfigSource();
157         cfg.setEncoding( "UTF-8" );
158         DefaultMavenReaderFilter mavenReaderFilter = new DefaultMavenReaderFilter();
159         cfg.setMavenReaderFilter( mavenReaderFilter );
160         cfg.setEscapeString( null );
161         cfg.setMavenProject( createBasicMavenProject() );
162         return cfg;
163     }
164 
165     private PlexusIoVirtualFileResource dummyResource()
166     {
167         return new PlexusIoVirtualFileResource( new File( "fud" ), "fud" )
168         {
169         };
170     }
171 }