GeezerVison is showing github.com — Cleanup tests and drop dependency to plexus-utils · codehaus-plexus/plexus-xml@bf03664
File treeExpand file treeCollapse file tree Open diff view settings src/test/java/org/codehaus/plexus/util/xml
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pom.xml +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,6 +49,7 @@ limitations under the License. | 49 | 49 |
| 50 | 50 | <properties> | 51 | 51 | <javaVersion>17</javaVersion> | | 52 | + <jmhVersion>1.37</jmhVersion> | 52 | 53 | <project.build.outputTimestamp>2024-05-21T21:02:17Z</project.build.outputTimestamp> | 53 | 54 | </properties> | 54 | 55 |
| @@ -67,24 +68,23 @@ limitations under the License. | 67 | 68 | <dependency> | 68 | 69 | <groupId>org.openjdk.jmh</groupId> | 69 | 70 | <artifactId>jmh-core</artifactId> | 70 | | - <version>1.37</version> | | 71 | + <version>${jmhVersion}</version> | 71 | 72 | <scope>test</scope> | 72 | 73 | </dependency> | 73 | 74 | <dependency> | 74 | 75 | <groupId>org.openjdk.jmh</groupId> | 75 | 76 | <artifactId>jmh-generator-annprocess</artifactId> | 76 | | - <version>1.37</version> | | 77 | + <version>${jmhVersion}</version> | 77 | 78 | <scope>test</scope> | 78 | 79 | </dependency> | 79 | 80 | <dependency> | 80 | 81 | <groupId>org.junit.jupiter</groupId> | 81 | | - <artifactId>junit-jupiter</artifactId> | | 82 | + <artifactId>junit-jupiter-api</artifactId> | 82 | 83 | <scope>test</scope> | 83 | 84 | </dependency> | 84 | 85 | <dependency> | 85 | | - <groupId>org.codehaus.plexus</groupId> | 86 | | - <artifactId>plexus-utils</artifactId> | 87 | | - <version>4.0.2</version> | | 86 | + <groupId>org.junit.jupiter</groupId> | | 87 | + <artifactId>junit-jupiter-params</artifactId> | 88 | 88 | <scope>test</scope> | 89 | 89 | </dependency> | 90 | 90 | </dependencies> | |
Collapse file Copy file name to clipboardExpand all lines: src/test/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriterTest.java +25-31Lines changed: 25 additions & 31 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 26 | 26 | import java.nio.file.Files; | 27 | 27 | import java.util.NoSuchElementException; | 28 | 28 |
| 29 | | -import org.codehaus.plexus.util.StringUtils; | 30 | 29 | import org.junit.jupiter.api.AfterEach; | 31 | 30 | import org.junit.jupiter.api.BeforeEach; | 32 | 31 | import org.junit.jupiter.api.Disabled; | @@ -165,7 +164,7 @@ void testendElementAlreadyClosed() { | 165 | 164 | } | 166 | 165 |
| 167 | 166 | /** | 168 | | - * Issue #51: https://github.com/codehaus-plexus/plexus-utils/issues/51 Purpose: test if concatenation string | | 167 | + * Issue #51: <a href="https://github.com/codehaus-plexus/plexus-utils/issues/51">Issue 51</a> Purpose: test if concatenation string | 169 | 168 | * optimization bug is present. Target environment: Java 7 (u79 and u80 verified) running on Windows. Detection | 170 | 169 | * strategy: Tries to build a big XML file (~750MB size) and with many nested tags to force the JVM to trigger the | 171 | 170 | * concatenation string optimization bug that throws a NoSuchElementException when calling endElement() method. | @@ -232,34 +231,29 @@ private String expectedResult(String lineSeparator) { | 232 | 231 | } | 233 | 232 |
| 234 | 233 | private String expectedResult(String lineIndenter, String lineSeparator) { | 235 | | - StringBuilder expected = new StringBuilder(); | 236 | | - | 237 | | - expected.append("<html>").append(lineSeparator); | 238 | | - expected.append(StringUtils.repeat(lineIndenter, 1)).append("<head>").append(lineSeparator); | 239 | | - expected.append(StringUtils.repeat(lineIndenter, 2)) | 240 | | - .append("<title>title</title>") | 241 | | - .append(lineSeparator); | 242 | | - expected.append(StringUtils.repeat(lineIndenter, 2)) | 243 | | - .append("<meta name=\"author\" content=\"Author\"/>") | 244 | | - .append(lineSeparator); | 245 | | - expected.append(StringUtils.repeat(lineIndenter, 2)) | 246 | | - .append("<meta name=\"date\" content=\"Date\"/>") | 247 | | - .append(lineSeparator); | 248 | | - expected.append(StringUtils.repeat(lineIndenter, 1)).append("</head>").append(lineSeparator); | 249 | | - expected.append(StringUtils.repeat(lineIndenter, 1)).append("<body>").append(lineSeparator); | 250 | | - expected.append(StringUtils.repeat(lineIndenter, 2)) | 251 | | - .append("<p>Paragraph 1, line 1. Paragraph 1, line 2.</p>") | 252 | | - .append(lineSeparator); | 253 | | - expected.append(StringUtils.repeat(lineIndenter, 2)) | 254 | | - .append("<div class=\"section\">") | 255 | | - .append(lineSeparator); | 256 | | - expected.append(StringUtils.repeat(lineIndenter, 3)) | 257 | | - .append("<h2>Section title</h2>") | 258 | | - .append(lineSeparator); | 259 | | - expected.append(StringUtils.repeat(lineIndenter, 2)).append("</div>").append(lineSeparator); | 260 | | - expected.append(StringUtils.repeat(lineIndenter, 1)).append("</body>").append(lineSeparator); | 261 | | - expected.append("</html>"); | 262 | | - | 263 | | - return expected.toString(); | | 234 | + return "<html>" + lineSeparator + lineIndenter | | 235 | + + "<head>" + lineSeparator + lineIndenter | | 236 | + + lineIndenter + "<title>title</title>" | | 237 | + + lineSeparator | | 238 | + + lineIndenter | | 239 | + + lineIndenter + "<meta name=\"author\" content=\"Author\"/>" | | 240 | + + lineSeparator | | 241 | + + lineIndenter | | 242 | + + lineIndenter + "<meta name=\"date\" content=\"Date\"/>" | | 243 | + + lineSeparator | | 244 | + + lineIndenter | | 245 | + + "</head>" + lineSeparator + lineIndenter | | 246 | + + "<body>" + lineSeparator + lineIndenter | | 247 | + + lineIndenter + "<p>Paragraph 1, line 1. Paragraph 1, line 2.</p>" | | 248 | + + lineSeparator | | 249 | + + lineIndenter | | 250 | + + lineIndenter + "<div class=\"section\">" | | 251 | + + lineSeparator | | 252 | + + lineIndenter | | 253 | + + lineIndenter + lineIndenter + "<h2>Section title</h2>" | | 254 | + + lineSeparator | | 255 | + + lineIndenter | | 256 | + + lineIndenter + "</div>" + lineSeparator + lineIndenter | | 257 | + + "</body>" + lineSeparator + "</html>"; | 264 | 258 | } | 265 | 259 | } |
Collapse file Copy file name to clipboard +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package org.codehaus.plexus.util.xml; | | 2 | + | | 3 | +import java.io.IOException; | | 4 | +import java.io.Reader; | | 5 | +import java.io.StringWriter; | | 6 | + | | 7 | +public class TestUtils { | | 8 | + | | 9 | + public static String readAllFrom(Reader input) throws IOException { | | 10 | + StringWriter output = new StringWriter(); | | 11 | + char[] buffer = new char[16384]; | | 12 | + int n = 0; | | 13 | + while (0 <= (n = input.read(buffer))) { | | 14 | + output.write(buffer, 0, n); | | 15 | + } | | 16 | + output.flush(); | | 17 | + return output.toString(); | | 18 | + } | | 19 | + /** | | 20 | + * <p> | | 21 | + * How many times is the substring in the larger String. | | 22 | + * </p> | | 23 | + * <p> | | 24 | + * <code>null</code> returns <code>0</code>. | | 25 | + * </p> | | 26 | + * | | 27 | + * @param str the String to check | | 28 | + * @param sub the substring to count | | 29 | + * @return the number of occurrences, 0 if the String is <code>null</code> | | 30 | + * @throws NullPointerException if sub is <code>null</code> | | 31 | + */ | | 32 | + public static int countMatches(String str, String sub) { | | 33 | + if (sub.isEmpty()) { | | 34 | + return 0; | | 35 | + } | | 36 | + if (str == null) { | | 37 | + return 0; | | 38 | + } | | 39 | + int count = 0; | | 40 | + int idx = 0; | | 41 | + while ((idx = str.indexOf(sub, idx)) != -1) { | | 42 | + count++; | | 43 | + idx += sub.length(); | | 44 | + } | | 45 | + return count; | | 46 | + } | | 47 | +} |
Collapse file Copy file name to clipboardExpand all lines: src/test/java/org/codehaus/plexus/util/xml/XmlStreamReaderTest.java +4-7Lines changed: 4 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | import java.io.InputStream; | 22 | 22 | import java.io.SequenceInputStream; | 23 | 23 |
| 24 | | -import org.codehaus.plexus.util.IOUtil; | 25 | 24 | import org.junit.jupiter.api.Test; | 26 | 25 | import org.opentest4j.AssertionFailedError; | 27 | 26 |
| | 27 | +import static org.codehaus.plexus.util.xml.TestUtils.readAllFrom; | 28 | 28 | import static org.junit.jupiter.api.Assertions.assertEquals; | 29 | 29 | import static org.junit.jupiter.api.Assertions.assertThrows; | 30 | 30 |
| @@ -68,8 +68,7 @@ private static String createXmlContent(String text, String encoding) { | 68 | 68 | if (encoding != null) { | 69 | 69 | xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>"; | 70 | 70 | } | 71 | | - String xml = xmlDecl + "\n<text>" + text + "</text>"; | 72 | | - return xml; | | 71 | + return xmlDecl + "\n<text>" + text + "</text>"; | 73 | 72 | } | 74 | 73 |
| 75 | 74 | private static void checkXmlContent(String xml, String encoding) throws IOException { | @@ -86,8 +85,7 @@ private static void checkXmlContent(String xml, String encoding, byte... bom) th | 86 | 85 |
| 87 | 86 | XmlStreamReader reader = new XmlStreamReader(in); | 88 | 87 | assertEquals(encoding, reader.getEncoding()); | 89 | | - String result = IOUtil.toString(reader); | 90 | | - assertEquals(xml, result); | | 88 | + assertEquals(xml, readAllFrom(reader)); | 91 | 89 | } | 92 | 90 |
| 93 | 91 | private static void checkXmlStreamReader(String text, String encoding, String effectiveEncoding) | @@ -228,10 +226,9 @@ void ebcdicEncoding() throws IOException { | 228 | 226 | /** | 229 | 227 | * <p>testInappropriateEncoding.</p> | 230 | 228 | * | 231 | | - * @throws java.io.IOException if any. | 232 | 229 | */ | 233 | 230 | @Test | 234 | | - void inappropriateEncoding() throws IOException { | | 231 | + void inappropriateEncoding() { | 235 | 232 | // expected failure, since the encoding does not contain some characters | 236 | 233 | assertThrows( | 237 | 234 | AssertionFailedError.class, | |
Collapse file Copy file name to clipboardExpand all lines: src/test/java/org/codehaus/plexus/util/xml/XmlStreamWriterTest.java +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,8 +52,7 @@ private static String createXmlContent(String text, String encoding) { | 52 | 52 | if (encoding != null) { | 53 | 53 | xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>"; | 54 | 54 | } | 55 | | - String xml = xmlDecl + "\n<text>" + text + "</text>"; | 56 | | - return xml; | | 55 | + return xmlDecl + "\n<text>" + text + "</text>"; | 57 | 56 | } | 58 | 57 |
| 59 | 58 | private static void checkXmlContent(String xml, String encoding) throws IOException { | |
Collapse file Copy file name to clipboardExpand all lines: src/test/java/org/codehaus/plexus/util/xml/XmlUtilTest.java +7-6Lines changed: 7 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 25 | 25 | import java.io.Writer; | 26 | 26 | import java.nio.file.Files; | 27 | 27 |
| 28 | | -import org.codehaus.plexus.util.IOUtil; | 29 | | -import org.codehaus.plexus.util.StringUtils; | 30 | 28 | import org.junit.jupiter.api.Test; | 31 | 29 |
| | 30 | +import static org.codehaus.plexus.util.xml.TestUtils.readAllFrom; | 32 | 31 | import static org.junit.jupiter.api.Assertions.assertNotNull; | 33 | 32 | import static org.junit.jupiter.api.Assertions.assertTrue; | 34 | 33 |
| @@ -116,18 +115,20 @@ void prettyFormatString() throws Exception { | 116 | 115 |
| 117 | 116 | String content; | 118 | 117 | try (Reader reader = new XmlStreamReader(testDocument)) { | 119 | | - content = IOUtil.toString(reader); | | 118 | + content = readAllFrom(reader); | 120 | 119 | } | 121 | 120 |
| 122 | | - Writer writer = new StringWriter(); | | 121 | + String contentPretty; | 123 | 122 | try (Reader reader = new XmlStreamReader(testDocument)) { | | 123 | + Writer writer = new StringWriter(); | 124 | 124 | XmlUtil.prettyFormat(reader, writer); | | 125 | + contentPretty = writer.toString(); | 125 | 126 | } | 126 | 127 |
| 127 | 128 | assertNotNull(content); | 128 | 129 |
| 129 | | - int countEOL = StringUtils.countMatches(content, XmlUtil.DEFAULT_LINE_SEPARATOR); | 130 | | - assertTrue(countEOL < StringUtils.countMatches(writer.toString(), XmlUtil.DEFAULT_LINE_SEPARATOR)); | | 130 | + int countEOL = TestUtils.countMatches(content, XmlUtil.DEFAULT_LINE_SEPARATOR); | | 131 | + assertTrue(countEOL < TestUtils.countMatches(contentPretty, XmlUtil.DEFAULT_LINE_SEPARATOR)); | 131 | 132 | } | 132 | 133 |
| 133 | 134 | /** | |
Collapse file Copy file name to clipboardExpand all lines: src/test/java/org/codehaus/plexus/util/xml/XmlWriterUtilTest.java +12-16Lines changed: 12 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | import java.io.OutputStream; | 21 | 21 | import java.io.Writer; | 22 | 22 |
| 23 | | -import org.codehaus.plexus.util.StringUtils; | 24 | 23 | import org.junit.jupiter.api.AfterEach; | 25 | 24 | import org.junit.jupiter.api.BeforeEach; | 26 | 25 | import org.junit.jupiter.api.Test; | @@ -76,7 +75,7 @@ void tearDown() throws Exception { | 76 | 75 | void writeLineBreakXMLWriter() throws Exception { | 77 | 76 | XmlWriterUtil.writeLineBreak(xmlWriter); | 78 | 77 | writer.close(); | 79 | | - assertEquals(1, StringUtils.countMatches(output.toString(), XmlWriterUtil.LS)); | | 78 | + assertEquals(1, TestUtils.countMatches(output.toString(), XmlWriterUtil.LS)); | 80 | 79 | } | 81 | 80 |
| 82 | 81 | /** | @@ -89,7 +88,7 @@ void writeLineBreakXMLWriter() throws Exception { | 89 | 88 | void writeLineBreakXMLWriterInt() throws Exception { | 90 | 89 | XmlWriterUtil.writeLineBreak(xmlWriter, 10); | 91 | 90 | writer.close(); | 92 | | - assertEquals(10, StringUtils.countMatches(output.toString(), XmlWriterUtil.LS)); | | 91 | + assertEquals(10, TestUtils.countMatches(output.toString(), XmlWriterUtil.LS)); | 93 | 92 | } | 94 | 93 |
| 95 | 94 | /** | @@ -102,11 +101,8 @@ void writeLineBreakXMLWriterInt() throws Exception { | 102 | 101 | void writeLineBreakXMLWriterIntInt() throws Exception { | 103 | 102 | XmlWriterUtil.writeLineBreak(xmlWriter, 10, 2); | 104 | 103 | writer.close(); | 105 | | - assertEquals(10, StringUtils.countMatches(output.toString(), XmlWriterUtil.LS)); | 106 | | - assertEquals( | 107 | | - 1, | 108 | | - StringUtils.countMatches( | 109 | | - output.toString(), StringUtils.repeat(" ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE))); | | 104 | + assertEquals(10, TestUtils.countMatches(output.toString(), XmlWriterUtil.LS)); | | 105 | + assertEquals(1, TestUtils.countMatches(output.toString(), " ")); // | 110 | 106 | } | 111 | 107 |
| 112 | 108 | /** | @@ -119,8 +115,8 @@ void writeLineBreakXMLWriterIntInt() throws Exception { | 119 | 115 | void writeLineBreakXMLWriterIntIntInt() throws Exception { | 120 | 116 | XmlWriterUtil.writeLineBreak(xmlWriter, 10, 2, 4); | 121 | 117 | writer.close(); | 122 | | - assertEquals(10, StringUtils.countMatches(output.toString(), XmlWriterUtil.LS)); | 123 | | - assertEquals(1, StringUtils.countMatches(output.toString(), StringUtils.repeat(" ", 2 * 4))); | | 118 | + assertEquals(10, TestUtils.countMatches(output.toString(), XmlWriterUtil.LS)); | | 119 | + assertEquals(1, TestUtils.countMatches(output.toString(), " ")); | 124 | 120 | } | 125 | 121 |
| 126 | 122 | /** | @@ -211,7 +207,7 @@ void writeCommentXMLWriterString() throws Exception { | 211 | 207 | */ | 212 | 208 | @Test | 213 | 209 | void writeCommentXMLWriterStringInt() throws Exception { | 214 | | - String indent = StringUtils.repeat(" ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE); | | 210 | + String indent = " "; | 215 | 211 |
| 216 | 212 | XmlWriterUtil.writeComment(xmlWriter, "hello", 2); | 217 | 213 | writer.close(); | @@ -253,7 +249,7 @@ void writeCommentXMLWriterStringInt() throws Exception { | 253 | 249 | */ | 254 | 250 | @Test | 255 | 251 | void writeCommentXMLWriterStringIntInt() throws Exception { | 256 | | - String repeat = StringUtils.repeat(" ", 2 * 4); | | 252 | + String repeat = " "; | 257 | 253 |
| 258 | 254 | XmlWriterUtil.writeComment(xmlWriter, "hello", 2, 4); | 259 | 255 | writer.close(); | @@ -291,7 +287,7 @@ void writeCommentXMLWriterStringIntInt() throws Exception { | 291 | 287 | */ | 292 | 288 | @Test | 293 | 289 | void writeCommentXMLWriterStringIntIntInt() throws Exception { | 294 | | - String indent = StringUtils.repeat(" ", 2 * 4); | | 290 | + String indent = " "; | 295 | 291 |
| 296 | 292 | XmlWriterUtil.writeComment(xmlWriter, "hello", 2, 4, 50); | 297 | 293 | writer.close(); | @@ -339,7 +335,7 @@ void writeCommentTextXMLWriterStringInt() throws Exception { | 339 | 335 | tearDown(); | 340 | 336 | setUp(); | 341 | 337 |
| 342 | | - String indent = StringUtils.repeat(" ", 2 * 2); | | 338 | + String indent = " "; | 343 | 339 |
| 344 | 340 | XmlWriterUtil.writeCommentText( | 345 | 341 | xmlWriter, | @@ -380,7 +376,7 @@ void writeCommentTextXMLWriterStringInt() throws Exception { | 380 | 376 | */ | 381 | 377 | @Test | 382 | 378 | void writeCommentTextXMLWriterStringIntInt() throws Exception { | 383 | | - String indent = StringUtils.repeat(" ", 2 * 4); | | 379 | + String indent = " "; | 384 | 380 |
| 385 | 381 | XmlWriterUtil.writeCommentText(xmlWriter, "hello", 2, 4); | 386 | 382 | writer.close(); | @@ -411,7 +407,7 @@ void writeCommentTextXMLWriterStringIntInt() throws Exception { | 411 | 407 | */ | 412 | 408 | @Test | 413 | 409 | void writeCommentTextXMLWriterStringIntIntInt() throws Exception { | 414 | | - String indent = StringUtils.repeat(" ", 2 * 4); | | 410 | + String indent = " "; | 415 | 411 |
| 416 | 412 | XmlWriterUtil.writeCommentText(xmlWriter, "hello", 2, 4, 50); | 417 | 413 | writer.close(); | |
Collapse file Copy file name to clipboardExpand all lines: src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java +6-13Lines changed: 6 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -194,18 +194,11 @@ void shouldNotMergeValues() { | 194 | 194 | @Test | 195 | 195 | void nullAttributeNameOrValue() { | 196 | 196 | Xpp3Dom t1 = new Xpp3Dom("top"); | 197 | | - try { | 198 | | - t1.setAttribute("attr", null); | 199 | | - fail("null attribute values shouldn't be allowed"); | 200 | | - } catch (NullPointerException e) { | 201 | | - } | 202 | | - t1.toString(); | 203 | | - try { | 204 | | - t1.setAttribute(null, "value"); | 205 | | - fail("null attribute names shouldn't be allowed"); | 206 | | - } catch (NullPointerException e) { | 207 | | - } | 208 | | - t1.toString(); | | 197 | + assertThrows(NullPointerException.class, () -> t1.setAttribute("attr", null)); | | 198 | + assertNotNull(t1.toString()); | | 199 | + | | 200 | + assertThrows(NullPointerException.class, () -> t1.setAttribute(null, "value")); | | 201 | + assertNotNull(t1.toString()); | 209 | 202 | } | 210 | 203 |
| 211 | 204 | /** | @@ -217,7 +210,7 @@ void equals() { | 217 | 210 |
| 218 | 211 | assertEquals(dom, dom); | 219 | 212 | assertNotEquals(null, dom); | 220 | | - assertNotEquals(dom, new Xpp3Dom("")); | | 213 | + assertNotEquals(new Xpp3Dom(""), dom); | 221 | 214 | } | 222 | 215 |
| 223 | 216 | /** | |
|