java - 测试文件系统交互 : Setting file permissions

标签 java unit-testing nio jimfs

我偶然发现了 Jimfs并想用它来测试与文件系统交互的方法。 例如,我写了一个很长的方法来计算是否可以成功写入文件列表:

static boolean exportable(List<Path> paths, boolean force) {
    List<Path> created = new LinkedList<>();
    boolean success = true;
    for (Path p : paths) {
        if (Files.exists(p)) {
            if (Files.isDirectory(p)) {
                success = false;
                log.error("Can't export results to file '{}': It's a directory!", p);
            } else if (force) {
                if (!Files.isWritable(p)) {
                    success = false;
                    log.error("Can't export to file '{}': No write access!", p);
                }
            } else {
                success = false;
                log.error("Can't export to file '{}': File does already exist and overwrite (-f) is not enabled!",
                        p);
            }
        } else { // does not exist
            Path parent = p.toAbsolutePath().normalize().getParent();
            if (Files.exists(parent)) {
                try {
                    Files.createFile(p);
                    created.add(p);
                    log.debug("Created file '{}'", p);
                } catch (AccessDeniedException e) {
                    success = false;
                    log.error("Can't export to file '{}': File could not be created. Access denied!", p);
                } catch (IOException e) {
                    success = false;
                    log.error("Can't export to file '{}': File could not be created!", p, e);
                }
            } else if (force) {
                List<Path> createdDirs = new ArrayList<>();
                try {
                    createParentDirectories(parent, createdDirs);
                } catch (IOException e) {
                    success = false;
                    log.error("Can't export to file '{}': Failed to create all parent directories!", p, e);
                }
                created.addAll(createdDirs);
                try {
                    Files.createFile(p);
                    created.add(p);
                    log.debug("Created file '{}'.", p);
                } catch (IOException e) {
                    success = false;
                    log.error("Can't export to file '{}': File could not be created!", p, e);
                }
            } else {
                success = false;
                log.error("Can't export to file '{}': File could not be created, because the parent directory '{}'"
                        + " does not exist and automatic parent directory creation (-f) is not enabled!", p, parent);
            }
        }
    }
    if (!success && created.size() > 0) {
        log.debug("Beginning to delete files and directories created while checking exportability.");
        Collections.reverse(created); // delete created folders in reverse order
        for (Path p : created) {
            try {
                Files.delete(p);
                log.debug("Successfully deleted '{}'.", p);
            } catch (IOException e) {
                log.warn("Deleting file '{}' failed, which was created during exportability check!", p, e);
            }
        }
        log.debug("Finished cleaning up created files and directories.");
    }
}

我现在想做的是编写这样的测试:

public void testExportToExistingFile_forceButNotWritable() throws Exception {
    FileSystem fs = Jimfs.newFileSystem();
    Path file = fs.getPath("file");
    Files.createFile(file);
    // How to deny writing to 'file' here???
    assertFalse(exportable(ImmutableList.of(file), true));
}

我可以使用 Jimfs 来执行此操作吗?如果没有,您将如何测试此方法?

最佳答案

遗憾的是,Jimfs 目前不支持任何类型的权限检查。这绝对是将来可能会很好的东西,它只是有点复杂(例如,POSIX 和 Windows 文件系统之间的权限设置方式不同)并且对于大多数用例来说似乎没有必要。

关于java - 测试文件系统交互 : Setting file permissions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24887693/

相关文章:

java - 将 json 转换为对象

java - 在 java DBCP 连接池中——什么是空闲连接?

java - 玩框架 2.5 初学者。 JavaForms 错误

java - 如何在 Jetty 中启用 SSL 调试?

c# - 单元测试 Asp.Net WebApi : how to test correct routing of a method with [FromUri] parameters

c# - 任何用于 .NET 的 NIO 框架?

iphone - 如何运行和调试 iPhone 应用程序的单元测试

c# - 无法将单元测试添加到 .NET Core 单元测试项目 VS2017

从客户端到服务器的 Java NIO + AES 加密 - ByteBuffer 问题

java - 扩展SocketChannel添加自定义读写