2

私は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 を使用してこれを行うことはできますか? そうでない場合、このメソッドをどのようにテストしますか?

4

1 に答える 1

1

残念ながら、Jimfs は現在、いかなる種類のパーミッション チェックもサポートしていません。これは間違いなく将来的には便利なものかもしれませんが、多少複雑で (たとえば、POSIX ファイル システムと Windows ファイル システムの間でパーミッションが設定される方法が異なるため)、ほとんどのユース ケースでは必要ではないように思われます。

于 2014-09-08T18:18:33.413 に答える