@Test public void testNoLatestVersion() throws IOException { File oldFile = File.createTempFile("test", ".txt", tmpDir); oldFile.delete(); URI uri = oldFile.toURI(); Assert.assertNull( finder.getLatestVersion(uri, Pattern.compile(".*\\.txt")) ); }
/** * Matches based on a pattern in the file name. Returns the file with the latest timestamp. * * @param uri If it is a file, then the parent is searched. If it is a directory, then the directory is searched. * @param pattern The matching filter to down-select the file names in the directory of interest. Passing `null` * results in matching any file * * @return The URI of the most recently modified file which matches the pattern, or `null` if it cannot be found */ @Override public URI getLatestVersion(URI uri, final @Nullable Pattern pattern) { final File file = new File(uri); try { return RetryUtils.retry( () -> mostRecentInDir( file.isDirectory() ? file.toPath() : file.getParentFile().toPath(), pattern ), shouldRetryPredicate(), DEFAULT_RETRY_COUNT ); } catch (Exception e) { if (e instanceof FileNotFoundException) { return null; } throw Throwables.propagate(e); } }
@Before public void setup() throws IOException { tmpDir = temporaryFolder.newFolder(); finder = new LocalFileTimestampVersionFinder(); }
/** * Matches based on a pattern in the file name. Returns the file with the latest timestamp. * * @param uri If it is a file, then the parent is searched. If it is a directory, then the directory is searched. * @param pattern The matching filter to down-select the file names in the directory of interest. Passing `null` results in matching any file * * @return The URI of the most recently modified file which matches the pattern, or `null` if it cannot be found */ @Override public URI getLatestVersion(URI uri, final @Nullable Pattern pattern) { final File file = new File(uri); try { return RetryUtils.retry( () -> mostRecentInDir( file.isDirectory() ? file.toPath() : file.getParentFile().toPath(), pattern ), shouldRetryPredicate(), DEFAULT_RETRY_COUNT ); } catch (Exception e) { if (e instanceof FileNotFoundException) { return null; } throw Throwables.propagate(e); } }
@Test public void testSimpleOneFileLatestVersion() throws IOException { File oldFile = File.createTempFile("old", ".txt", tmpDir); Assert.assertTrue(oldFile.exists()); Assert.assertEquals( oldFile.getAbsolutePath(), finder.getLatestVersion(oldFile.toURI(), Pattern.compile(".*\\.txt")).getPath() ); }
@Test public void testSimpleOneFileLatestVersionNullMatcher() throws IOException { File oldFile = File.createTempFile("old", ".txt", tmpDir); Assert.assertTrue(oldFile.exists()); Assert.assertEquals( oldFile.getAbsolutePath(), finder.getLatestVersion(oldFile.toURI(), null).getPath() ); }
@Test public void testExampleRegex() throws IOException { File tmpFile = new File(tmpDir, "renames-123.gz"); tmpFile.createNewFile(); Assert.assertTrue(tmpFile.exists()); Assert.assertFalse(tmpFile.isDirectory()); Assert.assertEquals( tmpFile.getAbsolutePath(), finder.getLatestVersion(tmpDir.toURI(), Pattern.compile("renames-[0-9]*\\.gz")).getPath() ); } }