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.Matchers.not;
23  import static org.hamcrest.Matchers.sameInstance;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertThat;
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.codehaus.plexus.logging.console.ConsoleLogger;
51  import org.junit.Test;
52  import org.mockito.ArgumentCaptor;
53  
54  
55  public class ReaderFormatterTest
56  {
57      @Test
58      public void lineDosFeed()
59          throws IOException, AssemblyFormattingException
60      {
61          final PojoConfigSource cfg = getPojoConfigSource();
62          InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, true, Collections.<String>emptySet(), "dos" );
63          InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a\ntest." ) );
64          assertEquals( "This is a\r\ntest.", readResultStream( fud ) );
65      }
66  
67      @Test
68      public void lineDosFeed_withoutFiltering()
69          throws IOException, AssemblyFormattingException
70      {
71          final PojoConfigSource cfg = getPojoConfigSource();
72          InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, false, Collections.<String>emptySet(), "dos" );
73          InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a\ntest." ) );
74          assertEquals( "This is a\r\ntest.", readResultStream( fud ) );
75      }
76  
77      @Test
78      public void lineUnixFeedWithInterpolation()
79          throws IOException, AssemblyFormattingException
80      {
81          final PojoConfigSource cfg = getPojoConfigSource();
82          InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, true, Collections.<String>emptySet(), "unix" );
83          InputStream fud = fileSetTransformers.transform( dummyResource(), payload(
84              "This is a test for project: ${artifactId} @artifactId@." ) );
85          assertEquals( "This is a test for project: anArtifact anArtifact.", readResultStream( fud ) );
86      }
87  
88      @Test
89      public void nonFilteredFileExtensions() throws Exception
90      {
91          final PojoConfigSource cfg = getPojoConfigSource();
92          Set<String> nonFilteredFileExtensions = new HashSet<>( Arrays.asList( "jpg", "tar.gz" ) );
93          InputStreamTransformer transformer = ReaderFormatter.getFileSetTransformers( cfg, true, nonFilteredFileExtensions, "unix" );
94  
95          final InputStream is = new ByteArrayInputStream( new byte[0] );
96          PlexusIoResource resource = mock( PlexusIoResource.class );
97  
98          when( resource.getName() ).thenReturn( "file.jpg", "file.tar.gz", "file.txt", "file.nojpg", "file.gz", "file" );
99          assertThat( transformer.transform( resource, is ), sameInstance( is ) );
100         assertThat( transformer.transform( resource, is ), 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         assertThat( transformer.transform( resource, is ), not( sameInstance( is ) ) );
105     }
106 
107     @Test
108     public void additionalProperties() throws Exception
109     {
110         final MavenReaderFilter mavenReaderFilter = mock( MavenReaderFilter.class );
111 
112         final PojoConfigSource cfg = getPojoConfigSource();
113         cfg.setMavenReaderFilter( mavenReaderFilter );
114         Properties additionalProperties = new Properties();
115         cfg.setAdditionalProperties( additionalProperties );
116         
117         InputStreamTransformer transformer =  ReaderFormatter.getFileSetTransformers( cfg, true, Collections.<String>emptySet(), "unix" );
118         
119         final InputStream inputStream = new ByteArrayInputStream( new byte[0] );
120         PlexusIoResource resource = mock( PlexusIoResource.class );
121         when( resource.getName() ).thenReturn( "file.txt" );
122 
123         transformer.transform( resource, inputStream );
124 
125         ArgumentCaptor<MavenReaderFilterRequest> filteringRequest = 
126                         ArgumentCaptor.forClass(MavenReaderFilterRequest.class);
127         verify( mavenReaderFilter ).filter( filteringRequest.capture() );
128         assertThat( filteringRequest.getValue().getAdditionalProperties(), sameInstance( additionalProperties ) );
129     }
130 
131     private MavenProject createBasicMavenProject()
132     {
133         final Model model = new Model();
134         model.setArtifactId( "anArtifact" );
135         model.setGroupId( "group" );
136         model.setVersion( "version" );
137 
138         return new MavenProject( model );
139     }
140 
141 
142     private String readResultStream( InputStream fud )
143         throws IOException
144     {
145         byte[] actual = new byte[100];
146         int read = IOUtils.read( fud, actual );
147         return new String( actual, 0, read );
148     }
149 
150     private ByteArrayInputStream payload( String payload )
151     {
152         return new ByteArrayInputStream( payload.getBytes() );
153     }
154 
155     private PojoConfigSource getPojoConfigSource()
156     {
157         final PojoConfigSourcembly/testutils/PojoConfigSource.html#PojoConfigSource">PojoConfigSource cfg = new PojoConfigSource();
158         cfg.setEncoding( "UTF-8" );
159         DefaultMavenReaderFilter mavenReaderFilter = new DefaultMavenReaderFilter();
160         mavenReaderFilter.enableLogging( new ConsoleLogger( 2, "fud" ) );
161         cfg.setMavenReaderFilter( mavenReaderFilter );
162         cfg.setEscapeString( null );
163         cfg.setMavenProject( createBasicMavenProject() );
164         return cfg;
165     }
166 
167     private PlexusIoVirtualFileResource dummyResource()
168     {
169         return new PlexusIoVirtualFileResource( new File( "fud" ), "fud" )
170         {
171         };
172     }
173 }