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

jochen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/creadur-rat.git


The following commit(s) were added to refs/heads/master by this push:
     new 8dc90b6  Fixed tests on Java 16, and 17 by eliminating the attempt to 
change file.encoding at runtime.
8dc90b6 is described below

commit 8dc90b6eec9be8941738981ee843ec9e9a4aa709
Author: Jochen Wiedmann <[email protected]>
AuthorDate: Thu Mar 31 23:00:08 2022 +0100

    Fixed tests on Java 16, and 17 by eliminating the attempt to change 
file.encoding at runtime.
---
 apache-rat-plugin/pom.xml                          |  8 ++++
 .../java/org/apache/rat/mp/RatCheckMojoTest.java   | 49 ++++----------------
 apache-rat-tasks/pom.xml                           |  8 ++++
 .../java/org/apache/rat/anttasks/ReportTest.java   | 53 ++++++----------------
 src/changes/changes.xml                            |  5 ++
 5 files changed, 44 insertions(+), 79 deletions(-)

diff --git a/apache-rat-plugin/pom.xml b/apache-rat-plugin/pom.xml
index 255ce9d..fe4bca6 100644
--- a/apache-rat-plugin/pom.xml
+++ b/apache-rat-plugin/pom.xml
@@ -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>
diff --git 
a/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java 
b/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
index 9e785dc..b12e114 100644
--- a/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
+++ b/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
@@ -28,13 +28,10 @@ import java.io.File;
 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 class RatCheckMojoTest extends 
AbstractMojoTestCase {
      * @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 class RatCheckMojoTest extends AbstractMojoTestCase 
{
             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);
-        }
-    }
 }
diff --git a/apache-rat-tasks/pom.xml b/apache-rat-tasks/pom.xml
index 639a573..130c893 100644
--- a/apache-rat-tasks/pom.xml
+++ b/apache-rat-tasks/pom.xml
@@ -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>
diff --git 
a/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportTest.java 
b/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportTest.java
index c2bb271..ebed7f6 100644
--- a/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportTest.java
+++ b/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportTest.java
@@ -30,6 +30,7 @@ import java.nio.charset.StandardCharsets;
 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 class ReportTest extends AbstractRatAntTaskTest {
      */
     @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 class ReportTest extends AbstractRatAntTaskTest {
      */
     @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 class ReportTest extends AbstractRatAntTaskTest {
         }
         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);
-        }
-    }
 }
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 50f2014..156e7ae 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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.

Reply via email to