diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/musichub/main/Main.java | 2 | ||||
-rw-r--r-- | src/main/java/musichub/util/PathValidation.java | 14 | ||||
-rw-r--r-- | src/test/java/musichub/util/PathValidationTest.java | 7 |
3 files changed, 17 insertions, 6 deletions
diff --git a/src/main/java/musichub/main/Main.java b/src/main/java/musichub/main/Main.java index 0669795..cd1fd5f 100644 --- a/src/main/java/musichub/main/Main.java +++ b/src/main/java/musichub/main/Main.java @@ -95,7 +95,7 @@ public class Main { System.out.println("Song content: "); String content = scan.nextLine(); if (!isPathValid(content)) { - String logMsg = "The music file was not found with the path you've provided."; + String logMsg = "The music file was not found with the path you've provided or the extension is not .wav"; LogHandler.write(logMsg, "WARNING"); //write a line in the log file System.out.println(logMsg + "\nType h for available commands"); choice = scan.nextLine(); diff --git a/src/main/java/musichub/util/PathValidation.java b/src/main/java/musichub/util/PathValidation.java index df11388..ea00c24 100644 --- a/src/main/java/musichub/util/PathValidation.java +++ b/src/main/java/musichub/util/PathValidation.java @@ -25,15 +25,25 @@ public final class PathValidation { } /** - * Method that checks the validity of a given path + * Method that checks the validity of a given path and file. * * @param inputPath the path given by the user * @return a boolean */ public static boolean isPathValid(String inputPath) { + boolean isExtensionValid = false; + + int index = inputPath.lastIndexOf('.'); + if (index > 0) { + String extension = inputPath.substring(index + 1); + if (extension.equals("wav")) { + isExtensionValid = true; + } + } + Path path = Paths.get(inputPath); - return Files.exists(path); + return (Files.exists(path) & isExtensionValid); } }
\ No newline at end of file diff --git a/src/test/java/musichub/util/PathValidationTest.java b/src/test/java/musichub/util/PathValidationTest.java index b0e0c38..f087ac1 100644 --- a/src/test/java/musichub/util/PathValidationTest.java +++ b/src/test/java/musichub/util/PathValidationTest.java @@ -19,8 +19,9 @@ public class PathValidationTest { @Test void testIsPathValid() { - assertTrue(isPathValid("Song/Side_To_Side.wav")); //the right path - assertFalse(isPathValid("wrong_path/Side_To_Side.wav")); //wrong path - assertFalse(isPathValid("Song/Side_To_Side.mp3")); //wrong extension + assertTrue(isPathValid("Song/Side_To_Side.wav")); //right path, right file, right extension + assertFalse(isPathValid("wrong_path/Side_To_Side.wav")); //wrong path, right file, right extension + assertFalse(isPathValid("Song/Side_To_Side.mp3")); //right path, right file, wrong extension + assertFalse(isPathValid("Song/Side_To_Sidewav")); //right path, right file, wrong extension (without a dot) } } |