Repository: mina-sshd Updated Branches: refs/heads/master 20695fddf -> 313551c29
Replaced usage of java.io.File with java.nio.file.Path wherever applicable Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/313551c2 Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/313551c2 Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/313551c2 Branch: refs/heads/master Commit: 313551c29e3645580aeecef671a0d83f0a95e577 Parents: 42acff5 Author: Lyor Goldstein <[email protected]> Authored: Sat Oct 6 17:06:23 2018 +0300 Committer: Goldstein Lyor <[email protected]> Committed: Sun Oct 7 07:44:02 2018 +0300 ---------------------------------------------------------------------- .../apache/sshd/cli/server/SshFsMounter.java | 8 +- .../common/keyprovider/FileKeyPairProvider.java | 5 - .../sshd/util/test/CommonTestSupportUtils.java | 120 +++++-------------- .../apache/sshd/util/test/JUnitTestSupport.java | 4 +- .../KnownHostsServerKeyVerifierTest.java | 4 +- .../file/root/RootedFileSystemProviderTest.java | 2 +- .../sshd/server/auth/BaseAuthenticatorTest.java | 27 +++-- .../org/apache/sshd/client/scp/ScpTest.java | 41 ++++--- 8 files changed, 77 insertions(+), 134 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/313551c2/sshd-cli/src/test/java/org/apache/sshd/cli/server/SshFsMounter.java ---------------------------------------------------------------------- diff --git a/sshd-cli/src/test/java/org/apache/sshd/cli/server/SshFsMounter.java b/sshd-cli/src/test/java/org/apache/sshd/cli/server/SshFsMounter.java index 7a15421..914714c 100644 --- a/sshd-cli/src/test/java/org/apache/sshd/cli/server/SshFsMounter.java +++ b/sshd-cli/src/test/java/org/apache/sshd/cli/server/SshFsMounter.java @@ -19,11 +19,11 @@ package org.apache.sshd.cli.server; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -310,11 +310,11 @@ public final class SshFsMounter extends SshServerCliSupport { Map<String, Object> props = sshd.getProperties(); props.putAll(options); PropertyResolver resolver = PropertyResolverUtils.toPropertyResolver(options); - File targetFolder = Objects.requireNonNull(CommonTestSupportUtils.detectTargetFolder(MounterCommandFactory.class), "Failed to detect target folder"); + Path targetFolder = Objects.requireNonNull(CommonTestSupportUtils.detectTargetFolder(MounterCommandFactory.class), "Failed to detect target folder"); if (SecurityUtils.isBouncyCastleRegistered()) { - sshd.setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(new File(targetFolder, "key.pem").toPath())); + sshd.setKeyPairProvider(SecurityUtils.createGeneratorHostKeyProvider(targetFolder.resolve("key.pem"))); } else { - sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File(targetFolder, "key.ser").toPath())); + sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(targetFolder.resolve("key.ser"))); } // Should come AFTER key pair provider setup so auto-welcome can be generated if needed setupServerBanner(sshd, resolver); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/313551c2/sshd-common/src/main/java/org/apache/sshd/common/keyprovider/FileKeyPairProvider.java ---------------------------------------------------------------------- diff --git a/sshd-common/src/main/java/org/apache/sshd/common/keyprovider/FileKeyPairProvider.java b/sshd-common/src/main/java/org/apache/sshd/common/keyprovider/FileKeyPairProvider.java index c4aae97..fa52eb2 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/keyprovider/FileKeyPairProvider.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/keyprovider/FileKeyPairProvider.java @@ -18,7 +18,6 @@ */ package org.apache.sshd.common.keyprovider; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; @@ -64,10 +63,6 @@ public class FileKeyPairProvider extends AbstractResourceKeyPairProvider<Path> { return files; } - public void setFiles(Collection<File> files) { - setPaths(GenericUtils.map(files, File::toPath)); - } - public void setPaths(Collection<? extends Path> paths) { // use absolute path in order to have unique cache keys Collection<Path> resolved = GenericUtils.map(paths, Path::toAbsolutePath); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/313551c2/sshd-common/src/test/java/org/apache/sshd/util/test/CommonTestSupportUtils.java ---------------------------------------------------------------------- diff --git a/sshd-common/src/test/java/org/apache/sshd/util/test/CommonTestSupportUtils.java b/sshd-common/src/test/java/org/apache/sshd/util/test/CommonTestSupportUtils.java index 7ae3703..38dd8b0 100644 --- a/sshd-common/src/test/java/org/apache/sshd/util/test/CommonTestSupportUtils.java +++ b/sshd-common/src/test/java/org/apache/sshd/util/test/CommonTestSupportUtils.java @@ -31,6 +31,7 @@ import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; +import java.nio.file.Paths; import java.security.CodeSource; import java.security.GeneralSecurityException; import java.security.KeyPair; @@ -305,26 +306,26 @@ public final class CommonTestSupportUtils { * as the starting point for the "target" folder lookup up the * hierarchy * @return The "target" <U>folder</U> - {@code null} if not found - * @see #detectTargetFolder(File) + * @see #detectTargetFolder(Path) */ - public static File detectTargetFolder(Class<?> anchor) { - return detectTargetFolder(getClassContainerLocationFile(anchor)); + public static Path detectTargetFolder(Class<?> anchor) { + return detectTargetFolder(getClassContainerLocationPath(anchor)); } /** * @param clazz A {@link Class} object - * @return A {@link File} of the location of the class bytes container + * @return A {@link Path} of the location of the class bytes container * - e.g., the root folder, the containing JAR, etc.. Returns * {@code null} if location could not be resolved - * @throws IllegalArgumentException If location is not a valid {@link File} location + * @throws IllegalArgumentException If location is not a valid {@link Path} location * @see #getClassContainerLocationURI(Class) - * @see #toFileSource(URI) + * @see #toPathSource(URI) */ - public static File getClassContainerLocationFile(Class<?> clazz) + public static Path getClassContainerLocationPath(Class<?> clazz) throws IllegalArgumentException { try { URI uri = getClassContainerLocationURI(clazz); - return toFileSource(uri); + return toPathSource(uri); } catch (URISyntaxException | MalformedURLException e) { throw new IllegalArgumentException(e.getClass().getSimpleName() + ": " + e.getMessage(), e); } @@ -332,21 +333,21 @@ public final class CommonTestSupportUtils { /** * Converts a {@link URL} that may refer to an internal resource to - * a {@link File} representing is "source" container (e.g., + * a {@link Path} representing is "source" container (e.g., * if it is a resource in a JAR, then the result is the JAR's path) * * @param url The {@link URL} - ignored if {@code null} - * @return The matching {@link File} + * @return The matching {@link Path} * @throws MalformedURLException If source URL does not refer to a file location - * @see #toFileSource(URI) + * @see #toPathSource(URI) */ - public static File toFileSource(URL url) throws MalformedURLException { + public static Path toPathSource(URL url) throws MalformedURLException { if (url == null) { return null; } try { - return toFileSource(url.toURI()); + return toPathSource(url.toURI()); } catch (URISyntaxException e) { throw new MalformedURLException("toFileSource(" + url.toExternalForm() + ")" + " cannot (" + e.getClass().getSimpleName() + ")" @@ -356,15 +357,15 @@ public final class CommonTestSupportUtils { /** * Converts a {@link URI} that may refer to an internal resource to - * a {@link File} representing is "source" container (e.g., + * a {@link Path} representing is "source" container (e.g., * if it is a resource in a JAR, then the result is the JAR's path) * * @param uri The {@link URI} - ignored if {@code null} - * @return The matching {@link File} + * @return The matching {@link Path} * @throws MalformedURLException If source URI does not refer to a file location * @see #getURLSource(URI) */ - public static File toFileSource(URI uri) throws MalformedURLException { + public static Path toPathSource(URI uri) throws MalformedURLException { String src = getURLSource(uri); if (GenericUtils.isEmpty(src)) { return null; @@ -375,7 +376,7 @@ public final class CommonTestSupportUtils { } try { - return new File(new URI(src)); + return Paths.get(new URI(src)); } catch (URISyntaxException e) { throw new MalformedURLException("toFileSource(" + src + ")" + " cannot (" + e.getClass().getSimpleName() + ")" @@ -384,18 +385,18 @@ public final class CommonTestSupportUtils { } /** - * @param anchorFile An anchor {@link File} we want to use + * @param anchorFile An anchor {@link Path} we want to use * as the starting point for the "target" or "build" folder * lookup up the hierarchy * @return The "target" <U>folder</U> - {@code null} if not found */ - public static File detectTargetFolder(File anchorFile) { - for (File file = anchorFile; file != null; file = file.getParentFile()) { - if (!file.isDirectory()) { + public static Path detectTargetFolder(Path anchorFile) { + for (Path file = anchorFile; file != null; file = file.getParent()) { + if (!Files.isDirectory(file)) { continue; } - String name = file.getName(); + String name = Objects.toString(file.getFileName(), ""); if (TARGET_FOLDER_NAMES.contains(name)) { return file; } @@ -425,8 +426,8 @@ public final class CommonTestSupportUtils { return provider; } - File targetFolder = Objects.requireNonNull(CommonTestSupportUtils.detectTargetFolder(anchor), "Failed to detect target folder"); - File file = new File(targetFolder, "hostkey." + DEFAULT_TEST_HOST_KEY_PROVIDER_ALGORITHM.toLowerCase()); + Path targetFolder = Objects.requireNonNull(CommonTestSupportUtils.detectTargetFolder(anchor), "Failed to detect target folder"); + Path file = targetFolder.resolve("hostkey." + DEFAULT_TEST_HOST_KEY_PROVIDER_ALGORITHM.toLowerCase()); provider = createTestHostKeyProvider(file); KeyPairProvider prev = KEYPAIR_PROVIDER_HOLDER.getAndSet(provider); @@ -437,10 +438,6 @@ public final class CommonTestSupportUtils { } } - public static KeyPairProvider createTestHostKeyProvider(File file) { - return createTestHostKeyProvider(Objects.requireNonNull(file, "No file").toPath()); - } - public static KeyPairProvider createTestHostKeyProvider(Path path) { SimpleGeneratorHostKeyProvider keyProvider = new SimpleGeneratorHostKeyProvider(); keyProvider.setPath(Objects.requireNonNull(path, "No path")); @@ -460,69 +457,15 @@ public final class CommonTestSupportUtils { return Objects.requireNonNull(iter.next(), "No key pair in iterator"); } - private static File getFile(String resource) { + private static Path getFile(String resource) { URL url = CommonTestSupportUtils.class.getClassLoader().getResource(resource); try { - return new File(url.toURI()); + return Paths.get(url.toURI()); } catch (URISyntaxException e) { - return new File(url.getPath()); - } - } - - public static File resolve(File root, String... children) { - if (GenericUtils.isEmpty(children)) { - return root; - } else { - return resolve(root, Arrays.asList(children)); + return Paths.get(url.getPath()); } } - public static File resolve(File root, Collection<String> children) { - File path = root; - if (!GenericUtils.isEmpty(children)) { - for (String child : children) { - path = new File(path, child); - } - } - - return path; - } - - /** - * Removes the specified file - if it is a directory, then its children - * are deleted recursively and then the directory itself. <B>Note:</B> - * no attempt is made to make sure that {@link File#delete()} was successful - * - * @param file The {@link File} to be deleted - ignored if {@code null} - * or does not exist anymore - * @return The <tt>file</tt> argument - */ - public static File deleteRecursive(File file) { - if ((file == null) || (!file.exists())) { - return file; - } - - if (file.isDirectory()) { - File[] children = file.listFiles(); - if (!GenericUtils.isEmpty(children)) { - for (File child : children) { - deleteRecursive(child); - } - } - } - - // seems that if a file is not writable it cannot be deleted - if (!file.canWrite()) { - file.setWritable(true, false); - } - - if (!file.delete()) { - System.err.append("Failed to delete ").println(file.getAbsolutePath()); - } - - return file; - } - /** * Removes the specified file - if it is a directory, then its children * are deleted recursively and then the directory itself. @@ -568,15 +511,16 @@ public final class CommonTestSupportUtils { } public static FileKeyPairProvider createTestKeyPairProvider(String resource) { - File file = getFile(resource); - String filePath = file.getAbsolutePath(); + Path file = getFile(resource); + file = file.toAbsolutePath(); + String filePath = Objects.toString(file, ""); FileKeyPairProvider provider = PROVIDERS_MAP.get(filePath); if (provider != null) { return provider; } provider = new FileKeyPairProvider(); - provider.setFiles(Collections.singletonList(file)); + provider.setPaths(Collections.singletonList(file)); provider = validateKeyPairProvider(provider); FileKeyPairProvider prev = PROVIDERS_MAP.put(filePath, provider); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/313551c2/sshd-common/src/test/java/org/apache/sshd/util/test/JUnitTestSupport.java ---------------------------------------------------------------------- diff --git a/sshd-common/src/test/java/org/apache/sshd/util/test/JUnitTestSupport.java b/sshd-common/src/test/java/org/apache/sshd/util/test/JUnitTestSupport.java index a854699..af74b4b 100644 --- a/sshd-common/src/test/java/org/apache/sshd/util/test/JUnitTestSupport.java +++ b/sshd-common/src/test/java/org/apache/sshd/util/test/JUnitTestSupport.java @@ -172,8 +172,8 @@ public abstract class JUnitTestSupport extends Assert { protected Path detectTargetFolder() throws IllegalArgumentException { synchronized (TEMP_SUBFOLDER_NAME) { if (targetFolder == null) { - File path = CommonTestSupportUtils.detectTargetFolder(getClass()); - targetFolder = Objects.requireNonNull(path, "Failed to detect target folder").toPath(); + Path path = CommonTestSupportUtils.detectTargetFolder(getClass()); + targetFolder = Objects.requireNonNull(path, "Failed to detect target folder"); } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/313551c2/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/KnownHostsServerKeyVerifierTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/KnownHostsServerKeyVerifierTest.java b/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/KnownHostsServerKeyVerifierTest.java index 9151af2..165d172 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/KnownHostsServerKeyVerifierTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/keyverifier/KnownHostsServerKeyVerifierTest.java @@ -19,12 +19,12 @@ package org.apache.sshd.client.keyverifier; -import java.io.File; import java.io.IOException; import java.net.SocketAddress; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.security.KeyPair; import java.security.PublicKey; @@ -79,7 +79,7 @@ public class KnownHostsServerKeyVerifierTest extends BaseTestSupport { public static void loadHostsEntries() throws Exception { URL url = KnownHostsServerKeyVerifierTest.class.getResource(KnownHostEntry.STD_HOSTS_FILENAME); assertNotNull("Missing test file resource", url); - entriesFile = new File(url.toURI()).toPath(); + entriesFile = Paths.get(url.toURI()); outputDebugMessage("loadHostsEntries(%s)", entriesFile); hostsEntries = loadEntries(entriesFile); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/313551c2/sshd-core/src/test/java/org/apache/sshd/common/file/root/RootedFileSystemProviderTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/common/file/root/RootedFileSystemProviderTest.java b/sshd-core/src/test/java/org/apache/sshd/common/file/root/RootedFileSystemProviderTest.java index 86c8c93..db2f1be 100644 --- a/sshd-core/src/test/java/org/apache/sshd/common/file/root/RootedFileSystemProviderTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/common/file/root/RootedFileSystemProviderTest.java @@ -67,7 +67,7 @@ public class RootedFileSystemProviderTest extends AssertableFile { @BeforeClass public static void initializeFileSystem() throws IOException { Path targetFolder = Objects.requireNonNull( - CommonTestSupportUtils.detectTargetFolder(RootedFileSystemProviderTest.class), "Failed to detect target folder").toPath(); + CommonTestSupportUtils.detectTargetFolder(RootedFileSystemProviderTest.class), "Failed to detect target folder"); rootSandbox = FileHelper.createTestSandbox(targetFolder.resolve(TEMP_SUBFOLDER_NAME)); fileSystem = (RootedFileSystem) new RootedFileSystemProvider().newFileSystem(rootSandbox, Collections.emptyMap()); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/313551c2/sshd-ldap/src/test/java/org/apache/sshd/server/auth/BaseAuthenticatorTest.java ---------------------------------------------------------------------- diff --git a/sshd-ldap/src/test/java/org/apache/sshd/server/auth/BaseAuthenticatorTest.java b/sshd-ldap/src/test/java/org/apache/sshd/server/auth/BaseAuthenticatorTest.java index d4881ca..1807985 100644 --- a/sshd-ldap/src/test/java/org/apache/sshd/server/auth/BaseAuthenticatorTest.java +++ b/sshd-ldap/src/test/java/org/apache/sshd/server/auth/BaseAuthenticatorTest.java @@ -20,6 +20,7 @@ package org.apache.sshd.server.auth; import java.io.File; +import java.nio.file.Path; import java.util.AbstractMap.SimpleImmutableEntry; import java.util.Comparator; import java.util.List; @@ -103,25 +104,25 @@ public abstract class BaseAuthenticatorTest extends BaseTestSupport { @SuppressWarnings("checkstyle:avoidnestedblocks") public static SimpleImmutableEntry<LdapServer, DirectoryService> startApacheDs(Class<?> anchor) throws Exception { Logger log = LoggerFactory.getLogger(anchor); - File targetFolder = Objects.requireNonNull(CommonTestSupportUtils.detectTargetFolder(anchor), "Failed to detect target folder"); - File anchorFolder = CommonTestSupportUtils.resolve(targetFolder, anchor.getSimpleName(), "apacheds-work"); - File workingDirectory = assertHierarchyTargetFolderExists(CommonTestSupportUtils.deleteRecursive(anchorFolder)); + Path targetFolder = Objects.requireNonNull(CommonTestSupportUtils.detectTargetFolder(anchor), "Failed to detect target folder"); + Path anchorFolder = CommonTestSupportUtils.resolve(targetFolder, anchor.getSimpleName(), "apacheds-work"); + Path workingDirectory = assertHierarchyTargetFolderExists(CommonTestSupportUtils.deleteRecursive(anchorFolder)); DirectoryService directoryService = new DefaultDirectoryService(); - directoryService.setWorkingDirectory(workingDirectory); + directoryService.setWorkingDirectory(workingDirectory.toFile()); SchemaService schemaService = directoryService.getSchemaService(); SchemaPartition schemaPartition = schemaService.getSchemaPartition(); LdifPartition ldifPartition = new LdifPartition(); // see DefaultSchemaLdifExtractor#SCHEMA... - File schemaRepository = assertHierarchyTargetFolderExists(new File(workingDirectory, "schema")); - ldifPartition.setWorkingDirectory(schemaRepository.getAbsolutePath()); + Path schemaRepository = assertHierarchyTargetFolderExists(workingDirectory.resolve("schema")); + ldifPartition.setWorkingDirectory(Objects.toString(schemaRepository.toAbsolutePath(), null)); - SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(workingDirectory); + SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(workingDirectory.toFile()); extractor.extractOrCopy(true); schemaPartition.setWrappedPartition(ldifPartition); - SchemaLoader loader = new LdifSchemaLoader(schemaRepository); + SchemaLoader loader = new LdifSchemaLoader(schemaRepository.toFile()); SchemaManager schemaManager = new DefaultSchemaManager(loader); directoryService.setSchemaManager(schemaManager); @@ -142,8 +143,8 @@ public abstract class BaseAuthenticatorTest extends BaseTestSupport { JdbmPartition systemPartition = new JdbmPartition(); systemPartition.setId("system"); - File partitionFolder = CommonTestSupportUtils.deleteRecursive(new File(workingDirectory, systemPartition.getId())); - systemPartition.setPartitionDir(assertHierarchyTargetFolderExists(partitionFolder)); + Path partitionFolder = CommonTestSupportUtils.deleteRecursive(workingDirectory.resolve(systemPartition.getId())); + systemPartition.setPartitionDir(assertHierarchyTargetFolderExists(partitionFolder).toFile()); systemPartition.setSuffix(ServerDNConstants.SYSTEM_DN); systemPartition.setSchemaManager(schemaManager); directoryService.setSystemPartition(systemPartition); @@ -155,8 +156,8 @@ public abstract class BaseAuthenticatorTest extends BaseTestSupport { partition.setId("users"); partition.setSuffix(BASE_DN_TEST); - File partitionFolder = CommonTestSupportUtils.deleteRecursive(new File(workingDirectory, partition.getId())); - partition.setPartitionDir(assertHierarchyTargetFolderExists(partitionFolder)); + Path partitionFolder = CommonTestSupportUtils.deleteRecursive(workingDirectory.resolve(partition.getId())); + partition.setPartitionDir(assertHierarchyTargetFolderExists(partitionFolder.toFile())); directoryService.addPartition(partition); } @@ -256,7 +257,7 @@ public abstract class BaseAuthenticatorTest extends BaseTestSupport { log.info("Directory service shut down"); log.info("Deleting " + workDir.getAbsolutePath()); - CommonTestSupportUtils.deleteRecursive(workDir); + CommonTestSupportUtils.deleteRecursive(workDir.toPath()); log.info(workDir.getAbsolutePath() + " deleted"); } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/313551c2/sshd-scp/src/test/java/org/apache/sshd/client/scp/ScpTest.java ---------------------------------------------------------------------- diff --git a/sshd-scp/src/test/java/org/apache/sshd/client/scp/ScpTest.java b/sshd-scp/src/test/java/org/apache/sshd/client/scp/ScpTest.java index 21ff1a4..f6b89db 100644 --- a/sshd-scp/src/test/java/org/apache/sshd/client/scp/ScpTest.java +++ b/sshd-scp/src/test/java/org/apache/sshd/client/scp/ScpTest.java @@ -28,12 +28,14 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.OpenOption; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.nio.file.attribute.PosixFilePermission; import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.EnumSet; +import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -931,19 +933,19 @@ public class ScpTest extends BaseTestSupport { String unixDir = "target/scp"; String fileName = getCurrentTestName() + ".txt"; String unixPath = unixDir + "/" + fileName; - File root = new File(unixDir); - File target = new File(unixPath); + Path root = Paths.get(unixDir); + Path target = Paths.get(unixPath); CommonTestSupportUtils.deleteRecursive(root); - root.mkdirs(); - assertTrue("Failed to ensure existence of " + root, root.exists()); + Files.createDirectories(root); + assertTrue("Failed to ensure existence of " + root, Files.exists(root)); - target.delete(); - assertFalse("Failed to delete 1st time: " + target, target.exists()); + Files.deleteIfExists(target); + assertFalse("Failed to delete 1st time: " + target, Files.exists(target)); sendFile(session, unixPath, target, data); assertFileLength(target, data.length(), TimeUnit.SECONDS.toMillis(11L)); - target.delete(); - assertFalse("Failed to delete 2nd time: " + target, target.exists()); + Files.deleteIfExists(target); + assertFalse("Failed to delete 2nd time: " + target, Files.exists(target)); sendFile(session, unixDir, target, data); assertFileLength(target, data.length(), TimeUnit.SECONDS.toMillis(11L)); @@ -954,8 +956,8 @@ public class ScpTest extends BaseTestSupport { assertEquals("Mismatched file data", data, readFile(session, unixPath, target)); assertEquals("Mismatched dir data", data, readDir(session, unixDir, target)); - target.delete(); - root.delete(); + Files.deleteIfExists(target); + Files.deleteIfExists(root); sendDir(session, "target", ScpHelper.SCP_COMMAND_PREFIX, fileName, data); assertFileLength(target, data.length(), TimeUnit.SECONDS.toMillis(11L)); @@ -1024,12 +1026,12 @@ public class ScpTest extends BaseTestSupport { } } - protected String readFile(com.jcraft.jsch.Session session, String path, File target) throws Exception { + protected String readFile(com.jcraft.jsch.Session session, String path, Path target) throws Exception { ChannelExec c = (ChannelExec) session.openChannel(Channel.CHANNEL_EXEC); c.setCommand("scp -f " + path); c.connect(); - String fileName = target.getName(); + String fileName = Objects.toString(target.getFileName(), null); try (OutputStream os = c.getOutputStream(); InputStream is = c.getInputStream()) { @@ -1037,7 +1039,7 @@ public class ScpTest extends BaseTestSupport { os.flush(); String header = readLine(is); - String expHeader = "C" + ScpHelper.DEFAULT_FILE_OCTAL_PERMISSIONS + " " + target.length() + " " + fileName; + String expHeader = "C" + ScpHelper.DEFAULT_FILE_OCTAL_PERMISSIONS + " " + Files.size(target) + " " + fileName; assertEquals("Mismatched header for " + path, expHeader, header); String lenValue = header.substring(6, header.indexOf(' ', 6)); @@ -1059,7 +1061,7 @@ public class ScpTest extends BaseTestSupport { } } - protected String readDir(com.jcraft.jsch.Session session, String path, File target) throws Exception { + protected String readDir(com.jcraft.jsch.Session session, String path, Path target) throws Exception { ChannelExec c = (ChannelExec) session.openChannel(Channel.CHANNEL_EXEC); c.setCommand("scp -r -f " + path); c.connect(); @@ -1076,7 +1078,8 @@ public class ScpTest extends BaseTestSupport { os.flush(); header = readLine(is); - String expHeader = "C" + ScpHelper.DEFAULT_FILE_OCTAL_PERMISSIONS + " " + target.length() + " " + target.getName(); + String fileName = Objects.toString(target.getFileName(), null); + String expHeader = "C" + ScpHelper.DEFAULT_FILE_OCTAL_PERMISSIONS + " " + Files.size(target) + " " + fileName; assertEquals("Mismatched dir header for " + path, expHeader, header); int length = Integer.parseInt(header.substring(6, header.indexOf(' ', 6))); os.write(0); @@ -1118,7 +1121,7 @@ public class ScpTest extends BaseTestSupport { } } - protected void sendFile(com.jcraft.jsch.Session session, String path, File target, String data) throws Exception { + protected void sendFile(com.jcraft.jsch.Session session, String path, Path target, String data) throws Exception { ChannelExec c = (ChannelExec) session.openChannel(Channel.CHANNEL_EXEC); String command = "scp -t " + path; c.setCommand(command); @@ -1129,10 +1132,10 @@ public class ScpTest extends BaseTestSupport { assertAckReceived(is, command); - File parent = target.getParentFile(); - Collection<PosixFilePermission> perms = IoUtils.getPermissions(parent.toPath()); + Path parent = target.getParent(); + Collection<PosixFilePermission> perms = IoUtils.getPermissions(parent); String octalPerms = ScpHelper.getOctalPermissions(perms); - String name = target.getName(); + String name = Objects.toString(target.getFileName(), null); assertAckReceived(os, is, "C" + octalPerms + " " + data.length() + " " + name); os.write(data.getBytes(StandardCharsets.UTF_8));
