diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/XMLRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/XMLRenderer.java index 2ca0038a33..e0509625dd 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/XMLRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/XMLRenderer.java @@ -32,15 +32,19 @@ public final class XMLRenderer implements Renderer { * Creates a XML Renderer with the default (platform dependent) encoding. */ public XMLRenderer() { - this(System.getProperty("file.encoding")); + this(null); } /** * Creates a XML Renderer with a specific output encoding. - * @param encoding the encoding to use + * @param encoding the encoding to use or null. If null, default (platform dependent) encoding is used. */ public XMLRenderer(String encoding) { - this.encoding = encoding; + if (encoding != null) { + this.encoding = encoding; + } else { + this.encoding = System.getProperty("file.encoding"); + } } private Document createDocument() { diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java index f391cb4c81..0dff26e0d6 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java @@ -9,9 +9,8 @@ import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.regex.Pattern; -import junit.framework.Assert; - import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -105,6 +104,16 @@ public class CPDCommandLineInterfaceTest { Assert.assertTrue(out.contains("Found a 5 line (13 tokens) duplication")); } + @Test + public void testFormatXmlWithoutEncoding() throws Exception { + runCPD("--minimum-tokens", "10", + "--language", "java", + "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/", + "--format", "xml"); + String out = bufferStdout.toString("UTF-8"); + Assert.assertTrue(out.contains("")); + } + private void runCPD(String... args) { System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true"); CPD.main(args); diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index dcc6a6448e..8697790908 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -81,6 +81,7 @@ and will be removed with the next release of PMD. * [#1316](https://sourceforge.net/p/pmd/bugs/1316/): Multi Rule Properties with delimiter not possible * [#1317](https://sourceforge.net/p/pmd/bugs/1317/): RuntimeException when parsing class with multiple lambdas * [#1319](https://sourceforge.net/p/pmd/bugs/1319/): PMD stops with NoClassDefFoundError (typeresolution) +* [#1321](https://sourceforge.net/p/pmd/bugs/1321/): CPD format XML fails with NullPointer **API Changes:**