From 419b0c92376e8ba837ff828ed68f5e60cf98d5ba Mon Sep 17 00:00:00 2001 From: Thomas Collignon Date: Mon, 25 Jun 2018 09:54:02 +0200 Subject: [PATCH] [MASSEMBLY-617] add ability to give a fileSuffix to a AbstractPlexusIoResourceCollection --- .../AbstractPlexusIoResourceCollection.java | 18 +++++++ .../PlexusIoFileResourceCollection.java | 29 ++++++++++ .../PlexusIoFileResourceCollectionTest.java | 53 +++++++++++++++++++ src/test/resources/Test-p1.txt | 38 +++++++++++++ src/test/resources/test.any.of.extension | 1 + src/test/resources/test.tar.gz | 1 + 6 files changed, 140 insertions(+) create mode 100644 src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollectionTest.java create mode 100644 src/test/resources/Test-p1.txt create mode 100644 src/test/resources/test.any.of.extension create mode 100644 src/test/resources/test.tar.gz diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollection.java b/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollection.java index 431b762e..a2fed81d 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollection.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/AbstractPlexusIoResourceCollection.java @@ -63,6 +63,8 @@ public InputStream transform( @Nonnull PlexusIoResource resource, @Nonnull Input private FileMapper[] fileMappers; private InputStreamTransformer streamTransformer = identityTransformer; + + private String fileSuffix; protected AbstractPlexusIoResourceCollection() { @@ -245,6 +247,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 iterator() { diff --git a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java index 3fe7b952..dca015d9 100644 --- a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java @@ -151,6 +151,7 @@ private void addResources( List result, String[] resources ) for ( String name : resources ) { String sourceDir = name.replace( '\\', '/' ); + name = addFileNameSuffix( name ); File f = new File( dir, sourceDir ); PlexusIoResourceAttributes attrs = new FileAttributes( f, cache1, cache2 ); @@ -259,4 +260,32 @@ public boolean isConcurrentAccessSupported() { return true; } + + /** + * Add a suffix to fileName (eg : test.xml => testSuffix.xml) + * Warning : the extension of the file is calculated after the first "dot" caracter. + * Example : + *
    + *
  • test.tar.gz => testSuffix.tar.gz
  • + *
  • test.any.of.extension => testSuffix.any.of.extension
  • + *
+ */ + 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; + } } diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollectionTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollectionTest.java new file mode 100644 index 00000000..7cb3ab3d --- /dev/null +++ b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollectionTest.java @@ -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() ); + } +} diff --git a/src/test/resources/Test-p1.txt b/src/test/resources/Test-p1.txt new file mode 100644 index 00000000..6430814e --- /dev/null +++ b/src/test/resources/Test-p1.txt @@ -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 diff --git a/src/test/resources/test.any.of.extension b/src/test/resources/test.any.of.extension new file mode 100644 index 00000000..30d74d25 --- /dev/null +++ b/src/test/resources/test.any.of.extension @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/src/test/resources/test.tar.gz b/src/test/resources/test.tar.gz new file mode 100644 index 00000000..30d74d25 --- /dev/null +++ b/src/test/resources/test.tar.gz @@ -0,0 +1 @@ +test \ No newline at end of file