creadur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pottlin...@apache.org
Subject svn commit: r1821056 - in /creadur/rat/trunk: apache-rat-plugin/src/main/java/org/apache/rat/mp/ apache-rat-plugin/src/test/java/org/apache/rat/mp/ apache-rat-plugin/src/test/resources/unit/it4/ apache-rat-tasks/src/main/java/org/apache/rat/anttasks/ a...
Date Sat, 13 Jan 2018 10:47:55 GMT
Author: pottlinger
Date: Sat Jan 13 10:47:55 2018
New Revision: 1821056

URL: http://svn.apache.org/viewvc?rev=1821056&view=rev
Log:
RAT-242: Add tests and fixes for encoding

Added:
    creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/
    creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/iso-8859-1.html
    creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/pom.xml
    creadur/rat/trunk/apache-rat-tasks/src/test/resources/org/
    creadur/rat/trunk/apache-rat-tasks/src/test/resources/org/example/
    creadur/rat/trunk/apache-rat-tasks/src/test/resources/org/example/iso-8859-1.html
Modified:
    creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java
    creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
    creadur/rat/trunk/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java
    creadur/rat/trunk/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportTest.java
    creadur/rat/trunk/apache-rat-tasks/src/test/resources/antunit/report-junit.xml

Modified: creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java?rev=1821056&r1=1821055&r2=1821056&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java
(original)
+++ creadur/rat/trunk/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java
Sat Jan 13 10:47:55 2018
@@ -105,7 +105,7 @@ public class RatCheckMojo extends Abstra
         Writer fw = null;
         try {
             fw = new OutputStreamWriter(
-                   new FileOutputStream(reportFile), 
+                   new FileOutputStream(reportFile),
                    Charset.forName("UTF-8"));
             final ClaimStatistic statistic = createReport(fw, getStyleSheet());
             fw.close();

Modified: creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java?rev=1821056&r1=1821055&r2=1821056&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
(original)
+++ creadur/rat/trunk/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java
Sat Jan 13 10:47:55 2018
@@ -28,6 +28,12 @@ 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 javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import static junit.framework.TestCase.assertTrue;
+import org.apache.rat.document.impl.guesser.BinaryGuesser;
 
 import static org.apache.rat.mp.RatTestHelpers.ensureRatReportIsCorrect;
 import static org.apache.rat.mp.RatTestHelpers.getSourceDirectory;
@@ -35,6 +41,7 @@ import static org.apache.rat.mp.RatTestH
 import static org.apache.rat.mp.RatTestHelpers.newArtifactRepository;
 import static org.apache.rat.mp.RatTestHelpers.newArtifactResolver;
 import static org.apache.rat.mp.RatTestHelpers.newSiteRenderer;
+import org.w3c.dom.Document;
 
 /**
  * Test case for the {@link RatCheckMojo} and {@link RatReportMojo}.
@@ -202,4 +209,81 @@ public class RatCheckMojoTest extends Ab
         assertFalse(firstLineModified.contains("--"));
     }
 
+    /**
+     * Test correct generation of XML file if non-UTF8 file.encoding is set.
+     *
+     * @throws Exception The test failed.
+     */
+    public void testIt4() throws Exception {
+        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();
+            // default value is "${project.build.directory}/rat.txt"
+            final String REPORTFILE = "rat.txt";
+
+            assertTrue("report filename was not contained in '" + msg + "'",
+                    msg.contains(REPORTFILE));
+            assertFalse("no null allowed in '" + msg + "'", (msg.toUpperCase()
+                    .contains("NULL")));
+        }
+        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+        FileInputStream fis = new FileInputStream(ratTxtFile);
+        boolean documentParsed = false;
+        try {
+            Document doc = db.parse(fis);
+            boolean byteSequencePresent = doc.getElementsByTagName("header-sample")
+                    .item(0)
+                    .getTextContent()
+                    .contains("\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF");
+            assertTrue("Report should contain test umlauts", byteSequencePresent);
+            documentParsed = true;
+        } catch (Exception ex) {
+            documentParsed = false;
+        } finally {
+            fis.close();
+        }
+        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
+            // 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
+            // run with the java.base module opened
+            throw new RuntimeException(ex);
+        }
+    }
 }

Added: creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/iso-8859-1.html
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/iso-8859-1.html?rev=1821056&view=auto
==============================================================================
--- creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/iso-8859-1.html (added)
+++ creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/iso-8859-1.html Sat Jan
13 10:47:55 2018
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<!--
+Demodata with ISO-8859-1 encoded characters:
+The german umlauts: äöüÄÖÜß
+-->
+<html>
+    <head>
+        <meta charset="windows-1252" />
+        <title>Demo Data</title>
+    </head>
+    <body>
+        
+    </body>
+</html>

Added: creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/pom.xml
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/pom.xml?rev=1821056&view=auto
==============================================================================
--- creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/pom.xml (added)
+++ creadur/rat/trunk/apache-rat-plugin/src/test/resources/unit/it4/pom.xml Sat Jan 13 10:47:55
2018
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<project
+    xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.rat.test</groupId>
+  <artifactId>it4</artifactId>
+  <version>1.0</version>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.rat</groupId>
+        <artifactId>apache-rat-plugin</artifactId>
+        <version>@pom.version@</version>
+        <configuration>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Modified: creadur/rat/trunk/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java?rev=1821056&r1=1821055&r2=1821056&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java (original)
+++ creadur/rat/trunk/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java Sat
Jan 13 10:47:55 2018
@@ -37,7 +37,7 @@ import org.apache.tools.ant.util.FileUti
 
 import javax.xml.transform.TransformerException;
 import java.io.File;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStreamWriter;
