/** * Verify that the supplied file or directory is within the test data directory. * * @param path the path to the file or directory; may not be null * @return true if inside the test data directory, or false otherwise */ public static boolean inTestDataDir(Path path) { Path target = FileSystems.getDefault().getPath(dataDir()).toAbsolutePath(); return path.toAbsolutePath().startsWith(target); } }
/** * Create the path to a file within the test data directory at the given relative path. * * @param relativePath the path of the file within the test data directory; may not be null * @return the reference to the existing readable and writable file */ public static Path createTestingPath(String relativePath) { return Paths.get(dataDir(), relativePath).toAbsolutePath(); }
@Test public void shouldRemoveDirectory() throws Exception { Path path = Paths.get(Testing.Files.dataDir(), "test-dir"); assertThat(path.toFile().mkdirs()).isTrue(); Path file = path.resolve("file.txt"); assertThat(file.toFile().createNewFile()).isTrue(); Testing.Files.delete(path); assertThat(java.nio.file.Files.exists(path)).isFalse(); } }
@Test public void shouldKnowDirectoriesInsideTestData() { assertThat(Testing.Files.inTestDataDir(Paths.get(Testing.Files.dataDir(), "somefile"))).isTrue(); assertThat(Testing.Files.inTestDataDir(new File("../debezium").toPath())).isFalse(); }