diff --git a/src/main/java/org/apache/maven/shared/verifier/Verifier.java b/src/main/java/org/apache/maven/shared/verifier/Verifier.java index 2229bd1..a478eec 100644 --- a/src/main/java/org/apache/maven/shared/verifier/Verifier.java +++ b/src/main/java/org/apache/maven/shared/verifier/Verifier.java @@ -950,6 +950,11 @@ public void writeFile( String path, String contents ) /** * Filters a text file by replacing some user-defined tokens. + * This method is equivalent to: + * + *
+     *     filterFile( srcPath, dstPath, fileEncoding, verifier.newDefaultFilterMap() )
+     * 
* * @param srcPath The path to the input file, relative to the base directory, must not be * null. @@ -957,21 +962,39 @@ public void writeFile( String path, String contents ) * input file, must not be null. * @param fileEncoding The file encoding to use, may be null or empty to use the platform's default * encoding. - * @param filterProperties The mapping from tokens to replacement values, must not be null. + * @return The path to the filtered output file, never null. + * @throws IOException If the file could not be filtered. + * @since 2.0 + */ + public File filterFile( String srcPath, String dstPath, String fileEncoding ) + throws IOException + { + return filterFile( srcPath, dstPath, fileEncoding, newDefaultFilterMap() ); + } + + /** + * Filters a text file by replacing some user-defined tokens. + * + * @param srcPath The path to the input file, relative to the base directory, must not be + * null. + * @param dstPath The path to the output file, relative to the base directory and possibly equal to the + * input file, must not be null. + * @param fileEncoding The file encoding to use, may be null or empty to use the platform's default + * encoding. + * @param filterMap The mapping from tokens to replacement values, must not be null. * @return The path to the filtered output file, never null. * @throws IOException If the file could not be filtered. * @since 1.2 */ - public File filterFile( String srcPath, String dstPath, String fileEncoding, Map filterProperties ) + public File filterFile( String srcPath, String dstPath, String fileEncoding, Map filterMap ) throws IOException { File srcFile = new File( getBasedir(), srcPath ); String data = FileUtils.fileRead( srcFile, fileEncoding ); - for ( String token : filterProperties.keySet() ) + for ( Map.Entry entry : filterMap.entrySet() ) { - String value = String.valueOf( filterProperties.get( token ) ); - data = StringUtils.replace( data, token, value ); + data = StringUtils.replace( data, entry.getKey() , entry.getValue() ); } File dstFile = new File( getBasedir(), dstPath ); @@ -1011,13 +1034,29 @@ public File filterFile( String srcPath, String dstPath, String fileEncoding, Pro * * @return The (modifiable) map with the default filter properties, never null. * @since 1.2 + * @deprecated use {@link #newDefaultFilterMap()} */ + @Deprecated public Properties newDefaultFilterProperties() { Properties filterProperties = new Properties(); + filterProperties.putAll( newDefaultFilterMap() ); + return filterProperties; + } + + /** + * Gets a new copy of the default filter map. These default filter map, contains the tokens "@basedir@" and + * "@baseurl@" to the test's base directory and its base file: URL, respectively. + * + * @return The (modifiable) map with the default filter map, never null. + * @since 2.0 + */ + public Map newDefaultFilterMap() + { + Map filterMap = new HashMap<>(); String basedir = new File( getBasedir() ).getAbsolutePath(); - filterProperties.put( "@basedir@", basedir ); + filterMap.put( "@basedir@", basedir ); /* * NOTE: Maven fails to properly handle percent-encoded "file:" URLs (WAGON-111) so don't use File.toURI() here @@ -1029,9 +1068,9 @@ public Properties newDefaultFilterProperties() baseurl = '/' + baseurl; } baseurl = "file://" + baseurl.replace( '\\', '/' ); - filterProperties.put( "@baseurl@", baseurl ); + filterMap.put( "@baseurl@", baseurl ); - return filterProperties; + return filterMap; } /** diff --git a/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java b/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java index d95356f..0531611 100644 --- a/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java +++ b/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java @@ -24,6 +24,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; +import java.util.Map; import java.util.Properties; import org.junit.jupiter.api.Test; @@ -32,6 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class VerifierTest { @@ -118,4 +120,14 @@ public void testDedicatedMavenHome() throws VerificationException, IOException ForkedLauncherTest.expectFileLine( logFile, "Hello World from Maven Home" ); } + @Test + void testDefaultFilterMap() throws VerificationException + { + Verifier verifier = new Verifier( "src/test/resources" ); + Map filterMap = verifier.newDefaultFilterMap(); + + assertEquals( 2, filterMap.size() ); + assertTrue( filterMap.containsKey( "@basedir@" ) ); + assertTrue( filterMap.containsKey( "@baseurl@" ) ); + } }