Skip to content
Merged
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
55 changes: 47 additions & 8 deletions src/main/java/org/apache/maven/shared/verifier/Verifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -950,28 +950,51 @@ public void writeFile( String path, String contents )

/**
* 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>.
* @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 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 );
Expand Down Expand Up @@ -1011,13 +1034,29 @@ public File filterFile( String srcPath, String dstPath, String fileEncoding, Pro
*
* @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
Expand All @@ -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;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/apache/maven/shared/verifier/VerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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<String, String> filterMap = verifier.newDefaultFilterMap();

assertEquals( 2, filterMap.size() );
assertTrue( filterMap.containsKey( "@basedir@" ) );
assertTrue( filterMap.containsKey( "@baseurl@" ) );
}
}