Skip to content

Commit

Permalink
Cleanup tests and drop dependency to plexus-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Dec 16, 2024
1 parent 5a1f0a3 commit bf03664
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 99 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ limitations under the License.

<properties>
<javaVersion>17</javaVersion>
<jmhVersion>1.37</jmhVersion>
<project.build.outputTimestamp>2024-05-21T21:02:17Z</project.build.outputTimestamp>
</properties>

Expand All @@ -67,24 +68,23 @@ limitations under the License.
<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>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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;
Expand Down Expand Up @@ -165,7 +164,7 @@ 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">Issue 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.
Expand Down Expand Up @@ -232,34 +231,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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import java.io.InputStream;
import java.io.SequenceInputStream;

import org.codehaus.plexus.util.IOUtil;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import static org.codehaus.plexus.util.xml.TestUtils.readAllFrom;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down Expand Up @@ -68,8 +68,7 @@ private static String createXmlContent(String text, String encoding) {
if (encoding != null) {
xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>";
}
String xml = xmlDecl + "\n<text>" + text + "</text>";
return xml;
return xmlDecl + "\n<text>" + text + "</text>";
}

private static void checkXmlContent(String xml, String encoding) throws IOException {
Expand All @@ -86,8 +85,7 @@ private static void checkXmlContent(String xml, String encoding, byte... bom) th

XmlStreamReader reader = new XmlStreamReader(in);
assertEquals(encoding, reader.getEncoding());
String result = IOUtil.toString(reader);
assertEquals(xml, result);
assertEquals(xml, readAllFrom(reader));
}

