From 90f78c0604b4e8fb8257bf7b2b114377685a8cb5 Mon Sep 17 00:00:00 2001 From: Thomas Collignon Date: Thu, 21 Jun 2018 09:26:53 +0200 Subject: [PATCH 1/2] [MASSEMBLY-617] add ability to give a fileSuffix to a AbstractPlexusIoResourceCollection --- .../AbstractPlexusIoResourceCollection.java | 18 +++++++++++ .../PlexusIoFileResourceCollection.java | 25 +++++++++++++++ .../PlexusIoFileResourceCollectionTest.java | 31 +++++++++++++++++++ ...PlexusIoZipFileResourceCollectionTest.java | 2 -- 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollectionTest.java 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 21eb6af4..8850d52e 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 @@ -64,6 +64,8 @@ public InputStream transform( @Nonnull PlexusIoResource resource, @Nonnull Input private FileMapper[] fileMappers; private InputStreamTransformer streamTransformer = identityTransformer; + + private String fileSuffix; protected AbstractPlexusIoResourceCollection() { @@ -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 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 fb0d228b..d74b0b6b 100755 --- a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java @@ -149,6 +149,8 @@ private void addResources( List result, String[] resources, for ( String name : resources ) { String sourceDir = name.replace( '\\', '/' ); + + name = addFileNameSuffix(name); File f = new File( dir, sourceDir ); @@ -185,6 +187,7 @@ private void addResourcesJava7( List 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 ); @@ -279,4 +282,26 @@ public Iterator getResources() return result.iterator(); } } + + /** + * Add a suffix to fileName (eg : test.xml => testSuffix.xml) + */ + private String addFileNameSuffix(String name) + { + String nameWithSuffix = name; + if (getFileSuffix() != null && getFileSuffix() != "") + { + if (name.contains(".")) + { + String[] split = name.split("\\."); + String extension = "." + split[split.length-1]; + nameWithSuffix = name.replace(extension, getFileSuffix() + extension); + } + 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..bdb7efc3 --- /dev/null +++ b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollectionTest.java @@ -0,0 +1,31 @@ +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() ); + } +} diff --git a/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoZipFileResourceCollectionTest.java b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoZipFileResourceCollectionTest.java index 9d12b62c..2d793863 100644 --- a/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoZipFileResourceCollectionTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoZipFileResourceCollectionTest.java @@ -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() ); } @@ -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(); } From 74a5abd166925e710a0ef1e3ef58002f14cfef35 Mon Sep 17 00:00:00 2001 From: Thomas Collignon Date: Thu, 21 Jun 2018 14:19:20 +0200 Subject: [PATCH 2/2] manage different extension and code style --- .../PlexusIoFileResourceCollection.java | 22 +++++++---- .../PlexusIoFileResourceCollectionTest.java | 22 +++++++++++ src/test/resources/test.any.of.extension | 38 +++++++++++++++++++ src/test/resources/test.tar.gz | 38 +++++++++++++++++++ 4 files changed, 112 insertions(+), 8 deletions(-) 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/PlexusIoFileResourceCollection.java b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java index d74b0b6b..c71cb1a8 100755 --- a/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java +++ b/src/main/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollection.java @@ -150,7 +150,7 @@ private void addResources( List result, String[] resources, { String sourceDir = name.replace( '\\', '/' ); - name = addFileNameSuffix(name); + name = addFileNameSuffix( name ); File f = new File( dir, sourceDir ); @@ -187,7 +187,7 @@ private void addResourcesJava7( List result, String[] resource for ( String name : resources ) { String sourceDir = name.replace( '\\', '/' ); - name = addFileNameSuffix(name); + name = addFileNameSuffix( name ); File f = new File( dir, sourceDir ); PlexusIoResourceAttributes attrs = new Java7FileAttributes( f, cache1, cache2 ); @@ -285,17 +285,23 @@ public Iterator getResources() /** * 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) + private String addFileNameSuffix( String name ) { String nameWithSuffix = name; - if (getFileSuffix() != null && getFileSuffix() != "") + if ( StringUtils.isNotBlank( getFileSuffix()) ) { - if (name.contains(".")) + if ( name.contains( "." ) ) { - String[] split = name.split("\\."); - String extension = "." + split[split.length-1]; - nameWithSuffix = name.replace(extension, getFileSuffix() + extension); + String beforeExtension = name.substring( 0, name.indexOf('.') ); + String afterExtension = name.substring( name.indexOf('.') + 1) ; + nameWithSuffix = beforeExtension + getFileSuffix() + "." + afterExtension; } else { 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 index bdb7efc3..7cb3ab3d 100644 --- a/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollectionTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/resources/PlexusIoFileResourceCollectionTest.java @@ -28,4 +28,26 @@ public void testWithNoFileSuffix() 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.any.of.extension b/src/test/resources/test.any.of.extension new file mode 100644 index 00000000..6430814e --- /dev/null +++ b/src/test/resources/test.any.of.extension @@ -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.tar.gz b/src/test/resources/test.tar.gz new file mode 100644 index 00000000..6430814e --- /dev/null +++ b/src/test/resources/test.tar.gz @@ -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