This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git
commit 98cd3596b25c406c675e8fda35bfd3edfa9b825f Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Tue Nov 17 00:01:51 2026 +0000 Use JUnit 5 assertions and Mockito best practices in tests --- .../maven/shared/utils/PropertyUtilsTest.java | 7 +++---- .../maven/shared/utils/XmlStreamReaderTest.java | 24 +++++++++++----------- .../utils/cli/AbstractStreamHandlerTest.java | 8 ++++---- .../shared/utils/cli/CommandLineUtilsTest.java | 1 - .../shared/utils/cli/StreamPollFeederTest.java | 2 +- .../shared/utils/io/DirectoryScannerTest.java | 4 ++-- .../maven/shared/utils/io/FileUtilsTest.java | 14 ++++++------- .../shared/utils/xml/PrettyPrintXmlWriterTest.java | 18 ++++++++-------- .../maven/shared/utils/xml/XmlWriterUtilTest.java | 15 +++++++------- .../maven/shared/utils/xml/Xpp3DomBuilderTest.java | 5 ++--- .../maven/shared/utils/xml/pull/Xpp3DomTest.java | 10 ++++----- 11 files changed, 52 insertions(+), 56 deletions(-) diff --git a/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java index 1055425..4bebaf5 100644 --- a/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java +++ b/src/test/java/org/apache/maven/shared/utils/PropertyUtilsTest.java @@ -23,7 +23,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -121,7 +120,7 @@ public class PropertyUtilsTest { @Test @SuppressWarnings("deprecation") - public void loadValidInputStream() throws UnsupportedEncodingException { + public void loadValidInputStream() throws Exception { Properties value = new Properties(); value.setProperty("a", "b"); @@ -133,7 +132,7 @@ public class PropertyUtilsTest { @Test @NeedsTemporaryFolder @SuppressWarnings("deprecation") - public void loadValidFile() throws IOException { + public void loadValidFile() throws Exception { File valid = newFile(tempFolder, "valid"); Properties value = new Properties(); value.setProperty("a", "b"); @@ -147,7 +146,7 @@ public class PropertyUtilsTest { @Test @NeedsTemporaryFolder @SuppressWarnings("deprecation") - public void loadValidURL() throws IOException { + public void loadValidURL() throws Exception { File valid = newFile(tempFolder, "valid"); Properties value = new Properties(); value.setProperty("a", "b"); diff --git a/src/test/java/org/apache/maven/shared/utils/XmlStreamReaderTest.java b/src/test/java/org/apache/maven/shared/utils/XmlStreamReaderTest.java index 4e16b37..6ae1fa6 100644 --- a/src/test/java/org/apache/maven/shared/utils/XmlStreamReaderTest.java +++ b/src/test/java/org/apache/maven/shared/utils/XmlStreamReaderTest.java @@ -103,63 +103,63 @@ public class XmlStreamReaderTest { } @Test - public void testNoXmlHeader() throws IOException { + public void testNoXmlHeader() throws Exception { String xml = "<text>text with no XML header</text>"; checkXmlContent(xml, "UTF-8"); checkXmlContent(xml, "UTF-8", BOM_UTF8); } @Test - public void testDefaultEncoding() throws IOException { + public void testDefaultEncoding() throws Exception { checkXmlStreamReader(TEXT_UNICODE, (String) null, "UTF-8", null); checkXmlStreamReader(TEXT_UNICODE, null, "UTF-8", BOM_UTF8); } @Test - public void testUTF8Encoding() throws IOException { + public void testUTF8Encoding() throws Exception { checkXmlStreamReader(TEXT_UNICODE, "UTF-8"); checkXmlStreamReader(TEXT_UNICODE, "UTF-8", "UTF-8", BOM_UTF8); } @Test - public void testUTF16Encoding() throws IOException { + public void testUTF16Encoding() throws Exception { checkXmlStreamReader(TEXT_UNICODE, "UTF-16", "UTF-16BE", null); checkXmlStreamReader(TEXT_UNICODE, "UTF-16", "UTF-16LE", BOM_UTF16LE); checkXmlStreamReader(TEXT_UNICODE, "UTF-16", "UTF-16BE", BOM_UTF16BE); } @Test - public void testUTF16BEEncoding() throws IOException { + public void testUTF16BEEncoding() throws Exception { checkXmlStreamReader(TEXT_UNICODE, "UTF-16BE"); } @Test - public void testUTF16LEEncoding() throws IOException { + public void testUTF16LEEncoding() throws Exception { checkXmlStreamReader(TEXT_UNICODE, "UTF-16LE"); } @Test - public void testLatin1Encoding() throws IOException { + public void testLatin1Encoding() throws Exception { checkXmlStreamReader(TEXT_LATIN1, "ISO-8859-1"); } @Test - public void testLatin7Encoding() throws IOException { + public void testLatin7Encoding() throws Exception { checkXmlStreamReader(TEXT_LATIN7, "ISO-8859-7"); } @Test - public void testLatin15Encoding() throws IOException { + public void testLatin15Encoding() throws Exception { checkXmlStreamReader(TEXT_LATIN15, "ISO-8859-15"); } @Test - public void testEUCJPEncoding() throws IOException { + public void testEUCJPEncoding() throws Exception { checkXmlStreamReader(TEXT_EUC_JP, "EUC-JP"); } @Test - public void testEBCDICEncoding() throws IOException { + public void testEBCDICEncoding() throws Exception { checkXmlStreamReader("simple text in EBCDIC", "CP1047"); } @@ -170,7 +170,7 @@ public class XmlStreamReaderTest { } @Test - public void testEncodingAttribute() throws IOException { + public void testEncodingAttribute() throws Exception { checkXmlContent("<?xml version='1.0' encoding='US-ASCII'?><element encoding='attribute value'/>", "US-ASCII"); checkXmlContent( "<?xml version='1.0' encoding = 'US-ASCII' ?><element encoding='attribute value'/>", "US-ASCII"); diff --git a/src/test/java/org/apache/maven/shared/utils/cli/AbstractStreamHandlerTest.java b/src/test/java/org/apache/maven/shared/utils/cli/AbstractStreamHandlerTest.java index a8a7bdc..c16de47 100644 --- a/src/test/java/org/apache/maven/shared/utils/cli/AbstractStreamHandlerTest.java +++ b/src/test/java/org/apache/maven/shared/utils/cli/AbstractStreamHandlerTest.java @@ -28,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class AbstractStreamHandlerTest { @Test - void setDoneNotifiesWaitUntilDone() throws InterruptedException { + void setDoneNotifiesWaitUntilDone() throws Exception { AbstractStreamHandler handler = new AbstractStreamHandler() {}; CountDownLatch waiting = new CountDownLatch(1); @@ -54,14 +54,14 @@ class AbstractStreamHandlerTest { } @Test - void waitUntilDoneWithTimeoutReturnsFalseWhenNotDone() throws InterruptedException { + void waitUntilDoneWithTimeoutReturnsFalseWhenNotDone() throws Exception { AbstractStreamHandler handler = new AbstractStreamHandler() {}; assertFalse(handler.waitUntilDone(100), "must time out while the handler is not done"); } @Test - void waitUntilDoneWithTimeoutReturnsTrueWhenDone() throws InterruptedException { + void waitUntilDoneWithTimeoutReturnsTrueWhenDone() throws Exception { AbstractStreamHandler handler = new AbstractStreamHandler() {}; handler.setDone(); @@ -69,7 +69,7 @@ class AbstractStreamHandlerTest { } @Test - void waitUntilDoneWithTimeoutIsInterruptible() throws InterruptedException { + void waitUntilDoneWithTimeoutIsInterruptible() throws Exception { AbstractStreamHandler handler = new AbstractStreamHandler() {}; Thread waiter = new Thread(() -> { diff --git a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java index cbc5b4a..9563f3e 100644 --- a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java +++ b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java @@ -98,7 +98,6 @@ public class CommandLineUtilsTest { try { new Commandline("echo \"let\"s go\"").execute(); } catch (CommandLineException e) { - assertTrue(true); return; } fail("Exception was not thrown when given invalid (3 unescaped double quote) input"); diff --git a/src/test/java/org/apache/maven/shared/utils/cli/StreamPollFeederTest.java b/src/test/java/org/apache/maven/shared/utils/cli/StreamPollFeederTest.java index 367e352..4c7c74c 100644 --- a/src/test/java/org/apache/maven/shared/utils/cli/StreamPollFeederTest.java +++ b/src/test/java/org/apache/maven/shared/utils/cli/StreamPollFeederTest.java @@ -82,7 +82,7 @@ public class StreamPollFeederTest { } @Test - public void dataShouldBeCopied() throws InterruptedException { + public void dataShouldBeCopied() throws Exception { StringBuilder testData = new StringBuilder(); for (int i = 0; i < 100; i++) { diff --git a/src/test/java/org/apache/maven/shared/utils/io/DirectoryScannerTest.java b/src/test/java/org/apache/maven/shared/utils/io/DirectoryScannerTest.java index 2f02ce6..84843c7 100644 --- a/src/test/java/org/apache/maven/shared/utils/io/DirectoryScannerTest.java +++ b/src/test/java/org/apache/maven/shared/utils/io/DirectoryScannerTest.java @@ -182,7 +182,7 @@ public class DirectoryScannerTest { } @Test - public void followSymlinksFalse() throws IOException { + public void followSymlinksFalse() throws Exception { assumeFalse(Os.isFamily(Os.FAMILY_WINDOWS)); File testDir = SymlinkTestSetup.createStandardSymlinkTestDir(new File("target/test/symlinkTestCase")); @@ -216,7 +216,7 @@ public class DirectoryScannerTest { } @Test - public void followSymlinks() throws IOException { + public void followSymlinks() throws Exception { assumeFalse(Os.isFamily(Os.FAMILY_WINDOWS)); DirectoryScanner ds = new DirectoryScanner(); diff --git a/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java index e081788..84d18a3 100644 --- a/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java +++ b/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java @@ -614,7 +614,7 @@ public class FileUtilsTest { } @Test - public void copyToSelf() throws IOException { + public void copyToSelf() throws Exception { File destination = new File(tempFolder, "copy3.txt"); // Prepare a test file FileUtils.copyFile(testFile1, destination); @@ -643,7 +643,7 @@ public class FileUtilsTest { } @Test - public void copyDirectoryToExistingDest() throws IOException { + public void copyDirectoryToExistingDest() throws Exception { createFile(testFile1, 1234); createFile(testFile2, 4321); File srcDir = tempFolder; @@ -946,7 +946,7 @@ public class FileUtilsTest { } @Test - public void deleteQuietlyDir() throws IOException { + public void deleteQuietlyDir() throws Exception { File testDirectory = new File(tempFolder, "testDeleteQuietlyDir"); File testFile = new File(testDirectory, "testDeleteQuietlyFile"); testDirectory.mkdirs(); @@ -960,7 +960,7 @@ public class FileUtilsTest { } @Test - public void deleteQuietlyFile() throws IOException { + public void deleteQuietlyFile() throws Exception { File testFile = new File(tempFolder, "testDeleteQuietlyFile"); createFile(testFile, 0); @@ -970,7 +970,7 @@ public class FileUtilsTest { } @Test - public void deleteQuietlyNonExistent() throws IOException { + public void deleteQuietlyNonExistent() throws Exception { File testFile = new File(tempFolder, "testDeleteQuietlyNonExistent"); assertFalse(testFile.exists()); @@ -1206,7 +1206,7 @@ public class FileUtilsTest { @Test @Disabled("Wait until we can run with assembly 2.5 which will support symlinks properly") @DisabledOnOs(OS.WINDOWS) - public void isASymbolicLink() throws IOException { + public void isASymbolicLink() throws Exception { // This testcase will pass when running under java7 or higher File file = new File("src/test/resources/symlinks/src/symDir"); assertTrue(FileUtils.isSymbolicLink(file)); @@ -1214,7 +1214,7 @@ public class FileUtilsTest { @Test @Disabled("Wait until we can run with assembly 2.5 which will support symlinks properly") - public void notASymbolicLink() throws IOException { + public void notASymbolicLink() throws Exception { File file = new File("src/test/resources/symlinks/src/"); assertFalse(FileUtils.isSymbolicLink(file)); } diff --git a/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java b/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java index 5ee265f..bcfad8e 100644 --- a/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java +++ b/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java @@ -40,7 +40,7 @@ public class PrettyPrintXmlWriterTest { private final PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter(w); @Test - public void testNoStartTag() throws IOException { + public void testNoStartTag() throws Exception { try { writer.startElement(""); @@ -51,7 +51,7 @@ public class PrettyPrintXmlWriterTest { } @Test - public void testDefaultPrettyPrintXMLWriter() throws IOException { + public void testDefaultPrettyPrintXMLWriter() throws Exception { writer.startElement(HTML.Tag.HTML.toString()); writeXhtmlHead(writer); @@ -64,7 +64,7 @@ public class PrettyPrintXmlWriterTest { } @Test - public void testPrettyPrintXMLWriterWithGivenLineSeparator() throws IOException { + public void testPrettyPrintXMLWriterWithGivenLineSeparator() throws Exception { writer.setLineSeparator("\n"); writer.startElement(HTML.Tag.HTML.toString()); @@ -79,7 +79,7 @@ public class PrettyPrintXmlWriterTest { } @Test - public void testPrettyPrintXMLWriterWithGivenLineIndenter() throws IOException { + public void testPrettyPrintXMLWriterWithGivenLineIndenter() throws Exception { writer.setLineIndenter(" "); writer.startElement(HTML.Tag.HTML.toString()); @@ -94,7 +94,7 @@ public class PrettyPrintXmlWriterTest { } @Test - public void testPrettyPrintXMLWriterWithNullLineIndent() throws IOException { + public void testPrettyPrintXMLWriterWithNullLineIndent() throws Exception { StringWriter sw = new StringWriter(); PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter(sw, (String) null); @@ -107,7 +107,7 @@ public class PrettyPrintXmlWriterTest { } @Test - public void testEscapeXmlAttributeWindows() throws IOException { + public void testEscapeXmlAttributeWindows() throws Exception { // Windows writer.startElement(HTML.Tag.DIV.toString()); writer.addAttribute("class", "sect\r\nion"); @@ -116,7 +116,7 @@ public class PrettyPrintXmlWriterTest { } @Test - public void testEscapeXmlAttributeMac() throws IOException { + public void testEscapeXmlAttributeMac() throws Exception { // Mac writer.startElement(HTML.Tag.DIV.toString()); writer.addAttribute("class", "sect\rion"); @@ -125,7 +125,7 @@ public class PrettyPrintXmlWriterTest { } @Test - public void testEscapeXmlAttributeTrailingCR() throws IOException { + public void testEscapeXmlAttributeTrailingCR() throws Exception { // Mac writer.startElement(HTML.Tag.DIV.toString()); writer.addAttribute("class", "section\r"); @@ -134,7 +134,7 @@ public class PrettyPrintXmlWriterTest { } @Test - public void testEscapeXmlAttributeUnix() throws IOException { + public void testEscapeXmlAttributeUnix() throws Exception { // Unix writer.startElement(HTML.Tag.DIV.toString()); writer.addAttribute("class", "sect\nion"); diff --git a/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java b/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java index 34e9878..8df04bb 100644 --- a/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java +++ b/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java @@ -19,7 +19,6 @@ package org.apache.maven.shared.utils.xml; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.OutputStream; import java.io.Writer; @@ -133,7 +132,7 @@ public class XmlWriterUtilTest { } @Test - public void testWriteCommentLineBreak() throws IOException { + public void testWriteCommentLineBreak() throws Exception { XmlWriterUtil.writeCommentLineBreak(xmlWriter, 10); writer.close(); assertEquals("<!-- -->" + "\r\n", output.toString()); @@ -155,7 +154,7 @@ public class XmlWriterUtilTest { } @Test - public void testWriteComment() throws IOException { + public void testWriteComment() throws Exception { XmlWriterUtil.writeComment( xmlWriter, "hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"); writer.close(); @@ -166,7 +165,7 @@ public class XmlWriterUtilTest { } @Test - public void testWriteComment2() throws IOException { + public void testWriteComment2() throws Exception { XmlWriterUtil.writeComment(xmlWriter, "hello\nworld"); writer.close(); String sb = "<!-- hello -->\r\n" @@ -194,7 +193,7 @@ public class XmlWriterUtilTest { } @Test - public void testWriteComment3() throws IOException { + public void testWriteComment3() throws Exception { String indent = StringUtils.repeat(" ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE); XmlWriterUtil.writeComment(xmlWriter, "hello\nworld", 2); writer.close(); @@ -224,7 +223,7 @@ public class XmlWriterUtilTest { } @Test - public void testWriteCommentXMLWriterStringIntInt2() throws IOException { + public void testWriteCommentXMLWriterStringIntInt2() throws Exception { String repeat = StringUtils.repeat(" ", 2 * 4); XmlWriterUtil.writeComment(xmlWriter, "hello\nworld", 2, 4); writer.close(); @@ -254,7 +253,7 @@ public class XmlWriterUtilTest { } @Test - public void testWriteCommentXMLWriterStringIntIntInt2() throws IOException { + public void testWriteCommentXMLWriterStringIntIntInt2() throws Exception { String indent = StringUtils.repeat(" ", 2 * 4); XmlWriterUtil.writeComment(xmlWriter, "hello", 2, 4, 10); writer.close(); @@ -279,7 +278,7 @@ public class XmlWriterUtilTest { } @Test - public void testWriteCommentTextXMLWriterStringInt2() throws IOException { + public void testWriteCommentTextXMLWriterStringInt2() throws Exception { String indent = StringUtils.repeat(" ", 2 * 2); XmlWriterUtil.writeCommentText( diff --git a/src/test/java/org/apache/maven/shared/utils/xml/Xpp3DomBuilderTest.java b/src/test/java/org/apache/maven/shared/utils/xml/Xpp3DomBuilderTest.java index 7885489..bc6c45b 100644 --- a/src/test/java/org/apache/maven/shared/utils/xml/Xpp3DomBuilderTest.java +++ b/src/test/java/org/apache/maven/shared/utils/xml/Xpp3DomBuilderTest.java @@ -19,7 +19,6 @@ package org.apache.maven.shared.utils.xml; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.io.StringWriter; @@ -108,7 +107,7 @@ public class Xpp3DomBuilderTest { } @Test - public void attributeEscaping() throws IOException, XmlPullParserException { + public void attributeEscaping() throws Exception { String s = getAttributeEncodedString(); Xpp3Dom dom = Xpp3DomBuilder.build(new StringReader(s)); @@ -120,7 +119,7 @@ public class Xpp3DomBuilderTest { } @Test - public void contentEscaping() throws IOException, XmlPullParserException { + public void contentEscaping() throws Exception { Xpp3Dom dom = Xpp3DomBuilder.build(new StringReader(getEncodedString())); assertEquals("\"msg\"", dom.getChild("a1").getValue()); diff --git a/src/test/java/org/apache/maven/shared/utils/xml/pull/Xpp3DomTest.java b/src/test/java/org/apache/maven/shared/utils/xml/pull/Xpp3DomTest.java index dddd984..4d18451 100644 --- a/src/test/java/org/apache/maven/shared/utils/xml/pull/Xpp3DomTest.java +++ b/src/test/java/org/apache/maven/shared/utils/xml/pull/Xpp3DomTest.java @@ -65,7 +65,7 @@ public class Xpp3DomTest { } @Test - public void mergePrecedenceSelfClosed() throws XmlPullParserException { + public void mergePrecedenceSelfClosed() throws Exception { Xpp3Dom parentConfig = build("<configuration><items><item/></items></configuration>"); Xpp3Dom childConfig = build("<configuration><items><item>ooopise</item></items></configuration>"); @@ -78,7 +78,7 @@ public class Xpp3DomTest { } @Test - public void mergePrecedenceOpenClose() throws XmlPullParserException { + public void mergePrecedenceOpenClose() throws Exception { Xpp3Dom parentConfig = build("<configuration><items><item></item></items></configuration>"); Xpp3Dom childConfig = build("<configuration><items><item>ooopise</item></items></configuration>"); @@ -180,7 +180,7 @@ public class Xpp3DomTest { } @Test - public void dominantWinsCollections() throws XmlPullParserException { + public void dominantWinsCollections() throws Exception { Xpp3Dom parent = build("<root><entries><entry>uno</entry><entry>dos</entry></entries></root>"); Xpp3Dom dominant = build("<root><entries><entry>tres</entry></entries></root>"); @@ -193,7 +193,7 @@ public class Xpp3DomTest { } @Test - public void combineChildrenAppendTest() throws XmlPullParserException { + public void combineChildrenAppendTest() throws Exception { Xpp3Dom parent = build("<root><entries><entry>uno</entry><entry>dos</entry><entry>tres</entry></entries></root>"); Xpp3Dom child = build("<root><entries combine.children=\"append\"><entry>quatro</entry></entries></root>"); @@ -255,7 +255,7 @@ public class Xpp3DomTest { } @Test - public void duplicatedChildren() throws XmlPullParserException { + public void duplicatedChildren() throws Exception { String dupes = "<root><baz>x</baz><baz>y</baz></root>"; assertEquals("y", build(dupes).getChild("baz").getValue()); }
