From 8a89a4c7861a2fecb3710c0242ded6f3958c2250 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 1 Jun 2023 17:29:24 +0200 Subject: [PATCH] Use explicit encoding in ruleset files --- .../resources/category/apex/bestpractices.xml | 2 +- .../resources/category/apex/codestyle.xml | 2 +- .../main/resources/category/apex/design.xml | 2 +- .../resources/category/apex/documentation.xml | 2 +- .../resources/category/apex/errorprone.xml | 2 +- .../category/apex/multithreading.xml | 2 +- .../resources/category/apex/performance.xml | 2 +- .../main/resources/category/apex/security.xml | 2 +- .../pmd/lang/apex/DefaultRulesetTest.java | 9 ++++- .../resources/category/html/bestpractices.xml | 2 +- .../resources/category/html/codestyle.xml | 2 +- .../main/resources/category/html/design.xml | 2 +- .../resources/category/html/documentation.xml | 2 +- .../resources/category/html/errorprone.xml | 2 +- .../category/html/multithreading.xml | 2 +- .../resources/category/html/performance.xml | 2 +- .../main/resources/category/html/security.xml | 2 +- .../resources/category/java/bestpractices.xml | 2 +- .../resources/category/java/codestyle.xml | 2 +- .../main/resources/category/java/design.xml | 2 +- .../resources/category/java/documentation.xml | 2 +- .../resources/category/java/errorprone.xml | 2 +- .../category/java/multithreading.xml | 2 +- .../resources/category/java/performance.xml | 2 +- .../main/resources/category/java/security.xml | 2 +- .../resources/rulesets/java/quickstart.xml | 2 +- .../pmd/lang/java/QuickstartRulesetTest.java | 9 ++++- .../category/ecmascript/bestpractices.xml | 2 +- .../category/ecmascript/codestyle.xml | 2 +- .../resources/category/ecmascript/design.xml | 2 +- .../category/ecmascript/documentation.xml | 2 +- .../category/ecmascript/errorprone.xml | 2 +- .../category/ecmascript/multithreading.xml | 2 +- .../category/ecmascript/performance.xml | 2 +- .../category/ecmascript/security.xml | 2 +- .../resources/category/jsp/bestpractices.xml | 2 +- .../main/resources/category/jsp/codestyle.xml | 2 +- .../main/resources/category/jsp/design.xml | 2 +- .../resources/category/jsp/documentation.xml | 2 +- .../resources/category/jsp/errorprone.xml | 2 +- .../resources/category/jsp/multithreading.xml | 2 +- .../resources/category/jsp/performance.xml | 2 +- .../main/resources/category/jsp/security.xml | 2 +- .../category/kotlin/bestpractices.xml | 2 +- .../resources/category/kotlin/errorprone.xml | 2 +- .../category/modelica/bestpractices.xml | 2 +- .../category/plsql/bestpractices.xml | 2 +- .../resources/category/plsql/codestyle.xml | 2 +- .../main/resources/category/plsql/design.xml | 2 +- .../category/plsql/documentation.xml | 2 +- .../resources/category/plsql/errorprone.xml | 2 +- .../category/plsql/multithreading.xml | 2 +- .../resources/category/plsql/performance.xml | 2 +- .../resources/category/plsql/security.xml | 2 +- .../category/scala/bestpractices.xml | 2 +- .../resources/category/scala/codestyle.xml | 2 +- .../main/resources/category/scala/design.xml | 2 +- .../category/scala/documentation.xml | 2 +- .../resources/category/scala/errorprone.xml | 12 +++---- .../category/scala/multithreading.xml | 2 +- .../resources/category/scala/performance.xml | 2 +- .../resources/category/scala/security.xml | 2 +- .../category/swift/bestpractices.xml | 2 +- .../resources/category/swift/codestyle.xml | 2 +- .../main/resources/category/swift/design.xml | 2 +- .../category/swift/documentation.xml | 4 +-- .../resources/category/swift/errorprone.xml | 2 +- .../category/swift/multithreading.xml | 4 +-- .../resources/category/swift/performance.xml | 2 +- .../resources/category/swift/security.xml | 2 +- .../pmd/AbstractRuleSetFactoryTest.java | 35 +++++++++++++++++-- .../resources/category/vf/bestpractices.xml | 2 +- .../main/resources/category/vf/codestyle.xml | 2 +- .../src/main/resources/category/vf/design.xml | 2 +- .../resources/category/vf/documentation.xml | 2 +- .../main/resources/category/vf/errorprone.xml | 2 +- .../resources/category/vf/multithreading.xml | 2 +- .../resources/category/vf/performance.xml | 2 +- .../main/resources/category/vf/security.xml | 2 +- .../resources/category/vm/bestpractices.xml | 2 +- .../main/resources/category/vm/codestyle.xml | 2 +- .../src/main/resources/category/vm/design.xml | 2 +- .../resources/category/vm/documentation.xml | 2 +- .../main/resources/category/vm/errorprone.xml | 2 +- .../resources/category/vm/multithreading.xml | 2 +- .../resources/category/vm/performance.xml | 2 +- .../main/resources/category/vm/security.xml | 2 +- .../resources/category/pom/bestpractices.xml | 2 +- .../main/resources/category/pom/codestyle.xml | 2 +- .../main/resources/category/pom/design.xml | 2 +- .../resources/category/pom/documentation.xml | 2 +- .../resources/category/pom/errorprone.xml | 2 +- .../resources/category/pom/multithreading.xml | 2 +- .../resources/category/pom/performance.xml | 2 +- .../main/resources/category/pom/security.xml | 2 +- .../resources/category/wsdl/bestpractices.xml | 2 +- .../resources/category/wsdl/codestyle.xml | 2 +- .../main/resources/category/wsdl/design.xml | 2 +- .../resources/category/wsdl/documentation.xml | 2 +- .../resources/category/wsdl/errorprone.xml | 2 +- .../category/wsdl/multithreading.xml | 2 +- .../resources/category/wsdl/performance.xml | 2 +- .../main/resources/category/wsdl/security.xml | 2 +- .../resources/category/xml/bestpractices.xml | 2 +- .../main/resources/category/xml/codestyle.xml | 2 +- .../main/resources/category/xml/design.xml | 2 +- .../resources/category/xml/documentation.xml | 2 +- .../resources/category/xml/errorprone.xml | 2 +- .../resources/category/xml/multithreading.xml | 2 +- .../resources/category/xml/performance.xml | 2 +- .../main/resources/category/xml/security.xml | 2 +- .../resources/category/xsl/bestpractices.xml | 2 +- .../main/resources/category/xsl/codestyle.xml | 2 +- .../main/resources/category/xsl/design.xml | 2 +- .../resources/category/xsl/documentation.xml | 2 +- .../resources/category/xsl/errorprone.xml | 2 +- .../resources/category/xsl/multithreading.xml | 2 +- .../resources/category/xsl/performance.xml | 2 +- .../main/resources/category/xsl/security.xml | 2 +- 119 files changed, 171 insertions(+), 128 deletions(-) diff --git a/pmd-apex/src/main/resources/category/apex/bestpractices.xml b/pmd-apex/src/main/resources/category/apex/bestpractices.xml index 6763f71d9a..178f94e82e 100644 --- a/pmd-apex/src/main/resources/category/apex/bestpractices.xml +++ b/pmd-apex/src/main/resources/category/apex/bestpractices.xml @@ -1,4 +1,4 @@ - + + + + + + + + { - RuleSet ruleset = rulesetLoader().loadFromResource("rulesets/apex/quickstart.xml"); + RuleSet ruleset = rulesetLoader().loadFromResource(QUICKSTART_RULESET); assertNotNull(ruleset); }); assertTrue(log.isEmpty(), "No Logging expected"); } + @Test + void correctEncoding() throws Exception { + assertTrue(AbstractRuleSetFactoryTest.hasCorrectEncoding(QUICKSTART_RULESET)); + } + private RuleSetLoader rulesetLoader() { return new RuleSetLoader().enableCompatibility(false); } diff --git a/pmd-html/src/main/resources/category/html/bestpractices.xml b/pmd-html/src/main/resources/category/html/bestpractices.xml index 19c07662e9..775e366565 100644 --- a/pmd-html/src/main/resources/category/html/bestpractices.xml +++ b/pmd-html/src/main/resources/category/html/bestpractices.xml @@ -1,4 +1,4 @@ - + + + + + + + + + + + + + + + + + { - RuleSet quickstart = ruleSetLoader.loadFromResource("rulesets/java/quickstart.xml"); + RuleSet quickstart = ruleSetLoader.loadFromResource(QUICKSTART_RULESET); assertFalse(quickstart.getRules().isEmpty()); }); assertTrue(errorOutput.isEmpty()); } + + @Test + void correctEncoding() throws Exception { + assertTrue(AbstractRuleSetFactoryTest.hasCorrectEncoding(QUICKSTART_RULESET)); + } } diff --git a/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml b/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml index 4c76991f98..3e476dacdd 100644 --- a/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml +++ b/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml @@ -1,4 +1,4 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd"> - + Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors. - - diff --git a/pmd-scala-modules/pmd-scala-common/src/main/resources/category/scala/multithreading.xml b/pmd-scala-modules/pmd-scala-common/src/main/resources/category/scala/multithreading.xml index 88c22dfc8b..0681a5d987 100644 --- a/pmd-scala-modules/pmd-scala-common/src/main/resources/category/scala/multithreading.xml +++ b/pmd-scala-modules/pmd-scala-common/src/main/resources/category/scala/multithreading.xml @@ -1,4 +1,4 @@ - + + + + + + + Rules that are related to code documentation. - \ No newline at end of file + diff --git a/pmd-swift/src/main/resources/category/swift/errorprone.xml b/pmd-swift/src/main/resources/category/swift/errorprone.xml index e4dab5022d..7bf277656b 100644 --- a/pmd-swift/src/main/resources/category/swift/errorprone.xml +++ b/pmd-swift/src/main/resources/category/swift/errorprone.xml @@ -1,4 +1,4 @@ - + + Rules that flag issues when dealing with multiple threads of execution. - \ No newline at end of file + diff --git a/pmd-swift/src/main/resources/category/swift/performance.xml b/pmd-swift/src/main/resources/category/swift/performance.xml index be669f985c..f791f2f69f 100644 --- a/pmd-swift/src/main/resources/category/swift/performance.xml +++ b/pmd-swift/src/main/resources/category/swift/performance.xml @@ -1,4 +1,4 @@ - + + ruleSetFileNames = getRuleSetFileNames(); + StringBuilder messages = new StringBuilder(); + for (String fileName : ruleSetFileNames) { + boolean valid = hasCorrectEncoding(fileName); + allValid = allValid && valid; + if (!valid) { + messages.append("RuleSet ") + .append(fileName) + .append(" is missing XML encoding or not using UTF8\n"); + } + } + assertTrue(allValid, "All XML must use correct XML encoding\n" + messages); + } + + public static boolean hasCorrectEncoding(String fileName) throws IOException { + try (InputStream inputStream = loadResourceAsStream(fileName)) { + // first bytes must be: + byte[] expectedBytes = "".getBytes(StandardCharsets.UTF_8); + byte[] bytes = new byte[expectedBytes.length]; + int count = inputStream.read(bytes); + if (count != expectedBytes.length || !Arrays.equals(expectedBytes, bytes)) { + return false; + } + } + return true; + } + /** * Verifies that all rulesets are valid XML according to the DTD. * @@ -381,8 +412,8 @@ public abstract class AbstractRuleSetFactoryTest { } } - private InputStream loadResourceAsStream(String resource) { - return getClass().getClassLoader().getResourceAsStream(resource); + private static InputStream loadResourceAsStream(String resource) { + return AbstractRuleSetFactoryTest.class.getClassLoader().getResourceAsStream(resource); } private void testRuleSet(String fileName) throws IOException, SAXException { diff --git a/pmd-visualforce/src/main/resources/category/vf/bestpractices.xml b/pmd-visualforce/src/main/resources/category/vf/bestpractices.xml index 58e7fd43a3..126b820901 100644 --- a/pmd-visualforce/src/main/resources/category/vf/bestpractices.xml +++ b/pmd-visualforce/src/main/resources/category/vf/bestpractices.xml @@ -1,4 +1,4 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +