import java.io.*;
import java.nio.file.*;

public class TargetDirCWD {
    private static String TEXT = "Sous le pont Mirabeau coule la Seine";

    public static void main(String[] args) throws IOException {
        test();
    }

    //
    // test will succeed if and only if the file is moved to the expected
    // location and has the expected content
    //
    private static void test() throws IOException {
        Path dir = Files.createDirectory(Path.of("dir"));
        Path file = Path.of("file");
        Path filepath = dir.resolve(file);
        Path cwd = Path.of(System.getProperty("user.dir"));
        Path target = cwd.resolve(file);
        Files.writeString(filepath, TEXT);
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir);) {
            if (ds instanceof SecureDirectoryStream<Path> sds) {
                sds.move(file, null, file);
                if (!Files.exists(target))
                    throw new RuntimeException(target + " does not exist");
                if (!TEXT.equals(Files.readString(target)))
                    throw new RuntimeException(target + " content incorrect");
            } else {
                throw new RuntimeException("Not a SecureDirectoryStream");
            }
            System.out.println("Success: \"" + TEXT + "\"");
        } finally {
            boolean fileDeleted = Files.deleteIfExists(filepath);
            Files.deleteIfExists(dir);
            if (!fileDeleted)
                Files.deleteIfExists(target);
        }
    }
}
