Skip to content
Permalink
Browse files

Ask ftp/http server for its assigned port instead of trying to assign…

… an available port
  • Loading branch information...
rfscholte committed Aug 22, 2019
1 parent 184cdad commit d300b118476aaec5a755eae68ea3bcff75fd247e
@@ -42,10 +42,10 @@ public void testStreamingWagon()
{
if ( supportsGetIfNewer() )
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

streamRoundTripTesting();

tearDownWagonTestingFixtures();
@@ -55,10 +55,10 @@ public void testStreamingWagon()
public void testFailedGetToStream()
throws Exception
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

message( "Getting test artifact from test repository " + testRepository );

StreamingWagon wagon = (StreamingWagon) getWagon();
@@ -103,8 +103,9 @@ public void testWagonGetIfNewerToStreamIsNewer()
{
if ( supportsGetIfNewer() )
{
setupRepositories();
setupWagonTestingFixtures();
setupRepositories();

int expectedSize = putFile();
// CHECKSTYLE_OFF: MagicNumber
getIfNewerToStream( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ) + 30000, false,
@@ -118,8 +119,8 @@ public void testWagonGetIfNewerToStreamIsOlder()
{
if ( supportsGetIfNewer() )
{
setupRepositories();
setupWagonTestingFixtures();
setupRepositories();
int expectedSize = putFile();
getIfNewerToStream( new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2006-01-01" ).getTime(), true,
expectedSize );
@@ -131,8 +132,8 @@ public void testWagonGetIfNewerToStreamIsSame()
{
if ( supportsGetIfNewer() )
{
setupRepositories();
setupWagonTestingFixtures();
setupRepositories();
int expectedSize = putFile();
getIfNewerToStream( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ), false,
expectedSize );
@@ -172,8 +173,8 @@ public void testFailedGetIfNewerToStream()
{
if ( supportsGetIfNewer() )
{
setupRepositories();
setupWagonTestingFixtures();
setupRepositories();
message( "Getting test artifact from test repository " + testRepository );
StreamingWagon wagon = (StreamingWagon) getWagon();
wagon.addTransferListener( checksumObserver );
@@ -42,7 +42,6 @@

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
@@ -136,23 +135,6 @@ protected abstract String getTestRepositoryUrl()
*/
protected abstract String getProtocol();

public static ServerSocket newServerSocket( int... ports )
{
for ( int port : ports )
{
try
{
return new ServerSocket( port );
}
catch ( IOException ex )
{
continue;
}
}

throw new RuntimeException( "no port available" );
}

// ----------------------------------------------------------------------
// 1. Create a local file repository which mimic a users local file
// Repository.
@@ -270,10 +252,10 @@ protected void message( String message )
public void testWagon()
throws Exception
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

fileRoundTripTesting();

tearDownWagonTestingFixtures();
@@ -284,8 +266,8 @@ public void testWagonGetIfNewerIsNewer()
{
if ( supportsGetIfNewer() )
{
setupRepositories();
setupWagonTestingFixtures();
setupRepositories();
int expectedSize = putFile();
// CHECKSTYLE_OFF: MagicNumber
getIfNewer( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ) + 30000, false,
@@ -315,8 +297,8 @@ public void testWagonGetIfNewerIsSame()
{
if ( supportsGetIfNewer() )
{
setupRepositories();
setupWagonTestingFixtures();
setupRepositories();
int expectedSize = putFile();
getIfNewer( getExpectedLastModifiedOnGet( testRepository, new Resource( resource ) ), false, expectedSize );
}
@@ -327,8 +309,8 @@ public void testWagonGetIfNewerIsOlder()
{
if ( supportsGetIfNewer() )
{
setupRepositories();
setupWagonTestingFixtures();
setupRepositories();
int expectedSize = putFile();
getIfNewer( new SimpleDateFormat( "yyyy-MM-dd" ).parse( "2006-01-01" ).getTime(), true, expectedSize );
}
@@ -431,10 +413,10 @@ private void replaceMockForSkippedGetIfNewer( Wagon wagon, int expectedSize )
public void testWagonPutDirectory()
throws Exception
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

Wagon wagon = getWagon();

if ( wagon.supportsDirectoryCopy() )
@@ -479,10 +461,10 @@ public void testWagonPutDirectory()
public void testWagonPutDirectoryDeepDestination()
throws Exception
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

Wagon wagon = getWagon();

if ( wagon.supportsDirectoryCopy() )
@@ -533,10 +515,10 @@ public void testWagonPutDirectoryWhenDirectoryAlreadyExists()

final String[] resources = { "a/test-resource-2.txt", "a/b/test-resource-3.txt", "c/test-resource-4.txt" };

setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

Wagon wagon = getWagon();

if ( wagon.supportsDirectoryCopy() )
@@ -585,10 +567,10 @@ public void testWagonPutDirectoryForDot()

final String[] resources = { "a/test-resource-2.txt", "a/b/test-resource-3.txt", "c/test-resource-4.txt" };

setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

Wagon wagon = getWagon();

if ( wagon.supportsDirectoryCopy() )
@@ -688,10 +670,10 @@ private void writeTestFile( String child )
public void testFailedGet()
throws Exception
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

message( "Getting test artifact from test repository " + testRepository );

Wagon wagon = getWagon();
@@ -729,8 +711,8 @@ public void testFailedGetIfNewer()
{
if ( supportsGetIfNewer() )
{
setupRepositories();
setupWagonTestingFixtures();
setupRepositories();
message( "Getting test artifact from test repository " + testRepository );
Wagon wagon = getWagon();
wagon.addTransferListener( checksumObserver );
@@ -767,10 +749,10 @@ public void testFailedGetIfNewer()
public void testWagonGetFileList()
throws Exception
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

String dirName = "file-list";

String filenames[] =
@@ -821,10 +803,10 @@ public void testWagonGetFileList()
public void testWagonGetFileListWhenDirectoryDoesNotExist()
throws Exception
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

String dirName = "file-list-unexisting";

Wagon wagon = getWagon();
@@ -857,10 +839,10 @@ public void testWagonGetFileListWhenDirectoryDoesNotExist()
public void testWagonResourceExists()
throws Exception
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

Wagon wagon = getWagon();

putFile();
@@ -883,10 +865,10 @@ public void testWagonResourceExists()
public void testWagonResourceNotExists()
throws Exception
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

Wagon wagon = getWagon();

wagon.connect( testRepository, getAuthInfo() );
@@ -123,8 +123,6 @@ protected void setupWagonTestingFixtures()
server.setHandler( handlers );

server.start();

testRepository.setUrl( getTestRepositoryUrl() );
}

protected final int getTestRepositoryPort()
@@ -20,8 +20,6 @@
*/

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;

@@ -30,6 +28,7 @@
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.Listener;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
@@ -49,30 +48,15 @@
public class FtpWagonTest
extends StreamingWagonTestCase
{
static private FtpServer server;
private FtpServer server;

// private static final int testRepositoryPort = 10023 + new Random().nextInt( 16 );

private static int testRepositoryPort;
private int testRepositoryPort;

protected String getProtocol()
{
return "ftp";
}

static
{
// claim number, release it again so it can be reclaimed by ftp server
try (ServerSocket socket = newServerSocket( 10023, 10024, 10025, 10026 ))
{
testRepositoryPort = socket.getLocalPort();
}
catch ( IOException e )
{
e.printStackTrace();
}
}

protected int getTestRepositoryPort() {
return testRepositoryPort;
}
@@ -86,17 +70,18 @@ protected void setupWagonTestingFixtures()
ftpHomeDir.mkdirs();
}

if (server == null)
if ( server == null )
{
FtpServerFactory serverFactory = new FtpServerFactory();

ListenerFactory factory = new ListenerFactory();

// set the port of the listener
factory.setPort(getTestRepositoryPort());
factory.setPort( 0 );

// replace the default listener
serverFactory.addListener("default", factory.createListener());
Listener defaultListener = factory.createListener();
serverFactory.addListener("default", defaultListener );

PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
UserManager um = userManagerFactory.createUserManager();
@@ -122,6 +107,7 @@ protected void setupWagonTestingFixtures()
// start the server
server.start();

testRepositoryPort = defaultListener.getPort();
}
}

@@ -207,10 +193,10 @@ public void testDefaultUserName()
public void testPutDirectoryCreation()
throws Exception
{
setupRepositories();

setupWagonTestingFixtures();

setupRepositories();

Wagon wagon = getWagon();

if ( wagon.supportsDirectoryCopy() )

0 comments on commit d300b11

Please sign in to comment.
You can’t perform that action at this time.