@Test void cantGetDirectoryWithoutValidDirectory() { Mockito.when(this.directory.isDirectory()).thenReturn(false); Assertions .assertThatIllegalArgumentException() .isThrownBy(() -> this.writer.getDirectory(this.directory, UUID.randomUUID().toString(), false)); }
protected Directory getDirectory(final File directory, final String requestUrl, final boolean includeParent) { if (!directory.isDirectory()) { throw new IllegalArgumentException("Input directory is not a valid directory. Unable to continue."); } if (StringUtils.isBlank(requestUrl)) { throw new IllegalArgumentException("No request url entered. Unable to continue."); } final Directory dir = new Directory(); if (includeParent) { final Entry parent = new Entry(); String url = requestUrl; if (url.charAt(url.length() - 1) == '/') { url = url.substring(0, url.length() - 2); } url = url.substring(0, url.lastIndexOf('/')); parent.setName("../"); parent.setUrl(url); parent.setSize(0L); parent.setLastModified(Instant.ofEpochMilli(directory.getParentFile().getAbsoluteFile().lastModified())); dir.setParent(parent); } final File[] files = directory.listFiles(); dir.setDirectories(Lists.newArrayList()); dir.setFiles(Lists.newArrayList()); final String baseURL = requestUrl.endsWith("/") ? requestUrl : requestUrl + "/"; if (files != null) { for (final File file : files) { final Entry entry = new Entry(); entry.setLastModified(Instant.ofEpochMilli(file.getAbsoluteFile().lastModified())); if (file.isDirectory()) { entry.setName(file.getName() + "/"); entry.setUrl(baseURL + file.getName() + "/"); entry.setSize(0L); dir.getDirectories().add(entry); } else { entry.setName(file.getName()); entry.setUrl(baseURL + file.getName()); entry.setSize(file.getAbsoluteFile().length()); dir.getFiles().add(entry); } } } dir.getDirectories().sort( Comparator.comparing(Entry::getName) ); dir.getFiles().sort( Comparator.comparing(Entry::getName) ); return dir; }
DefaultDirectoryWriter implements DirectoryWriter { protected Directory getDirectory(final File directory, final String requestUrl, final boolean includeParent) { if (!directory.isDirectory()) { throw new IllegalArgumentException("Input directory is not a valid directory. Unable to continue."); } if (StringUtils.isBlank(requestUrl)) { throw new IllegalArgumentException("No request url entered. Unable to continue."); } final Directory dir = new Directory(); if (includeParent) { final Entry parent = new Entry(); String url = requestUrl; if (url.charAt(url.length() - 1) == '/') { url = url.substring(0, url.length() - 2); } url = url.substring(0, url.lastIndexOf('/')); parent.setName("../"); parent.setUrl(url); parent.setSize(0L); parent.setLastModified(Instant.ofEpochMilli(directory.getParentFile().getAbsoluteFile().lastModified())); dir.setParent(parent); } final File[] files = directory.listFiles(); dir.setDirectories(Lists.newArrayList()); dir.setFiles(Lists.newArrayList()); final String baseURL = requestUrl.endsWith("/") ? requestUrl : requestUrl + "/"; if (files != null) { for (final File file : files) { final Entry entry = new Entry(); entry.setLastModified(Instant.ofEpochMilli(file.getAbsoluteFile().lastModified())); if (file.isDirectory()) { entry.setName(file.getName() + "/"); entry.setUrl(baseURL + file.getName() + "/"); entry.setSize(0L); dir.getDirectories().add(entry); } else { entry.setName(file.getName()); entry.setUrl(baseURL + file.getName()); entry.setSize(file.getAbsoluteFile().length()); dir.getFiles().add(entry); } } } dir.getDirectories().sort( Comparator.comparing(Entry::getName) ); dir.getFiles().sort( Comparator.comparing(Entry::getName) ); return dir; } }
DefaultDirectoryWriter implements DirectoryWriter { protected Directory getDirectory(final File directory, final String requestUrl, final boolean includeParent) { if (!directory.isDirectory()) { throw new IllegalArgumentException("Input directory is not a valid directory. Unable to continue."); } if (StringUtils.isBlank(requestUrl)) { throw new IllegalArgumentException("No request url entered. Unable to continue."); } final Directory dir = new Directory(); if (includeParent) { final Entry parent = new Entry(); String url = requestUrl; if (url.charAt(url.length() - 1) == '/') { url = url.substring(0, url.length() - 2); } url = url.substring(0, url.lastIndexOf('/')); parent.setName("../"); parent.setUrl(url); parent.setSize(0L); parent.setLastModified(Instant.ofEpochMilli(directory.getParentFile().getAbsoluteFile().lastModified())); dir.setParent(parent); } final File[] files = directory.listFiles(); dir.setDirectories(Lists.newArrayList()); dir.setFiles(Lists.newArrayList()); final String baseURL = requestUrl.endsWith("/") ? requestUrl : requestUrl + "/"; if (files != null) { for (final File file : files) { final Entry entry = new Entry(); entry.setLastModified(Instant.ofEpochMilli(file.getAbsoluteFile().lastModified())); if (file.isDirectory()) { entry.setName(file.getName() + "/"); entry.setUrl(baseURL + file.getName() + "/"); entry.setSize(0L); dir.getDirectories().add(entry); } else { entry.setName(file.getName()); entry.setUrl(baseURL + file.getName()); entry.setSize(file.getAbsoluteFile().length()); dir.getFiles().add(entry); } } } dir.getDirectories().sort( Comparator.comparing(Entry::getName) ); dir.getFiles().sort( Comparator.comparing(Entry::getName) ); return dir; }  }
DefaultDirectoryWriter implements DirectoryWriter { protected Directory getDirectory(final File directory, final String requestUrl, final boolean includeParent) { if (!directory.isDirectory()) { throw new IllegalArgumentException("Input directory is not a valid directory. Unable to continue."); } if (StringUtils.isBlank(requestUrl)) { throw new IllegalArgumentException("No request url entered. Unable to continue."); } final Directory dir = new Directory(); if (includeParent) { final Entry parent = new Entry(); String url = requestUrl; if (url.charAt(url.length() - 1) == '/') { url = url.substring(0, url.length() - 2); } url = url.substring(0, url.lastIndexOf('/')); parent.setName("../"); parent.setUrl(url); parent.setSize(0L); parent.setLastModified(Instant.ofEpochMilli(directory.getParentFile().getAbsoluteFile().lastModified())); dir.setParent(parent); } final File[] files = directory.listFiles(); dir.setDirectories(Lists.newArrayList()); dir.setFiles(Lists.newArrayList()); final String baseURL = requestUrl.endsWith("/") ? requestUrl : requestUrl + "/"; if (files != null) { for (final File file : files) { final Entry entry = new Entry(); entry.setLastModified(Instant.ofEpochMilli(file.getAbsoluteFile().lastModified())); if (file.isDirectory()) { entry.setName(file.getName() + "/"); entry.setUrl(baseURL + file.getName() + "/"); entry.setSize(0L); dir.getDirectories().add(entry); } else { entry.setName(file.getName()); entry.setUrl(baseURL + file.getName()); entry.setSize(file.getAbsoluteFile().length()); dir.getFiles().add(entry); } } } dir.getDirectories().sort( Comparator.comparing(Entry::getName) ); dir.getFiles().sort( Comparator.comparing(Entry::getName) ); return dir; }  static String directoryToHTML(final String directoryName, final Directory directory); @Override String toHtml(
        @NotNull final File directory,
        @URL final String requestURL,
        final boolean includeParent
    ); @Override String toJson(
        @NotNull final File directory,
        @URL final String requestURL,
        final boolean includeParent
    ); }
