FileUtil { public static Either<Exception, Optional<File>> findFolder(final String folderName) { final Path root = new File(System.getProperty("user.dir")).toPath(); try (var folders = Files.find(root, 1, (path, attr) -> attr.isDirectory())) { return Either.right(folders.map(Path::toFile) .filter(f -> Objects.equals(f.getName(), folderName)) .findFirst()); } catch (Exception ex) { LOGGER.warn("Exception while walking file tree.", ex); return Either.left(ex); } } private  FileUtil(); static boolean configurePermissions(final File f, final boolean executable); static boolean isJarFile(final File f); static Either<Exception, Optional<File>> findFolder(final String folderName); static Stream<URL> filesToUrls(final File... jars);  }
@Test void lookupExistingFolder() { assertThat(FileUtil.findFolder("target") .get()).isPresent(); }
@Test void lookupNonExistentFolder() { assertThat(FileUtil.findFolder(UUID.randomUUID() .toString()) .get()).isEmpty(); }
@Test void lookupExistingFolderIncludingNonWritableFolder() { SOME_DIR.mkdir(); SOME_DIR.setReadable(false); String folderName = UUID.randomUUID() .toString(); assertThat(FileUtil.findFolder(folderName) .isRight()).isTrue(); assertThat(FileUtil.findFolder(folderName) .get()).isEmpty(); }
@Test void lookupNonExistentFolderIncludingNonWritableFolder() { SOME_DIR.mkdir(); SOME_DIR.setReadable(false); assertThat(FileUtil.findFolder("target") .isRight()).isTrue(); assertThat(FileUtil.findFolder("target") .get()).isPresent(); }
@Test void lookupSomethingPureWrong() { final String old = System.getProperty("user.dir"); String folderName = UUID.randomUUID() .toString(); System.setProperty("user.dir", folderName); assertThat(FileUtil.findFolder(folderName) .isLeft()).isTrue(); System.setProperty("user.dir", old); }
@Test void lookupExistingFileAsFolder() { assertThat(FileUtil.findFolder("pom.xml") .get()).isEmpty(); }