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 cfb7145b3e12177bf10ce4d52885c8766ea75a18 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Feb 24 12:21:11 2024 -0500 Sort members --- .../apache/commons/net/ftp/FTPClientModeZTest.java | 204 ++++++++------------- 1 file changed, 81 insertions(+), 123 deletions(-) diff --git a/src/test/java/org/apache/commons/net/ftp/FTPClientModeZTest.java b/src/test/java/org/apache/commons/net/ftp/FTPClientModeZTest.java index a47d1249..b37aa1a9 100644 --- a/src/test/java/org/apache/commons/net/ftp/FTPClientModeZTest.java +++ b/src/test/java/org/apache/commons/net/ftp/FTPClientModeZTest.java @@ -1,34 +1,15 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package org.apache.commons.net.ftp; import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.file.Files.readAllBytes; -import static java.nio.file.Files.write; import static org.apache.commons.io.FileUtils.deleteDirectory; import static org.apache.commons.net.ftp.FTP.COMPRESSED_MODE_Z_TRANSFER_MODE; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.time.Instant; @@ -45,97 +26,10 @@ import org.apache.ftpserver.listener.ListenerFactory; import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; import org.apache.ftpserver.usermanager.impl.BaseUser; import org.apache.ftpserver.usermanager.impl.WritePermission; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - -@RunWith(Parameterized.class) -public class FTPClientModeZTest { - - private static final String DEFAULT_HOME = "ftp_root/"; - private final boolean useLocalPassiveFTP; - - @Parameters(name = "useLocalPassiveFTP={0}") - public static Boolean[] testParameters() { - return new Boolean[] { true, false }; - } - - public FTPClientModeZTest(boolean useLocalPassiveFTP) { - this.useLocalPassiveFTP = useLocalPassiveFTP; - } - - @Test - public void testRetrievingFiles() throws Exception { - - new File(DEFAULT_HOME).mkdirs(); - String filename = "test_download.txt"; - String fileContent = "Created at " + Instant.now(); - write(Paths.get(DEFAULT_HOME).resolve(filename), fileContent.getBytes(UTF_8)); - - runWithFTPserver((port, user, password) -> { - FTPClient client = new FTPClient(); - try { - client.connect("localhost", port); - client.login(user, password); - if (useLocalPassiveFTP) { - client.enterLocalPassiveMode(); - } - assertTrue("Mode Z successfully activated", - client.setFileTransferMode(COMPRESSED_MODE_Z_TRANSFER_MODE)); - - FTPFile[] files = client.listFiles(); - assertEquals("Only single file in home directory", 1, files.length); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - assertTrue("File successfully transferred", client.retrieveFile(files[0].getName(), bos)); - assertEquals("File content is not corrupted", fileContent, new String(bos.toByteArray(), UTF_8)); - } finally { - client.logout(); - } - }); - } - - @Test - public void testStoringFiles() throws Exception { - - runWithFTPserver((port, user, password) -> { - FTPClient client = new FTPClient(); - try { - client.connect("localhost", port); - client.login(user, password); - if (useLocalPassiveFTP) { - client.enterLocalPassiveMode(); - } - assertTrue("Mode Z successfully activated", - client.setFileTransferMode(COMPRESSED_MODE_Z_TRANSFER_MODE)); - FTPFile[] filesBeforeUpload = client.listFiles(); - assertEquals("No files in home directory before upload", 0, filesBeforeUpload.length); - - String filename = "test_upload.txt"; - String fileContent = "Created at " + Instant.now(); - assertTrue("File successfully transferred", - client.storeFile(filename, new ByteArrayInputStream(fileContent.getBytes(UTF_8)))); +import junit.framework.TestCase; - FTPFile[] filesAfterUpload = client.listFiles(); - assertEquals("Single file in home directory after upload", 1, filesAfterUpload.length); - - Path p = Paths.get(DEFAULT_HOME, filename); - assertEquals("File content is not corrupted", fileContent, new String(readAllBytes(p), UTF_8)); - } finally { - client.logout(); - } - }); - } - - @Before - @After - public void cleanup() throws IOException { - deleteDirectory(new File(DEFAULT_HOME)); - } +public class FTPClientModeZTest extends TestCase { private static final class FtpServerAndPort { private final int port; @@ -152,6 +46,26 @@ public class FTPClientModeZTest { void run(int port, String user, String password) throws Exception; } + private static final String DEFAULT_HOME = "ftp_root/"; + + private static UserManager initUserManager(final String username, final String password) throws FtpException { + + final PropertiesUserManagerFactory propertiesUserManagerFactory = new PropertiesUserManagerFactory(); + final UserManager userManager = propertiesUserManagerFactory.createUserManager(); + BaseUser user = new BaseUser(); + user.setName(username); + user.setPassword(password); + + List<Authority> authorities = new ArrayList<Authority>(); + authorities.add(new WritePermission()); + user.setAuthorities(authorities); + + new File(DEFAULT_HOME).mkdirs(); + user.setHomeDirectory(DEFAULT_HOME); + userManager.save(user); + return userManager; + } + private static void runWithFTPserver(Runner runner) throws Exception { String username = "test"; @@ -164,8 +78,7 @@ public class FTPClientModeZTest { } } - private static FtpServerAndPort setupPlainFTPserver(final String username, final String password) - throws FtpException { + private static FtpServerAndPort setupPlainFTPserver(final String username, final String password) throws FtpException { final FtpServerFactory serverFactory = new FtpServerFactory(); @@ -187,22 +100,67 @@ public class FTPClientModeZTest { return new FtpServerAndPort(server, listener.getPort()); } - private static UserManager initUserManager(final String username, final String password) throws FtpException { + @Override + protected void setUp() throws IOException { + deleteDirectory(new File(DEFAULT_HOME)); + } - final PropertiesUserManagerFactory propertiesUserManagerFactory = new PropertiesUserManagerFactory(); - final UserManager userManager = propertiesUserManagerFactory.createUserManager(); - BaseUser user = new BaseUser(); - user.setName(username); - user.setPassword(password); + @Override + protected void tearDown() throws Exception { + deleteDirectory(new File(DEFAULT_HOME)); + } - List<Authority> authorities = new ArrayList<Authority>(); - authorities.add(new WritePermission()); - user.setAuthorities(authorities); + public void testRetrievingFiles() throws Exception { new File(DEFAULT_HOME).mkdirs(); - user.setHomeDirectory(DEFAULT_HOME); - userManager.save(user); - return userManager; + String filename = "test_download.txt"; + String fileContent = "Created at " + Instant.now(); + Files.write(Paths.get(DEFAULT_HOME).resolve(filename), fileContent.getBytes(UTF_8)); + + runWithFTPserver((port, user, password) -> { + FTPClient client = new FTPClient(); + try { + client.connect("localhost", port); + client.login(user, password); + assertTrue("Mode Z successfully activated", client.setFileTransferMode(COMPRESSED_MODE_Z_TRANSFER_MODE)); + + FTPFile[] files = client.listFiles(); + assertEquals("Only single file in home directory", 1, files.length); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + assertTrue("File successfully transferred", client.retrieveFile(files[0].getName(), bos)); + assertEquals("File content is not corrupted", fileContent, new String(bos.toByteArray(), UTF_8)); + } finally { + client.logout(); + } + }); + } + + public void testStoringFiles() throws Exception { + + runWithFTPserver((port, user, password) -> { + FTPClient client = new FTPClient(); + try { + client.connect("localhost", port); + client.login(user, password); + assertTrue("Mode Z successfully activated", client.setFileTransferMode(COMPRESSED_MODE_Z_TRANSFER_MODE)); + + FTPFile[] filesBeforeUpload = client.listFiles(); + assertEquals("No files in home directory before upload", 0, filesBeforeUpload.length); + + String filename = "test_upload.txt"; + String fileContent = "Created at " + Instant.now(); + assertTrue("File successfully transferred", client.storeFile(filename, new ByteArrayInputStream(fileContent.getBytes(UTF_8)))); + + FTPFile[] filesAfterUpload = client.listFiles(); + assertEquals("Single file in home directory after upload", 1, filesAfterUpload.length); + + Path p = Paths.get(DEFAULT_HOME, filename); + assertEquals("File content is not corrupted", fileContent, new String(readAllBytes(p), UTF_8)); + } finally { + client.logout(); + } + }); } }