From 8addd05369b60e5169a7db304fc92fff6c08c018 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 13 May 2022 16:43:17 +0200 Subject: [PATCH 1/2] Fix tests under Windows --- .../pmd/docs/RuleDocGenerator.java | 19 ++++--------------- .../sourceforge/pmd/docs/RuleSetUtils.java | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java index a1e23965cd..0cc2923649 100644 --- a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java +++ b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleDocGenerator.java @@ -31,7 +31,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.SystemUtils; import org.apache.commons.text.StringEscapeUtils; import net.sourceforge.pmd.Rule; @@ -609,13 +608,13 @@ public class RuleDocGenerator { // is replaced by a correct path. for (List rulesets : sortedRulesets.values()) { for (RuleSet ruleset : rulesets) { - String rulesetFilename = normalizeForwardSlashes(StringUtils.chomp(ruleset.getFileName())); + String rulesetFilename = RuleSetUtils.normalizeForwardSlashes(StringUtils.chomp(ruleset.getFileName())); allRulesets.put(ruleset.getFileName(), rulesetFilename); for (Rule rule : ruleset.getRules()) { String ruleClass = rule.getRuleClass(); String relativeSourceFilename = ruleClass.replaceAll("\\.", Matcher.quoteReplacement(File.separator)) + ".java"; - allRules.put(ruleClass, normalizeForwardSlashes(relativeSourceFilename)); + allRules.put(ruleClass, RuleSetUtils.normalizeForwardSlashes(relativeSourceFilename)); } } } @@ -637,7 +636,7 @@ public class RuleDocGenerator { } if (foundRuleClass != null) { Path foundPath = root.relativize(file); - allRules.put(foundRuleClass, normalizeForwardSlashes(foundPath.toString())); + allRules.put(foundRuleClass, RuleSetUtils.normalizeForwardSlashes(foundPath.toString())); } String foundRuleset = null; @@ -649,7 +648,7 @@ public class RuleDocGenerator { } if (foundRuleset != null) { Path foundPath = root.relativize(file); - allRulesets.put(foundRuleset, normalizeForwardSlashes(foundPath.toString())); + allRulesets.put(foundRuleset, RuleSetUtils.normalizeForwardSlashes(foundPath.toString())); } } return FileVisitResult.CONTINUE; @@ -659,14 +658,4 @@ public class RuleDocGenerator { throw new RuntimeException(e); } } - - private static String normalizeForwardSlashes(String path) { - String normalized = IOUtil.normalizePath(path); - if (SystemUtils.IS_OS_WINDOWS) { - // Note: windows path separators are changed to forward slashes, - // so that the editme link works - normalized = normalized.replaceAll(Pattern.quote(File.separator), "/"); - } - return normalized; - } } diff --git a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleSetUtils.java b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleSetUtils.java index a7afef7b5b..0ab05a68bc 100644 --- a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleSetUtils.java +++ b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/RuleSetUtils.java @@ -5,8 +5,10 @@ package net.sourceforge.pmd.docs; import java.io.File; +import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.SystemUtils; import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleSet; @@ -52,8 +54,8 @@ public final class RuleSetUtils { } public static String getRuleSetClasspath(RuleSet ruleset) { - final String RESOURCES_PATH = File.separator + "resources" + File.separator; - String filename = IOUtil.normalizePath(StringUtils.chomp(ruleset.getFileName())); + final String RESOURCES_PATH = "/resources/"; + String filename = normalizeForwardSlashes(StringUtils.chomp(ruleset.getFileName())); int startIndex = filename.lastIndexOf(RESOURCES_PATH); if (startIndex > -1) { return filename.substring(startIndex + RESOURCES_PATH.length()); @@ -62,6 +64,16 @@ public final class RuleSetUtils { } } + public static String normalizeForwardSlashes(String path) { + String normalized = IOUtil.normalizePath(path); + if (SystemUtils.IS_OS_WINDOWS) { + // Note: windows path separators are changed to forward slashes, + // so that the editme link works + normalized = normalized.replaceAll(Pattern.quote(File.separator), "/"); + } + return normalized; + } + /** * Recursively resolves rule references until the last reference. * The last reference is returned. From a9219b7967ed1fbbc3029625c40823fd49e84a5f Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 13 May 2022 17:13:16 +0200 Subject: [PATCH 2/2] [core] Fix bug in IOUtil.fromReader --- .../java/net/sourceforge/pmd/util/IOUtil.java | 2 +- .../java/net/sourceforge/pmd/util/IOUtilTest.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/IOUtil.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/IOUtil.java index 791561bc7a..881bc7efa2 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/IOUtil.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/IOUtil.java @@ -325,7 +325,7 @@ public final class IOUtil { charBuffer.flip(); encoder.encode(charBuffer, byteBuffer, eof); byteBuffer.flip(); - charBuffer.flip(); + charBuffer.compact(); } if (byteBuffer.hasRemaining()) { diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/util/IOUtilTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/util/IOUtilTest.java index 7b21230fb6..b577de8f11 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/util/IOUtilTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/util/IOUtilTest.java @@ -193,6 +193,21 @@ public class IOUtilTest { } } + @Test + public void testInputStreamFromReader2() throws IOException { + int size = 8192 + 8192 + 10; + char[] data = new char[size]; + for (int i = 0; i < size; i++) { + data[i] = 'A'; + } + data[8192] = 'รค'; // block size border - in UTF-8 these are two bytes. Decoding needs to take the bytes + // from previous block and new block + try (InputStream inputStream = IOUtil.fromReader(new StringReader(new String(data)))) { + byte[] bytes = IOUtil.toByteArray(inputStream); + Assert.assertEquals(new String(data), new String(bytes, StandardCharsets.UTF_8)); + } + } + @Test public void testCopyStream() throws IOException { int size = 8192 + 8192 + 10;