private static void checkXmlStreamReader(String text, String encoding, String effectiveEncoding)
Expand Down Expand Up @@ -228,10 +226,9 @@ void ebcdicEncoding() throws IOException {
/**
* <p>testInappropriateEncoding.</p>
*
* @throws java.io.IOException if any.
*/
@Test
void inappropriateEncoding() throws IOException {
void inappropriateEncoding() {
// expected failure, since the encoding does not contain some characters
assertThrows(
AssertionFailedError.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ private static String createXmlContent(String text, String encoding) {
if (encoding != null) {
xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>";
}
String xml = xmlDecl + "\n<text>" + text + "</text>";
return xml;
return xmlDecl + "\n<text>" + text + "</text>";
}

private static void checkXmlContent(String xml, String encoding) throws IOException {
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/org/codehaus/plexus/util/xml/XmlUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
import java.io.Writer;
import java.nio.file.Files;

import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.junit.jupiter.api.Test;

import static org.codehaus.plexus.util.xml.TestUtils.readAllFrom;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -116,18 +115,20 @@ void prettyFormatString() throws Exception {

String content;
try (Reader reader = new XmlStreamReader(testDocument)) {
content = IOUtil.toString(reader);
content = readAllFrom(reader);
}

Writer writer = new StringWriter();
String contentPretty;
try (Reader reader = new XmlStreamReader(testDocument)) {
Writer writer = new StringWriter();
XmlUtil.prettyFormat(reader, writer);
contentPretty = writer.toString();
}

assertNotNull(content);

int countEOL = StringUtils.countMatches(content, XmlUtil.DEFAULT_LINE_SEPARATOR);
assertTrue(countEOL < StringUtils.countMatches(writer.toString(), XmlUtil.DEFAULT_LINE_SEPARATOR));
int countEOL = TestUtils.countMatches(content, XmlUtil.DEFAULT_LINE_SEPARATOR);
assertTrue(countEOL < TestUtils.countMatches(contentPretty, XmlUtil.DEFAULT_LINE_SEPARATOR));
}

/**
Expand Down
28 changes: 12 additions & 16 deletions src/test/java/org/codehaus/plexus/util/xml/XmlWriterUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.OutputStream;
import java.io.Writer;

import org.codehaus.plexus.util.StringUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -76,7 +75,7 @@ void tearDown() throws Exception {
void writeLineBreakXMLWriter() throws Exception {
XmlWriterUtil.writeLineBreak(xmlWriter);
writer.close();
assertEquals(1, StringUtils.countMatches(output.toString(), XmlWriterUtil.LS));
assertEquals(1, TestUtils.countMatches(output.toString(), XmlWriterUtil.LS));
}

/**
Expand All @@ -89,7 +88,7 @@ void writeLineBreakXMLWriter() throws Exception {
void writeLineBreakXMLWriterInt() throws Exception {
XmlWriterUtil.writeLineBreak(xmlWriter, 10);
writer.close();
assertEquals(10, StringUtils.countMatches(output.toString(), XmlWriterUtil.LS));
assertEquals(10, TestUtils.countMatches(output.toString(), XmlWriterUtil.LS));
}

/**
Expand All @@ -102,11 +101,8 @@ void writeLineBreakXMLWriterInt() throws Exception {
void writeLineBreakXMLWriterIntInt() throws Exception {
XmlWriterUtil.writeLineBreak(xmlWriter, 10, 2);
writer.close();
assertEquals(10, StringUtils.countMatches(output.toString(), XmlWriterUtil.LS));
assertEquals(
1,
StringUtils.countMatches(
output.toString(), StringUtils.repeat(" ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE)));
assertEquals(10, TestUtils.countMatches(output.toString(), XmlWriterUtil.LS));
assertEquals(1, TestUtils.countMatches(output.toString(), " ")); //
}

/**
Expand All @@ -119,8 +115,8 @@ void writeLineBreakXMLWriterIntInt() throws Exception {
void writeLineBreakXMLWriterIntIntInt() throws Exception {
XmlWriterUtil.writeLineBreak(xmlWriter, 10, 2, 4);
writer.close();
assertEquals(10, StringUtils.countMatches(output.toString(), XmlWriterUtil.LS));
assertEquals(1, StringUtils.countMatches(output.toString(), StringUtils.repeat(" ", 2 * 4)));
assertEquals(10, TestUtils.countMatches(output.toString(), XmlWriterUtil.LS));
assertEquals(1, TestUtils.countMatches(output.toString(), " "));
}

/**
Expand Down Expand Up @@ -211,7 +207,7 @@ void writeCommentXMLWriterString() throws Exception {
*/
@Test
void writeCommentXMLWriterStringInt() throws Exception {
String indent = StringUtils.repeat(" ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE);
String indent = " ";

XmlWriterUtil.writeComment(xmlWriter, "hello", 2);
writer.close();
Expand Down Expand Up @@ -253,7 +249,7 @@ void writeCommentXMLWriterStringInt() throws Exception {
*/
@Test
void writeCommentXMLWriterStringIntInt() throws Exception {
String repeat = StringUtils.repeat(" ", 2 * 4);
String repeat = " ";

XmlWriterUtil.writeComment(xmlWriter, "hello", 2, 4);
writer.close();
Expand Down Expand Up @@ -291,7 +287,7 @@ void writeCommentXMLWriterStringIntInt() throws Exception {
*/
@Test
void writeCommentXMLWriterStringIntIntInt() throws Exception {
String indent = StringUtils.repeat(" ", 2 * 4);
String indent = " ";

XmlWriterUtil.writeComment(xmlWriter, "hello", 2, 4, 50);
writer.close();
Expand Down Expand Up @@ -339,7 +335,7 @@ void writeCommentTextXMLWriterStringInt() throws Exception {
tearDown();
setUp();

String indent = StringUtils.repeat(" ", 2 * 2);
String indent = " ";

XmlWriterUtil.writeCommentText(
xmlWriter,
Expand Down Expand Up @@ -380,7 +376,7 @@ void writeCommentTextXMLWriterStringInt() throws Exception {
*/
@Test
void writeCommentTextXMLWriterStringIntInt() throws Exception {
String indent = StringUtils.repeat(" ", 2 * 4);
String indent = " ";

XmlWriterUtil.writeCommentText(xmlWriter, "hello", 2, 4);
writer.close();
Expand Down Expand Up @@ -411,7 +407,7 @@ void writeCommentTextXMLWriterStringIntInt() throws Exception {
*/
@Test
void writeCommentTextXMLWriterStringIntIntInt() throws Exception {
String indent = StringUtils.repeat(" ", 2 * 4);
String indent = " ";

XmlWriterUtil.writeCommentText(xmlWriter, "hello", 2, 4, 50);
writer.close();
Expand Down
19 changes: 6 additions & 13 deletions src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,11 @@ void shouldNotMergeValues() {
@Test
void nullAttributeNameOrValue() {
Xpp3Dom t1 = new Xpp3Dom("top");
try {
t1.setAttribute("attr", null);
fail("null attribute values shouldn't be allowed");
} catch (NullPointerException e) {
}
t1.toString();
try {
t1.setAttribute(null, "value");
fail("null attribute names shouldn't be allowed");
} catch (NullPointerException e) {
}
t1.toString();
assertThrows(NullPointerException.class, () -> t1.setAttribute("attr", null));
assertNotNull(t1.toString());

assertThrows(NullPointerException.class, () -> t1.setAttribute(null, "value"));
assertNotNull(t1.toString());
}

/**
Expand All @@ -217,7 +210,7 @@ void equals() {

assertEquals(dom, dom);
assertNotEquals(null, dom);
assertNotEquals(dom, new Xpp3Dom(""));
assertNotEquals(new Xpp3Dom(""), dom);
}

/**
Expand Down
Loading

0 comments on commit bf03664

Please sign in to comment.