WildcardMatcher { public static File[] glob(String filePattern) throws IOException { Set<File> fileSet = new TreeSet<>(); glob(filePattern, fileSet); return fileSet.toArray(new File[fileSet.size()]); } WildcardMatcher(String wildcard);   WildcardMatcher(String wildcard, boolean windowsFs); static File[] glob(String filePattern); static void glob(String filePattern, Set<File> fileSet); boolean matches(String text);  }
@Test public void testGlobInCwd() throws Exception { File cwd = new File(".").getCanonicalFile(); File[] alreadyExistingTxtFiles = cwd.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".txt"); } }); final File[] testFiles = { new File(cwd, "WildcardMatcherTest-1.txt"), new File(cwd, "WildcardMatcherTest-2.txt"), new File(cwd, "WildcardMatcherTest-3.dat"), new File(cwd, "WildcardMatcherTest-4.txt"), new File(cwd, "WildcardMatcherTest-5.dat"), }; int expectedTxtFileCount = alreadyExistingTxtFiles.length + 3; try { for (File file : testFiles) { if (!file.createNewFile()) { System.out.println("Warning: test file could not be created: " + file); System.out.println("Warning: testGlobInCwd() not performed"); return; } } File[] files = WildcardMatcher.glob("*.txt"); assertNotNull(files); assertEquals(expectedTxtFileCount, files.length); assertTrue(containsFile(files, testFiles[0])); assertTrue(containsFile(files, testFiles[1])); assertTrue(containsFile(files, testFiles[3])); assertTrue(getIndexOf(testFiles[0], files) < getIndexOf(testFiles[1], files)); assertTrue(getIndexOf(testFiles[1], files) < getIndexOf(testFiles[3], files)); files = WildcardMatcher.glob("./*.txt"); assertNotNull(files); assertEquals(expectedTxtFileCount, files.length); assertTrue(containsFile(files, testFiles[0])); assertTrue(containsFile(files, testFiles[1])); assertTrue(containsFile(files, testFiles[3])); assertTrue(getIndexOf(testFiles[0], files) < getIndexOf(testFiles[1], files)); assertTrue(getIndexOf(testFiles[1], files) < getIndexOf(testFiles[3], files)); } finally { for (File file : testFiles) { if (file.exists() && !file.delete()) { System.out.println("Warning: test file could not be deleted: " + file); } } } }
@Test public void testGlobWithDoubleStar() throws Exception { String dir = getTestdataDir(); File[] files = WildcardMatcher.glob(dir + "*.txt"); assertNotNull(files); for (File file : files) { } assertEquals(3, files.length); Arrays.sort(files); assertEquals(new File(dir, "foo/bar/test1.txt"), files[0]); assertEquals(new File(dir, "foo/bar/test3.txt"), files[1]); assertEquals(new File(dir, "foo/test1.txt"), files[2]); }
@Test public void testGlobStarAtEnd() throws Exception { String dir = getTestdataDir(); File[] files = WildcardMatcher.glob(dir + "/foo/bar/*"); assertNotNull(files); for (File file : files) { } assertEquals(3, files.length); Arrays.sort(files); assertEquals(new File(dir, "foo/bar/test1.txt"), files[0]); assertEquals(new File(dir, "foo/bar/test2.dat"), files[1]); assertEquals(new File(dir, "foo/bar/test3.txt"), files[2]); }
@Test public void testGlobDoubleStarAtEnd() throws Exception { String dir = getTestdataDir(); File[] files = WildcardMatcher.glob(dir + "/foo/**"); assertNotNull(files); for (File file : files) { } assertEquals(7, files.length); Arrays.sort(files); assertEquals(new File(dir, "foo/bar"), files[0]); assertEquals(new File(dir, "foo/bar/test1.txt"), files[1]); assertEquals(new File(dir, "foo/bar/test2.dat"), files[2]); assertEquals(new File(dir, "foo/bar/test3.txt"), files[3]); assertEquals(new File(dir, "foo/test1.txt"), files[4]); assertEquals(new File(dir, "foo/test2.dat"), files[5]); assertEquals(new File(dir, "foo/test3.dat"), files[6]); }
@Test public void testGlobAllFiles() throws Exception { String dir = getTestdataDir(); File[] files = WildcardMatcher.glob(dir + "/foo*.*"); assertNotNull(files); for (File file : files) { } assertEquals(6, files.length); Arrays.sort(files); assertEquals(new File(dir, "foo/bar/test1.txt"), files[0]); assertEquals(new File(dir, "foo/bar/test2.dat"), files[1]); assertEquals(new File(dir, "foo/bar/test3.txt"), files[2]); assertEquals(new File(dir, "foo/test1.txt"), files[3]); assertEquals(new File(dir, "foo/test2.dat"), files[4]); assertEquals(new File(dir, "foo/test3.dat"), files[5]); }
@Test public void testGlobExistingDir() throws Exception { String dir = getTestdataDir(); File[] files = WildcardMatcher.glob(dir + "/foo"); assertNotNull(files); for (File file : files) { } assertEquals(1, files.length); assertEquals(new File(dir, "foo"), files[0]); }