TaildirMatcher { List<File> getMatchingFiles() { long now = TimeUnit.SECONDS.toMillis( TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())); long currentParentDirMTime = parentDir.lastModified(); List<File> result; if (!cachePatternMatching || lastSeenParentDirMTime < currentParentDirMTime || !(currentParentDirMTime < lastCheckedTime)) { lastMatchedFiles = sortByLastModifiedTime(getMatchingFilesNoCache()); lastSeenParentDirMTime = currentParentDirMTime; lastCheckedTime = now; } return lastMatchedFiles; } TaildirMatcher(String fileGroup, String filePattern, boolean cachePatternMatching); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); String getFileGroup();  }
@Test public void getMatchingFiles() throws Exception { append("file0"); append("file1"); TaildirMatcher tm = new TaildirMatcher("f1", tmpDir.getAbsolutePath() + File.separator + "file.*", isCachingNeeded); List<String> files = filesToNames(tm.getMatchingFiles()); assertEquals(msgAlreadyExistingFile, 2, files.size()); assertTrue(msgAlreadyExistingFile, files.contains("file1")); append("file1"); files = filesToNames(tm.getMatchingFiles()); assertEquals(msgAfterNewFileCreated, 2, files.size()); assertTrue(msgAfterNewFileCreated, files.contains("file0")); assertTrue(msgAfterNewFileCreated, files.contains("file1")); append("file2"); append("file3"); files = filesToNames(tm.getMatchingFiles()); assertEquals(msgAfterAppend, 4, files.size()); assertTrue(msgAfterAppend, files.contains("file0")); assertTrue(msgAfterAppend, files.contains("file1")); assertTrue(msgAfterAppend, files.contains("file2")); assertTrue(msgAfterAppend, files.contains("file3")); this.files.get("file0").delete(); files = filesToNames(tm.getMatchingFiles()); assertEquals(msgAfterDelete, 3, files.size()); assertFalse(msgAfterDelete, files.contains("file0")); assertTrue(msgNoChange, files.contains("file1")); assertTrue(msgNoChange, files.contains("file2")); assertTrue(msgNoChange, files.contains("file3")); }
@Test public void testEmtpyDirMatching() throws Exception { TaildirMatcher tm = new TaildirMatcher("empty", tmpDir.getAbsolutePath() + File.separator + ".*", isCachingNeeded); List<File> files = tm.getMatchingFiles(); assertNotNull(msgEmptyDir, files); assertTrue(msgEmptyDir, files.isEmpty()); }
@Test public void testNoMatching() throws Exception { TaildirMatcher tm = new TaildirMatcher( "nomatch", tmpDir.getAbsolutePath() + File.separator + "abracadabra_nonexisting", isCachingNeeded); List<File> files = tm.getMatchingFiles(); assertNotNull(msgNoMatch, files); assertTrue(msgNoMatch, files.isEmpty()); }
@Test public void testDirectoriesAreNotListed() throws Exception { new File(tmpDir, "outerFile").createNewFile(); new File(tmpDir, "recursiveDir").mkdir(); new File(tmpDir + File.separator + "recursiveDir", "innerFile").createNewFile(); TaildirMatcher tm = new TaildirMatcher("f1", tmpDir.getAbsolutePath() + File.separator + ".*", isCachingNeeded); List<String> files = filesToNames(tm.getMatchingFiles()); assertEquals(msgSubDirs, 1, files.size()); assertTrue(msgSubDirs, files.contains("outerFile")); }
@Test public void testRegexFileNameFiltering() throws IOException { append("a.log"); append("a.log.1"); append("b.log"); append("c.log.yyyy.MM-01"); append("c.log.yyyy.MM-02"); TaildirMatcher tm1 = new TaildirMatcher("ab", tmpDir.getAbsolutePath() + File.separator + "[ab].log", isCachingNeeded); TaildirMatcher tm2 = new TaildirMatcher("c", tmpDir.getAbsolutePath() + File.separator + "c.log.*", isCachingNeeded); List<String> files1 = filesToNames(tm1.getMatchingFiles()); List<String> files2 = filesToNames(tm2.getMatchingFiles()); assertEquals(2, files1.size()); assertEquals(2, files2.size()); assertTrue("Regex pattern for ab should have matched a.log file", files1.contains("a.log")); assertFalse("Regex pattern for ab should NOT have matched a.log.1 file", files1.contains("a.log.1")); assertTrue("Regex pattern for ab should have matched b.log file", files1.contains("b.log")); assertTrue("Regex pattern for c should have matched c.log.yyyy-MM-01 file", files2.contains("c.log.yyyy.MM-01")); assertTrue("Regex pattern for c should have matched c.log.yyyy-MM-02 file", files2.contains("c.log.yyyy.MM-02")); }