You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/09/01 18:46:15 UTC

[maven-verifier] branch MSHARED-1124 created (now ea1ee9d)

This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a change to branch MSHARED-1124
in repository https://gitbox.apache.org/repos/asf/maven-verifier.git


      at ea1ee9d  [MSHARED-1124] Add new version of methods filterFile and newDefaultFilterMap

This branch includes the following new commits:

     new ea1ee9d  [MSHARED-1124] Add new version of methods filterFile and newDefaultFilterMap

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-verifier] 01/01: [MSHARED-1124] Add new version of methods filterFile and newDefaultFilterMap

Posted by sj...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch MSHARED-1124
in repository https://gitbox.apache.org/repos/asf/maven-verifier.git

commit ea1ee9de18143d9a268a76541e79fc1b9dee1547
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Thu Sep 1 20:45:47 2022 +0200

    [MSHARED-1124] Add new version of methods filterFile and newDefaultFilterMap
    
    in order to easier migrate
---
 .../org/apache/maven/shared/verifier/Verifier.java | 55 ++++++++++++++++++----
 .../apache/maven/shared/verifier/VerifierTest.java | 14 ++++++
 2 files changed, 61 insertions(+), 8 deletions(-)

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 class Verifier
 
     /**
      * Filters a text file by replacing some user-defined tokens.
+     * This method is equivalent to:
+     *
+     * <pre>
+     *     filterFile( srcPath, dstPath, fileEncoding, verifier.newDefaultFilterMap() )
+     * </pre>
      *
      * @param srcPath          The path to the input file, relative to the base directory, must not be
      *                         <code>null</code>.
@@ -957,21 +962,39 @@ public class Verifier
      *                         input file, must not be <code>null</code>.
      * @param fileEncoding     The file encoding to use, may be <code>null</code> or empty to use the platform's default
      *                         encoding.
-     * @param filterProperties The mapping from tokens to replacement values, must not be <code>null</code>.
+     * @return The path to the filtered output file, never <code>null</code>.
+     * @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
+     *                     <code>null</code>.
+     * @param dstPath      The path to the output file, relative to the base directory and possibly equal to the
+     *                     input file, must not be <code>null</code>.
+     * @param fileEncoding The file encoding to use, may be <code>null</code> or empty to use the platform's default
+     *                     encoding.
+     * @param filterMap    The mapping from tokens to replacement values, must not be <code>null</code>.
      * @return The path to the filtered output file, never <code>null</code>.
      * @throws IOException If the file could not be filtered.
      * @since 1.2
      */
-    public File filterFile( String srcPath, String dstPath, String fileEncoding, Map<String, String> filterProperties )
+    public File filterFile( String srcPath, String dstPath, String fileEncoding, Map<String, String> filterMap )
         throws IOException
     {
         File srcFile = new File( getBasedir(), srcPath );
         String data = FileUtils.fileRead( srcFile, fileEncoding );
 
-        for ( String token : filterProperties.keySet() )
+        for ( Map.Entry<String, String> 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 class Verifier
      *
      * @return The (modifiable) map with the default filter properties, never <code>null</code>.
      * @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 <code>file:</code> URL, respectively.
+     *
+     * @return The (modifiable) map with the default filter map, never <code>null</code>.
+     * @since 2.0
+     */
+    public Map<String, String> newDefaultFilterMap()
+    {
+        Map<String, String> 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 class Verifier
             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..cb53683 100644
--- a/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java
+++ b/src/test/java/org/apache/maven/shared/verifier/VerifierTest.java
@@ -19,19 +19,23 @@ package org.apache.maven.shared.verifier;
  * under the License.
  */
 
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 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.Assertions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 
 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 +122,14 @@ public class VerifierTest
         ForkedLauncherTest.expectFileLine( logFile, "Hello World from Maven Home" );
     }
 
+    @Test
+    void testDefaultFilterMap() throws VerificationException
+    {
+        Verifier verifier = new Verifier( "src/test/resources" );
+        Map<String, String> filterMap = verifier.newDefaultFilterMap();
+
+        assertEquals( 2, filterMap.size() );
+        assertTrue( filterMap.containsKey( "@basedir@" ) );
+        assertTrue( filterMap.containsKey( "@baseurl@" ) );
+    }
 }