diff options
Diffstat (limited to 'src')
24 files changed, 692 insertions, 5 deletions
diff --git a/src/musichub/business/Album.java b/src/main/java/musichub/business/Album.java index 01fd179..01fd179 100644 --- a/src/musichub/business/Album.java +++ b/src/main/java/musichub/business/Album.java diff --git a/src/musichub/business/AudioBook.java b/src/main/java/musichub/business/AudioBook.java index 80392d6..80392d6 100644 --- a/src/musichub/business/AudioBook.java +++ b/src/main/java/musichub/business/AudioBook.java diff --git a/src/musichub/business/AudioElement.java b/src/main/java/musichub/business/AudioElement.java index e0a686b..80a649a 100644 --- a/src/musichub/business/AudioElement.java +++ b/src/main/java/musichub/business/AudioElement.java @@ -1,8 +1,13 @@ package musichub.business; +import musichub.util.LogHandler; import org.w3c.dom.Document; import org.w3c.dom.Element; +import javax.sound.sampled.*; +import java.io.File; +import java.io.IOException; +import java.util.Scanner; import java.util.UUID; public abstract class AudioElement { @@ -84,5 +89,44 @@ public abstract class AudioElement { parentElement.appendChild(contentElement); } + + public void manageAudioElement() throws UnsupportedAudioFileException, IOException, LineUnavailableException { + + Scanner scanner = new Scanner(System.in); + + File file = new File(this.content); + AudioInputStream audioStream = AudioSystem.getAudioInputStream(file); + Clip clip = AudioSystem.getClip(); + clip.open(audioStream); + + String action = ""; + + while(!action.equals("Q")) { + System.out.println("P = Play \b S = Stop \b R = Reset \b Q = Quit"); + System.out.println("Enter your choice"); + action = scanner.next(); + action = action.toUpperCase(); + + switch (action) { + case "S", "Q" -> { + clip.stop(); + LogHandler.write("Music stopped", "INFO"); + } + case "P" -> { + clip.start(); + LogHandler.write("Music started", "INFO"); + } + case "R" -> { + clip.setMicrosecondPosition(0); + LogHandler.write("Music reseted", "INFO"); + } + default -> System.out.println("try again"); + } + System.out.println("You stopped the Audio element"); + } + + clip.close(); + + } }
\ No newline at end of file diff --git a/src/musichub/business/Category.java b/src/main/java/musichub/business/Category.java index f676e51..f676e51 100644 --- a/src/musichub/business/Category.java +++ b/src/main/java/musichub/business/Category.java diff --git a/src/musichub/business/Genre.java b/src/main/java/musichub/business/Genre.java index 18deca6..18deca6 100644 --- a/src/musichub/business/Genre.java +++ b/src/main/java/musichub/business/Genre.java diff --git a/src/musichub/business/Language.java b/src/main/java/musichub/business/Language.java index 679e586..679e586 100644 --- a/src/musichub/business/Language.java +++ b/src/main/java/musichub/business/Language.java diff --git a/src/musichub/business/MusicHub.java b/src/main/java/musichub/business/MusicHub.java index 38e2214..8db2f11 100644 --- a/src/musichub/business/MusicHub.java +++ b/src/main/java/musichub/business/MusicHub.java @@ -6,6 +6,9 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; +import java.io.IOException; import java.util.*; class SortByDate implements Comparator<Album> { @@ -326,4 +329,77 @@ public class MusicHub { } xmlHandler.createXMLFile(document, ELEMENTS_FILE_PATH); } + + public void getAudioElement(List<AudioElement> audios, String elementTitle) throws NoAlbumFoundException, UnsupportedAudioFileException, IOException, LineUnavailableException { + for (AudioElement el : audios) { + if (el.getTitle().equalsIgnoreCase(elementTitle)) { + el.manageAudioElement(); + } + } + + } + + public void searchAudioElement() throws UnsupportedAudioFileException, NoAlbumFoundException, LineUnavailableException, IOException, NoElementFoundException { + 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()) { + throw new NoElementFoundException("Any result for your search"); + } + if (searchResult.size() == 1) { + this.getAudioElement(searchResult, searchResult.get(0).getTitle()); + } + } + + /** + * Method getting a list of playlists + * + * @return a list of playlist titles + * @author Anthony BOULANT + */ + public String getPlayListsTitles() { + StringBuilder titleList = new StringBuilder(); + + for (PlayList pl : playlists) + titleList.append(pl.getTitle()).append("\n"); + return titleList.toString(); + } + + /** + * Method checking the songs contained in a chosen playlist and returning them if found. + * + * @param playListTitle the title of a (chosen) playlist + * @return a list of songs from a playlist + * @throws NoPlayListFoundException if the chosen playlist doesn't exist + * @author Anthony BOULANT + */ + public List<AudioElement> getPlayListSongs(String playListTitle) throws NoPlayListFoundException { + PlayList thePlayList = null; + ArrayList<AudioElement> songsInPlayList = new ArrayList<>(); + for (PlayList pl : playlists) { + if (pl.getTitle().equalsIgnoreCase(playListTitle)) { + thePlayList = pl; + break; + } + } + if (thePlayList == null) throw new NoPlayListFoundException("No playlist with this title in the MusicHub!"); + + List<UUID> songIDs = thePlayList.getElements(); + for (UUID id : songIDs) + for (AudioElement el : elements) { + if (el instanceof Song) { + if (el.getUUID().equals(id)) songsInPlayList.add(el); + } + } + return songsInPlayList; + + } }
\ No newline at end of file diff --git a/src/musichub/business/NoAlbumFoundException.java b/src/main/java/musichub/business/NoAlbumFoundException.java index 04cbfcd..04cbfcd 100644 --- a/src/musichub/business/NoAlbumFoundException.java +++ b/src/main/java/musichub/business/NoAlbumFoundException.java diff --git a/src/musichub/business/NoElementFoundException.java b/src/main/java/musichub/business/NoElementFoundException.java index a9b0d76..a9b0d76 100644 --- a/src/musichub/business/NoElementFoundException.java +++ b/src/main/java/musichub/business/NoElementFoundException.java diff --git a/src/musichub/business/NoPlayListFoundException.java b/src/main/java/musichub/business/NoPlayListFoundException.java index c5eb413..c5eb413 100644 --- a/src/musichub/business/NoPlayListFoundException.java +++ b/src/main/java/musichub/business/NoPlayListFoundException.java diff --git a/src/musichub/business/PlayList.java b/src/main/java/musichub/business/PlayList.java index 4180a62..4180a62 100644 --- a/src/musichub/business/PlayList.java +++ b/src/main/java/musichub/business/PlayList.java diff --git a/src/musichub/business/Song.java b/src/main/java/musichub/business/Song.java index 3e9011b..564b7c5 100644 --- a/src/musichub/business/Song.java +++ b/src/main/java/musichub/business/Song.java @@ -53,4 +53,6 @@ public class Song extends AudioElement { parentElement.appendChild(song); } + + }
\ No newline at end of file diff --git a/src/musichub/main/Main.java b/src/main/java/musichub/main/Main.java index f917b01..07786d9 100644 --- a/src/musichub/main/Main.java +++ b/src/main/java/musichub/main/Main.java @@ -1,12 +1,22 @@ package musichub.main; import musichub.business.*; +import musichub.util.LogHandler; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; +import java.io.IOException; import java.util.Iterator; +import java.util.List; import java.util.Scanner; +import static musichub.util.PathValidation.isPathValid; +import static musichub.util.Policy.showTerm; + public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException, NoAlbumFoundException { + + showTerm(); MusicHub theHub = new MusicHub(); @@ -20,8 +30,10 @@ public class Main { if (choice.length() == 0) System.exit(0); - while (choice.charAt(0) != 'q') { + while (!choice.equals("")) { //if the user puts nothing, quit the loop/system switch (choice.charAt(0)) { + case 'q': //added the option directly in the switch instead of the loop + System.exit(0); case 'h': printAvailableCommands(); choice = scan.nextLine(); @@ -39,8 +51,10 @@ public class Main { albumTitle = scan.nextLine(); try { - System.out.println(theHub.getAlbumSongsSortedByGenre(albumTitle)); + List<Song> songs = theHub.getAlbumSongsSortedByGenre(albumTitle); + System.out.println(songs); } catch (NoAlbumFoundException ex) { + LogHandler.write("No album found with the requested title", "WARNING"); System.out.println("No album found with the requested title " + ex.getMessage()); } printAvailableCommands(); @@ -53,8 +67,12 @@ public class Main { albumTitle = scan.nextLine(); try { + List<AudioElement> songs = theHub.getAlbumSongs(albumTitle); System.out.println(theHub.getAlbumSongs(albumTitle)); + String song = scan.nextLine(); + theHub.getAudioElement(songs, song); } catch (NoAlbumFoundException ex) { + LogHandler.write("No album found with the requested title", "WARNING"); System.out.println("No album found with the requested title " + ex.getMessage()); } printAvailableCommands(); @@ -75,15 +93,37 @@ public class Main { String genre = scan.nextLine(); System.out.println("Song artist: "); String artist = scan.nextLine(); - System.out.println("Song length in seconds: "); - int length = Integer.parseInt(scan.nextLine()); + + int length; + try { + System.out.println("Song length in seconds: "); + length = Integer.parseInt(scan.nextLine()); + } catch (NumberFormatException ex) { + String logMsg = "You've not provided a number for the length."; + LogHandler.write(logMsg, "ERROR"); //write a line in the log file + System.err.println(logMsg); + System.out.println("Type h for available commands"); + choice = scan.nextLine(); + break; + } + System.out.println("Song content: "); String content = scan.nextLine(); + if (!isPathValid(content)) { + String logMsg = "The music file cannot be found with the path you've provided or the extension is not .wav"; + LogHandler.write(logMsg, "ERROR"); //write a line in the log file + System.err.println(logMsg); + System.out.println("Type h for available commands"); + choice = scan.nextLine(); + break; + } + Song s = new Song(title, artist, length, content, genre); theHub.addElement(s); System.out.println("New element list: "); Iterator<AudioElement> it = theHub.elements(); while (it.hasNext()) System.out.println(it.next().getTitle()); + LogHandler.write("Song successfully created", "INFO"); System.out.println("Song created!"); printAvailableCommands(); choice = scan.nextLine(); @@ -104,6 +144,7 @@ public class Main { System.out.println("New list of albums: "); Iterator<Album> ita = theHub.albums(); while (ita.hasNext()) System.out.println(ita.next().getTitle()); + LogHandler.write("Album successfully created", "INFO"); System.out.println("Album created!"); printAvailableCommands(); choice = scan.nextLine(); @@ -131,6 +172,7 @@ public class Main { } catch (NoAlbumFoundException | NoElementFoundException ex) { System.out.println(ex.getMessage()); } + LogHandler.write("Song successfully added to the album", "INFO"); System.out.println("Song added to the album!"); printAvailableCommands(); choice = scan.nextLine(); @@ -152,6 +194,7 @@ public class Main { String bLanguage = scan.nextLine(); AudioBook b = new AudioBook(bTitle, bArtist, bLength, bContent, bLanguage, bCategory); theHub.addElement(b); + LogHandler.write("Audiobook successfully created", "INFO"); System.out.println("Audiobook created! New element list: "); Iterator<AudioElement> itl = theHub.elements(); while (itl.hasNext()) System.out.println(itl.next().getTitle()); @@ -190,6 +233,7 @@ public class Main { System.out.println("Type y to add a new one, n to end"); choice = scan.nextLine(); } + LogHandler.write("Playlist successfully created", "INFO"); System.out.println("Playlist created!"); printAvailableCommands(); choice = scan.nextLine(); @@ -208,6 +252,7 @@ public class Main { } catch (NoPlayListFoundException ex) { System.out.println(ex.getMessage()); } + LogHandler.write("Playlist successfully deleted", "INFO"); System.out.println("Playlist deleted!"); printAvailableCommands(); choice = scan.nextLine(); @@ -217,10 +262,50 @@ public class Main { theHub.saveElements(); theHub.saveAlbums(); theHub.savePlayLists(); + LogHandler.write("Elements, albums and playlists successfully saved", "INFO"); System.out.println("Elements, albums and playlists saved!"); printAvailableCommands(); choice = scan.nextLine(); break; + case 'r': + //search a music + try { + theHub.searchAudioElement(); + } + catch (NoElementFoundException e){ + System.err.println(e.getMessage()); + } + catch (java.io.FileNotFoundException e){ + LogHandler.write("Please create a file with the extension .wav inside the song folder at root of the app", "ERROR"); + System.err.println(e.getMessage() + " Please create a file with the extension .wav inside the song folder at root of the app"); + } + printAvailableCommands(); + choice = scan.nextLine(); + break; + case 'o': + //consult the app logs + LogHandler.read(); + System.out.println("Type h for available commands"); + choice = scan.nextLine(); + break; + case 'm': + //songs of a playlist + System.out.println("Songs of a playlist will be displayed; enter the playlist name, available playlists are:"); + System.out.println(theHub.getPlayListsTitles()); + + playListTitle = scan.nextLine(); + try { + List<AudioElement> songs = theHub.getPlayListSongs(playListTitle); + System.out.println(theHub.getPlayListSongs(playListTitle)); + String song = scan.nextLine(); + theHub.getAudioElement(songs, song); + } catch (NoPlayListFoundException ex) { + LogHandler.write("No playlist found with the requested title", "WARNING"); + System.out.println("No playlist found with the requested title " + ex.getMessage()); + } + printAvailableCommands(); + choice = scan.nextLine(); + break; default: break; @@ -234,13 +319,17 @@ 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"); System.out.println("l: add a new audiobook"); System.out.println("p: create a new playlist from existing songs and audio books"); + System.out.println("m: display songs of a playlist"); 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/util/PathValidation.java b/src/main/java/musichub/util/PathValidation.java new file mode 100644 index 0000000..ea00c24 --- /dev/null +++ b/src/main/java/musichub/util/PathValidation.java @@ -0,0 +1,49 @@ +package musichub.util; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * Class offering a path validation method. + * + * @author Aimeric ADJUTOR + * @version 1.0 + */ + +public final class PathValidation { + + /** + * Method that just throws an AssertionError if the class is called + * + * @throws AssertionError you can't instantiate this class + * @author Aimeric ADJUTOR + */ + + public PathValidation() { + throw new AssertionError("You just can't instantiate this class."); + } + + /** + * 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) & isExtensionValid); + } + +}
\ No newline at end of file diff --git a/src/main/java/musichub/util/Policy.java b/src/main/java/musichub/util/Policy.java new file mode 100644 index 0000000..01c7d20 --- /dev/null +++ b/src/main/java/musichub/util/Policy.java @@ -0,0 +1,76 @@ +package musichub.util; + +import java.io.IOException; +import java.util.Scanner; + +/** + * Class offering methods to prompt a policy agreement form to the user. + * + * @author Anthony BOULANT + * @version 1.0 + */ + +public final class Policy { + + /** + * Method that just throws an AssertionError if the class is called + * + * @throws AssertionError you can't instantiate this class + * @author Anthony BOULANT + */ + + public Policy() { + throw new AssertionError("You just can't instantiate this class."); + } + + /** + * Method prompting the whole policy agreement process + * + * @author Anthony BOULANT + */ + public static void showTerm() { + + String termsAndConditions = """ + Terms and Conditions + In using this website you are deemed to have read and agreed to the following terms and conditions: + Privacy Statement + We are committed to protecting your privacy. Authorized employees within the company on a need to know basis only use any information collected from individual customers. We constantly review our systems and data to ensure the best possible service to our customers. Parliament has created specific offences for unauthorised actions against computer systems and data. We will investigate any such actions with a view to prosecuting and/or taking civil proceedings to recover damages against those responsible + These terms and conditions form part of the Agreement between the Client and ourselves. Your accessing of this website and/or undertaking of a booking or Agreement indicates your understanding, agreement to and acceptance, of the Disclaimer Notice and the full Terms and Conditions contained herein. Your statutory Consumer Rights are unaffected. s + © Company Name 2021 All Rights Reserved + """; + + printAgreement(); + Scanner scan = new Scanner(System.in); + String choice = scan.nextLine().toLowerCase(); + + while (!choice.equals("y")) { + switch (choice.charAt(0)) { + case 'n' -> System.exit(0); + case 'r' -> { + System.out.println(termsAndConditions); + printAgreement(); + choice = scan.nextLine().toLowerCase(); + } + default -> { + printAgreement(); + choice = scan.nextLine().toLowerCase(); + } + } + } + try { + LogHandler.write("Terms accepted by the user.", "INFO"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Method printing the commands the user can use. + * + * @author Anthony BOULANT + */ + private static void printAgreement() { + System.out.println("By using our app, do you accept our Terms and Conditions ?"); + System.out.println("[YES : y NO : n READ : r]"); + } +} diff --git a/src/musichub/util/XMLHandler.java b/src/main/java/musichub/util/XMLHandler.java index 4b61842..4b61842 100644 --- a/src/musichub/util/XMLHandler.java +++ b/src/main/java/musichub/util/XMLHandler.java diff --git a/src/test/java/musichub/business/CategoryTest.java b/src/test/java/musichub/business/CategoryTest.java new file mode 100644 index 0000000..f1fef50 --- /dev/null +++ b/src/test/java/musichub/business/CategoryTest.java @@ -0,0 +1,25 @@ +package musichub.business; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +public class CategoryTest { + + @Test + void testGetter() { + + String t1 = "t1"; + String a1 = "a1"; + int l1 = 1; + String c1 = a1+" - "+t1; + String lan1 = "french"; + String cat1 = "novel"; + AudioBook s1 = new AudioBook(t1, a1, l1, c1, lan1, cat1); + + assertEquals(s1.getCategory(), Category.NOVEL); + s1.setCategory("NOVEL"); + assertEquals(s1.getCategory(), Category.NOVEL); + } +} diff --git a/src/test/java/musichub/business/LanguageTest.java b/src/test/java/musichub/business/LanguageTest.java new file mode 100644 index 0000000..93e9d1c --- /dev/null +++ b/src/test/java/musichub/business/LanguageTest.java @@ -0,0 +1,23 @@ +package musichub.business; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class LanguageTest { + @Test + void testGetter() { + + String t1 = "t1"; + String a1 = "a1"; + int l1 = 1; + String c1 = a1+" - "+t1; + String lan1 = "FRENCH"; + String cat1 = "novel"; + AudioBook s1 = new AudioBook(t1, a1, l1, c1, lan1, cat1); + assertEquals(s1.getLanguage(), Language.FRENCH); + s1.setLanguage("french"); + assertEquals(s1.getLanguage(), Language.FRENCH); + } + +} 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 |