DynamicPath { public List<FileTuple> getAllFiles(final String backupSuffix) { List<FileTuple> tuples = new ArrayList<FileTuple>(); Collection<File> originals = backupSuffix == null ? new ArrayList<File>() : new HashSet<File>(); collectFiles(folder, suffix, originals); if (backupSuffix != null) { Collection<File> backups = new ArrayList<File>(); collectFiles(folder, suffix + backupSuffix, backups); for (File backup : backups) { String path = backup.getAbsolutePath(); File original = new File(path.substring(0, path.length() - backupSuffix.length())); tuples.add(new FileTuple(original, backup)); originals.remove(original); } } for (File original : originals) { String path = original.getAbsolutePath(); File backup = new File(backupSuffix == null ? path : path + backupSuffix); tuples.add(new FileTuple(original, backup)); } Collections.sort(tuples, LastModifiedFileTupleComparator.INSTANCE); return tuples; } DynamicPath(final String path); String resolve(); List<FileTuple> getAllFiles(final String backupSuffix); boolean isValid(final File file);  }
@Test public void getFilesForEmptyFolder() { String pattern = new File(folder.getRoot(), "{pid}.log").getAbsolutePath(); DynamicPath path = new DynamicPath(pattern); assertThat(path.getAllFiles(null)).isEmpty(); }
@Test public void getExistingOriginalFiles() throws IOException { File first = folder.newFile("1.log"); File second = folder.newFile("42.log"); folder.newFile("42.old"); String pattern = new File(folder.getRoot(), "{pid}.log").getAbsolutePath(); DynamicPath path = new DynamicPath(pattern); assertThat(path.getAllFiles(null)) .extracting(FileTuple::getOriginal) .containsExactlyInAnyOrder(first, second); }
@Test public void getExistingBackupFiles() throws IOException { File first = folder.newFile("1.log"); File second = folder.newFile("42.log"); folder.newFile("1.log.backup"); folder.newFile("42.log.backup"); folder.newFile("42.old.backup"); String pattern = new File(folder.getRoot(), "{pid}.log").getAbsolutePath(); DynamicPath path = new DynamicPath(pattern); assertThat(path.getAllFiles(".backup")) .extracting(FileTuple::getOriginal) .containsExactlyInAnyOrder(first, second); }
@Test public void getAllExistingFiles() throws IOException { File first = folder.newFile("1.log"); File second = folder.newFile("2.log"); File third = folder.newFile("3.log"); first.delete(); folder.newFile("1.log.backup"); folder.newFile("2.log.backup"); String pattern = new File(folder.getRoot(), "{pid}.log").getAbsolutePath(); DynamicPath path = new DynamicPath(pattern); assertThat(path.getAllFiles(".backup")) .extracting(FileTuple::getOriginal) .containsExactlyInAnyOrder(first, second, third); }
@Test public void getNestedFiles() throws IOException { File subFolder = folder.newFolder("2018"); File file = new File(subFolder, "42.log"); file.createNewFile(); new File(subFolder, "42.old").createNewFile(); String pattern = new File(folder.getRoot(), "{date:YYYY}" + File.separator + "{pid}.log").getAbsolutePath(); DynamicPath path = new DynamicPath(pattern); assertThat(path.getAllFiles(null)) .extracting(FileTuple::getOriginal) .containsExactly(file); }
@Test public void getMultiTokenFiles() throws IOException { File first = folder.newFile("2018_1.log"); File second = folder.newFile("2018_2.log"); folder.newFile("2018+3.log"); String pattern = new File(folder.getRoot(), "{date:YYYY}_{count}.log").getAbsolutePath(); DynamicPath path = new DynamicPath(pattern); assertThat(path.getAllFiles(null)) .extracting(FileTuple::getOriginal) .containsExactlyInAnyOrder(first, second); }
@Test public void getSortedFiles() throws IOException { ZonedDateTime now = ZonedDateTime.now(); File first = folder.newFile("1.log"); first.setLastModified(now.toEpochSecond()); File second = folder.newFile("2.log"); second.setLastModified(now.minus(1, ChronoUnit.DAYS).toEpochSecond()); File third = folder.newFile("3.log"); third.setLastModified(now.plus(1, ChronoUnit.DAYS).toEpochSecond()); String pattern = new File(folder.getRoot(), "{count}.log").getAbsolutePath(); DynamicPath path = new DynamicPath(pattern); assertThat(path.getAllFiles(null)) .extracting(FileTuple::getOriginal) .containsExactlyInAnyOrder(third, first, second); }