FileNameUtil { public static List<File> identifyFiles(Map parameters) { String baseDir = (String) parameters.get("baseDir"); if (Strings.isNullOrEmpty(baseDir) || !new File(baseDir).exists()) { return new ArrayList<>(); } DirectoryScanner scanner = new DirectoryScanner(); scanner.setBasedir(baseDir); setIncludesExcludes(parameters.get("includes"), scanner::setIncludes); setIncludesExcludes(parameters.get("excludes"), scanner::setExcludes); scanner.setCaseSensitive(false); scanner.scan(); String[] files = scanner.getIncludedFiles(); return Arrays.stream(files) .map(filename -> new File(baseDir, filename)) .sorted(Comparator.comparing(File::getAbsolutePath)) .collect(Collectors.toList()); } private  FileNameUtil(); static List<File> identifyFiles(Map parameters); static boolean isIncluded(String name, Map parameters); static boolean matches(String name, List<String> patterns, boolean defaultValue); static boolean matches(String name, String pattern); static String getParentPath(String fileName); static String getCanonicalPath(String path);  }
@Test public void testIdentifyClassFilesWithAntScanner() { Map<String, String> parameters = Maps.newHashMap(); parameters.put("baseDir", TEST_CLASSES_DIR); parameters.put("includes", "**/*.class"); List<File> files = FileNameUtil.identifyFiles(parameters); String thisFileName = this.getClass().getCanonicalName().replaceAll("\\.", "/"); File thisTestFile = new File(TEST_CLASSES_DIR + thisFileName + ".class"); assertThat(files.contains(new File(thisTestFile.getPath())), CoreMatchers.is(true)); String aTestFileName = ProcessUtilTest.class.getCanonicalName().replaceAll("\\.", "/"); File aTestFile = new File(TEST_CLASSES_DIR + aTestFileName + ".class"); assertThat(files.contains(new File(aTestFile.getPath())), CoreMatchers.is(true)); }
@Test public void testIdentifyClassFilesWithAntScannerWithExcludes() { Map<String, String> parameters = Maps.newHashMap(); parameters.put("baseDir", TEST_CLASSES_DIR); parameters.put("includes", "***Test.class"); List<File> files = FileNameUtil.identifyFiles(parameters); String thisFileName = this.getClass().getCanonicalName().replaceAll("\\.", "/"); File thisTestFile = new File(TEST_CLASSES_DIR + thisFileName + ".class"); assertThat(files.contains(new File(thisTestFile.getPath())), CoreMatchers.is(false)); String aTestFileName = A.class.getCanonicalName().replaceAll("\\.", "/"); File aTestFile = new File(TEST_CLASSES_DIR + aTestFileName + ".class"); assertThat(files.contains(new File(aTestFile.getPath())), CoreMatchers.is(true)); }
@Test public void testIdentifyClassFilesWithAntScannerWithListIncludesExcludes() { Map<String, Object> parameters = Maps.newHashMap(); parameters.put("baseDir", TEST_CLASSES_DIR); parameters.put("includes", Arrays.asList("***UtilTest.class")); parameters.put("excludes", Collections.singletonList("**/Java*.class")); List<File> files = FileNameUtil.identifyFiles(parameters); String aTestFileName = JarFileUtilTest.class.getCanonicalName().replaceAll("\\.", "/"); File aTestFile = new File(TEST_CLASSES_DIR + aTestFileName + ".class"); assertThat(files.contains(new File(aTestFile.getPath())), CoreMatchers.is(true)); String bTestFileName = ClassHandler.class.getCanonicalName().replaceAll("\\.", "/"); File bTestFile = new File(TEST_CLASSES_DIR + bTestFileName + ".class"); assertThat(files.contains(new File(bTestFile.getPath())), CoreMatchers.is(false)); }
@Test public void testIdentifyClassFilesWithNotExistingDir() { Map<String, String> parameters = Maps.newHashMap(); parameters.put("baseDir", "not/existing/dir"); parameters.put("includes", "***Test.class"); List<File> files = FileNameUtil.identifyFiles(parameters); assertThat(files, hasSize(0)); }
@Test public void testIdentifyClassFilesWithoutBaseDir() { Map<String, String> parameters = Maps.newHashMap(); parameters.put("includes", "***Test.class"); List<File> files = FileNameUtil.identifyFiles(parameters); assertThat(files, hasSize(0)); }
@Test(expected = IllegalArgumentException.class) public void testIdentifyClassFilesWithWrongInput() { Map<String, Object> parameters = Maps.newHashMap(); parameters.put("baseDir", TEST_CLASSES_DIR); Set<String> input = new HashSet<>(); input.add("**/*.class"); parameters.put("includes", input); FileNameUtil.identifyFiles(parameters); }