Skip to content

Commit 2dd4992

Browse files
authored
Merge pull request #4612 from JemimaBrewer/release/4.0
Make `getPathsInPath` function more robust, including null handling
2 parents f90b1db + ef4ad6b commit 2dd4992

2 files changed

Lines changed: 120 additions & 12 deletions

File tree

app/src/main/java/com/amaze/filemanager/filesystem/files/FileUtils.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -586,27 +586,55 @@ public static String[] getFolderNamesInPath(String path) {
586586
* ["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"]
587587
* </code>
588588
*
589-
* @param path
589+
* @param pathParam
590590
* @return string array of incremental path segments
591591
*/
592-
public static String[] getPathsInPath(String path) {
592+
public static String[] getPathsInPath(String pathParam) {
593+
String path = pathParam;
594+
path = path.trim();
595+
if (path.isEmpty()) {
596+
return new String[0];
597+
}
598+
if (path.equals("/")) {
599+
return new String[] {"/"};
600+
}
593601
if (path.endsWith("/")) {
594602
path = path.substring(0, path.length() - 1);
595603
}
596604
path = path.trim();
597605

598-
ArrayList<String> paths = new ArrayList<>();
599606
@Nullable String urlPrefix = null;
600607
@Nullable Pair<String, String> splitUri = splitUri(path);
601608
if (splitUri != null) {
602609
urlPrefix = splitUri.first;
603610
path = splitUri.second;
611+
612+
if (path == null) {
613+
return new String[] {urlPrefix};
614+
}
604615
}
605616

606617
if (!path.startsWith("/")) {
607618
path = "/" + path;
608619
}
609620

621+
ArrayList<String> paths = buildPaths(path, urlPrefix);
622+
623+
paths.add(urlPrefix != null ? urlPrefix : "/");
624+
Collections.reverse(paths);
625+
626+
return paths.toArray(new String[0]);
627+
}
628+
629+
/**
630+
* Splits a given path to URI prefix (if exists) and path.
631+
*
632+
* @param pathParam
633+
* @return string array of incremental path segments
634+
*/
635+
public static ArrayList<String> buildPaths(String pathParam, String urlPrefix) {
636+
ArrayList<String> paths = new ArrayList<>();
637+
String path = pathParam;
610638
while (path.length() > 0) {
611639
if (urlPrefix != null) {
612640
paths.add(urlPrefix + path);
@@ -619,15 +647,7 @@ public static String[] getPathsInPath(String path) {
619647
break;
620648
}
621649
}
622-
623-
if (urlPrefix != null) {
624-
paths.add(urlPrefix);
625-
} else {
626-
paths.add("/");
627-
}
628-
Collections.reverse(paths);
629-
630-
return paths.toArray(new String[0]);
650+
return paths;
631651
}
632652

633653
/**

app/src/test/java/com/amaze/filemanager/filesystem/files/FileUtilsTest.kt

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,75 @@ import java.util.TimeZone
4141
@Config(sdk = [LOLLIPOP, P, Build.VERSION_CODES.R])
4242
@Suppress("TooManyFunctions", "StringLiteralDuplication")
4343
class FileUtilsTest {
44+
/**
45+
* Test FileUtils.getPathsInPath() with empty
46+
*
47+
* @see FileUtils.getPathsInPath
48+
*/
49+
@Test
50+
fun testGetPathsInPathForEmpty() {
51+
getPathsInPath("").run {
52+
assertEquals(0, size)
53+
assertArrayEquals(
54+
arrayOf(),
55+
this,
56+
)
57+
}
58+
}
59+
60+
/**
61+
* Test FileUtils.getPathsInPath() with just whitespace
62+
*
63+
* @see FileUtils.getPathsInPath
64+
*/
65+
@Test
66+
fun testGetPathsInPathForWhitespace() {
67+
getPathsInPath(" ").run {
68+
assertEquals(0, size)
69+
assertArrayEquals(
70+
arrayOf(),
71+
this,
72+
)
73+
}
74+
}
75+
76+
/**
77+
* Test FileUtils.getPathsInPath() with single slash
78+
*
79+
* @see FileUtils.getPathsInPath
80+
*/
81+
@Test
82+
fun testGetPathsInPathForSingleSlash() {
83+
getPathsInPath("/").run {
84+
assertEquals(1, size)
85+
assertArrayEquals(
86+
arrayOf(
87+
"/",
88+
),
89+
this,
90+
)
91+
}
92+
}
93+
94+
/**
95+
* Test FileUtils.getPathsInPath() for folder with slash at end
96+
*
97+
* @see FileUtils.getPathsInPath
98+
*/
99+
@Test
100+
fun testGetPathsInPathForSingleFolderWithSlashAtEnd() {
101+
getPathsInPath("/dir/").run {
102+
assertEquals(2, size)
103+
assertArrayEquals(
104+
arrayOf(
105+
"/",
106+
"/dir",
107+
),
108+
this,
109+
)
110+
}
111+
}
112+
44113
/**
45114
* Test FileUtils.getPathsInPath() for directory
46115
*
@@ -186,6 +255,25 @@ class FileUtilsTest {
186255
}
187256
}
188257

258+
/**
259+
* Test FileUtils.getPathsInPath() with an URI that is just a scheme.
260+
*
261+
* @see FileUtils.getPathsInPath
262+
*/
263+
@Test
264+
fun testGetPathsInPathOnlyScheme() {
265+
getPathsInPath("file:///").run {
266+
assertEquals(2, size)
267+
assertArrayEquals(
268+
arrayOf(
269+
"file://",
270+
"file:///",
271+
),
272+
this,
273+
)
274+
}
275+
}
276+
189277
/**
190278
* Test FileUtils.getPathsInPath() with SMB URI
191279
*

0 commit comments

Comments
 (0)