Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -586,27 +586,55 @@ public static String[] getFolderNamesInPath(String path) {
* ["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);
Expand All @@ -619,15 +647,7 @@ public static String[] getPathsInPath(String path) {
break;
}
}

if (urlPrefix != null) {
paths.add(urlPrefix);
} else {
paths.add("/");
}
Collections.reverse(paths);

return paths.toArray(new String[0]);
return paths;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down