/** * Returns all jars that are in the directory. It is useful in expanding a * wildcard path to return all jars from the directory to use in a classpath. * * @param path the path to the directory. The path may include the wildcard. * @return the list of jars as URLs, or an empty list if there are no jars, or * the directory does not exist */ public static List<Path> getJarsInDirectory(String path, boolean useLocal) { List<Path> paths = new ArrayList<>(); try { // add the wildcard if it is not provided if (!path.endsWith("*")) { path += File.separator + "*"; } Path globPath = new Path(path).suffix("{.jar,.JAR}"); FileContext context = useLocal ? FileContext.getLocalFSFileContext() : FileContext.getFileContext(globPath.toUri()); FileStatus[] files = context.util().globStatus(globPath); if (files != null) { for (FileStatus file: files) { paths.add(file.getPath()); } } } catch (IOException ignore) {} // return the empty list return paths; }
@Test public void testGlobStatusWithMultipleWildCardMatches() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA2), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } //Should return all 4 items ("/test/hadoop/aaa", "/test/hadoop/axa" //"/test/hadoop/axx", and "/test/hadoop2/axx") FileStatus[] paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop*/*")); Assert.assertEquals(4, paths.length); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AAA), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXA), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXX), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AAA2), paths)); }
@Test public void testGlobStatusWithMultipleMatchesOfSingleChar() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA2), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } //Should return only 2 items ("/test/hadoop/axa", "/test/hadoop/axx") FileStatus[] paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/ax?")); Assert.assertEquals(2, paths.length); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXA), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXX), paths)); }
@Test public void testGlobStatusSomeMatchesInDirectories() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA2), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } // Should return two items ("/test/hadoop" and "/test/hadoop2") FileStatus[] paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop*")); Assert.assertEquals(2, paths.length); Assert.assertTrue(containsPath(getTestRootPath(fc, "test/hadoop"), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, "test/hadoop2"), paths)); }
@Test public void testGlobStatusWithMultipleMatchesOfSingleChar() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA2), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } //Should return only 2 items ("/test/hadoop/axa", "/test/hadoop/axx") FileStatus[] paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/ax?")); Assert.assertEquals(2, paths.length); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXA), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXX), paths)); }
@Test public void testGlobStatusWithMultipleMatchesOfSingleChar() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA2), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } //Should return only 2 items ("/test/hadoop/axa", "/test/hadoop/axx") FileStatus[] paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/ax?")); Assert.assertEquals(2, paths.length); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXA), paths)); Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXX), paths)); }
@Test public void testGlobStatusFilterWithEmptyPathResults() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXX), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } //This should return an empty set FileStatus[] filteredPaths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/?"), DEFAULT_FILTER); Assert.assertEquals(0,filteredPaths.length); }
@Test public void testGlobStatusFilterWithEmptyPathResults() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXX), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } //This should return an empty set FileStatus[] filteredPaths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/?"), DEFAULT_FILTER); Assert.assertEquals(0,filteredPaths.length); }
@Test public void testGlobStatusFilterWithNoMatchingPathsAndNonTrivialFilter() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXX), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } //This should return an empty set FileStatus[] filteredPaths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/?"), TEST_X_FILTER); Assert.assertEquals(0,filteredPaths.length); }
@Test public void testGlobStatusFilterWithNoMatchingPathsAndNonTrivialFilter() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXX), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } //This should return an empty set FileStatus[] filteredPaths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/?"), TEST_X_FILTER); Assert.assertEquals(0,filteredPaths.length); }
@Test public void testGlobStatusFilterWithNoMatchingPathsAndNonTrivialFilter() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXX), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } //This should return an empty set FileStatus[] filteredPaths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/?"), TEST_X_FILTER); Assert.assertEquals(0,filteredPaths.length); }
@Test public void testGlobStatusFilterWithEmptyPathResults() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AXX), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } //This should return an empty set FileStatus[] filteredPaths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/?"), DEFAULT_FILTER); Assert.assertEquals(0,filteredPaths.length); }
@Test public void testGlobStatusWithNoMatchesInPath() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA2), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } // should return nothing FileStatus[] paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/?")); Assert.assertEquals(0, paths.length); }
@Test public void testGlobStatusWithNoMatchesInPath() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA2), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } // should return nothing FileStatus[] paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/?")); Assert.assertEquals(0, paths.length); }
@Test public void testGlobStatusWithNoMatchesInPath() throws Exception { Path[] testDirs = { getTestRootPath(fc, TEST_DIR_AAA), getTestRootPath(fc, TEST_DIR_AXA), getTestRootPath(fc, TEST_DIR_AXX), getTestRootPath(fc, TEST_DIR_AAA2), }; if (exists(fc, testDirs[0]) == false) { for (Path path : testDirs) { fc.mkdir(path, FsPermission.getDefault(), true); } } // should return nothing FileStatus[] paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoop/?")); Assert.assertEquals(0, paths.length); }
@Test public void testGlobStatusNonExistentFile() throws Exception { FileStatus[] paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoopfsdf")); Assert.assertNull(paths); paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoopfsdf/?")); Assert.assertEquals(0, paths.length); paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoopfsdf/xyz*/?")); Assert.assertEquals(0, paths.length); }
@Override public FileStatus[] globStatus(Path pathPattern, PathFilter filter) throws IOException { return fc.util().globStatus(pathPattern, filter); } }
@Test public void testGlobStatusThrowsExceptionForNonExistentFile() throws Exception { try { // This should throw a FileNotFoundException fc.util().globStatus( getTestRootPath(fc, "test/hadoopfsdf/?")); Assert.fail("Should throw FileNotFoundException"); } catch (FileNotFoundException fnfe) { // expected } }
@Test public void testGlobStatusNonExistentFile() throws Exception { FileStatus[] paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoopfsdf")); Assert.assertNull(paths); paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoopfsdf/?")); Assert.assertEquals(0, paths.length); paths = fc.util().globStatus( getTestRootPath(fc, "test/hadoopfsdf/xyz*/?")); Assert.assertEquals(0, paths.length); }
@Override public FileStatus[] globStatus(Path pathPattern, PathFilter filter) throws IOException { return fc.util().globStatus(pathPattern, filter); } }