From 556fe50302c510b5f73fe3c203551010e26fca66 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Tue, 17 Nov 2026 00:36:05 +0000 Subject: [PATCH 1/3] Use System.lineSeparator(), @Override and try-with-resources --- .../java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriter.java | 2 +- src/main/java/org/codehaus/plexus/util/xml/StringUtils.java | 2 +- src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java | 2 +- src/main/java/org/codehaus/plexus/util/xml/XmlWriterUtil.java | 2 +- .../java/org/codehaus/plexus/util/xml/Xpp3DomBuilderTest.java | 1 + src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java | 1 + .../java/org/codehaus/plexus/util/xml/Xpp3DomWriterTest.java | 2 +- 7 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriter.java b/src/main/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriter.java index cd35289d..a45d9ee4 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriter.java +++ b/src/main/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriter.java @@ -30,7 +30,7 @@ */ public class PrettyPrintXMLWriter implements XMLWriter { /** Line separator ("\n" on UNIX) */ - protected static final String LS = System.getProperty("line.separator"); + protected static final String LS = System.lineSeparator(); private PrintWriter writer; diff --git a/src/main/java/org/codehaus/plexus/util/xml/StringUtils.java b/src/main/java/org/codehaus/plexus/util/xml/StringUtils.java index 1b382644..5501f0d7 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/StringUtils.java +++ b/src/main/java/org/codehaus/plexus/util/xml/StringUtils.java @@ -227,7 +227,7 @@ public static String unifyLineSeparators(String s, String ls) { } if (ls == null) { - ls = System.getProperty("line.separator"); + ls = System.lineSeparator(); } if (!("\n".equals(ls) || "\r".equals(ls) || "\r\n".equals(ls))) { diff --git a/src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java b/src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java index 148406a0..feaad3a7 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java +++ b/src/main/java/org/codehaus/plexus/util/xml/XmlUtil.java @@ -40,7 +40,7 @@ public class XmlUtil { public static final int DEFAULT_INDENTATION_SIZE = 2; /** The default line separator ("\n" on UNIX) */ - public static final String DEFAULT_LINE_SEPARATOR = System.getProperty("line.separator"); + public static final String DEFAULT_LINE_SEPARATOR = System.lineSeparator(); /** * Determines if a given File shall be handled as XML. diff --git a/src/main/java/org/codehaus/plexus/util/xml/XmlWriterUtil.java b/src/main/java/org/codehaus/plexus/util/xml/XmlWriterUtil.java index 01870e51..880f3442 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/XmlWriterUtil.java +++ b/src/main/java/org/codehaus/plexus/util/xml/XmlWriterUtil.java @@ -24,7 +24,7 @@ */ public class XmlWriterUtil { /** The vm line separator */ - public static final String LS = System.getProperty("line.separator"); + public static final String LS = System.lineSeparator(); /** The default line indenter size i.e. 2. */ public static final int DEFAULT_INDENTATION_SIZE = 2; diff --git a/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomBuilderTest.java b/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomBuilderTest.java index e7e281ec..c183de4d 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomBuilderTest.java +++ b/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomBuilderTest.java @@ -179,6 +179,7 @@ void escapingInAttributes() throws Exception { @Test void inputLocationTracking() throws Exception { Xpp3DomBuilder.InputLocationBuilder ilb = new Xpp3DomBuilder.InputLocationBuilder() { + @Override public Object toInputLocation(XmlPullParser parser) { return parser.getLineNumber(); // store only line number as a simple Integer } diff --git a/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java b/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java index ff5abd2b..73f1a3d4 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java +++ b/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java @@ -515,6 +515,7 @@ public FixedInputLocationBuilder(Object location) { this.location = location; } + @Override public Object toInputLocation(XmlPullParser parser) { return location; } diff --git a/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomWriterTest.java b/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomWriterTest.java index fc4cc279..cfea0d37 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomWriterTest.java +++ b/src/test/java/org/codehaus/plexus/util/xml/Xpp3DomWriterTest.java @@ -30,7 +30,7 @@ * @since 3.4.0 */ class Xpp3DomWriterTest { - private static final String LS = System.getProperty("line.separator"); + private static final String LS = System.lineSeparator(); /** *

testWriter.

From 5f625b30dd45d105f6a50741dac03ee0a25bb967 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Tue, 17 Nov 2026 00:36:55 +0000 Subject: [PATCH 2/3] Use JUnit 5 assertions and Mockito best practices in tests --- ...sts_Test_IBMXMLConformanceTestSuite_Production2_Test.java | 5 ++--- ...ts_Test_IBMXMLConformanceTestSuite_Production32_Test.java | 3 +-- ...eduni_misc_Test_BjoernHoehrmannviaHST2013_09_18_Test.java | 5 ++--- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production2_Test.java b/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production2_Test.java index e513a12b..eb722010 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production2_Test.java +++ b/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production2_Test.java @@ -4,7 +4,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileReader; -import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.Charset; @@ -636,7 +635,7 @@ void testibm_not_wf_P02_ibm02n29xml() throws Exception { */ @Test @Disabled("test file is malformed in the original suite") - public void testibm_not_wf_P02_ibm02n30xml() throws IOException { + public void testibm_not_wf_P02_ibm02n30xml() throws Exception { try (BufferedReader reader = Files.newBufferedReader( Paths.get(testResourcesDir.getCanonicalPath(), "not-wf/P02/ibm02n30.xml"), Charset.forName("ISO-8859-15"))) { @@ -661,7 +660,7 @@ public void testibm_not_wf_P02_ibm02n30xml() throws IOException { */ @Test @Disabled("test file is malformed in the original suite") - public void testibm_not_wf_P02_ibm02n31xml() throws IOException { + public void testibm_not_wf_P02_ibm02n31xml() throws Exception { try (FileInputStream is = new FileInputStream(new File(testResourcesDir, "not-wf/P02/ibm02n31.xml")); InputStreamReader reader = new InputStreamReader(is, "ISO-8859-15")) { parser.setInput(reader); diff --git a/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production32_Test.java b/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production32_Test.java index 878d52a9..3e9da4b2 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production32_Test.java +++ b/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production32_Test.java @@ -2,7 +2,6 @@ import java.io.File; import java.io.FileReader; -import java.io.IOException; import java.io.Reader; import org.junit.jupiter.api.BeforeEach; @@ -213,7 +212,7 @@ void testibm_not_wf_P32_ibm32n08xml() throws Exception { */ @Test @Disabled("MXParser does not support parsing inside DOCTYPEDECL") - public void testibm_not_wf_P32_ibm32n09xml() throws IOException { + public void testibm_not_wf_P32_ibm32n09xml() throws Exception { try (Reader reader = new FileReader(new File(testResourcesDir, "not-wf/P32/ibm32n09.xml"))) { parser.setInput(reader); while (parser.nextToken() != XmlPullParser.END_DOCUMENT) {} diff --git a/src/test/java/org/codehaus/plexus/util/xml/pull/eduni_misc_Test_BjoernHoehrmannviaHST2013_09_18_Test.java b/src/test/java/org/codehaus/plexus/util/xml/pull/eduni_misc_Test_BjoernHoehrmannviaHST2013_09_18_Test.java index fc26f501..eded924a 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/pull/eduni_misc_Test_BjoernHoehrmannviaHST2013_09_18_Test.java +++ b/src/test/java/org/codehaus/plexus/util/xml/pull/eduni_misc_Test_BjoernHoehrmannviaHST2013_09_18_Test.java @@ -3,7 +3,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileReader; -import java.io.IOException; import java.io.InputStream; import java.io.Reader; @@ -131,7 +130,7 @@ void testhst_bh_004() throws Exception { */ @Test @Disabled("MXParser does not support DOCDECL parsing") - public void testhst_bh_005() throws IOException { + public void testhst_bh_005() throws Exception { try (Reader reader = new FileReader(new File(testResourcesDir, "005.xml"))) { parser.setInput(reader); while (parser.nextToken() != XmlPullParser.END_DOCUMENT) {} @@ -153,7 +152,7 @@ public void testhst_bh_005() throws IOException { */ @Test @Disabled("MXParser does not support DOCDECL parsing") - public void testhst_bh_006() throws IOException { + public void testhst_bh_006() throws Exception { try (Reader reader = new FileReader(new File(testResourcesDir, "006.xml"))) { parser.setInput(reader); while (parser.nextToken() != XmlPullParser.END_DOCUMENT) {} From a015c82caa8679a004a5386275016d4f152566a2 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Tue, 17 Nov 2026 00:37:35 +0000 Subject: [PATCH 3/3] Use Java 17 pattern matching and String.formatted --- .../util/xml/PrettyPrintXMLWriterTest.java | 3 +-- ...XMLConformanceTestSuite_Production2_Test.java | 4 ++-- .../plexus/util/xml/pull/MXParserTest.java | 16 ++++++++-------- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/test/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriterTest.java b/src/test/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriterTest.java index 940ba37d..04df4d7e 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriterTest.java +++ b/src/test/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriterTest.java @@ -21,7 +21,6 @@ import java.io.File; import java.io.StringWriter; import java.io.Writer; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.NoSuchElementException; @@ -181,7 +180,7 @@ void issue51DetectJava7ConcatenationBug() throws Exception { int iterations = 20000; - try (Writer osw = Files.newBufferedWriter(xmlFile.toPath(), StandardCharsets.UTF_8)) { + try (Writer osw = Files.newBufferedWriter(xmlFile.toPath())) { writer = new PrettyPrintXMLWriter(osw); for (int i = 0; i < iterations; ++i) { writer.startElement(Tag.DIV.toString() + i); diff --git a/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production2_Test.java b/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production2_Test.java index eb722010..bfa89ad8 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production2_Test.java +++ b/src/test/java/org/codehaus/plexus/util/xml/pull/IBMXML10Tests_Test_IBMXMLConformanceTestSuite_not_wftests_Test_IBMXMLConformanceTestSuite_Production2_Test.java @@ -9,7 +9,7 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; @@ -637,7 +637,7 @@ void testibm_not_wf_P02_ibm02n29xml() throws Exception { @Disabled("test file is malformed in the original suite") public void testibm_not_wf_P02_ibm02n30xml() throws Exception { try (BufferedReader reader = Files.newBufferedReader( - Paths.get(testResourcesDir.getCanonicalPath(), "not-wf/P02/ibm02n30.xml"), + Path.of(testResourcesDir.getCanonicalPath(), "not-wf/P02/ibm02n30.xml"), Charset.forName("ISO-8859-15"))) { parser.setInput(reader); while (parser.nextToken() != XmlPullParser.END_DOCUMENT) {} diff --git a/src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java b/src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java index 520081a0..eb057851 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java +++ b/src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java @@ -25,7 +25,7 @@ import java.io.StringReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Paths; +import java.nio.file.Path; import org.codehaus.plexus.util.xml.XmlStreamReader; import org.junit.jupiter.api.Test; @@ -880,7 +880,7 @@ void xmlDeclVersionEncodingStandaloneNoSpace() throws Exception { */ @Test void encodingISO88591NewXmlReader() throws Exception { - try (Reader reader = new XmlStreamReader(Paths.get("src/test/resources/xml", "test-encoding-ISO-8859-1.xml"))) { + try (Reader reader = new XmlStreamReader(Path.of("src/test/resources/xml", "test-encoding-ISO-8859-1.xml"))) { MXParser parser = new MXParser(); parser.setInput(reader); while (parser.nextToken() != XmlPullParser.END_DOCUMENT) {} @@ -899,7 +899,7 @@ void encodingISO88591NewXmlReader() throws Exception { @Test void encodingISO88591InputStream() throws Exception { try (InputStream input = - Files.newInputStream(Paths.get("src/test/resources/xml", "test-encoding-ISO-8859-1.xml"))) { + Files.newInputStream(Path.of("src/test/resources/xml", "test-encoding-ISO-8859-1.xml"))) { MXParser parser = new MXParser(); parser.setInput(input, null); while (parser.nextToken() != XmlPullParser.END_DOCUMENT) {} @@ -920,7 +920,7 @@ void encodingISO88591InputStream() throws Exception { @Test void encodingISO88591StringReader() throws Exception { String xmlFileContents; - try (Reader reader = new XmlStreamReader(Paths.get("src/test/resources/xml", "test-encoding-ISO-8859-1.xml"))) { + try (Reader reader = new XmlStreamReader(Path.of("src/test/resources/xml", "test-encoding-ISO-8859-1.xml"))) { xmlFileContents = readAllFrom(reader); } @@ -948,7 +948,7 @@ void encodingISO88591NewReader() throws Exception { // because the decoder created by new InputStreamReader() is lenient while the one created by // Files.newBufferedReader() is not. try (Reader reader = new InputStreamReader( - Files.newInputStream(Paths.get("src/test/resources/xml", "test-encoding-ISO-8859-1.xml")), + Files.newInputStream(Path.of("src/test/resources/xml", "test-encoding-ISO-8859-1.xml")), StandardCharsets.UTF_8)) { MXParser parser = new MXParser(); parser.setInput(reader); @@ -970,7 +970,7 @@ void encodingISO88591NewReader() throws Exception { @Test void encodingISO88591InputStreamEncoded() throws Exception { try (InputStream input = - Files.newInputStream(Paths.get("src/test/resources/xml", "test-encoding-ISO-8859-1.xml"))) { + Files.newInputStream(Path.of("src/test/resources/xml", "test-encoding-ISO-8859-1.xml"))) { MXParser parser = new MXParser(); parser.setInput(input, StandardCharsets.UTF_8.name()); while (parser.nextToken() != XmlPullParser.END_DOCUMENT) {} @@ -988,7 +988,7 @@ void encodingISO88591InputStreamEncoded() throws Exception { */ @Test void encodingUTF8NewXmlReader() throws Exception { - try (Reader reader = new XmlStreamReader(Paths.get("src/test/resources/xml", "test-encoding-ISO-8859-1.xml"))) { + try (Reader reader = new XmlStreamReader(Path.of("src/test/resources/xml", "test-encoding-ISO-8859-1.xml"))) { MXParser parser = new MXParser(); parser.setInput(reader); while (parser.nextToken() != XmlPullParser.END_DOCUMENT) {} @@ -1215,7 +1215,7 @@ void docdeclTextWithEntitiesInAttributesDOS() throws Exception { } private void testDocdeclTextWithEntitiesInAttributes(String filename) throws IOException { - try (InputStream input = Files.newInputStream(Paths.get("src/test/resources/xml", filename))) { + try (InputStream input = Files.newInputStream(Path.of("src/test/resources/xml", filename))) { MXParser parser = new MXParser(); parser.setInput(input, null); parser.defineEntityReplacementText("nbsp", " ");