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 e46e975800759e6afc857dd078eebec09a08b139
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Wed Jul 19 09:22:19 2023 -0400

    Port test from JUnit 3 to 5
---
 .../org/apache/commons/net/tftp/TFTPServer.java    |  72 +++++-----
 .../apache/commons/net/tftp/TFTPServerMain.java    |   2 +-
 .../commons/net/tftp/TFTPServerPathTest.java       | 154 +++++++++++----------
 3 files changed, 120 insertions(+), 108 deletions(-)

diff --git a/src/test/java/org/apache/commons/net/tftp/TFTPServer.java 
b/src/test/java/org/apache/commons/net/tftp/TFTPServer.java
index e08b32c5..1a994896 100644
--- a/src/test/java/org/apache/commons/net/tftp/TFTPServer.java
+++ b/src/test/java/org/apache/commons/net/tftp/TFTPServer.java
@@ -53,31 +53,26 @@ import org.apache.commons.net.io.ToNetASCIIInputStream;
  * Example usage is below:
  *
  * <code>
- * public static void main(String[] args) throws Exception
- *  {
- *      if (args.length != 1)
- *      {
- *          System.out
- *                  .println("You must provide 1 argument - the base path for 
the server to serve from.");
+ * public static void main(String[] args) throws Exception {
+ *      if (args.length != 1) {
+ *          System.out.println("You must provide 1 argument - the base path 
for the server to serve from.");
  *          System.exit(1);
  *      }
  *
- *      TFTPServer ts = new TFTPServer(new File(args[0]), new File(args[0]), 
GET_AND_PUT);
- *      ts.setSocketTimeout(2000);
- *
- *      System.out.println("TFTP Server running.  Press enter to stop.");
- *      new InputStreamReader(System.in).read();
+ *      try (TFTPServer ts = new TFTPServer(new File(args[0]), new 
File(args[0]), GET_AND_PUT)) {
+ *        ts.setSocketTimeout(2000);
+ *        System.out.println("TFTP Server running.  Press enter to stop.");
+ *        new InputStreamReader(System.in).read();
+ *      }
  *
- *      ts.shutdown();
  *      System.out.println("Server shut down.");
  *      System.exit(0);
- *  }
- *
+ * }
  * </code>
  *
  * @since 2.0
  */
-public class TFTPServer implements Runnable {
+public class TFTPServer implements Runnable, AutoCloseable {
 
     public enum ServerMode {
         GET_ONLY, PUT_ONLY, GET_AND_PUT
@@ -585,6 +580,30 @@ public class TFTPServer implements Runnable {
         this(serverReadDirectory, serverWriteDirectory, DEFAULT_TFTP_PORT, 
mode, null, null);
     }
 
+    /**
+     * Closes the tftp server (and any currently running transfers) and 
release all opened network resources.
+     */
+    @Override
+    public void close() {
+        shutdownServer = true;
+
+        synchronized (transfers) {
+            transfers.forEach(TFTPTransfer::shutdown);
+        }
+
+        try {
+            serverTftp.close();
+        } catch (final RuntimeException e) {
+            // noop
+        }
+
+        try {
+            serverThread.join();
+        } catch (final InterruptedException e) {
+            // we've done the best we could, return
+        }
+    }
+
     @Override
     protected void finalize() throws Throwable {
         shutdown();
@@ -748,25 +767,12 @@ public class TFTPServer implements Runnable {
     }
 
     /**
-     * Stop the tftp server (and any currently running transfers) and release 
all opened network resources.
+     * Closes the TFTP server (and any currently running transfers) and 
release all opened network resources.
+     *
+     * @deprecated Use {@link #close()}.
      */
+    @Deprecated
     public void shutdown() {
-        shutdownServer = true;
-
-        synchronized (transfers) {
-            transfers.forEach(TFTPTransfer::shutdown);
-        }
-
-        try {
-            serverTftp.close();
-        } catch (final RuntimeException e) {
-            // noop
-        }
-
-        try {
-            serverThread.join();
-        } catch (final InterruptedException e) {
-            // we've done the best we could, return
-        }
+        close();
     }
 }
diff --git a/src/test/java/org/apache/commons/net/tftp/TFTPServerMain.java 
b/src/test/java/org/apache/commons/net/tftp/TFTPServerMain.java
index a4334703..027d2208 100644
--- a/src/test/java/org/apache/commons/net/tftp/TFTPServerMain.java
+++ b/src/test/java/org/apache/commons/net/tftp/TFTPServerMain.java
@@ -129,7 +129,7 @@ public class TFTPServerMain {
             @Override
             public void run() {
                 System.out.println("Server shutting down");
-                tftpS.shutdown();
+                tftpS.close();
                 System.out.println("Server exit");
             }
         });
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 d9d89aeb..649f891e 100644
--- a/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java
+++ b/src/test/java/org/apache/commons/net/tftp/TFTPServerPathTest.java
@@ -16,127 +16,133 @@
  */
 package org.apache.commons.net.tftp;
 
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 
 import org.apache.commons.net.tftp.TFTPServer.ServerMode;
-
-import junit.framework.TestCase;
+import org.junit.jupiter.api.Test;
 
 /**
- * Some basic tests to ensure that the TFTP Server is honoring its read/write 
mode, and preventing files from being read or written from outside of the 
assigned
+ * Basic tests to ensure that the TFTP Server is honoring its read/write mode, 
and preventing files from being read or written from outside of the assigned
  * roots.
  */
-public class TFTPServerPathTest extends TestCase {
+public class TFTPServerPathTest {
+
     private static final int SERVER_PORT = 6901;
     String filePrefix = "tftp-";
     File serverDirectory = new File(System.getProperty("java.io.tmpdir"));
 
+    @Test
     public void testReadOnly() throws IOException {
         // Start a read-only server
-        final TFTPServer tftpS = new TFTPServer(serverDirectory, 
serverDirectory, SERVER_PORT, ServerMode.GET_ONLY, null, null);
+        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);
+            // Create our TFTP instance to handle the file transfer.
+            final 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("Couldn't clear output location", out.exists());
+            // 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("file not created", out.exists());
+            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();
-        tftpS.shutdown();
     }
 
+    @Test
     public void testWriteOnly() throws IOException {
         // Start a write-only server
-        final TFTPServer tftpS = new TFTPServer(serverDirectory, 
serverDirectory, SERVER_PORT, ServerMode.PUT_ONLY, null, null);
+        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);
+            // Create our TFTP instance to handle the file transfer.
+            final 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("Couldn't clear output location", out.exists());
+            // 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("file not created", out.exists());
+            assertTrue(out.exists(), "file not created");
 
-        // cleanup
-        file.delete();
-        out.delete();
-        tftpS.shutdown();
+            // cleanup
+            file.delete();
+            out.delete();
+        }
     }
 
+    @Test
     public void testWriteOutsideHome() throws IOException {
         // Start a server
-        final TFTPServer tftpS = new TFTPServer(serverDirectory, 
serverDirectory, SERVER_PORT, ServerMode.GET_AND_PUT, null, null);
+        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();
+            // Create our TFTP instance to handle the file transfer.
+            final 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("test construction error", new File(serverDirectory, 
"../foo").exists());
+            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("file created when it should not have been", new 
File(serverDirectory, "../foo").exists());
+            assertFalse(new File(serverDirectory, "../foo").exists(), "file 
created when it should not have been");
 
-        // cleanup
-        file.delete();
-
-        tftpS.shutdown();
+            // cleanup
+            file.delete();
+        }
     }
 
 }

Reply via email to