Skip to content

Commit d518e1a

Browse files
committed
Cleanup tests and drop dependency to plexus-utils
1 parent 134bc1c commit d518e1a

9 files changed

+118
-129
lines changed

Diff for: pom.xml

-6
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ limitations under the License.
7070
<artifactId>junit-jupiter-api</artifactId>
7171
<scope>test</scope>
7272
</dependency>
73-
<dependency>
74-
<groupId>org.codehaus.plexus</groupId>
75-
<artifactId>plexus-utils</artifactId>
76-
<version>4.0.2</version>
77-
<scope>test</scope>
78-
</dependency>
7973
</dependencies>
8074

8175
<build>

Diff for: src/test/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriterTest.java

+16-21
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import java.nio.file.Files;
2727
import java.util.NoSuchElementException;
2828

29-
import org.codehaus.plexus.util.StringUtils;
3029
import org.junit.jupiter.api.AfterEach;
3130
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Disabled;
3232
import org.junit.jupiter.api.Test;
3333

3434
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -172,20 +172,19 @@ void testendElementAlreadyClosed() {
172172
*
173173
* @throws IOException if an I/O error occurs
174174
*/
175+
@Disabled("This test is only relevant on JDK 1.7, which is not supported anymore")
175176
@Test
176177
void issue51DetectJava7ConcatenationBug() throws IOException {
177178
File dir = new File("target/test-xml");
178179
if (!dir.exists()) {
179180
assertTrue(dir.mkdir(), "cannot create directory test-xml");
180181
}
181182
File xmlFile = new File(dir, "test-issue-51.xml");
182-
OutputStreamWriter osw =
183-
new OutputStreamWriter(Files.newOutputStream(xmlFile.toPath()), StandardCharsets.UTF_8);
184-
writer = new PrettyPrintXMLWriter(osw);
185183

186184
int iterations = 20000;
187-
188-
try {
185+
try (OutputStreamWriter osw =
186+
new OutputStreamWriter(Files.newOutputStream(xmlFile.toPath()), StandardCharsets.UTF_8)) {
187+
writer = new PrettyPrintXMLWriter(osw);
189188
for (int i = 0; i < iterations; ++i) {
190189
writer.startElement(Tag.DIV.toString() + i);
191190
writer.addAttribute("class", "someattribute");
@@ -195,10 +194,6 @@ void issue51DetectJava7ConcatenationBug() throws IOException {
195194
}
196195
} catch (NoSuchElementException e) {
197196
fail("Should not throw a NoSuchElementException");
198-
} finally {
199-
if (osw != null) {
200-
osw.close();
201-
}
202197
}
203198
}
204199

@@ -237,29 +232,29 @@ private String expectedResult(String lineSeparator) {
237232
}
238233

239234
private String expectedResult(String lineIndenter, String lineSeparator) {
240-
return "<html>" + lineSeparator + StringUtils.repeat(lineIndenter, 1)
241-
+ "<head>" + lineSeparator + StringUtils.repeat(lineIndenter, 2)
235+
return "<html>" + lineSeparator + lineIndenter
236+
+ "<head>" + lineSeparator + lineIndenter + lineIndenter
242237
+ "<title>title</title>"
243238
+ lineSeparator
244-
+ StringUtils.repeat(lineIndenter, 2)
239+
+ lineIndenter + lineIndenter
245240
+ "<meta name=\"author\" content=\"Author\"/>"
246241
+ lineSeparator
247-
+ StringUtils.repeat(lineIndenter, 2)
242+
+ lineIndenter + lineIndenter
248243
+ "<meta name=\"date\" content=\"Date\"/>"
249244
+ lineSeparator
250-
+ StringUtils.repeat(lineIndenter, 1)
251-
+ "</head>" + lineSeparator + StringUtils.repeat(lineIndenter, 1)
252-
+ "<body>" + lineSeparator + StringUtils.repeat(lineIndenter, 2)
245+
+ lineIndenter
246+
+ "</head>" + lineSeparator + lineIndenter
247+
+ "<body>" + lineSeparator + lineIndenter + lineIndenter
253248
+ "<p>Paragraph 1, line 1. Paragraph 1, line 2.</p>"
254249
+ lineSeparator
255-
+ StringUtils.repeat(lineIndenter, 2)
250+
+ lineIndenter + lineIndenter
256251
+ "<div class=\"section\">"
257252
+ lineSeparator
258-
+ StringUtils.repeat(lineIndenter, 3)
253+
+ lineIndenter + lineIndenter + lineIndenter
259254
+ "<h2>Section title</h2>"
260255
+ lineSeparator
261-
+ StringUtils.repeat(lineIndenter, 2)
262-
+ "</div>" + lineSeparator + StringUtils.repeat(lineIndenter, 1)
256+
+ lineIndenter + lineIndenter
257+
+ "</div>" + lineSeparator + lineIndenter
263258
+ "</body>" + lineSeparator + "</html>";
264259
}
265260
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

Diff for: src/test/java/org/codehaus/plexus/util/xml/XmlStreamReaderTest.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
import java.io.InputStream;
2222
import java.io.SequenceInputStream;
2323

24-
import org.codehaus.plexus.util.IOUtil;
2524
import org.junit.jupiter.api.Test;
2625
import org.opentest4j.AssertionFailedError;
2726

27+
import static org.codehaus.plexus.util.xml.TestUtils.readAllFrom;
2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.junit.jupiter.api.Assertions.assertThrows;
3030

@@ -68,8 +68,7 @@ private static String createXmlContent(String text, String encoding) {
6868
if (encoding != null) {
6969
xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>";
7070
}
71-
String xml = xmlDecl + "\n<text>" + text + "</text>";
72-
return xml;
71+
return xmlDecl + "\n<text>" + text + "</text>";
7372
}
7473

7574
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
8685

8786
XmlStreamReader reader = new XmlStreamReader(in);
8887
assertEquals(encoding, reader.getEncoding());
89-
String result = IOUtil.toString(reader);
90-
assertEquals(xml, result);
88+
assertEquals(xml, readAllFrom(reader));
9189
}
9290

9391
private static void checkXmlStreamReader(String text, String encoding, String effectiveEncoding)
@@ -228,10 +226,9 @@ void ebcdicEncoding() throws IOException {
228226
/**
229227
* <p>testInappropriateEncoding.</p>
230228
*
231-
* @throws java.io.IOException if any.
232229
*/
233230
@Test
234-
void inappropriateEncoding() throws IOException {
231+
void inappropriateEncoding() {
235232
// expected failure, since the encoding does not contain some characters
236233
assertThrows(
237234
AssertionFailedError.class,

Diff for: src/test/java/org/codehaus/plexus/util/xml/XmlStreamWriterTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ private static String createXmlContent(String text, String encoding) {
5252
if (encoding != null) {
5353
xmlDecl = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>";
5454
}
55-
String xml = xmlDecl + "\n<text>" + text + "</text>";
56-
return xml;
55+
return xmlDecl + "\n<text>" + text + "</text>";
5756
}
5857

5958
private static void checkXmlContent(String xml, String encoding) throws IOException {

Diff for: src/test/java/org/codehaus/plexus/util/xml/XmlUtilTest.java

+21-47
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,12 @@
1616
* limitations under the License.
1717
*/
1818

19-
import java.io.File;
20-
import java.io.IOException;
21-
import java.io.InputStream;
22-
import java.io.OutputStream;
23-
import java.io.Reader;
24-
import java.io.StringWriter;
25-
import java.io.Writer;
19+
import java.io.*;
2620
import java.nio.file.Files;
2721

28-
import org.codehaus.plexus.util.IOUtil;
29-
import org.codehaus.plexus.util.StringUtils;
3022
import org.junit.jupiter.api.Test;
3123

24+
import static org.codehaus.plexus.util.xml.TestUtils.readAllFrom;
3225
import static org.junit.jupiter.api.Assertions.assertNotNull;
3326
import static org.junit.jupiter.api.Assertions.assertTrue;
3427

@@ -73,20 +66,14 @@ void prettyFormatInputStreamOutputStream() throws Exception {
7366
File testDocument = new File(getBasedir(), "src/test/resources/testDocument.xhtml");
7467
assertTrue(testDocument.exists());
7568

76-
InputStream is = null;
77-
OutputStream os = null;
78-
try {
79-
is = Files.newInputStream(testDocument.toPath());
80-
os = Files.newOutputStream(getTestOutputFile("target/test/prettyFormatTestDocumentOutputStream.xml")
81-
.toPath());
82-
69+
try (InputStream is = Files.newInputStream(testDocument.toPath());
70+
OutputStream os =
71+
Files.newOutputStream(getTestOutputFile("target/test/prettyFormatTestDocumentOutputStream.xml")
72+
.toPath())) {
8373
assertNotNull(is);
8474
assertNotNull(os);
8575

8676
XmlUtil.prettyFormat(is, os);
87-
} finally {
88-
IOUtil.close(is);
89-
IOUtil.close(os);
9077
}
9178
}
9279

@@ -100,19 +87,14 @@ void prettyFormatReaderWriter() throws Exception {
10087
File testDocument = new File(getBasedir(), "src/test/resources/testDocument.xhtml");
10188
assertTrue(testDocument.exists());
10289

103-
Reader reader = null;
104-
Writer writer = null;
105-
try {
106-
reader = ReaderFactory.newXmlReader(testDocument);
107-
writer = WriterFactory.newXmlWriter(getTestOutputFile("target/test/prettyFormatTestDocumentWriter.xml"));
90+
try (Reader reader = ReaderFactory.newXmlReader(testDocument);
91+
Writer writer = WriterFactory.newXmlWriter(
92+
getTestOutputFile("target/test/prettyFormatTestDocumentWriter.xml"))) {
10893

10994
assertNotNull(reader);
11095
assertNotNull(writer);
11196

11297
XmlUtil.prettyFormat(reader, writer);
113-
} finally {
114-
IOUtil.close(reader);
115-
IOUtil.close(writer);
11698
}
11799
}
118100

@@ -126,25 +108,22 @@ void prettyFormatString() throws Exception {
126108
File testDocument = new File(getBasedir(), "src/test/resources/testDocument.xhtml");
127109
assertTrue(testDocument.exists());
128110

129-
Reader reader = null;
130-
Writer writer = null;
131111
String content;
132-
try {
133-
reader = ReaderFactory.newXmlReader(testDocument);
134-
content = IOUtil.toString(reader);
112+
try (Reader reader = ReaderFactory.newXmlReader(testDocument)) {
113+
content = readAllFrom(reader);
114+
}
135115

136-
reader = ReaderFactory.newXmlReader(testDocument);
137-
writer = new StringWriter();
116+
String contentPretty;
117+
try (Reader reader = ReaderFactory.newXmlReader(testDocument)) {
118+
Writer writer = new StringWriter();
138119
XmlUtil.prettyFormat(reader, writer);
139-
} finally {
140-
IOUtil.close(reader);
141-
IOUtil.close(writer);
120+
contentPretty = writer.toString();
142121
}
143122

144123
assertNotNull(content);
145124

146-
int countEOL = StringUtils.countMatches(content, XmlUtil.DEFAULT_LINE_SEPARATOR);
147-
assertTrue(countEOL < StringUtils.countMatches(writer.toString(), XmlUtil.DEFAULT_LINE_SEPARATOR));
125+
int countEOL = TestUtils.countMatches(content, XmlUtil.DEFAULT_LINE_SEPARATOR);
126+
assertTrue(countEOL < TestUtils.countMatches(contentPretty, XmlUtil.DEFAULT_LINE_SEPARATOR));
148127
}
149128

150129
/**
@@ -157,19 +136,14 @@ void prettyFormatReaderWriter2() throws Exception {
157136
File testDocument = new File(getBasedir(), "src/test/resources/test.xdoc.xhtml");
158137
assertTrue(testDocument.exists());
159138

160-
Reader reader = null;
161-
Writer writer = null;
162-
try {
163-
reader = ReaderFactory.newXmlReader(testDocument);
164-
writer = WriterFactory.newXmlWriter(getTestOutputFile("target/test/prettyFormatTestXdocWriter.xml"));
139+
try (Reader reader = ReaderFactory.newXmlReader(testDocument);
140+
Writer writer =
141+
WriterFactory.newXmlWriter(getTestOutputFile("target/test/prettyFormatTestXdocWriter.xml"))) {
165142

166143
assertNotNull(reader);
167144
assertNotNull(writer);
168145

169146
XmlUtil.prettyFormat(reader, writer);
170-
} finally {
171-
IOUtil.close(reader);
172-
IOUtil.close(writer);
173147
}
174148
}
175149
}

0 commit comments

Comments
 (0)