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 51eb9fec Port some test code from IO to NIO APIs
51eb9fec is described below
commit 51eb9fec73ae493fdea1fe9110c568c16599c60c
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Jan 28 09:41:52 2023 -0500
Port some test code from IO to NIO APIs
---
.../vfs2/provider/DefaultFileContentTest.java | 26 ++++++++-------
.../commons/vfs2/provider/local/TempFileTests.java | 11 +++---
.../sftp/AbstractSftpProviderTestCase.java | 15 +++++----
.../commons/vfs2/provider/zip/FileLockTest.java | 26 +++++++--------
.../vfs2/provider/zip/ParseXmlInZipTest.java | 24 +++++++------
.../vfs2/provider/zip/ZipFileObjectTest.java | 39 ++++++++++++----------
6 files changed, 76 insertions(+), 65 deletions(-)
diff --git
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/DefaultFileContentTest.java
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/DefaultFileContentTest.java
index d76e0bb5..4f0834c5 100644
---
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/DefaultFileContentTest.java
+++
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/DefaultFileContentTest.java
@@ -27,6 +27,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.lang3.ArrayUtils;
@@ -64,10 +66,10 @@ public class DefaultFileContentTest {
}
private void testInputStreamBufferSize(final int bufferSize) throws
Exception {
- final File temp = File.createTempFile("temp-file-name", ".tmp");
+ final Path temp = Files.createTempFile("temp-file-name", ".tmp");
final FileSystemManager fileSystemManager = VFS.getManager();
- try (FileObject file =
fileSystemManager.resolveFile(temp.getAbsolutePath())) {
+ try (FileObject file =
fileSystemManager.resolveFile(temp.toAbsolutePath().toString())) {
file.getContent().getInputStream(bufferSize);
}
}
@@ -94,10 +96,10 @@ public class DefaultFileContentTest {
@Test
public void testMarkingWhenReadingEOS() throws Exception {
- final File temp = File.createTempFile("temp-file-name", ".tmp");
+ final Path temp = Files.createTempFile("temp-file-name", ".tmp");
final FileSystemManager fileSystemManager = VFS.getManager();
- try (FileObject file =
fileSystemManager.resolveFile(temp.getAbsolutePath())) {
+ try (FileObject file =
fileSystemManager.resolveFile(temp.toAbsolutePath().toString())) {
try (OutputStream outputStream =
file.getContent().getOutputStream()) {
outputStream.write(expected.getBytes());
outputStream.flush();
@@ -123,10 +125,10 @@ public class DefaultFileContentTest {
@Test
public void testMarkingWorks() throws Exception {
- final File temp = File.createTempFile("temp-file-name", ".tmp");
+ final Path temp = Files.createTempFile("temp-file-name", ".tmp");
final FileSystemManager fileSystemManager = VFS.getManager();
- try (FileObject file =
fileSystemManager.resolveFile(temp.getAbsolutePath())) {
+ try (FileObject file =
fileSystemManager.resolveFile(temp.toAbsolutePath().toString())) {
try (OutputStream outputStream =
file.getContent().getOutputStream()) {
outputStream.write(expected.getBytes());
outputStream.flush();
@@ -147,10 +149,10 @@ public class DefaultFileContentTest {
}
private void testOutputStreamBufferSize(final int bufferSize) throws
Exception {
- final File temp = File.createTempFile("temp-file-name", ".tmp");
+ final Path temp = Files.createTempFile("temp-file-name", ".tmp");
final FileSystemManager fileSystemManager = VFS.getManager();
- try (FileObject file =
fileSystemManager.resolveFile(temp.getAbsolutePath())) {
+ try (FileObject file =
fileSystemManager.resolveFile(temp.toAbsolutePath().toString())) {
file.getContent().getOutputStream(bufferSize).close();
}
}
@@ -172,10 +174,10 @@ public class DefaultFileContentTest {
@Test
public void testOutputStreamBufferSizeNegativeWithAppendFlag() throws
Exception {
- final File temp = File.createTempFile("temp-file-name", ".tmp");
+ final Path temp = Files.createTempFile("temp-file-name", ".tmp");
final FileSystemManager fileSystemManager = VFS.getManager();
- try (FileObject file =
fileSystemManager.resolveFile(temp.getAbsolutePath())) {
+ try (FileObject file =
fileSystemManager.resolveFile(temp.toAbsolutePath().toString())) {
assertThrows(IllegalArgumentException.class, () ->
file.getContent().getOutputStream(true, -1));
}
}
@@ -186,10 +188,10 @@ public class DefaultFileContentTest {
}
private <T extends Closeable> void
testStreamClosedInADifferentThread(final FailableFunction<FileContent, T,
IOException> getStream) throws Exception {
- final File temp = File.createTempFile("temp-file-name", ".tmp");
+ final Path temp = Files.createTempFile("temp-file-name", ".tmp");
final FileSystemManager fileSystemManager = VFS.getManager();
- try (FileObject file =
fileSystemManager.resolveFile(temp.getAbsolutePath())) {
+ try (FileObject file =
fileSystemManager.resolveFile(temp.toAbsolutePath().toString())) {
final T stream = getStream.apply(file.getContent());
final AtomicBoolean check = new AtomicBoolean();
final Thread thread = new Thread(() -> {
diff --git
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/local/TempFileTests.java
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/local/TempFileTests.java
index 72d9898f..6606c07d 100644
---
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/local/TempFileTests.java
+++
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/local/TempFileTests.java
@@ -16,8 +16,9 @@
*/
package org.apache.commons.vfs2.provider.local;
-import java.io.File;
import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Path;
import org.apache.commons.vfs2.AbstractProviderTestCase;
import org.apache.commons.vfs2.FileContent;
@@ -36,13 +37,13 @@ public class TempFileTests extends AbstractProviderTestCase
{
@Test
public void testLocalFile() throws Exception {
final String prefix = "\u0074\u0065\u0074";
- final File file = File.createTempFile(prefix + "-", "-" + prefix);
- assertTrue(file.exists());
- final URI uri = file.toURI();
+ final Path file = Files.createTempFile(prefix + "-", "-" + prefix);
+ assertTrue(Files.exists(file));
+ final URI uri = file.toUri();
try (FileSystemManager manager = getManager()) {
try (FileObject fileObject = manager.resolveFile(uri)) {
try (FileContent sourceContent = fileObject.getContent()) {
- assertEquals(sourceContent.getSize(), file.length());
+ assertEquals(sourceContent.getSize(), Files.size(file));
}
}
}
diff --git
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/sftp/AbstractSftpProviderTestCase.java
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/sftp/AbstractSftpProviderTestCase.java
index 503fd65b..36c2612c 100644
---
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/sftp/AbstractSftpProviderTestCase.java
+++
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/sftp/AbstractSftpProviderTestCase.java
@@ -26,6 +26,9 @@ import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
@@ -484,20 +487,20 @@ abstract class AbstractSftpProviderTestCase extends
AbstractProviderTestConfig {
return;
}
// System.setProperty("vfs.sftp.sshdir", getTestDirectory() +
"/../vfs.sftp.sshdir");
- final String tmpDir = System.getProperty("java.io.tmpdir");
+ final Path tmpDir = Paths.get(System.getProperty("java.io.tmpdir"));
Server = SshServer.setUpDefaultServer();
Server.setPort(0);
if (SecurityUtils.isBouncyCastleRegistered()) {
// A temporary file will hold the key
- final File keyFile = File.createTempFile("key", ".pem", new
File(tmpDir));
- keyFile.deleteOnExit();
+ final Path keyFile = Files.createTempFile(tmpDir, "key", ".pem");
+ keyFile.toFile().deleteOnExit();
// It has to be deleted in order to be generated
- keyFile.delete();
+ Files.delete(keyFile);
- final PEMGeneratorHostKeyProvider keyProvider = new
PEMGeneratorHostKeyProvider(keyFile.getAbsolutePath());
+ final PEMGeneratorHostKeyProvider keyProvider = new
PEMGeneratorHostKeyProvider(keyFile.toAbsolutePath().toString());
Server.setKeyPairProvider(keyProvider);
} else {
- Server.setKeyPairProvider(new
SimpleGeneratorHostKeyProvider(tmpDir + "/key.ser"));
+ Server.setKeyPairProvider(new
SimpleGeneratorHostKeyProvider(tmpDir.resolve("key.ser").toString()));
}
final List<NamedFactory<Command>> list = new ArrayList<>(1);
list.add(new NamedFactory<Command>() {
diff --git
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/FileLockTest.java
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/FileLockTest.java
index ab200ab0..a01f8d73 100644
---
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/FileLockTest.java
+++
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/FileLockTest.java
@@ -19,15 +19,15 @@ package org.apache.commons.vfs2.provider.zip;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
-import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.vfs2.FileObject;
@@ -43,13 +43,13 @@ import org.junit.jupiter.api.Test;
public class FileLockTest {
private FileSystemManager manager;
- private File newZipFile;
+ private Path newZipFile;
private String zipFileUri;
- private void assertDelete() {
+ private void assertDelete() throws IOException {
// We do not use newZipFile in the Assert message to avoid touching it
before calling delete().
- assertTrue(newZipFile.delete(), "Could not delete file");
+ Files.delete(newZipFile);
}
private void readAndAssert(final InputStream inputStream) throws
IOException {
@@ -80,11 +80,11 @@ public class FileLockTest {
@BeforeEach
public void setup() throws IOException {
- final File zipFile = new File("src/test/resources/test-data/test.zip");
- newZipFile = File.createTempFile(getClass().getSimpleName(), ".zip");
- newZipFile.deleteOnExit();
- FileUtils.copyFile(zipFile, newZipFile);
- zipFileUri = "zip:file:" + newZipFile.getAbsolutePath() +
"!/read-tests/file1.txt";
+ final Path zipFile =
Paths.get("src/test/resources/test-data/test.zip");
+ newZipFile = Files.createTempFile(getClass().getSimpleName(), ".zip");
+ newZipFile.toFile().deleteOnExit();
+ Files.copy(zipFile, newZipFile);
+ zipFileUri = "zip:file:" + newZipFile.toAbsolutePath() +
"!/read-tests/file1.txt";
manager = VFS.getManager();
}
@@ -94,7 +94,7 @@ public class FileLockTest {
try (InputStream inputStream =
zipFileObject.getContent().getInputStream()) {
if (SystemUtils.IS_OS_WINDOWS) {
// We do not use newZipFile in the Assert message to avoid
touching it before calling delete().
- assertFalse(newZipFile.delete(), "Could not delete file");
+ assertFalse(newZipFile.toFile().delete(), "Could not
delete file");
}
}
}
@@ -107,7 +107,7 @@ public class FileLockTest {
try (FileObject zipFileObject = manager.resolveFile(zipFileUri)) {
try (InputStream inputStream =
zipFileObject.getContent().getInputStream()) {
// We do not use newZipFile in the Assert message to avoid
touching it before calling delete().
- assertFalse(newZipFile.delete(), "Could not delete file");
+ assertFalse(newZipFile.toFile().delete(), "Could not delete
file");
}
}
}
diff --git
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ParseXmlInZipTest.java
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ParseXmlInZipTest.java
index edd6fde2..72972063 100644
---
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ParseXmlInZipTest.java
+++
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ParseXmlInZipTest.java
@@ -19,10 +19,13 @@ package org.apache.commons.vfs2.provider.zip;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
import java.util.Locale;
import java.util.regex.Pattern;
@@ -30,7 +33,6 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
@@ -57,11 +59,11 @@ public class ParseXmlInZipTest {
Locale.setDefault(new Locale("en", "US"));
}
- private File createTempFile() throws IOException {
- final File zipFile = new
File("src/test/resources/test-data/read-xml-tests.zip");
- final File newZipFile =
File.createTempFile(getClass().getSimpleName(), ".zip");
- newZipFile.deleteOnExit();
- FileUtils.copyFile(zipFile, newZipFile);
+ private Path createTempFile() throws IOException {
+ final Path zipFile =
Paths.get("src/test/resources/test-data/read-xml-tests.zip");
+ final Path newZipFile =
Files.createTempFile(getClass().getSimpleName(), ".zip");
+ newZipFile.toFile().deleteOnExit();
+ Files.copy(zipFile, newZipFile, StandardCopyOption.REPLACE_EXISTING);
return newZipFile;
}
@@ -101,8 +103,8 @@ public class ParseXmlInZipTest {
@Test
public void testParseXmlInZip() throws IOException, SAXException {
- final File newZipFile = createTempFile();
- final String xmlFilePath = "zip:file:" + newZipFile.getAbsolutePath()
+ "!/read-xml-tests/file1.xml";
+ final Path newZipFile = createTempFile();
+ final String xmlFilePath = "zip:file:" + newZipFile.toAbsolutePath() +
"!/read-xml-tests/file1.xml";
final FileSystemManager manager = VFS.getManager();
try (FileObject zipFileObject = manager.resolveFile(xmlFilePath)) {
try (InputStream inputStream =
zipFileObject.getContent().getInputStream()) {
@@ -150,8 +152,8 @@ public class ParseXmlInZipTest {
private void testResolveAndParseXmlInZip(final String xmlPathInZip, final
String xsdPathInZip)
throws IOException, FileSystemException, SAXException {
- final File newZipFile = createTempFile();
- final String zipFilePath = "zip:file:" + newZipFile.getAbsolutePath();
+ final Path newZipFile = createTempFile();
+ final String zipFilePath = "zip:file:" + newZipFile.toAbsolutePath();
final FileSystemManager manager = VFS.getManager();
try (FileObject zipFileObject = manager.resolveFile(zipFilePath)) {
try (FileObject xmlFileObject =
zipFileObject.resolveFile(xmlPathInZip)) {
diff --git
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ZipFileObjectTest.java
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ZipFileObjectTest.java
index ca28bfb0..dd04ac03 100644
---
a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ZipFileObjectTest.java
+++
b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ZipFileObjectTest.java
@@ -20,8 +20,11 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
@@ -36,15 +39,15 @@ public class ZipFileObjectTest {
private static final String NESTED_FILE_1 = "/read-xml-tests/file1.xml";
private static final String NESTED_FILE_2 = "/read-xml-tests/file2.xml";
- private void assertDelete(final File fileObject) {
- Assert.assertTrue("Could not delete file", fileObject.delete());
+ private void assertDelete(final Path fileObject) throws IOException {
+ Files.delete(fileObject);
}
- private File createTempFile() throws IOException {
- final File zipFile = new
File("src/test/resources/test-data/read-xml-tests.zip");
- final File newZipFile =
File.createTempFile(getClass().getSimpleName(), ".zip");
- newZipFile.deleteOnExit();
- FileUtils.copyFile(zipFile, newZipFile);
+ private Path createTempFile() throws IOException {
+ final Path zipFile =
Paths.get("src/test/resources/test-data/read-xml-tests.zip");
+ final Path newZipFile =
Files.createTempFile(getClass().getSimpleName(), ".zip");
+ newZipFile.toFile().deleteOnExit();
+ Files.copy(zipFile, newZipFile, StandardCopyOption.REPLACE_EXISTING);
return newZipFile;
}
@@ -82,9 +85,9 @@ public class ZipFileObjectTest {
@Test
@Disabled("Shows that leaving a stream open and not closing any resource
leaves the container file locked")
public void testLeaveNestedFileOpen() throws IOException {
- final File newZipFile = createTempFile();
+ final Path newZipFile = createTempFile();
final FileSystemManager manager = VFS.getManager();
- try (FileObject zipFileObject = manager.resolveFile("zip:file:" +
newZipFile.getAbsolutePath())) {
+ try (FileObject zipFileObject = manager.resolveFile("zip:file:" +
newZipFile.toAbsolutePath())) {
@SuppressWarnings({ "resource" })
final FileObject zipFileObject1 =
zipFileObject.resolveFile(NESTED_FILE_1);
getInputStreamAndAssert(zipFileObject1, "1");
@@ -99,9 +102,9 @@ public class ZipFileObjectTest {
*/
@Test
public void testReadingFilesInZipFile() throws IOException {
- final File newZipFile = createTempFile();
+ final Path newZipFile = createTempFile();
final FileSystemManager manager = VFS.getManager();
- try (FileObject zipFileObject = manager.resolveFile("zip:file:" +
newZipFile.getAbsolutePath())) {
+ try (FileObject zipFileObject = manager.resolveFile("zip:file:" +
newZipFile.toAbsolutePath())) {
try (FileObject zipFileObject1 =
zipFileObject.resolveFile(NESTED_FILE_1)) {
try (InputStream inputStream =
zipFileObject1.getContent().getInputStream()) {
readAndAssert(zipFileObject1, inputStream, "1");
@@ -120,11 +123,11 @@ public class ZipFileObjectTest {
*/
@Test
public void testReadingOneAfterClosingAnotherFile() throws IOException {
- final File newZipFile = createTempFile();
+ final Path newZipFile = createTempFile();
final FileSystemManager manager = VFS.getManager();
final FileObject zipFileObject1;
final InputStream inputStream1;
- try (FileObject zipFileObject = manager.resolveFile("zip:file:" +
newZipFile.getAbsolutePath())) {
+ try (FileObject zipFileObject = manager.resolveFile("zip:file:" +
newZipFile.toAbsolutePath())) {
// leave resources open
zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1);
inputStream1 = zipFileObject1.getContent().getInputStream();
@@ -144,11 +147,11 @@ public class ZipFileObjectTest {
*/
@Test
public void testReadingOneAfterClosingAnotherStream() throws IOException {
- final File newZipFile = createTempFile();
+ final Path newZipFile = createTempFile();
final FileSystemManager manager = VFS.getManager();
final FileObject zipFileObject1;
final InputStream inputStream1;
- try (FileObject zipFileObject = manager.resolveFile("zip:file:" +
newZipFile.getAbsolutePath())) {
+ try (FileObject zipFileObject = manager.resolveFile("zip:file:" +
newZipFile.toAbsolutePath())) {
// leave resources open (note that internal counters are updated)
zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1);
inputStream1 = zipFileObject1.getContent().getInputStream();
@@ -194,9 +197,9 @@ public class ZipFileObjectTest {
*/
@Test
public void testResolveNestedFileWithoutCleanup() throws IOException {
- final File newZipFile = createTempFile();
+ final Path newZipFile = createTempFile();
final FileSystemManager manager = VFS.getManager();
- try (FileObject zipFileObject = manager.resolveFile("zip:file:" +
newZipFile.getAbsolutePath())) {
+ try (FileObject zipFileObject = manager.resolveFile("zip:file:" +
newZipFile.toAbsolutePath())) {
@SuppressWarnings({ "unused", "resource" })
// We resolve a nested file and do nothing else.
final FileObject zipFileObject1 =
zipFileObject.resolveFile(NESTED_FILE_1);