DefaultDirectoryWriter implements DirectoryWriter { protected Directory getDirectory(final File directory, final String requestUrl, final boolean includeParent) { if (!directory.isDirectory()) { throw new IllegalArgumentException("Input directory is not a valid directory. Unable to continue."); } if (StringUtils.isBlank(requestUrl)) { throw new IllegalArgumentException("No request url entered. Unable to continue."); } final Directory dir = new Directory(); if (includeParent) { final Entry parent = new Entry(); String url = requestUrl; if (url.charAt(url.length() - 1) == '/') { url = url.substring(0, url.length() - 2); } url = url.substring(0, url.lastIndexOf('/')); parent.setName("../"); parent.setUrl(url); parent.setSize(0L); parent.setLastModified(Instant.ofEpochMilli(directory.getParentFile().getAbsoluteFile().lastModified())); dir.setParent(parent); } final File[] files = directory.listFiles(); dir.setDirectories(Lists.newArrayList()); dir.setFiles(Lists.newArrayList()); final String baseURL = requestUrl.endsWith("/") ? requestUrl : requestUrl + "/"; if (files != null) { for (final File file : files) { final Entry entry = new Entry(); entry.setLastModified(Instant.ofEpochMilli(file.getAbsoluteFile().lastModified())); if (file.isDirectory()) { entry.setName(file.getName() + "/"); entry.setUrl(baseURL + file.getName() + "/"); entry.setSize(0L); dir.getDirectories().add(entry); } else { entry.setName(file.getName()); entry.setUrl(baseURL + file.getName()); entry.setSize(file.getAbsoluteFile().length()); dir.getFiles().add(entry); } } } dir.getDirectories().sort( Comparator.comparing(Entry::getName) ); dir.getFiles().sort( Comparator.comparing(Entry::getName) ); return dir; }  static String directoryToHTML(final String directoryName, final Directory directory); @Override String toHtml(
        @NotNull final File directory,
        @URL final String requestURL,
        final boolean includeParent
    ); @Override String toJson(
        @NotNull final File directory,
        @URL final String requestURL,
        final boolean includeParent
    );  }