You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/09/13 19:05:00 UTC

[commons-vfs] branch master updated: - Use ephemeral ports in tests instead of a searching for free port which may end up in use anyway: FTP, FTPS, HTTP HC3, HTTP HC4, HTTP HC5, URL. - TODO WebDev/JackRabbit 1: unclear how to extract the port once JR has started. - TODO WebDev/JackRabbit 2: unclear how to extract the port once JR has started.

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-vfs.git


The following commit(s) were added to refs/heads/master by this push:
     new 7ef5498  - Use ephemeral ports in tests instead of a searching for free port which may end up in use anyway: FTP, FTPS, HTTP HC3, HTTP HC4, HTTP HC5, URL. - TODO WebDev/JackRabbit 1: unclear how to extract the port once JR has started. - TODO WebDev/JackRabbit 2: unclear how to extract the port once JR has started.
7ef5498 is described below

commit 7ef5498e5c490c6ca313c8ec6e353eb7feb6041f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 13 15:04:49 2020 -0400

    - Use ephemeral ports in tests instead of a searching for free port
    which may end up in use anyway: FTP, FTPS, HTTP HC3, HTTP HC4, HTTP HC5,
    URL.
    - TODO WebDev/JackRabbit 1: unclear how to extract the port once JR has
    started.
    - TODO WebDev/JackRabbit 2: unclear how to extract the port once JR has
    started.
---
 .../commons/vfs2/provider/ftp/FTPClientWrapper.java      |  6 ++----
 .../vfs2/provider/ftp/test/FtpProviderTestCase.java      | 12 +++---------
 .../provider/ftps/test/AbstractFtpsProviderTestCase.java | 14 ++++----------
 .../provider/ftps/test/FtpsProviderExplicitTestCase.java |  1 +
 .../vfs2/provider/http/test/HttpProviderTestCase.java    | 16 +++-------------
 .../vfs2/provider/http4/test/Http4ProviderTestCase.java  | 16 +++-------------
 .../vfs2/provider/http5/test/Http5ProviderTestCase.java  | 16 +++-------------
 .../provider/sftp/test/AbstractSftpProviderTestCase.java |  8 +++-----
 .../vfs2/provider/url/test/UrlProviderHttpTestCase.java  |  9 ++++-----
 .../org/apache/commons/vfs2/util/NHttpFileServer.java    |  8 ++++++++
 10 files changed, 34 insertions(+), 72 deletions(-)

diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FTPClientWrapper.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FTPClientWrapper.java
index 9b80390..53a042d 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FTPClientWrapper.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FTPClientWrapper.java
@@ -172,12 +172,10 @@ public class FTPClientWrapper implements FtpClient {
     public FTPFile[] listFiles(final String relPath) throws IOException {
         try {
             // VFS-210: return getFtpClient().listFiles(relPath);
-            final FTPFile[] files = listFilesInDirectory(relPath);
-            return files;
+            return listFilesInDirectory(relPath);
         } catch (final IOException e) {
             disconnect();
-            final FTPFile[] files = listFilesInDirectory(relPath);
-            return files;
+            return listFilesInDirectory(relPath);
         }
     }
 
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpProviderTestCase.java
index 1471517..964d6a0 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpProviderTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/test/FtpProviderTestCase.java
@@ -28,7 +28,6 @@ import org.apache.commons.vfs2.provider.ftp.FtpFileSystemConfigBuilder;
 import org.apache.commons.vfs2.provider.ftp.FtpFileType;
 import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
 import org.apache.commons.vfs2.test.ProviderTestSuite;
-import org.apache.commons.vfs2.util.FreeSocketPortUtil;
 import org.apache.ftpserver.FtpServer;
 import org.apache.ftpserver.FtpServerFactory;
 import org.apache.ftpserver.ftplet.FileSystemFactory;
@@ -70,12 +69,6 @@ public class FtpProviderTestCase extends AbstractProviderTestConfig {
         return System.getProperty(TEST_URI);
     }
 
-    static void init() throws IOException {
-        SocketPort = FreeSocketPortUtil.findFreeLocalPort();
-        // Use %40 for @ in a URL
-        ConnectionUri = "ftp://test:test@localhost:" + SocketPort;
-    }
-
     /**
      * Creates and starts an embedded Apache FTP Server (MINA).
      *
@@ -89,7 +82,6 @@ public class FtpProviderTestCase extends AbstractProviderTestConfig {
         if (Server != null) {
             return;
         }
-        init();
         final FtpServerFactory serverFactory = new FtpServerFactory();
         final PropertiesUserManagerFactory propertiesUserManagerFactory = new PropertiesUserManagerFactory();
         final URL userPropsResource = ClassLoader.getSystemClassLoader().getResource(USER_PROPS_RES);
@@ -107,7 +99,7 @@ public class FtpProviderTestCase extends AbstractProviderTestConfig {
         }
         final ListenerFactory factory = new ListenerFactory();
         // set the port of the listener
-        factory.setPort(SocketPort);
+        factory.setPort(0);
 
         // replace the default listener
         serverFactory.addListener("default", factory.createListener());
@@ -115,6 +107,8 @@ public class FtpProviderTestCase extends AbstractProviderTestConfig {
         // start the server
         Server = serverFactory.createServer();
         Server.start();
+        SocketPort = ((org.apache.ftpserver.impl.DefaultFtpServer) Server).getListener("default").getPort();
+        ConnectionUri = "ftp://test:test@localhost:" + SocketPort;
     }
 
     /**
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/test/AbstractFtpsProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/test/AbstractFtpsProviderTestCase.java
index bbe02f4..f31f4c0 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/test/AbstractFtpsProviderTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/test/AbstractFtpsProviderTestCase.java
@@ -29,7 +29,6 @@ import org.apache.commons.vfs2.provider.ftps.FtpsFileProvider;
 import org.apache.commons.vfs2.provider.ftps.FtpsFileSystemConfigBuilder;
 import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
 import org.apache.commons.vfs2.test.ProviderTestSuite;
-import org.apache.commons.vfs2.util.FreeSocketPortUtil;
 import org.apache.ftpserver.FtpServer;
 import org.apache.ftpserver.FtpServerFactory;
 import org.apache.ftpserver.ftplet.FtpException;
@@ -73,12 +72,6 @@ abstract class AbstractFtpsProviderTestCase extends AbstractProviderTestConfig {
         return System.getProperty(TEST_URI);
     }
 
-    static void init() throws IOException {
-        SocketPort = FreeSocketPortUtil.findFreeLocalPort();
-        // Use %40 for @ in a URL
-        ConnectionUri = "ftps://test:test@localhost:" + SocketPort;
-    }
-
     /**
      * Creates and starts an embedded Apache FTP Server (MINA).
      *
@@ -90,7 +83,6 @@ abstract class AbstractFtpsProviderTestCase extends AbstractProviderTestConfig {
         if (Server != null) {
             return;
         }
-        init();
         final FtpServerFactory serverFactory = new FtpServerFactory();
         final PropertiesUserManagerFactory propertiesUserManagerFactory = new PropertiesUserManagerFactory();
         final URL userPropsResource = ClassLoader.getSystemClassLoader().getResource(USER_PROPS_RES);
@@ -103,8 +95,8 @@ abstract class AbstractFtpsProviderTestCase extends AbstractProviderTestConfig {
         user.setHomeDirectory(getTestDirectory());
         serverFactory.setUserManager(userManager);
         final ListenerFactory factory = new ListenerFactory();
-        // set the port of the listener
-        factory.setPort(SocketPort);
+        // Let the OS find use an ephemeral port by using 0 here.
+        factory.setPort(0);
 
         // define SSL configuration
         final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(SERVER_JKS_RES);
@@ -125,6 +117,8 @@ abstract class AbstractFtpsProviderTestCase extends AbstractProviderTestConfig {
         // start the server
         Server = serverFactory.createServer();
         Server.start();
+        SocketPort = ((org.apache.ftpserver.impl.DefaultFtpServer) Server).getListener("default").getPort();
+        ConnectionUri = "ftps://test:test@localhost:" + SocketPort;
     }
 
     /**
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/test/FtpsProviderExplicitTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/test/FtpsProviderExplicitTestCase.java
index 47c1daa..c82a6fa 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/test/FtpsProviderExplicitTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/test/FtpsProviderExplicitTestCase.java
@@ -26,6 +26,7 @@ import junit.framework.Test;
  * Tests for FTPS file systems with explicit FTPS connection.
  */
 public class FtpsProviderExplicitTestCase extends AbstractFtpsProviderTestCase {
+
     @Override
     protected boolean isImplicit() {
         return false;
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/test/HttpProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/test/HttpProviderTestCase.java
index 86c8d91..057c8cf 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/test/HttpProviderTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/test/HttpProviderTestCase.java
@@ -31,7 +31,6 @@ import org.apache.commons.vfs2.provider.http.HttpFileProvider;
 import org.apache.commons.vfs2.provider.http.HttpFileSystemConfigBuilder;
 import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
 import org.apache.commons.vfs2.test.ProviderTestSuite;
-import org.apache.commons.vfs2.util.FreeSocketPortUtil;
 import org.apache.commons.vfs2.util.NHttpFileServer;
 import org.junit.Assert;
 
@@ -63,7 +62,9 @@ public class HttpProviderTestCase extends AbstractProviderTestConfig {
      * @throws Exception
      */
     private static void setUpClass() throws Exception {
-        Server = NHttpFileServer.start(SocketPort, new File(getTestDirectory()), 5000);
+        Server = NHttpFileServer.start(0, new File(getTestDirectory()), 5000);
+        SocketPort = Server.getPort();
+        ConnectionUri = "http://localhost:" + SocketPort;
     }
 
     /**
@@ -110,17 +111,6 @@ public class HttpProviderTestCase extends AbstractProviderTestConfig {
         }
     }
 
-    /**
-     * Builds a new test case.
-     *
-     * @throws IOException Thrown if a free local socket port cannot be found.
-     */
-    public HttpProviderTestCase() throws IOException {
-        SocketPort = FreeSocketPortUtil.findFreeLocalPort();
-        // Use %40 for @ in a URL
-        ConnectionUri = "http://localhost:" + SocketPort;
-    }
-
     private void checkReadTestsFolder(final FileObject file) throws FileSystemException {
         Assert.assertNotNull(file.getChildren());
         Assert.assertTrue(file.getChildren().length > 0);
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/test/Http4ProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/test/Http4ProviderTestCase.java
index 4e6da32..3ee7706 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/test/Http4ProviderTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/test/Http4ProviderTestCase.java
@@ -31,7 +31,6 @@ import org.apache.commons.vfs2.provider.http4.Http4FileProvider;
 import org.apache.commons.vfs2.provider.http4.Http4FileSystemConfigBuilder;
 import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
 import org.apache.commons.vfs2.test.ProviderTestSuite;
-import org.apache.commons.vfs2.util.FreeSocketPortUtil;
 import org.apache.commons.vfs2.util.NHttpFileServer;
 import org.junit.Assert;
 
@@ -64,7 +63,9 @@ public class Http4ProviderTestCase extends AbstractProviderTestConfig {
      * @throws Exception
      */
     private static void setUpClass() throws Exception {
-        Server = NHttpFileServer.start(SocketPort, new File(getTestDirectory()), 5000);
+        Server = NHttpFileServer.start(0, new File(getTestDirectory()), 5000);
+        SocketPort = Server.getPort();
+        ConnectionUri = "http4://localhost:" + SocketPort;
     }
 
     /**
@@ -111,17 +112,6 @@ public class Http4ProviderTestCase extends AbstractProviderTestConfig {
         }
     }
 
-    /**
-     * Builds a new test case.
-     *
-     * @throws IOException Thrown if a free local socket port cannot be found.
-     */
-    public Http4ProviderTestCase() throws IOException {
-        SocketPort = FreeSocketPortUtil.findFreeLocalPort();
-        // Use %40 for @ in a URL
-        ConnectionUri = "http4://localhost:" + SocketPort;
-    }
-
     private void checkReadTestsFolder(final FileObject file) throws FileSystemException {
         Assert.assertNotNull(file.getChildren());
         Assert.assertTrue(file.getChildren().length > 0);
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5/test/Http5ProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5/test/Http5ProviderTestCase.java
index 4ef7673..c13a25e 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5/test/Http5ProviderTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5/test/Http5ProviderTestCase.java
@@ -31,7 +31,6 @@ import org.apache.commons.vfs2.provider.http5.Http5FileProvider;
 import org.apache.commons.vfs2.provider.http5.Http5FileSystemConfigBuilder;
 import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
 import org.apache.commons.vfs2.test.ProviderTestSuite;
-import org.apache.commons.vfs2.util.FreeSocketPortUtil;
 import org.apache.commons.vfs2.util.NHttpFileServer;
 import org.junit.Assert;
 
@@ -64,7 +63,9 @@ public class Http5ProviderTestCase extends AbstractProviderTestConfig {
      * @throws Exception
      */
     private static void setUpClass() throws Exception {
-        Server = NHttpFileServer.start(SocketPort, new File(getTestDirectory()), 5000);
+        Server = NHttpFileServer.start(0, new File(getTestDirectory()), 5000);
+        SocketPort = Server.getPort();
+        ConnectionUri = "http5://localhost:" + SocketPort;
     }
 
     /**
@@ -111,17 +112,6 @@ public class Http5ProviderTestCase extends AbstractProviderTestConfig {
         }
     }
 
-    /**
-     * Builds a new test case.
-     *
-     * @throws IOException Thrown if a free local socket port cannot be found.
-     */
-    public Http5ProviderTestCase() throws IOException {
-        SocketPort = FreeSocketPortUtil.findFreeLocalPort();
-        // Use %40 for @ in a URL
-        ConnectionUri = "http5://localhost:" + SocketPort;
-    }
-
     private void checkReadTestsFolder(final FileObject file) throws FileSystemException {
         Assert.assertNotNull(file.getChildren());
         Assert.assertTrue(file.getChildren().length > 0);
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/sftp/test/AbstractSftpProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/sftp/test/AbstractSftpProviderTestCase.java
index 51a30af..db02798 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/sftp/test/AbstractSftpProviderTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/sftp/test/AbstractSftpProviderTestCase.java
@@ -41,7 +41,6 @@ import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
 import org.apache.commons.vfs2.provider.sftp.TrustEveryoneUserInfo;
 import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
 import org.apache.commons.vfs2.test.ProviderTestSuite;
-import org.apache.commons.vfs2.util.FreeSocketPortUtil;
 import org.apache.ftpserver.ftplet.FtpException;
 import org.apache.sshd.SshServer;
 import org.apache.sshd.common.NamedFactory;
@@ -179,13 +178,10 @@ abstract class AbstractSftpProviderTestCase extends AbstractProviderTestConfig {
         if (Server != null) {
             return;
         }
-        SocketPort = FreeSocketPortUtil.findFreeLocalPort();
-        // Use %40 for @ in a URL
-        ConnectionUri = String.format("sftp://%s@localhost:%d", DEFAULT_USER, SocketPort);
         // System.setProperty("vfs.sftp.sshdir", getTestDirectory() + "/../vfs.sftp.sshdir");
         final String tmpDir = System.getProperty("java.io.tmpdir");
         Server = SshServer.setUpDefaultServer();
-        Server.setPort(SocketPort);
+        Server.setPort(0);
         if (SecurityUtils.isBouncyCastleRegistered()) {
             // A temporary file will hold the key
             final File keyFile = File.createTempFile("key", ".pem", new File(tmpDir));
@@ -242,6 +238,8 @@ abstract class AbstractSftpProviderTestCase extends AbstractProviderTestConfig {
         Server.setFileSystemFactory(new TestFileSystemFactory());
         // HACK End
         Server.start();
+        SocketPort = Server.getPort();
+        ConnectionUri = String.format("sftp://%s@localhost:%d", DEFAULT_USER, SocketPort);
         // HACK Start
         // How do we really do simple security?
         // Do this after we start the server to simplify this set up code.
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/url/test/UrlProviderHttpTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/url/test/UrlProviderHttpTestCase.java
index e4ede78..533db0e 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/url/test/UrlProviderHttpTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/url/test/UrlProviderHttpTestCase.java
@@ -26,7 +26,6 @@ import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
 import org.apache.commons.vfs2.provider.url.UrlFileProvider;
 import org.apache.commons.vfs2.test.AbstractProviderTestConfig;
 import org.apache.commons.vfs2.test.ProviderTestSuite;
-import org.apache.commons.vfs2.util.FreeSocketPortUtil;
 import org.apache.commons.vfs2.util.NHttpFileServer;
 
 import junit.framework.Test;
@@ -57,7 +56,9 @@ public class UrlProviderHttpTestCase extends AbstractProviderTestConfig {
      * @throws Exception
      */
     private static void setUpClass() throws Exception {
-        Server = NHttpFileServer.start(SocketPort, new File(getTestDirectory()), 5000);
+        Server = NHttpFileServer.start(0, new File(getTestDirectory()), 5000);
+        SocketPort = Server.getPort();
+        ConnectionUri = "http://localhost:" + SocketPort;
     }
 
     public static Test suite() throws Exception {
@@ -90,9 +91,7 @@ public class UrlProviderHttpTestCase extends AbstractProviderTestConfig {
     }
 
     public UrlProviderHttpTestCase() throws IOException {
-        SocketPort = FreeSocketPortUtil.findFreeLocalPort();
-        // Use %40 for @ in a URL
-        ConnectionUri = "http://localhost:" + SocketPort;
+        // empty
     }
 
     /**
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpFileServer.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpFileServer.java
index 288b53b..d9288d6 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpFileServer.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/util/NHttpFileServer.java
@@ -27,6 +27,7 @@ package org.apache.commons.vfs2.util;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.InetSocketAddress;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.security.KeyManagementException;
@@ -191,6 +192,13 @@ public class NHttpFileServer {
         });
     }
 
+    public int getPort() {
+        if (server == null) {
+            return port;
+        }
+        return ((InetSocketAddress) server.getEndpoint().getAddress()).getPort();
+    }
+
     public void shutdown(final long gracePeriod, final TimeUnit timeUnit) {
         if (server != null) {
             server.shutdown(gracePeriod, timeUnit);