Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work in progress: JUnit 5 migration #227

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/test/java/org/apache/commons/net/examples/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.commons.net.examples;

import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.io.IOException;
Expand All @@ -30,7 +30,7 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.junit.Test;
import org.junit.jupiter.api.Test;

public class MainTest {

Expand Down
172 changes: 162 additions & 10 deletions src/test/java/org/apache/commons/net/ftp/AbstractFtpsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

package org.apache.commons.net.ftp;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.SocketException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.Calendar;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.NullOutputStream;
Expand All @@ -38,7 +41,9 @@
import org.apache.ftpserver.ssl.SslConfigurationFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

/**
* Tests {@link FTPSClient}.
Expand All @@ -62,6 +67,10 @@ public abstract class AbstractFtpsTest {
private static final boolean TRACE_CALLS = Boolean.parseBoolean(System.getenv("TRACE_CALLS"));
private static final boolean ADD_LISTENER = Boolean.parseBoolean(System.getenv("ADD_LISTENER"));
private static final long startTime = System.nanoTime();
private static final String USER_PROPS_RES = "org/apache/commons/net/ftpsserver/users.properties";
private static final String SERVER_JKS_RES = "org/apache/commons/net/ftpsserver/ftpserver.jks";

private final boolean endpointCheckingEnabled;

/**
* Returns the test directory as a String.
Expand Down Expand Up @@ -92,7 +101,7 @@ protected synchronized static void setupServer(final boolean implicit, final Str
final FtpServerFactory serverFactory = new FtpServerFactory();
final PropertiesUserManagerFactory propertiesUserManagerFactory = new PropertiesUserManagerFactory();
final URL userPropsResource = ClassLoader.getSystemClassLoader().getResource(userPropertiesResource);
Assert.assertNotNull(userPropertiesResource, userPropsResource);
assertNotNull(userPropsResource, userPropertiesResource);
propertiesUserManagerFactory.setUrl(userPropsResource);
final UserManager userManager = propertiesUserManagerFactory.createUserManager();
final BaseUser user = (BaseUser) userManager.getUserByName("test");
Expand All @@ -105,11 +114,11 @@ protected synchronized static void setupServer(final boolean implicit, final Str

// define SSL configuration
final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(serverJksResourceResource);
Assert.assertNotNull(serverJksResourceResource, serverJksResource);
assertNotNull(serverJksResource, serverJksResourceResource);
// System.out.println("Loading " + serverJksResource);
final SslConfigurationFactory sllConfigFactory = new SslConfigurationFactory();
final File keyStoreFile = FileUtils.toFile(serverJksResource);
Assert.assertTrue(keyStoreFile.toString(), keyStoreFile.exists());
assertTrue(keyStoreFile.exists(), keyStoreFile.toString());
sllConfigFactory.setKeystoreFile(keyStoreFile);
sllConfigFactory.setKeystorePassword("password");

Expand All @@ -136,9 +145,12 @@ protected static void trace(final String msg) {
}
}

private final boolean endpointCheckingEnabled;
@BeforeAll
public static void setupServer() throws Exception {
AbstractFtpsTest.setupServer(AbstractFtpsTest.IMPLICIT, USER_PROPS_RES, SERVER_JKS_RES, "target/test-classes/org/apache/commons/net/test-data");
}

public AbstractFtpsTest(final boolean endpointCheckingEnabled, final String userPropertiesResource, final String serverJksResource) {
public AbstractFtpsTest(final boolean endpointCheckingEnabled) {
this.endpointCheckingEnabled = endpointCheckingEnabled;
}

Expand Down Expand Up @@ -202,10 +214,150 @@ protected void retrieveFile(final String pathname) throws SocketException, IOExc
try {
// Do it twice.
// Just testing that we are not getting an SSL error (the file MUST be present).
assertTrue(pathname, client.retrieveFile(pathname, NullOutputStream.INSTANCE));
assertTrue(pathname, client.retrieveFile(pathname, NullOutputStream.INSTANCE));
assertTrue(client.retrieveFile(pathname, NullOutputStream.INSTANCE), pathname);
assertTrue(client.retrieveFile(pathname, NullOutputStream.INSTANCE), pathname);
} finally {
client.disconnect();
}
}


@Test
@Timeout(TEST_TIMEOUT)
public void testHasFeature() throws SocketException, IOException {
trace(">>testHasFeature");
loginClient().disconnect();
trace("<<testHasFeature");
}

private void testListFiles(final String pathname) throws SocketException, IOException {
final FTPSClient client = loginClient();
try {
// do it twice
assertNotNull(client.listFiles(pathname));
assertNotNull(client.listFiles(pathname));
} finally {
client.disconnect();
}
}

@Test
@Timeout(TEST_TIMEOUT)
public void testListFilesPathNameEmpty() throws SocketException, IOException {
trace(">>testListFilesPathNameEmpty");
testListFiles("");
trace("<<testListFilesPathNameEmpty");
}

@Test
@Timeout(TEST_TIMEOUT)
public void testListFilesPathNameJunk() throws SocketException, IOException {
trace(">>testListFilesPathNameJunk");
testListFiles(" Junk ");
trace("<<testListFilesPathNameJunk");
}

@Test
@Timeout(TEST_TIMEOUT)
public void testListFilesPathNameNull() throws SocketException, IOException {
trace(">>testListFilesPathNameNull");
testListFiles(null);
trace("<<testListFilesPathNameNull");
}

@Test
@Timeout(TEST_TIMEOUT)
public void testListFilesPathNameRoot() throws SocketException, IOException {
trace(">>testListFilesPathNameRoot");
testListFiles("/");
trace("<<testListFilesPathNameRoot");
}

@Test
@Timeout(TEST_TIMEOUT)
public void testMdtmCalendar() throws SocketException, IOException {
trace(">>testMdtmCalendar");
testMdtmCalendar("/file.txt");
trace("<<testMdtmCalendar");
}

private void testMdtmCalendar(final String pathname) throws SocketException, IOException {
final FTPSClient client = loginClient();
try {
// do it twice
final Calendar mdtmCalendar1 = client.mdtmCalendar(pathname);
final Calendar mdtmCalendar2 = client.mdtmCalendar(pathname);
assertNotNull(mdtmCalendar1);
assertNotNull(mdtmCalendar2);
assertEquals(mdtmCalendar1, mdtmCalendar2);
} finally {
client.disconnect();
}
}

@Test
@Timeout(TEST_TIMEOUT)
public void testMdtmFile() throws SocketException, IOException {
trace(">>testMdtmFile");
testMdtmFile("/file.txt");
trace("<<testMdtmFile");
}

private void testMdtmFile(final String pathname) throws SocketException, IOException {
final FTPSClient client = loginClient();
try {
// do it twice
final FTPFile mdtmFile1 = client.mdtmFile(pathname);
final FTPFile mdtmFile2 = client.mdtmFile(pathname);
assertNotNull(mdtmFile1);
assertNotNull(mdtmFile2);
assertEquals(mdtmFile1.toString(), mdtmFile2.toString());
} finally {
client.disconnect();
}
}

@Test
@Timeout(TEST_TIMEOUT)
public void testMdtmInstant() throws SocketException, IOException {
trace(">>testMdtmInstant");
testMdtmInstant("/file.txt");
trace("<<testMdtmInstant");
}

private void testMdtmInstant(final String pathname) throws SocketException, IOException {
final FTPSClient client = loginClient();
try {
// do it twice
final Instant mdtmInstant1 = client.mdtmInstant(pathname);
final Instant mdtmInstant2 = client.mdtmInstant(pathname);
assertNotNull(mdtmInstant1);
assertNotNull(mdtmInstant2);
assertEquals(mdtmInstant1, mdtmInstant2);
} finally {
client.disconnect();
}
}

@Test
@Timeout(TEST_TIMEOUT)
public void testOpenClose() throws SocketException, IOException {
trace(">>testOpenClose");
final FTPSClient ftpsClient = loginClient();
try {
assertTrue(ftpsClient.hasFeature("MODE"));
assertTrue(ftpsClient.hasFeature(FTPCmd.MODE));
} finally {
ftpsClient.disconnect();
}
trace("<<testOpenClose");
}

@Test
@Timeout(TEST_TIMEOUT)
public void testRetrieveFilePathNameRoot() throws SocketException, IOException {
trace(">>testRetrieveFilePathNameRoot");
retrieveFile("/file.txt");
trace("<<testRetrieveFilePathNameRoot");
}
}
Loading