diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/musichub/business/MusicHub.java | 21 | ||||
-rw-r--r-- | src/main/java/musichub/main/Main.java | 23 | ||||
-rw-r--r-- | src/main/java/musichub/util/LogHandler.java | 68 | ||||
-rw-r--r-- | src/main/java/musichub/util/PathValidation.java (renamed from src/main/java/musichub/business/PathValidation.java) | 16 | ||||
-rw-r--r-- | src/test/java/musichub/business/PathValidationTest.java | 26 | ||||
-rw-r--r-- | src/test/java/musichub/business/PlayListTest.java | 54 | ||||
-rw-r--r-- | src/test/java/musichub/business/SongTest.java | 83 | ||||
-rw-r--r-- | src/test/java/musichub/util/LogHandlerTest.java | 41 | ||||
-rw-r--r-- | src/test/java/musichub/util/PathValidationTest.java | 27 | ||||
-rw-r--r-- | src/test/java/musichub/util/XMLHandlerTest.java | 30 |
10 files changed, 357 insertions, 32 deletions
diff --git a/src/main/java/musichub/business/MusicHub.java b/src/main/java/musichub/business/MusicHub.java index f7cf137..bbac505 100644 --- a/src/main/java/musichub/business/MusicHub.java +++ b/src/main/java/musichub/business/MusicHub.java @@ -339,4 +339,25 @@ public class MusicHub { } } + + public void searchAudioElement() throws UnsupportedAudioFileException, NoAlbumFoundException, LineUnavailableException, IOException { + Scanner scanner = new Scanner(System.in); + System.out.println("Entrez le titre ou l'artiste de la musique que vous souhaitez chercher dans la base de données"); + String word = scanner.next().toLowerCase(Locale.ROOT); + List<AudioElement> searchResult = new ArrayList<>(); + for(AudioElement el : elements){ + if(el.getTitle().toLowerCase(Locale.ROOT).contains(word) || el.getArtist().toLowerCase(Locale.ROOT).contains(word)){ + searchResult.add(el); + System.out.println(el); + } + } + + if(searchResult.isEmpty()){ + System.err.println("Aucun résultat pour votre recherche"); + } + if (searchResult.size()==1){ + this.getAudioElement(searchResult, searchResult.get(0).getTitle()); + } + } + }
\ No newline at end of file diff --git a/src/main/java/musichub/main/Main.java b/src/main/java/musichub/main/Main.java index e2e4731..cd1fd5f 100644 --- a/src/main/java/musichub/main/Main.java +++ b/src/main/java/musichub/main/Main.java @@ -1,6 +1,7 @@ package musichub.main; import musichub.business.*; +import musichub.util.LogHandler; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; @@ -9,10 +10,11 @@ import java.util.Iterator; import java.util.List; import java.util.Scanner; -import static musichub.business.PathValidation.isPathValid; +import static musichub.util.PathValidation.isPathValid; public class Main { - public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException { + public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException, NoAlbumFoundException { + MusicHub theHub = new MusicHub(); @@ -93,7 +95,9 @@ public class Main { System.out.println("Song content: "); String content = scan.nextLine(); if (!isPathValid(content)) { - System.out.println("The music file was not found with the path you've provided.\nType h for available commands"); + 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(); break; } @@ -240,6 +244,16 @@ public class Main { printAvailableCommands(); choice = scan.nextLine(); break; + case 'r': + //search a music + theHub.searchAudioElement(); + printAvailableCommands(); + case 'o': + //consult the app logs + LogHandler.read(); + System.out.println("Type h for available commands"); + choice = scan.nextLine(); + break; default: break; @@ -253,6 +267,7 @@ public class Main { System.out.println("g: display songs of an album, ordered by genre"); System.out.println("d: display songs of an album"); System.out.println("u: display audiobooks ordered by author"); + System.out.println("r: search audio elements"); System.out.println("c: add a new song"); System.out.println("a: add a new album"); System.out.println("+: add a song to an album"); @@ -260,6 +275,8 @@ public class Main { System.out.println("p: create a new playlist from existing songs and audio books"); System.out.println("-: delete an existing playlist"); System.out.println("s: save elements, albums, playlists"); + System.out.println("o: consult the app logs"); System.out.println("q: quit program"); } + }
\ No newline at end of file diff --git a/src/main/java/musichub/util/LogHandler.java b/src/main/java/musichub/util/LogHandler.java new file mode 100644 index 0000000..988b149 --- /dev/null +++ b/src/main/java/musichub/util/LogHandler.java @@ -0,0 +1,68 @@ +package musichub.util; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.sql.Timestamp; + +/** + * Class offering a log writing method. + * + * @author Aimeric ADJUTOR + * @version 1.0 + */ + +public final class LogHandler { + + /** + * Method that just throws an AssertionError if the class is called + * + * @throws AssertionError you can't instantiate this class + * @author Aimeric ADJUTOR + */ + + public LogHandler() { + throw new AssertionError("You just can't instantiate this class."); + } + + /** + * Method that writes a log message to spoteezer.log + * + * @param msg the message to write + * @param type the type of log + * @throws IOException if the file's not there + */ + public static void write(String msg, String type) throws IOException { + + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); + + //Create the log using the given message + String logMsg = "\n[" + timestamp + "] " + type + ": " + msg; + + // Define the file name of the file + Path fileName = Path.of("log/spoteezer.log"); + + // Write into the file + Files.writeString(fileName, logMsg, StandardOpenOption.APPEND); + + + } + + /** + * Method that prints the content of spoteezer.log + * + * @throws IOException if the file's not there + */ + public static void read() throws IOException { + Path fileName = Path.of("log/spoteezer.log"); + + // Read the content of the file + String file_content = Files.readString(fileName); + + // Print the content inside the file + System.out.println("\n" + file_content + "\n"); + + } + +}
\ No newline at end of file diff --git a/src/main/java/musichub/business/PathValidation.java b/src/main/java/musichub/util/PathValidation.java index b0281ba..ea00c24 100644 --- a/src/main/java/musichub/business/PathValidation.java +++ b/src/main/java/musichub/util/PathValidation.java @@ -1,4 +1,4 @@ -package musichub.business; +package musichub.util; import java.nio.file.Files; import java.nio.file.Path; @@ -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/business/PathValidationTest.java b/src/test/java/musichub/business/PathValidationTest.java deleted file mode 100644 index fffbdde..0000000 --- a/src/test/java/musichub/business/PathValidationTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package musichub.business; - - -import org.junit.jupiter.api.Test; - -import static musichub.business.PathValidation.isPathValid; -import static org.junit.jupiter.api.Assertions.*; - -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 - } - - @Test - void testPathValidationClass() { - try { - PathValidation pathValidationClass = new PathValidation(); - } catch (Error ex) { - assertTrue(ex instanceof AssertionError); - assertEquals("You just can't instantiate this class.", ex.getMessage()); - } - } -} diff --git a/src/test/java/musichub/business/PlayListTest.java b/src/test/java/musichub/business/PlayListTest.java new file mode 100644 index 0000000..9fe63cf --- /dev/null +++ b/src/test/java/musichub/business/PlayListTest.java @@ -0,0 +1,54 @@ +package musichub.business; + +import musichub.util.XMLHandler; +import org.junit.jupiter.api.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import java.util.ArrayList; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +public class PlayListTest { + final String DIR = System.getProperty("user.dir"); + final String PLAYLISTS_FILE_PATH = DIR + "/files/playlists.xml"; + final XMLHandler xmlHandler = new XMLHandler(); + String title = "Side To Side"; + String id = "66d277ca-cbc4-471c-a07e-082363375bcc"; + ArrayList<UUID> elementUUIDs = null; + + @Test + void testPlaylistClass() { + new PlayList(title, id, elementUUIDs); + new PlayList(title); + + } + + @Test + void testPlaylistClassXML() { + NodeList playlistNodes = xmlHandler.parseXMLFile(PLAYLISTS_FILE_PATH); + Element playlistElement = (Element) playlistNodes.item(1); + new PlayList(playlistElement); + } + + @Test + void testGetElements() { + assertNull(new PlayList(title, id, elementUUIDs).getElements()); + } + + @Test + void testGetTitle() { + assertEquals(new PlayList(title).getTitle(), "Side To Side"); + assertNotEquals(new PlayList(title).getTitle(), "God Is A Woman"); + } + + @Test + void testCreateXMLElement() { + PlayList p = new PlayList(title); + Document document = xmlHandler.createXMLDocument(); + Element root = document.createElement("playlists"); + p.createXMLElement(document, root); + } +} diff --git a/src/test/java/musichub/business/SongTest.java b/src/test/java/musichub/business/SongTest.java new file mode 100644 index 0000000..14f9af1 --- /dev/null +++ b/src/test/java/musichub/business/SongTest.java @@ -0,0 +1,83 @@ +package musichub.business; + +import musichub.util.XMLHandler; +import org.junit.jupiter.api.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +public class SongTest { + String title = "Side To Side"; + String artist = "Ariana Grande"; + int length = 186; + String uid = "66d277ca-cbc4-471c-a07e-082363375bcc"; + String content = "Song/Side_To_Side.wav"; + String genre = "rock"; + + final String DIR = System.getProperty("user.dir"); + final String ELEMENTS_FILE_PATH = DIR + "/files/elements.xml"; + final XMLHandler xmlHandler = new XMLHandler(); + + + @Test + void testSongClass() { + new Song(title, artist, length, uid, content, genre); + new Song(title, artist, length, content, genre); + + } + + @Test + void testSongClassXML() { + NodeList audioelementsNodes = xmlHandler.parseXMLFile(ELEMENTS_FILE_PATH); + Element audioElement = (Element) audioelementsNodes.item(1); + new Song(audioElement); + } + + @Test + void testGetGenre() { + assertEquals(new Song(title, artist, length, content, genre) + .getGenre(), + "rock"); + assertNotEquals(new Song(title, artist, length, content, genre) + .getGenre(), + "pop"); + } + + @Test + void testSetGenre() { + new Song(title, artist, length, content, "classic"); + new Song(title, artist, length, content, "hiphop"); + new Song(title, artist, length, content, "rock"); + new Song(title, artist, length, content, "pop"); + new Song(title, artist, length, content, "rap"); + + Song s = new Song(title, artist, length, content, "cgfdhdfhj"); + assertEquals(s.getGenre(), "jazz"); + } + + @Test + void testToString() { + assertEquals( + new Song(title, artist, length, content, genre) + .toString(), + "Title = Side To Side, Artist = Ariana Grande, Length = 186, Content = Song/Side_To_Side.wav, Genre = rock\n" + ); + assertNotEquals( + new Song(title, artist, length, content, genre) + .toString(), + "Title = God is a woman, Artist = Ariana Grande, Length = 186, Content = Song/Side_To_Side.wav, Genre = rock\n" + ); + } + + @Test + void testCreateXMLElement() { + Song s = new Song(title, artist, length, content, genre); + Document document = xmlHandler.createXMLDocument(); + Element root = document.createElement("elements"); + s.createXMLElement(document, root); + + } +} diff --git a/src/test/java/musichub/util/LogHandlerTest.java b/src/test/java/musichub/util/LogHandlerTest.java new file mode 100644 index 0000000..ab8e160 --- /dev/null +++ b/src/test/java/musichub/util/LogHandlerTest.java @@ -0,0 +1,41 @@ +package musichub.util; + + +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static musichub.util.LogHandler.read; +import static musichub.util.LogHandler.write; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class LogHandlerTest { + @Test + void testLogHandlerClass() { + try { + LogHandler logHandlerClass = new LogHandler(); + } catch (Error e) { + assertTrue(e instanceof AssertionError); + assertEquals("You just can't instantiate this class.", e.getMessage()); + } + } + + @Test + void testWrite() { + try { + write("JUnit test", "TEST"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + void testRead() { + try { + read(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/musichub/util/PathValidationTest.java b/src/test/java/musichub/util/PathValidationTest.java new file mode 100644 index 0000000..f087ac1 --- /dev/null +++ b/src/test/java/musichub/util/PathValidationTest.java @@ -0,0 +1,27 @@ +package musichub.util; + + +import org.junit.jupiter.api.Test; + +import static musichub.util.PathValidation.isPathValid; +import static org.junit.jupiter.api.Assertions.*; + +public class PathValidationTest { + @Test + void testPathValidationClass() { + try { + PathValidation pathValidationClass = new PathValidation(); + } catch (Error e) { + assertTrue(e instanceof AssertionError); + assertEquals("You just can't instantiate this class.", e.getMessage()); + } + } + + @Test + void testIsPathValid() { + 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) + } +} diff --git a/src/test/java/musichub/util/XMLHandlerTest.java b/src/test/java/musichub/util/XMLHandlerTest.java new file mode 100644 index 0000000..7b5a791 --- /dev/null +++ b/src/test/java/musichub/util/XMLHandlerTest.java @@ -0,0 +1,30 @@ +package musichub.util; + + +import org.junit.jupiter.api.Test; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + +public class XMLHandlerTest { + + final String DIR = System.getProperty("user.dir"); + final String JUNIT_FILE_PATH = DIR + "/files/JUnit.xml"; + + @Test + void testCreateXML() { + XMLHandler xmlHandler = new XMLHandler(); //XML class + Document document = xmlHandler.createXMLDocument(); //XMLDocument method + xmlHandler.createXMLFile(document, JUNIT_FILE_PATH); //XMLFile method + } + + @Test + void testParseXMLFile() { + final String PARSE_DIR = System.getProperty("user.dir"); + final String PARSE_FILE_PATH = PARSE_DIR + "/files/parse_JUnit.xml"; + XMLHandler xmlHandler = new XMLHandler(); + //wrong content of file resulting in an exception, will print it during test but it's normal + NodeList junitNodes = xmlHandler.parseXMLFile(JUNIT_FILE_PATH); + //right content of file + NodeList parseNodes = xmlHandler.parseXMLFile(PARSE_FILE_PATH); + } +}
\ No newline at end of file |