This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-net.git
The following commit(s) were added to refs/heads/master by this push: new 0f17b108 Use assertThrows 0f17b108 is described below commit 0f17b1087a0d19dd8b21328b66c24767d738d66c Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Aug 23 06:44:16 2023 -0400 Use assertThrows --- .../commons/net/ftp/FTPClientConfigTest.java | 17 +++------- .../DefaultFTPFileEntryParserFactoryTest.java | 9 ++---- .../net/ftp/parser/FTPTimestampParserImplTest.java | 16 +++------- .../commons/net/tftp/TFTPServerPathTest.java | 37 +++++++++++----------- 4 files changed, 30 insertions(+), 49 deletions(-) diff --git a/src/test/java/org/apache/commons/net/ftp/FTPClientConfigTest.java b/src/test/java/org/apache/commons/net/ftp/FTPClientConfigTest.java index 0ba88105..fbba6b03 100644 --- a/src/test/java/org/apache/commons/net/ftp/FTPClientConfigTest.java +++ b/src/test/java/org/apache/commons/net/ftp/FTPClientConfigTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.net.ftp; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.text.DateFormatSymbols; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -111,19 +113,8 @@ public class FTPClientConfigTest extends TestCase { assertEquals("different.parser.same.date", d1, d2); - try { - sdf1.parse("hij 31, 2004"); - fail("should.have.failed.to.parse.weird"); - } catch (final ParseException px) { - // expected - } - try { - sdf2.parse("dec 31, 2004"); - fail("should.have.failed.to.parse.standard"); - } catch (final ParseException px) { - // expected - } - + assertThrows(ParseException.class, () -> sdf1.parse("hij 31, 2004")); + assertThrows(ParseException.class, () -> sdf2.parse("dec 31, 2004")); } public void testGetServerLanguageCode() { diff --git a/src/test/java/org/apache/commons/net/ftp/parser/DefaultFTPFileEntryParserFactoryTest.java b/src/test/java/org/apache/commons/net/ftp/parser/DefaultFTPFileEntryParserFactoryTest.java index db5e4bdb..08c3df14 100644 --- a/src/test/java/org/apache/commons/net/ftp/parser/DefaultFTPFileEntryParserFactoryTest.java +++ b/src/test/java/org/apache/commons/net/ftp/parser/DefaultFTPFileEntryParserFactoryTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.net.ftp.parser; +import static org.junit.jupiter.api.Assertions.assertThrows; + import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPFileEntryParser; @@ -118,12 +120,7 @@ public class DefaultFTPFileEntryParserFactoryTest extends TestCase { public void testDefaultParserFactoryConfig() throws Exception { final DefaultFTPFileEntryParserFactory factory = new DefaultFTPFileEntryParserFactory(); - try { - factory.createFileEntryParser((FTPClientConfig) null); - fail("Expected NullPointerException"); - } catch (final NullPointerException npe) { - // expected - } + assertThrows(NullPointerException.class, () -> factory.createFileEntryParser((FTPClientConfig) null)); checkParserClass(factory, null, UnixFTPEntryParser.class); checkParserClass(factory, FTPClientConfig.SYST_OS400, OS400FTPEntryParser.class); diff --git a/src/test/java/org/apache/commons/net/ftp/parser/FTPTimestampParserImplTest.java b/src/test/java/org/apache/commons/net/ftp/parser/FTPTimestampParserImplTest.java index 7e3c7538..37524ab4 100644 --- a/src/test/java/org/apache/commons/net/ftp/parser/FTPTimestampParserImplTest.java +++ b/src/test/java/org/apache/commons/net/ftp/parser/FTPTimestampParserImplTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.net.ftp.parser; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -151,18 +153,8 @@ public class FTPTimestampParserImplTest extends TestCase { // Note: we use a known leap year for the target date to avoid rounding up final GregorianCalendar input = new GregorianCalendar(2000, Calendar.FEBRUARY, 29); final GregorianCalendar expected = new GregorianCalendar(1999, Calendar.FEBRUARY, 29); - try { - checkShortParse("Feb 29th 1999", server, input, expected, true); - fail("Should have failed to parse Feb 29th 1999"); - } catch (final ParseException pe) { - // expected - } - try { - checkShortParse("Feb 29th 1999", server, input, expected, false); - fail("Should have failed to parse Feb 29th 1999"); - } catch (final ParseException pe) { - // expected - } + assertThrows(ParseException.class, () -> checkShortParse("Feb 29th 1999", server, input, expected, true)); + assertThrows(ParseException.class, () -> checkShortParse("Feb 29th 1999", server, input, expected, false)); } // Lenient mode allows for dates up to 1 day in the future diff --git a/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java b/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java index f21c6ed0..972cd007 100644 --- a/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java +++ b/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java @@ -17,6 +17,7 @@ package org.apache.commons.net.tftp; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -89,12 +90,12 @@ public class TFTPServerPathTest { out.delete(); - try (final FileInputStream fis = new FileInputStream(file)) { - tftp.sendFile(out.getName(), TFTP.BINARY_MODE, fis, "localhost", SERVER_PORT); - fail("Server allowed write"); - } catch (final IOException e) { - // expected path - } + assertThrows(IOException.class, () -> { + try (final FileInputStream fis = new FileInputStream(file)) { + tftp.sendFile(out.getName(), TFTP.BINARY_MODE, fis, "localhost", SERVER_PORT); + fail("Server allowed write"); + } + }); } finally { deleteFixture(file); deleteFixture(out); @@ -117,12 +118,12 @@ public class TFTPServerPathTest { // check old failed runs assertFalse(out.exists(), () -> "Couldn't clear output location, deleted="); - try (final FileOutputStream output = new FileOutputStream(out)) { - tftp.receiveFile(file.getName(), TFTP.BINARY_MODE, output, "localhost", SERVER_PORT); - fail("Server allowed read"); - } catch (final IOException e) { - // expected path - } + assertThrows(IOException.class, () -> { + try (final FileOutputStream output = new FileOutputStream(out)) { + tftp.receiveFile(file.getName(), TFTP.BINARY_MODE, output, "localhost", SERVER_PORT); + fail("Server allowed read"); + } + }); out.delete(); try (final FileInputStream fis = new FileInputStream(file)) { @@ -152,12 +153,12 @@ public class TFTPServerPathTest { try { assertFalse(new File(serverDirectory, "../foo").exists(), "test construction error"); - try (final FileInputStream fis = new FileInputStream(file)) { - tftp.sendFile("../foo", TFTP.BINARY_MODE, fis, "localhost", SERVER_PORT); - fail("Server allowed write!"); - } catch (final IOException e) { - // expected path - } + assertThrows(IOException.class, () -> { + try (final FileInputStream fis = new FileInputStream(file)) { + tftp.sendFile("../foo", TFTP.BINARY_MODE, fis, "localhost", SERVER_PORT); + fail("Server allowed write!"); + } + }); assertFalse(new File(serverDirectory, "../foo").exists(), "file created when it should not have been");