@@ -189,11 +189,13 @@ public class Report extends Task {
             if (reportFile == null) {
                 out = new PrintWriter(
                           new OutputStreamWriter(
-                              new LogOutputStream(this, Project.MSG_INFO),
-                              Charset.forName("UTF-8"))
-                          );
+                              new LogOutputStream(this, Project.MSG_INFO)
+                          ));
             } else {
-                out = new PrintWriter(new FileWriter(reportFile));
+                out = new PrintWriter(new OutputStreamWriter(
+                        new FileOutputStream(reportFile),
+                        Charset.forName("UTF-8")
+                ));
             }
             createReport(out);
             out.flush();

Modified: creadur/rat/trunk/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportTest.java
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportTest.java?rev=1821056&r1=1821055&r2=1821056&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportTest.java
(original)
+++ creadur/rat/trunk/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportTest.java
Sat Jan 13 10:47:55 2018
@@ -25,6 +25,12 @@ 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 javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import org.apache.rat.document.impl.guesser.BinaryGuesser;
+import org.w3c.dom.Document;
 
 public class ReportTest extends AbstractRatAntTaskTest {
     private static final File antFile = new File("src/test/resources/antunit/report-junit.xml").getAbsoluteFile();
@@ -122,4 +128,77 @@ public class ReportTest extends Abstract
         assertFalse(modifiedFirstLine.contains("--"));
         assertTrue(modifiedFirstLine.contains("~~"));
     }
+
+    /**
+     * Test correct generation of string result if non-UTF8 file.encoding is set.
+     */
+    public void testISO88591() throws Exception {
+        String origEncoding = overrideFileEncoding("ISO-8859-1");
+        executeTarget("testISO88591");
+        overrideFileEncoding(origEncoding);
+        assertTrue("Log should contain the test umlauts", getLog().contains("\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF"));
+    }
+
+    /**
+     * Test correct generation of XML file if non-UTF8 file.encoding is set.
+     */
+    public void testISO88591WithFile() throws Exception {
+        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");
+        executeTarget("testISO88591WithReportFile");
+        overrideFileEncoding(origEncoding);
+        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+        FileInputStream fis = new FileInputStream(selftestOutput);
+        boolean documentParsed = false;
+        try {
+            Document doc = db.parse(fis);
+            boolean byteSequencePresent = doc.getElementsByTagName("header-sample")
+                    .item(0)
+                    .getTextContent()
+                    .contains("\u00E4\u00F6\u00FC\u00C4\u00D6\u00DC\u00DF");
+            assertTrue("Report should contain test umlauts", byteSequencePresent);
+            documentParsed = true;
+        } catch (Exception ex) {
+            documentParsed = false;
+        } finally {
+            fis.close();
+        }
+        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
+            // 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
+            // run with the java.base module opened
+            throw new RuntimeException(ex);
+        }
+    }
 }

Modified: creadur/rat/trunk/apache-rat-tasks/src/test/resources/antunit/report-junit.xml
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-tasks/src/test/resources/antunit/report-junit.xml?rev=1821056&r1=1821055&r2=1821056&view=diff
==============================================================================
--- creadur/rat/trunk/apache-rat-tasks/src/test/resources/antunit/report-junit.xml (original)
+++ creadur/rat/trunk/apache-rat-tasks/src/test/resources/antunit/report-junit.xml Sat Jan
13 10:47:55 2018
@@ -93,4 +93,17 @@
        <file file="${output.dir}/it-sources/index.apt"/>
     </rat:report>
   </target>
+
+  <target name="testISO88591">
+    <rat:report format="xml">
+      <file file="src/test/resources/org/example/iso-8859-1.html"/>
+    </rat:report>
+  </target>
+
+  <target name="testISO88591WithReportFile">
+    <property name="report.file" location="${output.dir}/selftest.report"/>
+    <rat:report reportFile="${report.file}" format="xml">
+      <file file="src/test/resources/org/example/iso-8859-1.html"/>
+    </rat:report>
+  </target>
 </project>

Added: creadur/rat/trunk/apache-rat-tasks/src/test/resources/org/example/iso-8859-1.html
URL: http://svn.apache.org/viewvc/creadur/rat/trunk/apache-rat-tasks/src/test/resources/org/example/iso-8859-1.html?rev=1821056&view=auto
==============================================================================
--- creadur/rat/trunk/apache-rat-tasks/src/test/resources/org/example/iso-8859-1.html (added)
+++ creadur/rat/trunk/apache-rat-tasks/src/test/resources/org/example/iso-8859-1.html Sat
Jan 13 10:47:55 2018
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<!--
+Demodata with ISO-8859-1 encoded characters:
+The german umlauts: äöüÄÖÜß
+-->
+<html>
+    <head>
+        <meta charset="windows-1252" />
+        <title>Demo Data</title>
+    </head>
+    <body>
+        
+    </body>
+</html>



Mime
View raw message