mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-25 00:09:02 +00:00
Provide graceful fallback for non-default NIO file systems
Closes gh-35443
This commit is contained in:
@@ -17,20 +17,29 @@
|
||||
package org.springframework.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link FileSystemUtils}.
|
||||
*
|
||||
* @author Rob Harrop
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
class FileSystemUtilsTests {
|
||||
|
||||
@Test
|
||||
void deleteRecursively() throws Exception {
|
||||
File root = new File("./tmp/root");
|
||||
void deleteRecursively(@TempDir File tempDir) throws Exception {
|
||||
File root = new File(tempDir, "root");
|
||||
File child = new File(root, "child");
|
||||
File grandchild = new File(child, "grandchild");
|
||||
|
||||
@@ -53,8 +62,8 @@ class FileSystemUtilsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyRecursively() throws Exception {
|
||||
File src = new File("./tmp/src");
|
||||
void copyRecursively(@TempDir File tempDir) throws Exception {
|
||||
File src = new File(tempDir, "src");
|
||||
File child = new File(src, "child");
|
||||
File grandchild = new File(child, "grandchild");
|
||||
|
||||
@@ -68,27 +77,25 @@ class FileSystemUtilsTests {
|
||||
assertThat(grandchild).exists();
|
||||
assertThat(bar).exists();
|
||||
|
||||
File dest = new File("./dest");
|
||||
File dest = new File(tempDir, "/dest");
|
||||
FileSystemUtils.copyRecursively(src, dest);
|
||||
|
||||
assertThat(dest).exists();
|
||||
assertThat(new File(dest, child.getName())).exists();
|
||||
assertThat(new File(dest, "child")).exists();
|
||||
assertThat(new File(dest, "child/bar.txt")).exists();
|
||||
|
||||
URI uri = URI.create("jar:file:/" + dest.toString().replace('\\', '/') + "/archive.zip");
|
||||
Map<String, String> env = Map.of("create", "true");
|
||||
FileSystem zipfs = FileSystems.newFileSystem(uri, env);
|
||||
Path ziproot = zipfs.getPath("/");
|
||||
FileSystemUtils.copyRecursively(src.toPath(), ziproot);
|
||||
|
||||
assertThat(zipfs.getPath("/child")).exists();
|
||||
assertThat(zipfs.getPath("/child/bar.txt")).exists();
|
||||
|
||||
zipfs.close();
|
||||
FileSystemUtils.deleteRecursively(src);
|
||||
assertThat(src).doesNotExist();
|
||||
}
|
||||
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
File tmp = new File("./tmp");
|
||||
if (tmp.exists()) {
|
||||
FileSystemUtils.deleteRecursively(tmp);
|
||||
}
|
||||
File dest = new File("./dest");
|
||||
if (dest.exists()) {
|
||||
FileSystemUtils.deleteRecursively(dest);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user