Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use System.lineSeparator() on all systems #335

Merged
merged 1 commit into from Mar 16, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/codehaus/mojo/flatten/FlattenMojo.java
Expand Up @@ -537,9 +537,9 @@ protected void writePom( Model pom, File pomFile, String headerComment, KeepComm
protected void writeStringToFile( String data, File file, String encoding )
throws MojoExecutionException
{
if ( System.getProperty( "os.name" ).contains( "Windows" ) )
if ( !"\n".equals( System.lineSeparator() ) )
{
data = data.replace( "\n", "\r\n" );
data = data.replace( "\n", System.lineSeparator() );
}
byte[] binaryData;

Expand Down
48 changes: 28 additions & 20 deletions src/test/java/org/codehaus/mojo/flatten/KeepCommentsInPomTest.java
Expand Up @@ -21,9 +21,10 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.regex.Pattern;

import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.project.MavenProject;
Expand All @@ -32,7 +33,7 @@
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

/**
* Test-Case for {@link FlattenMojo}.
Expand All @@ -48,6 +49,7 @@ public class KeepCommentsInPomTest
* Expected result since jdk11 with updated xml header and properties sequence.
*/
private static final String EXPECTED_FLATTENED_POM_JDK11 = PATH + "expected-flattened-pom-jdk11.xml";
private static final Pattern NEW_LINE_PATTERN = Pattern.compile( "\\n|\\r\\n?" );

@Rule
public MojoRule rule = new MojoRule();
Expand Down Expand Up @@ -77,18 +79,10 @@ public void keepsProfileActivationFile() throws Exception
// execute writes new FLATTENED_POM
flattenMojo.execute();

String tempExpectedContent;
if ( isJdk8() )
{
tempExpectedContent = getContent( EXPECTED_FLATTENED_POM );
}
else
{
tempExpectedContent = getContent( EXPECTED_FLATTENED_POM_JDK11 );
}
String tempActualContent = getContent( FLATTENED_POM );
assertEquals( "Expected POM does not match, see " + FLATTENED_POM, tempExpectedContent, tempActualContent );

Path expectedContentFile = Paths.get( isJdk8() ? EXPECTED_FLATTENED_POM : EXPECTED_FLATTENED_POM_JDK11 );
Path actualContentFile = Paths.get( FLATTENED_POM );
assertThat( actualContentFile ).hasSameTextualContentAs( expectedContentFile );
assertHasLineSeparator( actualContentFile , System.lineSeparator() );
}

/**
Expand All @@ -107,12 +101,26 @@ private boolean isJdk8()
return false;
}

/**
*
*/
private String getContent( String aPomFile ) throws IOException
private static void assertHasLineSeparator( final Path file, final String expectedSeparator ) throws IOException
{
return String.join( "\n", Files.readAllLines( Paths.get( aPomFile ), StandardCharsets.UTF_8 ) );
try ( Scanner scanner = new Scanner( file ) )
{
int lineNr = 0;
String actualSeparator;
while ( ( actualSeparator = scanner.findWithinHorizon( NEW_LINE_PATTERN, 0 ) ) != null )
{
lineNr++;
if ( !expectedSeparator.equals( actualSeparator ) )
{
final String actualDesc = actualSeparator.replace( "\r", "CR" ).replace( "\n", "LF" );
final String expectedDesc = expectedSeparator.replace( "\r", "CR" ).replace( "\n", "LF" );
throw new AssertionError(
String.format(
"\nLine %d of path %s has %s as line separator.\nExpected line separator: %s.",
lineNr, file, actualDesc, expectedDesc ) );
}
}
}
}

}