Skip to content
Permalink
Browse files
Fixed tests on Java 16, and 17 by eliminating the attempt to change f…
…ile.encoding at runtime.
  • Loading branch information
jochenw committed Mar 31, 2022
1 parent 082b3b5 commit 8dc90b6eec9be8941738981ee843ec9e9a4aa709
Showing 5 changed files with 44 additions and 79 deletions.
@@ -125,6 +125,14 @@
</lifecycleMappingMetadata>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>1</forkCount>
<argLine>-Dfile.encoding=ISO-8859-1</argLine>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
@@ -28,13 +28,10 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import static junit.framework.TestCase.assertTrue;
import org.apache.rat.document.impl.guesser.BinaryGuesser;
import org.junit.Assume;

import static org.apache.rat.mp.RatTestHelpers.ensureRatReportIsCorrect;
import static org.apache.rat.mp.RatTestHelpers.getSourceDirectory;
@@ -217,13 +214,18 @@ public void testIt3() throws Exception {
* @throws Exception The test failed.
*/
public void testIt4() throws Exception {
// In previous versions of the JDK, it used to be possible to
// change the value of file.encoding at runtime. As of Java 16,
// this is no longer possible. Instead, at this point, we check,
// that file.encoding is actually ISO-8859-1. (Within Maven, this
// is enforced by the configuration of the surefire plugin.) If not,
// we skip this test.
Assume.assumeTrue("Expected file.encoding=ISO-8859-1", "ISO-8859-1".equals(System.getProperty("file.encoding")));
final RatCheckMojo mojo = newRatCheckMojo("it4");
final File ratTxtFile = getRatTxtFile(mojo);
try {
setVariableValueToObject(mojo, "reportStyle", "xml");
String origEncoding = overrideFileEncoding("ISO-8859-1");
mojo.execute();
overrideFileEncoding(origEncoding);
fail("Expected RatCheckException");
} catch (RatCheckException e) {
final String msg = e.getMessage();
@@ -250,39 +252,4 @@ public void testIt4() throws Exception {
fail("Report file could not be parsed as XML: " + ex.getMessage());
}
}


private String overrideFileEncoding(String newEncoding) {
String current = System.getProperty("file.encoding");
System.setProperty("file.encoding", newEncoding);
setBinaryGuesserCharset(newEncoding);
clearDefaultCharset();
return current;
}

private void clearDefaultCharset() {
try {
Field f = Charset.class.getDeclaredField("defaultCharset");
f.setAccessible(true);
f.set(null, null);
} catch (Exception ex) {
// This is for unittesting - there is no good reason not to rethrow
// it. This could be happening in JDK 9, where the unittests need to
// run with the java.base module opened
throw new RuntimeException(ex);
}
}

private void setBinaryGuesserCharset(String charset) {
try {
Field f = BinaryGuesser.class.getDeclaredField("CHARSET_FROM_FILE_ENCODING_OR_UTF8");
f.setAccessible(true);
f.set(null, Charset.forName(charset));
} catch (Exception ex) {
// This is for unittesting - there is no good reason not to rethrow
// it. This could be happening in JDK 9, where the unittests need to
// run with the java.base module opened
throw new RuntimeException(ex);
}
}
}
@@ -158,6 +158,14 @@
</lifecycleMappingMetadata>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>1</forkCount>
<argLine>-Dfile.encoding=ISO-8859-1</argLine>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
@@ -30,6 +30,7 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.rat.document.impl.guesser.BinaryGuesser;
import org.junit.Assume;
import org.junit.Test;
import org.w3c.dom.Document;

@@ -144,9 +145,14 @@ public void testAddLicenseHeaders() throws Exception {
*/
@Test
public void testISO88591() throws Exception {
String origEncoding = overrideFileEncoding("ISO-8859-1");
// In previous versions of the JDK, it used to be possible to
// change the value of file.encoding at runtime. As of Java 16,
// this is no longer possible. Instead, at this point, we check,
// that file.encoding is actually ISO-8859-1. (Within Maven, this
// is enforced by the configuration of the surefire plugin.) If not,
// we skip this test.
Assume.assumeTrue("Expected file.encoding=ISO-8859-1", "ISO-8859-1".equals(System.getProperty("file.encoding")));
buildRule.executeTarget("testISO88591");
overrideFileEncoding(origEncoding);
assertTrue("Log should contain the test umlauts", buildRule.getLog().contains("\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF"));
}

@@ -155,12 +161,17 @@ public void testISO88591() throws Exception {
*/
@Test
public void testISO88591WithFile() throws Exception {
// In previous versions of the JDK, it used to be possible to
// change the value of file.encoding at runtime. As of Java 16,
// this is no longer possible. Instead, at this point, we check,
// that file.encoding is actually ISO-8859-1. (Within Maven, this
// is enforced by the configuration of the surefire plugin.) If not,
// we skip this test.
Assume.assumeTrue("Expected file.encoding=ISO-8859-1", "ISO-8859-1".equals(System.getProperty("file.encoding")));
Charset.defaultCharset();
String outputDir = System.getProperty("output.dir", "target/anttasks");
String selftestOutput = System.getProperty("report.file", outputDir + "/selftest.report");
String origEncoding = overrideFileEncoding("ISO-8859-1");
buildRule.executeTarget("testISO88591WithReportFile");
overrideFileEncoding(origEncoding);
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
boolean documentParsed = false;
try (FileInputStream fis = new FileInputStream(selftestOutput)) {
@@ -176,38 +187,4 @@ public void testISO88591WithFile() throws Exception {
}
assertTrue("Report file could not be parsed as XML", documentParsed);
}

private String overrideFileEncoding(String newEncoding) {
String current = System.getProperty("file.encoding");
System.setProperty("file.encoding", newEncoding);
setBinaryGuesserCharset(newEncoding);
clearDefaultCharset();
return current;
}

private void clearDefaultCharset() {
try {
Field f = Charset.class.getDeclaredField("defaultCharset");
f.setAccessible(true);
f.set(null, null);
} catch (Exception ex) {
// This is for unittesting - there is no good reason not to rethrow
// it. This could be happening in JDK 9, where the unittests need to
// run with the java.base module opened
throw new RuntimeException(ex);
}
}

private void setBinaryGuesserCharset(String charset) {
try {
Field f = BinaryGuesser.class.getDeclaredField("CHARSET_FROM_FILE_ENCODING_OR_UTF8");
f.setAccessible(true);
f.set(null, Charset.forName(charset));
} catch (Exception ex) {
// This is for unittesting - there is no good reason not to rethrow
// it. This could be happening in JDK 9, where the unittests need to
// run with the java.base module opened
throw new RuntimeException(ex);
}
}
}
@@ -55,6 +55,11 @@ The <action> type attribute can be add,update,fix,remove.

<body>
<release version="0.14-SNAPSHOT" date="2020-xx-xx" description="Current SNAPSHOT - to be done">
<action type="fix" dev="jochen">
Some tests were based on the assumption, that the value of file.encoding
can be changed on runtime. (Won't work nowadays, beginning with Java 16.)
Removed this assumption in favour of a proper surefire configuration.
</action>
<action issue="RAT-273" type="fix" dev="jochen">
Workaround for an incompatibility in the java.io.LineNumberReader, which is
being replaced by the org.apache.rat.header.LineNumberReader.

0 comments on commit 8dc90b6

Please sign in to comment.