Skip to content

Commit

Permalink
[MASSEMBLYE-849] Add nonFilteredFileExtensions to avoid filtering of …
Browse files Browse the repository at this point in the history
…binary files
  • Loading branch information
rfscholte committed Apr 26, 2020
1 parent 2c44462 commit 52a84fe
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ else if ( outputDirectory.length() < 1 )
{
final InputStreamTransformer fileSetTransformers =
ReaderFormatter.getFileSetTransformers( configSource, fileItem.isFiltered(),
Collections.<String>emptySet(),
fileItem.getLineEnding() );

final PlexusIoResource restoUse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -142,11 +143,13 @@ void addDependencySet( final DependencySet dependencySet, final Archiver archive

logger.debug( "Adding " + dependencyArtifacts.size() + " dependency artifacts." );

InputStreamTransformer fileSetTransformers =
isUnpackWithOptions( dependencySet )
? ReaderFormatter.getFileSetTransformers( configSource, dependencySet.getUnpackOptions().isFiltered(),
dependencySet.getUnpackOptions().getLineEnding() )
: null;
UnpackOptions unpackOptions = dependencySet.getUnpackOptions();
InputStreamTransformer fileSetTransformers = isUnpackWithOptions( dependencySet )
? ReaderFormatter.getFileSetTransformers( configSource,
unpackOptions.isFiltered(),
new HashSet<>( unpackOptions.getNonFilteredFileExtensions() ),
unpackOptions.getLineEnding() )
: null;

for ( final Artifact depArtifact : dependencyArtifacts )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
* under the License.
*/

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
Expand All @@ -33,11 +39,6 @@
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
*
*/
Expand Down Expand Up @@ -137,7 +138,10 @@ void addFileSet( final FileSet fileSet, final Archiver archiver, final Assembler
if ( fileSetDir.exists() )
{
InputStreamTransformer fileSetTransformers =
ReaderFormatter.getFileSetTransformers( configSource, fileSet.isFiltered(), fileSet.getLineEnding() );
ReaderFormatter.getFileSetTransformers( configSource,
fileSet.isFiltered(),
new HashSet<>( fileSet.getNonFilteredFileExtensions() ),
fileSet.getLineEnding() );
if ( fileSetTransformers == null )
{
logger.debug( "NOT reformatting any files in " + fileSetDir );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.Reader;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
*
Expand Down Expand Up @@ -114,7 +115,9 @@ private static void checkifFileTypeIsAppropriateForLineEndingTransformation( Ple

@Nullable
public static InputStreamTransformer getFileSetTransformers( final AssemblerConfigurationSource configSource,
final boolean isFiltered, String fileSetLineEnding )
final boolean isFiltered,
final Set<String> nonFilteredFileExtensions,
String fileSetLineEnding )
throws AssemblyFormattingException
{
final LineEndings lineEndingToUse = LineEndingsUtils.getLineEnding( fileSetLineEnding );
Expand All @@ -131,6 +134,15 @@ public InputStream transform( @Nonnull PlexusIoResource plexusIoResource,
@Nonnull InputStream inputStream )
throws IOException
{
final String fileName = plexusIoResource.getName();
for ( String extension : nonFilteredFileExtensions )
{
if ( fileName.endsWith( '.' + extension ) )
{
return inputStream;
}
}

InputStream result = inputStream;
if ( isFiltered )
{
Expand Down
22 changes: 22 additions & 0 deletions src/main/mdo/assembly-component.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@
<defaultValue>false</defaultValue>
<type>boolean</type>
</field>
<field>
<name>nonFilteredFileExtensions</name>
<version>2.1.0+</version>
<description>
Additional file extensions to not apply filtering (Since 3.2.0)
</description>
<association>
<type>String</type>
<multiplicity>*</multiplicity>
</association>
</field>
</fields>
</class>
<class>
Expand Down Expand Up @@ -1239,6 +1250,17 @@
<defaultValue>false</defaultValue>
<type>boolean</type>
</field>
<field>
<name>nonFilteredFileExtensions</name>
<version>2.1.0+</version>
<description>
Additional file extensions to not apply filtering (Since 3.2.0)
</description>
<association>
<type>String</type>
<multiplicity>*</multiplicity>
</association>
</field>
<field>
<name>lineEnding</name>
<version>1.1.2+</version>
Expand Down
22 changes: 22 additions & 0 deletions src/main/mdo/assembly.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,17 @@
<defaultValue>false</defaultValue>
<type>boolean</type>
</field>
<field>
<name>nonFilteredFileExtensions</name>
<version>2.1.0+</version>
<description>
Additional file extensions to not apply filtering (Since 3.2.0)
</description>
<association>
<type>String</type>
<multiplicity>*</multiplicity>
</association>
</field>
</fields>
</class>
<class>
Expand Down Expand Up @@ -1351,6 +1362,17 @@
<defaultValue>false</defaultValue>
<type>boolean</type>
</field>
<field>
<name>nonFilteredFileExtensions</name>
<version>2.1.0+</version>
<description>
Additional file extensions to not apply filtering (Since 3.2.0)
</description>
<association>
<type>String</type>
<multiplicity>*</multiplicity>
</association>
</field>
<field>
<name>lineEnding</name>
<version>1.1.2+</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,33 @@
* under the License.
*/

import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.maven.model.Model;
import org.apache.maven.plugins.assembly.testutils.PojoConfigSource;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.filtering.DefaultMavenReaderFilter;
import org.codehaus.plexus.archiver.resources.PlexusIoVirtualFileResource;
import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import static org.junit.Assert.assertEquals;


public class ReaderFormatterTest
{
Expand All @@ -44,7 +54,7 @@ public void lineDosFeed()
throws IOException, AssemblyFormattingException
{
final PojoConfigSource cfg = getPojoConfigSource();
InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, true, "dos" );
InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, true, Collections.<String>emptySet(), "dos" );
InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a\ntest." ) );
assertEquals( "This is a\r\ntest.", readResultStream( fud ) );
}
Expand All @@ -54,7 +64,7 @@ public void lineDosFeed_withoutFiltering()
throws IOException, AssemblyFormattingException
{
final PojoConfigSource cfg = getPojoConfigSource();
InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, false, "dos" );
InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, false, Collections.<String>emptySet(), "dos" );
InputStream fud = fileSetTransformers.transform( dummyResource(), payload( "This is a\ntest." ) );
assertEquals( "This is a\r\ntest.", readResultStream( fud ) );
}
Expand All @@ -64,12 +74,30 @@ public void lineUnixFeedWithInterpolation()
throws IOException, AssemblyFormattingException
{
final PojoConfigSource cfg = getPojoConfigSource();
InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, true, "unix" );
InputStreamTransformer fileSetTransformers = ReaderFormatter.getFileSetTransformers( cfg, true, Collections.<String>emptySet(), "unix" );
InputStream fud = fileSetTransformers.transform( dummyResource(), payload(
"This is a test for project: ${artifactId} @artifactId@." ) );
assertEquals( "This is a test for project: anArtifact anArtifact.", readResultStream( fud ) );
}

@Test
public void nonFilteredFileExtensions() throws Exception
{
final PojoConfigSource cfg = getPojoConfigSource();
Set<String> nonFilteredFileExtensions = new HashSet<>( Arrays.asList( "jpg", "tar.gz" ) );
InputStreamTransformer transformer = ReaderFormatter.getFileSetTransformers( cfg, true, nonFilteredFileExtensions, "unix" );

final InputStream is = new ByteArrayInputStream( new byte[0] );
PlexusIoResource resource = mock( PlexusIoResource.class );

when( resource.getName() ).thenReturn( "file.jpg", "file.tar.gz", "file.txt", "file.nojpg", "file.gz", "file" );
assertThat( transformer.transform( resource, is ), sameInstance( is ) );
assertThat( transformer.transform( resource, is ), sameInstance( is ) );
assertThat( transformer.transform( resource, is ), not( sameInstance( is ) ) );
assertThat( transformer.transform( resource, is ), not( sameInstance( is ) ) );
assertThat( transformer.transform( resource, is ), not( sameInstance( is ) ) );
assertThat( transformer.transform( resource, is ), not( sameInstance( is ) ) );
}

private MavenProject createBasicMavenProject()
{
Expand Down Expand Up @@ -104,13 +132,6 @@ private PojoConfigSource getPojoConfigSource()
cfg.setMavenReaderFilter( mavenReaderFilter );
cfg.setEscapeString( null );
cfg.setMavenProject( createBasicMavenProject() );

/* expect( configSource.getFilters()).andReturn( filters );
expect( configSource.isIncludeProjectBuildFilters()).andReturn( includeProjectBuildFilters );
expect( configSource.getDelimiters()).andReturn( delimiters );
*/
return cfg;
}

Expand Down

0 comments on commit 52a84fe

Please sign in to comment.