-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ContentSettingsFragment.isValidPath to helpers and add unit test…
… for it.
- Loading branch information
Showing
3 changed files
with
57 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/src/main/java/org/schabi/newpipe/util/FilePathUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.schabi.newpipe.util; | ||
|
||
import java.io.File; | ||
|
||
public final class FilePathUtils { | ||
private FilePathUtils() { } | ||
|
||
|
||
/** | ||
* Check that the path is a valid directory path and it exists. | ||
* | ||
* @param path full path of directory, | ||
* @return is path valid or not | ||
*/ | ||
public static boolean isValidDirectoryPath(final String path) { | ||
if (path == null || path.isEmpty()) { | ||
return false; | ||
} | ||
final File file = new File(path); | ||
return file.exists() && file.isDirectory(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/test/java/org/schabi/newpipe/util/FilePathHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.schabi.newpipe.util; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class FilePathHelperTest { | ||
@Test | ||
public void testIsValidDirectoryPath() throws IOException { | ||
// path that exists | ||
final File dir1 = Files.createTempDirectory("dir1").toFile(); | ||
assertTrue(FilePathUtils.isValidDirectoryPath(dir1.getAbsolutePath())); | ||
|
||
// a directory in above path that exists | ||
final File subDir = Files.createDirectory(dir1.toPath().resolve("subdir")).toFile(); | ||
assertTrue(FilePathUtils.isValidDirectoryPath(subDir.getAbsolutePath())); | ||
|
||
// a directory in above path that doesn't exist | ||
assertFalse(FilePathUtils.isValidDirectoryPath(dir1.toPath().resolve("not-exists-subdir"). | ||
toFile().getAbsolutePath())); | ||
|
||
// file is not a valid direcotry path | ||
final File tempFile = Files.createFile(dir1.toPath().resolve("simple_file")).toFile(); | ||
assertFalse(FilePathUtils.isValidDirectoryPath(tempFile.getAbsolutePath())); | ||
} | ||
|
||
} |