From 8351c4ec5eb6bdeeccdb9bc49c03f167d6c64269 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 10 May 2023 13:43:46 +0200 Subject: [PATCH] Use a ArrayDeque and enable test only for JDK < 1.8 (#5) --- .../plexus/util/xml/PrettyPrintXMLWriter.java | 11 ++++------- .../plexus/util/xml/PrettyPrintXMLWriterTest.java | 7 +++++-- 2 files changed, 9 insertions(+), 9 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 8977534f..c01109c9 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriter.java +++ b/src/main/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriter.java @@ -18,6 +18,8 @@ import java.io.PrintWriter; import java.io.Writer; +import java.util.ArrayDeque; +import java.util.Deque; import java.util.LinkedList; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -35,7 +37,7 @@ public class PrettyPrintXMLWriter private PrintWriter writer; - private LinkedList elementStack = new LinkedList(); + private final Deque elementStack = new ArrayDeque<>(); private boolean tagInProgress; @@ -307,11 +309,6 @@ public void endElement() { finishTag(); - // see issue #51: https://github.com/codehaus-plexus/plexus-utils/issues/51 - // Rationale: replaced 1 write() with string concatenations with 3 write() - // (this avoids the string concatenation optimization bug detected in Java 7) - // TODO: change the below code to a more efficient expression when the library - // be ready to target Java 8. write( "" ); @@ -518,7 +515,7 @@ protected String getDocType() /** * @return the current elementStack; */ - protected LinkedList getElementStack() + protected Deque getElementStack() { return elementStack; } 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 af0bf253..bb004c73 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriterTest.java +++ b/src/test/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriterTest.java @@ -18,8 +18,9 @@ import java.io.File; import java.io.IOException; -import java.io.OutputStreamWriter; import java.io.StringWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.NoSuchElementException; @@ -28,6 +29,7 @@ import org.codehaus.plexus.util.StringUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -178,6 +180,7 @@ public void testendElementAlreadyClosed() * * @throws java.io.IOException if an I/O error occurs */ + @Disabled( "This test is only relevant on JDK 1.7, which is not supported anymore" ) @Test public void testIssue51DetectJava7ConcatenationBug() throws IOException @@ -191,7 +194,7 @@ public void testIssue51DetectJava7ConcatenationBug() int iterations = 20000; - try ( OutputStreamWriter osw = new OutputStreamWriter( Files.newOutputStream( xmlFile.toPath() ), "UTF-8" ) ) + try ( Writer osw = Files.newBufferedWriter( xmlFile.toPath(), StandardCharsets.UTF_8 ) ) { writer = new PrettyPrintXMLWriter( osw ); for ( int i = 0; i < iterations; ++i )