Merge pull request #4612 from JemimaBrewer/release/4.0

Make `getPathsInPath` function more robust, including null handling
This commit is contained in:
Emmanuel M
2026-07-10 16:44:29 -03:00
committed by GitHub
2 changed files with 120 additions and 12 deletions
@@ -586,27 +586,55 @@ public class FileUtils {
* ["smb://user;workgroup:passw0rd@12.3.4", "smb://user;workgroup:passw0rd@12.3.4/user", "smb://user;workgroup:passw0rd@12.3.4/user/Documents", "smb://user;workgroup:passw0rd@12.3.4/user/Documents/flare.doc"]
* </code>
*
* @param path
* @param pathParam
* @return string array of incremental path segments
*/
public static String[] getPathsInPath(String path) {
public static String[] getPathsInPath(String pathParam) {
String path = pathParam;
path = path.trim();
if (path.isEmpty()) {
return new String[0];
}
if (path.equals("/")) {
return new String[] {"/"};
}
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
path = path.trim();
ArrayList<String> paths = new ArrayList<>();
@Nullable String urlPrefix = null;
@Nullable Pair<String, String> splitUri = splitUri(path);
if (splitUri != null) {
urlPrefix = splitUri.first;
path = splitUri.second;
if (path == null) {
return new String[] {urlPrefix};
}
}
if (!path.startsWith("/")) {
path = "/" + path;
}
ArrayList<String> paths = buildPaths(path, urlPrefix);
paths.add(urlPrefix != null ? urlPrefix : "/");
Collections.reverse(paths);
return paths.toArray(new String[0]);
}
/**
* Splits a given path to URI prefix (if exists) and path.
*
* @param pathParam
* @return string array of incremental path segments
*/
public static ArrayList<String> buildPaths(String pathParam, String urlPrefix) {
ArrayList<String> paths = new ArrayList<>();
String path = pathParam;
while (path.length() > 0) {
if (urlPrefix != null) {
paths.add(urlPrefix + path);
@@ -619,15 +647,7 @@ public class FileUtils {
break;
}
}
if (urlPrefix != null) {
paths.add(urlPrefix);
} else {
paths.add("/");
}
Collections.reverse(paths);
return paths.toArray(new String[0]);
return paths;
}
/**
@@ -41,6 +41,75 @@ import java.util.TimeZone
@Config(sdk = [LOLLIPOP, P, Build.VERSION_CODES.R])
@Suppress("TooManyFunctions", "StringLiteralDuplication")
class FileUtilsTest {
/**
* Test FileUtils.getPathsInPath() with empty
*
* @see FileUtils.getPathsInPath
*/
@Test
fun testGetPathsInPathForEmpty() {
getPathsInPath("").run {
assertEquals(0, size)
assertArrayEquals(
arrayOf(),
this,
)
}
}
/**
* Test FileUtils.getPathsInPath() with just whitespace
*
* @see FileUtils.getPathsInPath
*/
@Test
fun testGetPathsInPathForWhitespace() {
getPathsInPath(" ").run {
assertEquals(0, size)
assertArrayEquals(
arrayOf(),
this,
)
}
}
/**
* Test FileUtils.getPathsInPath() with single slash
*
* @see FileUtils.getPathsInPath
*/
@Test
fun testGetPathsInPathForSingleSlash() {
getPathsInPath("/").run {
assertEquals(1, size)
assertArrayEquals(
arrayOf(
"/",
),
this,
)
}
}
/**
* Test FileUtils.getPathsInPath() for folder with slash at end
*
* @see FileUtils.getPathsInPath
*/
@Test
fun testGetPathsInPathForSingleFolderWithSlashAtEnd() {
getPathsInPath("/dir/").run {
assertEquals(2, size)
assertArrayEquals(
arrayOf(
"/",
"/dir",
),
this,
)
}
}
/**
* Test FileUtils.getPathsInPath() for directory
*
@@ -186,6 +255,25 @@ class FileUtilsTest {
}
}
/**
* Test FileUtils.getPathsInPath() with an URI that is just a scheme.
*
* @see FileUtils.getPathsInPath
*/
@Test
fun testGetPathsInPathOnlyScheme() {
getPathsInPath("file:///").run {
assertEquals(2, size)
assertArrayEquals(
arrayOf(
"file://",
"file:///",
),
this,
)
}
}
/**
* Test FileUtils.getPathsInPath() with SMB URI
*