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
commit 02c69d0526c95e956e65846b1089f61a5c58428c Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Jul 19 09:37:44 2023 -0400 DatagramSocketClient implements AutoCloseable Fix leaks in tests --- src/changes/changes.xml | 3 + .../apache/commons/net/DatagramSocketClient.java | 2 +- .../commons/net/tftp/TFTPServerPathTest.java | 126 ++++++++++----------- .../java/org/apache/commons/net/tftp/TFTPTest.java | 64 ++++++----- 4 files changed, 100 insertions(+), 95 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 3a72879b..44e5f8f5 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -87,6 +87,9 @@ The <action> type attribute can be add,update,fix,remove. <action type="add" dev="ggregory" due-to="Gary Gregory"> TFTPServer implements AutoCloseable. </action> + <action type="add" dev="ggregory" due-to="Gary Gregory"> + DatagramSocketClient implements AutoCloseable. + </action> <!-- FIX --> <action type="fix" issue="NET-650" dev="ggregory" due-to="Matthew McGillis, exceptionfactory, sebbASF"> Delegate host resolution to Socket.connect() #138. diff --git a/src/main/java/org/apache/commons/net/DatagramSocketClient.java b/src/main/java/org/apache/commons/net/DatagramSocketClient.java index 340a8c38..cc90e145 100644 --- a/src/main/java/org/apache/commons/net/DatagramSocketClient.java +++ b/src/main/java/org/apache/commons/net/DatagramSocketClient.java @@ -38,7 +38,7 @@ import java.util.Objects; * @see DatagramSocketFactory */ -public abstract class DatagramSocketClient { +public abstract class DatagramSocketClient implements AutoCloseable { /** * The default DatagramSocketFactory shared by all DatagramSocketClient instances. */ 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 b957b318..bb49daaa 100644 --- a/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java +++ b/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java @@ -44,37 +44,37 @@ public class TFTPServerPathTest { try (TFTPServer tftpS = new TFTPServer(serverDirectory, serverDirectory, SERVER_PORT, ServerMode.GET_ONLY, null, null)) { // Create our TFTP instance to handle the file transfer. - final TFTPClient tftp = new TFTPClient(); - tftp.open(); - tftp.setSoTimeout(2000); + try (TFTPClient tftp = new TFTPClient()) { + tftp.open(); + tftp.setSoTimeout(2000); - // make a file to work with. - final File file = new File(serverDirectory, filePrefix + "source.txt"); - file.createNewFile(); + // make a file to work with. + final File file = new File(serverDirectory, filePrefix + "source.txt"); + file.createNewFile(); - // Read the file from the tftp server. - final File out = new File(serverDirectory, filePrefix + "out"); + // Read the file from the tftp server. + final File out = new File(serverDirectory, filePrefix + "out"); - // cleanup old failed runs - out.delete(); - assertFalse(out.exists(), "Couldn't clear output location"); + // cleanup old failed runs + out.delete(); + assertFalse(out.exists(), "Couldn't clear output location"); - try (final FileOutputStream output = new FileOutputStream(out)) { - tftp.receiveFile(file.getName(), TFTP.BINARY_MODE, output, "localhost", SERVER_PORT); - } + try (final FileOutputStream output = new FileOutputStream(out)) { + tftp.receiveFile(file.getName(), TFTP.BINARY_MODE, output, "localhost", SERVER_PORT); + } - assertTrue(out.exists(), "file not created"); + assertTrue(out.exists(), "file not created"); - out.delete(); + 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 + 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 + } + file.delete(); } - file.delete(); - tftp.close(); } } @@ -84,38 +84,38 @@ public class TFTPServerPathTest { try (TFTPServer tftpS = new TFTPServer(serverDirectory, serverDirectory, SERVER_PORT, ServerMode.PUT_ONLY, null, null)) { // Create our TFTP instance to handle the file transfer. - final TFTPClient tftp = new TFTPClient(); - tftp.open(); - tftp.setSoTimeout(2000); + try (TFTPClient tftp = new TFTPClient()) { + tftp.open(); + tftp.setSoTimeout(2000); - // make a file to work with. - final File file = new File(serverDirectory, filePrefix + "source.txt"); - file.createNewFile(); + // make a file to work with. + final File file = new File(serverDirectory, filePrefix + "source.txt"); + file.createNewFile(); - final File out = new File(serverDirectory, filePrefix + "out"); + final File out = new File(serverDirectory, filePrefix + "out"); - // cleanup old failed runs - out.delete(); - assertFalse(out.exists(), "Couldn't clear output location"); + // cleanup old failed runs + out.delete(); + assertFalse(out.exists(), "Couldn't clear output location"); - 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 - } - out.delete(); + 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 + } + out.delete(); - try (final FileInputStream fis = new FileInputStream(file)) { - tftp.sendFile(out.getName(), TFTP.BINARY_MODE, fis, "localhost", SERVER_PORT); - } + try (final FileInputStream fis = new FileInputStream(file)) { + tftp.sendFile(out.getName(), TFTP.BINARY_MODE, fis, "localhost", SERVER_PORT); + } - assertTrue(out.exists(), "file not created"); + assertTrue(out.exists(), "file not created"); - // cleanup - file.delete(); - out.delete(); - tftp.close(); + // cleanup + file.delete(); + out.delete(); + } } } @@ -125,26 +125,26 @@ public class TFTPServerPathTest { try (TFTPServer tftpS = new TFTPServer(serverDirectory, serverDirectory, SERVER_PORT, ServerMode.GET_AND_PUT, null, null)) { // Create our TFTP instance to handle the file transfer. - final TFTPClient tftp = new TFTPClient(); - tftp.open(); + try (TFTPClient tftp = new TFTPClient()) { + tftp.open(); - final File file = new File(serverDirectory, filePrefix + "source.txt"); - file.createNewFile(); + final File file = new File(serverDirectory, filePrefix + "source.txt"); + file.createNewFile(); - assertFalse(new File(serverDirectory, "../foo").exists(), "test construction error"); + 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 - } + 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 + } - assertFalse(new File(serverDirectory, "../foo").exists(), "file created when it should not have been"); + assertFalse(new File(serverDirectory, "../foo").exists(), "file created when it should not have been"); - // cleanup - file.delete(); - tftp.close(); + // cleanup + file.delete(); + } } } diff --git a/src/test/java/org/apache/commons/net/tftp/TFTPTest.java b/src/test/java/org/apache/commons/net/tftp/TFTPTest.java index 42d92f4f..b1a2fc2f 100644 --- a/src/test/java/org/apache/commons/net/tftp/TFTPTest.java +++ b/src/test/java/org/apache/commons/net/tftp/TFTPTest.java @@ -109,7 +109,7 @@ public class TFTPTest extends TestCase { testsLeftToRun--; if (testsLeftToRun <= 0) { if (tftpS != null) { - tftpS.shutdown(); + tftpS.close(); } for (final File file : files) { file.delete(); @@ -139,25 +139,26 @@ public class TFTPTest extends TestCase { private void testDownload(final int mode, final File file) throws IOException { // Create our TFTP instance to handle the file transfer. - final TFTPClient tftp = new TFTPClient(); - tftp.open(); - tftp.setSoTimeout(2000); + try (TFTPClient tftp = new TFTPClient()) { + tftp.open(); + tftp.setSoTimeout(2000); - final File out = new File(serverDirectory, filePrefix + "download"); + final File out = new File(serverDirectory, filePrefix + "download"); - // cleanup old failed runs - out.delete(); - assertFalse("Couldn't clear output location", out.exists()); + // cleanup old failed runs + out.delete(); + assertFalse("Couldn't clear output location", out.exists()); - try (final FileOutputStream output = new FileOutputStream(out)) { - tftp.receiveFile(file.getName(), mode, output, "localhost", SERVER_PORT); - } + try (final FileOutputStream output = new FileOutputStream(out)) { + tftp.receiveFile(file.getName(), mode, output, "localhost", SERVER_PORT); + } - assertTrue("file not created", out.exists()); - assertTrue("files not identical on file " + file, filesIdentical(out, file)); + assertTrue("file not created", out.exists()); + assertTrue("files not identical on file " + file, filesIdentical(out, file)); - // delete the downloaded file - out.delete(); + // delete the downloaded file + out.delete(); + } } public void testHugeDownloads() throws Exception { @@ -189,25 +190,26 @@ public class TFTPTest extends TestCase { private void testUpload(final int mode, final File file) throws Exception { // Create our TFTP instance to handle the file transfer. - final TFTPClient tftp = new TFTPClient(); - tftp.open(); - tftp.setSoTimeout(2000); + try (TFTPClient tftp = new TFTPClient()) { + tftp.open(); + tftp.setSoTimeout(2000); - final File in = new File(serverDirectory, filePrefix + "upload"); - // cleanup old failed runs - in.delete(); - assertFalse("Couldn't clear output location", in.exists()); + final File in = new File(serverDirectory, filePrefix + "upload"); + // cleanup old failed runs + in.delete(); + assertFalse("Couldn't clear output location", in.exists()); - try (final FileInputStream fis = new FileInputStream(file)) { - tftp.sendFile(in.getName(), mode, fis, "localhost", SERVER_PORT); - } + try (final FileInputStream fis = new FileInputStream(file)) { + tftp.sendFile(in.getName(), mode, fis, "localhost", SERVER_PORT); + } - // need to give the server a bit of time to receive our last packet, and - // close out its file buffers, etc. - Thread.sleep(100); - assertTrue("file not created", in.exists()); - assertTrue("files not identical on file " + file, filesIdentical(file, in)); + // need to give the server a bit of time to receive our last packet, and + // close out its file buffers, etc. + Thread.sleep(100); + assertTrue("file not created", in.exists()); + assertTrue("files not identical on file " + file, filesIdentical(file, in)); - in.delete(); + in.delete(); + } } }