Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

[MASSEMBLY-617] add ability to give a fileSuffix to a AbstractPlexusI… #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public InputStream transform( @Nonnull PlexusIoResource resource, @Nonnull Input
private FileMapper[] fileMappers;

private InputStreamTransformer streamTransformer = identityTransformer;

private String fileSuffix;

protected AbstractPlexusIoResourceCollection()
{
Expand Down Expand Up @@ -246,6 +248,22 @@ public void setFileMappers( FileMapper[] fileMappers )
{
this.fileMappers = fileMappers;
}

/**
* Returns the suffix apply to files, may be empty
*/
public String getFileSuffix()
{
return fileSuffix;
}

/**
* Add some suffix to file when it's copying
*/
public void setFileSuffix(String fileSuffix)
{
this.fileSuffix = fileSuffix;
}

public Iterator<PlexusIoResource> iterator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ private void addResources( List<PlexusIoResource> result, String[] resources,
for ( String name : resources )
{
String sourceDir = name.replace( '\\', '/' );

name = addFileNameSuffix( name );

File f = new File( dir, sourceDir );

Expand Down Expand Up @@ -185,6 +187,7 @@ private void addResourcesJava7( List<PlexusIoResource> result, String[] resource
for ( String name : resources )
{
String sourceDir = name.replace( '\\', '/' );
name = addFileNameSuffix( name );
File f = new File( dir, sourceDir );

PlexusIoResourceAttributes attrs = new Java7FileAttributes( f, cache1, cache2 );
Expand Down Expand Up @@ -279,4 +282,32 @@ public Iterator<PlexusIoResource> getResources()
return result.iterator();
}
}

/**
* Add a suffix to fileName (eg : test.xml => testSuffix.xml)
* Warning : the extension of the file is calculated after the first "dot" caracter.
* Example :
* <ul>
* <li>test.tar.gz => testSuffix.tar.gz</li>
* <li>test.any.of.extension => testSuffix.any.of.extension</li>
* </ul>
*/
private String addFileNameSuffix( String name )
{
String nameWithSuffix = name;
if ( StringUtils.isNotBlank( getFileSuffix()) )
{
if ( name.contains( "." ) )
{
String beforeExtension = name.substring( 0, name.indexOf('.') );
String afterExtension = name.substring( name.indexOf('.') + 1) ;
nameWithSuffix = beforeExtension + getFileSuffix() + "." + afterExtension;
}
else
{
nameWithSuffix += getFileSuffix();
}
}
return nameWithSuffix;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.codehaus.plexus.components.io.resources;

import java.io.File;

import org.codehaus.plexus.PlexusTestCase;

public class PlexusIoFileResourceCollectionTest
extends PlexusTestCase
{

public void testWithFileSuffix()
throws Exception
{
PlexusIoFileResourceCollection resourceCollection = new PlexusIoFileResourceCollection();
resourceCollection.setBaseDir(new File("src/test/resources") );
resourceCollection.setFileSuffix("TEST");
resourceCollection.setIncludes(new String[] {"Test-p1.txt"});
final PlexusIoFileResource entry = (PlexusIoFileResource) resourceCollection.getResources().next();
assertEquals( "Test-p1TEST.txt", entry.getName() );
}

public void testWithNoFileSuffix()
throws Exception
{
PlexusIoFileResourceCollection resourceCollection = new PlexusIoFileResourceCollection();
resourceCollection.setBaseDir(new File("src/test/resources") );
resourceCollection.setIncludes(new String[] {"Test-p1.txt"});
final PlexusIoFileResource entry = (PlexusIoFileResource) resourceCollection.getResources().next();
assertEquals( "Test-p1.txt", entry.getName() );
}

public void testWithFileWith2Dot()
throws Exception
{
PlexusIoFileResourceCollection resourceCollection = new PlexusIoFileResourceCollection();
resourceCollection.setBaseDir(new File("src/test/resources") );
resourceCollection.setIncludes(new String[] {"test.tar.gz"});
resourceCollection.setFileSuffix("_2");
final PlexusIoFileResource entry = (PlexusIoFileResource) resourceCollection.getResources().next();
assertEquals( "test_2.tar.gz", entry.getName() );
}

public void testWithFileWith3Dot()
throws Exception
{
PlexusIoFileResourceCollection resourceCollection = new PlexusIoFileResourceCollection();
resourceCollection.setBaseDir(new File("src/test/resources") );
resourceCollection.setIncludes(new String[] {"test.any.of.extension"});
resourceCollection.setFileSuffix("Suffix");
final PlexusIoFileResource entry = (PlexusIoFileResource) resourceCollection.getResources().next();
assertEquals( "testSuffix.any.of.extension", entry.getName() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public void testNamelessRootFolder()
Iterator iterator = resourceCollection.getResources();
PlexusIoURLResource entry = (PlexusIoURLResource) iterator.next();
assertEquals( "/dummy.txt", entry.getName() );
final URL url = entry.getURL();
BufferedReader d = new BufferedReader( new InputStreamReader( entry.getContents() ) );
assertEquals( "dummy content", d.readLine() );
}
Expand Down Expand Up @@ -78,7 +77,6 @@ public void testFilesThatAreNotThere()
{
final PlexusIoResource next = entries.next();
assertTrue( next.getName() + "was not present", seen.remove( next.getName() ) );
final URL url = next.getURL();
final InputStream contents = next.getContents();
contents.close();
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/resources/test.any.of.extension
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes:
totalt 0
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 .
drwxr-xr-x 4 1003 1002 80 2010-04-23 11:24 ..
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 org

/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes/org:
totalt 0
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 .
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 ..
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 chromattic

/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes/org/chromattic:
totalt 0
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 .
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 ..
drwxr-xr-x 4 1003 1002 80 2010-04-23 11:24 spi
build 23-Apr-2010 11:24:41
/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes/org/chromattic/spi:
totalt 0
drwxr-xr-x 4 1003 1002 80 2010-04-23 11:24 .
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 ..
drwxr-xr-x 2 1003 1002 100 2010-04-23 11:24 instrument
drwxr-xr-x 2 1003 1002 60 2010-04-23 11:24 jcr
build 23-Apr-2010 11:24:41
/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes/org/chromattic/spi/instrument:
totalt 12
drwxr-xr-x 2 1003 1002 100 2010-04-23 11:24 .
drwxr-xr-x 4 1003 1002 80 2010-04-23 11:24 ..
-rw-r--r-- 1 1003 1002 432 2010-04-23 11:24 Instrumentor.class
-rw-r--r-- 1 1003 1002 288 2010-04-23 11:24 MethodHandler.class
-rw-r--r-- 1 1003 1002 349 2010-04-23 11:24 ProxyFactory.class
build 23-Apr-2010 11:24:41
/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes/org/chromattic/spi/jcr:
totalt 4
drwxr-xr-x 2 1003 1002 60 2010-04-23 11:24 .
drwxr-xr-x 4 1003 1002 80 2010-04-23 11:24 ..
-rw-r--r-- 1 1003 1002 508 2010-04-23 11:24 SessionLifeCycle.class
38 changes: 38 additions & 0 deletions src/test/resources/test.tar.gz
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes:
totalt 0
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 .
drwxr-xr-x 4 1003 1002 80 2010-04-23 11:24 ..
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 org

/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes/org:
totalt 0
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 .
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 ..
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 chromattic

/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes/org/chromattic:
totalt 0
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 .
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 ..
drwxr-xr-x 4 1003 1002 80 2010-04-23 11:24 spi
build 23-Apr-2010 11:24:41
/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes/org/chromattic/spi:
totalt 0
drwxr-xr-x 4 1003 1002 80 2010-04-23 11:24 .
drwxr-xr-x 3 1003 1002 60 2010-04-23 11:24 ..
drwxr-xr-x 2 1003 1002 100 2010-04-23 11:24 instrument
drwxr-xr-x 2 1003 1002 60 2010-04-23 11:24 jcr
build 23-Apr-2010 11:24:41
/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes/org/chromattic/spi/instrument:
totalt 12
drwxr-xr-x 2 1003 1002 100 2010-04-23 11:24 .
drwxr-xr-x 4 1003 1002 80 2010-04-23 11:24 ..
-rw-r--r-- 1 1003 1002 432 2010-04-23 11:24 Instrumentor.class
-rw-r--r-- 1 1003 1002 288 2010-04-23 11:24 MethodHandler.class
-rw-r--r-- 1 1003 1002 349 2010-04-23 11:24 ProxyFactory.class
build 23-Apr-2010 11:24:41
/home/bamboo/agent1/xml-data/build-dir/PARALLEL-CH1/checkout/spi/target/classes/org/chromattic/spi/jcr:
totalt 4
drwxr-xr-x 2 1003 1002 60 2010-04-23 11:24 .
drwxr-xr-x 4 1003 1002 80 2010-04-23 11:24 ..
-rw-r--r-- 1 1003 1002 508 2010-04-23 11:24 SessionLifeCycle.class