Skip to content

Cleanup tests and drop dependency to plexus-utils #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,26 @@ limitations under the License.
</distributionManagement>

<properties>
<jmhVersion>1.37</jmhVersion>
<project.build.outputTimestamp>2024-05-21T21:12:10Z</project.build.outputTimestamp>
</properties>

<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.37</version>
<version>${jmhVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.37</version>
<version>${jmhVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.2</version>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.NoSuchElementException;

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;
Expand All @@ -42,7 +43,7 @@
* @version $Id: $Id
* @since 3.4.0
*/
public class PrettyPrintXMLWriterTest {
class PrettyPrintXMLWriterTest {
StringWriter w;

PrettyPrintXMLWriter writer;
Expand All @@ -51,15 +52,15 @@ public class PrettyPrintXMLWriterTest {
* <p>setUp.</p>
*/
@BeforeEach
public void setUp() {
void setUp() {
initWriter();
}

/**
* <p>tearDown.</p>
*/
@AfterEach
public void tearDown() {
void tearDown() {
writer = null;
w = null;
}
Expand All @@ -73,7 +74,7 @@ private void initWriter() {
* <p>testDefaultPrettyPrintXMLWriter.</p>
*/
@Test
public void testDefaultPrettyPrintXMLWriter() {
void defaultPrettyPrintXMLWriter() {
writer.startElement(Tag.HTML.toString());

writeXhtmlHead(writer);
Expand All @@ -89,7 +90,7 @@ public void testDefaultPrettyPrintXMLWriter() {
* <p>testPrettyPrintXMLWriterWithGivenLineSeparator.</p>
*/
@Test
public void testPrettyPrintXMLWriterWithGivenLineSeparator() {
void prettyPrintXMLWriterWithGivenLineSeparator() {
writer.setLineSeparator("\n");

writer.startElement(Tag.HTML.toString());
Expand All @@ -107,7 +108,7 @@ public void testPrettyPrintXMLWriterWithGivenLineSeparator() {
* <p>testPrettyPrintXMLWriterWithGivenLineIndenter.</p>
*/
@Test
public void testPrettyPrintXMLWriterWithGivenLineIndenter() {
void prettyPrintXMLWriterWithGivenLineIndenter() {
writer.setLineIndenter(" ");

writer.startElement(Tag.HTML.toString());
Expand All @@ -125,7 +126,7 @@ public void testPrettyPrintXMLWriterWithGivenLineIndenter() {
* <p>testEscapeXmlAttribute.</p>
*/
@Test
public void testEscapeXmlAttribute() {
void escapeXmlAttribute() {
// Windows
writer.startElement(Tag.DIV.toString());
writer.addAttribute("class", "sect\r\nion");
Expand All @@ -151,7 +152,7 @@ public void testEscapeXmlAttribute() {
* <p>testendElementAlreadyClosed.</p>
*/
@Test
public void testendElementAlreadyClosed() {
void testendElementAlreadyClosed() {
try {
writer.startElement(Tag.DIV.toString());
writer.addAttribute("class", "someattribute");
Expand All @@ -164,26 +165,26 @@ public void testendElementAlreadyClosed() {
}

/**
* Issue #51: https://github.com/codehaus-plexus/plexus-utils/issues/51 Purpose: test if concatenation string
* Issue #51: <a href="https://github.com/codehaus-plexus/plexus-utils/issues/51">...</a> Purpose: test if concatenation string
* optimization bug is present. Target environment: Java 7 (u79 and u80 verified) running on Windows. Detection
* strategy: Tries to build a big XML file (~750MB size) and with many nested tags to force the JVM to trigger the
* concatenation string optimization bug that throws a NoSuchElementException when calling endElement() method.
*
* @throws java.io.IOException if an I/O error occurs
* @throws 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 {
void issue51DetectJava7ConcatenationBug() throws IOException {
File dir = new File("target/test-xml");
if (!dir.exists()) {
assertTrue(dir.mkdir(), "cannot create directory test-xml");
}
File xmlFile = new File(dir, "test-issue-51.xml");
OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(xmlFile.toPath()), "UTF-8");
writer = new PrettyPrintXMLWriter(osw);

int iterations = 20000;

try {
try (OutputStreamWriter osw =
new OutputStreamWriter(Files.newOutputStream(xmlFile.toPath()), StandardCharsets.UTF_8)) {
writer = new PrettyPrintXMLWriter(osw);
for (int i = 0; i < iterations; ++i) {
writer.startElement(Tag.DIV.toString() + i);
writer.addAttribute("class", "someattribute");
Expand All @@ -193,10 +194,6 @@ public void testIssue51DetectJava7ConcatenationBug() throws IOException {
}
} catch (NoSuchElementException e) {
fail("Should not throw a NoSuchElementException");
} finally {
if (osw != null) {
osw.close();
}
}
}

Expand Down Expand Up @@ -235,34 +232,29 @@ private String expectedResult(String lineSeparator) {
}

private String expectedResult(String lineIndenter, String lineSeparator) {
StringBuilder expected = new StringBuilder();

expected.append("<html>").append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 1)).append("<head>").append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 2))
.append("<title>title</title>")
.append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 2))
.append("<meta name=\"author\" content=\"Author\"/>")
.append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 2))
.append("<meta name=\"date\" content=\"Date\"/>")
.append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 1)).append("</head>").append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 1)).append("<body>").append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 2))
.append("<p>Paragraph 1, line 1. Paragraph 1, line 2.</p>")
.append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 2))
.append("<div class=\"section\">")
.append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 3))
.append("<h2>Section title</h2>")
.append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 2)).append("</div>").append(lineSeparator);
expected.append(StringUtils.repeat(lineIndenter, 1)).append("</body>").append(lineSeparator);
expected.append("</html>");

return expected.toString();
return "<html>" + lineSeparator + lineIndenter
+ "<head>" + lineSeparator + lineIndenter + lineIndenter
+ "<title>title</title>"
+ lineSeparator
+ lineIndenter + lineIndenter
+ "<meta name=\"author\" content=\"Author\"/>"
+ lineSeparator
+ lineIndenter + lineIndenter
+ "<meta name=\"date\" content=\"Date\"/>"
+ lineSeparator
+ lineIndenter
+ "</head>" + lineSeparator + lineIndenter
+ "<body>" + lineSeparator + lineIndenter + lineIndenter
+ "<p>Paragraph 1, line 1. Paragraph 1, line 2.</p>"
+ lineSeparator
+ lineIndenter + lineIndenter
+ "<div class=\"section\">"
+ lineSeparator
+ lineIndenter + lineIndenter + lineIndenter
+ "<h2>Section title</h2>"
+ lineSeparator
+ lineIndenter + lineIndenter
+ "</div>" + lineSeparator + lineIndenter
+ "</body>" + lineSeparator + "</html>";
}
}
47 changes: 47 additions & 0 deletions src/test/java/org/codehaus/plexus/util/xml/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.codehaus.plexus.util.xml;

import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;

public class TestUtils {

public static String readAllFrom(Reader input) throws IOException {
StringWriter output = new StringWriter();
char[] buffer = new char[16384];
int n = 0;
while (0 <= (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
output.flush();
return output.toString();
}
/**
* <p>
* How many times is the substring in the larger String.
* </p>
* <p>
* <code>null</code> returns <code>0</code>.
* </p>
*
* @param str the String to check
* @param sub the substring to count
* @return the number of occurrences, 0 if the String is <code>null</code>
* @throws NullPointerException if sub is <code>null</code>
*/
public static int countMatches(String str, String sub) {
if (sub.isEmpty()) {
return 0;
}
if (str == null) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
count++;
idx += sub.length();
}
return count;
}
}
Loading
Loading