From 82d393d3b52d861fc1da1539c3759571deaa3899 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Thu, 25 Aug 2016 21:32:52 +0100 Subject: [PATCH] Refactored some code to more sensible locations. Made IDatabase abstract rather than an interface and self referential for implemented methods. --- pom.xml | 12 ++- .../musicplayer/StartPlayingException.java | 2 +- .../callbacks/LibraryCallbackInterface.java | 20 ----- .../musicplayer/db/HibernateDatabase.java | 3 +- src/main/java/musicplayer/db/IDatabase.java | 48 +++++++++-- .../java/musicplayer/library/ILibrary.java | 84 +++++-------------- .../musicplayer/library/JTreeLibrary.java | 29 ++++--- src/main/java/musicplayer/model/Album.java | 1 + .../musicplayer/model/ExtractedMetadata.java | 25 +++++- .../musicplayer/player/GStreamerPlayer.java | 2 +- .../java/musicplayer/player/VLCPlayer.java | 4 +- .../java/musicplayer/playlist/IPlaylist.java | 16 ++-- .../musicplayer/playlist/JTablePlaylist.java | 2 +- .../java/musicplayer/swingui/ControlBar.java | 4 +- .../java/musicplayer/swingui/PlayerGUI.java | 3 +- .../musicplayer/db/HibernateDatabaseTest.java | 51 ++++++++++- .../musicplayer/library/ILibraryTest.java | 79 ----------------- .../musicplayer/library/JTreeLibraryTest.java | 14 ++-- .../model/ExtractedMetadataTest.java | 38 +++++++++ .../player/GStreamerPlayerTest.java | 9 +- .../musicplayer/player/VLCPlayerTest.java | 10 +-- .../musicplayer/playlist/IPlaylistTest.java | 5 +- .../playlist/JTablePlaylistTest.java | 4 +- 23 files changed, 241 insertions(+), 224 deletions(-) delete mode 100644 src/main/java/musicplayer/callbacks/LibraryCallbackInterface.java delete mode 100644 src/test/java/musicplayer/library/ILibraryTest.java create mode 100644 src/test/java/musicplayer/model/ExtractedMetadataTest.java diff --git a/pom.xml b/pom.xml index 9a3b5d9..ed8c44e 100644 --- a/pom.xml +++ b/pom.xml @@ -134,9 +134,15 @@ compile - org.slf4j - slf4j-simple - 1.7.21 + org.apache.logging.log4j + log4j-core + 2.6.2 + compile + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.6.2 compile diff --git a/src/main/java/musicplayer/StartPlayingException.java b/src/main/java/musicplayer/StartPlayingException.java index 58f9661..ec8025f 100644 --- a/src/main/java/musicplayer/StartPlayingException.java +++ b/src/main/java/musicplayer/StartPlayingException.java @@ -4,7 +4,7 @@ import musicplayer.model.Song; public class StartPlayingException extends Exception { - private Song song; + private final Song song; public StartPlayingException(Song song){ this.song = song; diff --git a/src/main/java/musicplayer/callbacks/LibraryCallbackInterface.java b/src/main/java/musicplayer/callbacks/LibraryCallbackInterface.java deleted file mode 100644 index 2a02e03..0000000 --- a/src/main/java/musicplayer/callbacks/LibraryCallbackInterface.java +++ /dev/null @@ -1,20 +0,0 @@ -package musicplayer.callbacks; - -public interface LibraryCallbackInterface { - - /** - * Call this after performing library updates so the front-end knows that updating has completed. - * @param message Error message. - */ - void libraryUpdated(String message); - /** - * Call this after performing library updates so the front-end knows that updating has completed. - */ - void libraryUpdated(); - - /** - * Update the current display with the file/folder currently being parsed. - * @param name Data about the file/folder to be shown to the user. - */ - void currentlyUpdating(String name); -} diff --git a/src/main/java/musicplayer/db/HibernateDatabase.java b/src/main/java/musicplayer/db/HibernateDatabase.java index 7dcf030..1db9561 100644 --- a/src/main/java/musicplayer/db/HibernateDatabase.java +++ b/src/main/java/musicplayer/db/HibernateDatabase.java @@ -2,7 +2,6 @@ package musicplayer.db; import musicplayer.util.ConfigManager; import musicplayer.model.*; -import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; @@ -20,7 +19,7 @@ import java.util.Properties; /** * Class for managing connection to Hibernate. */ -public class HibernateDatabase implements IDatabase{ +public class HibernateDatabase extends IDatabase{ private SessionFactory sessionFactory; diff --git a/src/main/java/musicplayer/db/IDatabase.java b/src/main/java/musicplayer/db/IDatabase.java index d3a9da0..70a0759 100644 --- a/src/main/java/musicplayer/db/IDatabase.java +++ b/src/main/java/musicplayer/db/IDatabase.java @@ -1,16 +1,48 @@ package musicplayer.db; +import musicplayer.library.ILibrary; import musicplayer.model.*; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashSet; import java.util.List; import java.util.Optional; +import java.util.Set; -public interface IDatabase { - Optional getOneAlbum(String name); - Optional getOneArtist(String name); - Optional getOneSong(ExtractedMetadata metadata); - void batchDeleteNot(Long[] validIds); - Optional> listAllT(Class typeClass); - Song addSong(ExtractedMetadata metadata); - void addSong(Song song); +public abstract class IDatabase { + abstract public Optional getOneAlbum(String name); + abstract public Optional getOneArtist(String name); + abstract public Optional getOneSong(ExtractedMetadata metadata); + abstract public void batchDeleteNot(Long[] validIds); + abstract public Optional> listAllT(Class typeClass); + abstract public Song addSong(ExtractedMetadata metadata); + abstract public void addSong(Song song); + + + private static String musicFileExtensionRegex = "(?iu).*\\.(mp3|mp4|flac|ogg)"; + + /** + * Add all songs contained with all paths in rootDirectory to the database. + * @param rootDirectory Folder(s) containing music files to index. + */ + public void processSongsWithCallback(ILibrary library, List rootDirectory){ + rootDirectory.forEach(dir -> { + try { + Set seenIds = new HashSet<>(); + Files.walk(dir) + .filter(f -> f.toString().matches(musicFileExtensionRegex)).map(ExtractedMetadata::autoParse) + .forEach(x -> x.ifPresent(i -> { + Song song = addSong(i); + seenIds.add(song.getId()); + library.currentlyUpdating(i.toString()); + })); + batchDeleteNot(seenIds.toArray(new Long[seenIds.size()])); + } catch (IOException e) { + library.libraryUpdateFailed(e.getMessage()); + } + }); + library.libraryUpdated(); + } } diff --git a/src/main/java/musicplayer/library/ILibrary.java b/src/main/java/musicplayer/library/ILibrary.java index 28a68a5..7bfaf29 100644 --- a/src/main/java/musicplayer/library/ILibrary.java +++ b/src/main/java/musicplayer/library/ILibrary.java @@ -1,73 +1,33 @@ package musicplayer.library; -import musicplayer.callbacks.LibraryCallbackInterface; import musicplayer.db.IDatabase; -import musicplayer.model.ExtractedMetadata; import musicplayer.model.HasSongs; -import musicplayer.model.Song; -import org.jaudiotagger.audio.AudioFileIO; -import org.jaudiotagger.audio.exceptions.CannotReadException; -import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException; -import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; -import org.jaudiotagger.tag.Tag; -import org.jaudiotagger.tag.TagException; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.HashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; +public abstract class ILibrary { + IDatabase database; + abstract public void showSongs(); + abstract public > void showGroupedSongs(Class grouping); + abstract public void updateLibrary(); + abstract public void refreshLibrary(); -public interface ILibrary { - void showSongs(); - > void showGroupedSongs(Class grouping); - void updateLibrary(); - void refreshLibrary(); - - String musicFileExtensionRegex = "(?iu).*\\.(mp3|mp4|flac|ogg)"; - - /** - * Add all songs contained with all paths in rootDirectory to the database. - * @param rootDirectory Folder(s) containing music files to index. - * @param callbackInterface Object to send callback data to, set to null if the application doesn't require update data. - */ - static void processSongsWithCallback(IDatabase database, List rootDirectory, LibraryCallbackInterface callbackInterface){ - boolean callbacksEnabled = callbackInterface != null; - rootDirectory.forEach(dir -> { - try { - Set seenIds = new HashSet<>(); - Files.walk(dir) - .filter(f -> f.toString().matches(musicFileExtensionRegex)).map(ILibrary::autoParse) - .forEach(x -> x.ifPresent(i -> { - Song song = database.addSong(i); - seenIds.add(song.getId()); - if (callbacksEnabled) - callbackInterface.currentlyUpdating(i.toString()); - })); - database.batchDeleteNot(seenIds.toArray(new Long[seenIds.size()])); - } catch (IOException e) { - if(callbacksEnabled) - callbackInterface.libraryUpdated(e.getMessage()); - } - }); - if(callbacksEnabled) - callbackInterface.libraryUpdated(); + public ILibrary(IDatabase database){ + this.database = database; } /** - * Extract music metadata from the target file. - * - * @param targetFile Path to file to extract metadata from. - * @return Metadata contained in targetFile. + * The library was updated but there was an error (partial update may have occurred). + * @param message Error message. */ - static Optional autoParse(Path targetFile) { - Tag audioTags = null; - try { - audioTags = AudioFileIO.read(targetFile.toFile()).getTag(); - } catch (CannotReadException | IOException | ReadOnlyFileException | TagException | InvalidAudioFrameException ignored) { - } - return audioTags == null ? Optional.empty() : Optional.of(new ExtractedMetadata(audioTags, targetFile.toFile())); - } + abstract public void libraryUpdateFailed(String message); + + /** + * Library updated successfully. + */ + abstract public void libraryUpdated(); + + /** + * Show data on the current update item being indexed. + * @param name Data about the file/folder to be shown to the user. + */ + abstract public void currentlyUpdating(String name); } diff --git a/src/main/java/musicplayer/library/JTreeLibrary.java b/src/main/java/musicplayer/library/JTreeLibrary.java index 3b01728..f6d2410 100644 --- a/src/main/java/musicplayer/library/JTreeLibrary.java +++ b/src/main/java/musicplayer/library/JTreeLibrary.java @@ -2,7 +2,6 @@ package musicplayer.library; import com.google.inject.Inject; import musicplayer.util.ConfigManager; -import musicplayer.callbacks.LibraryCallbackInterface; import musicplayer.db.IDatabase; import musicplayer.model.Album; import musicplayer.model.Artist; @@ -31,10 +30,9 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; -public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInterface{ +public class JTreeLibrary extends ILibrary{ - private IDatabase database; - private IPlaylist playlist; + private final IPlaylist playlist; static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); private final AtomicBoolean libraryUpdating = new AtomicBoolean(false); private final JComboBox libraryDisplayType = new JComboBox<>(); @@ -44,6 +42,8 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt private final static Map albumArt = new ConcurrentHashMap<>(); private ExecutorService albumArtCollectorPool; + private final JPanel libraryPanel = new JPanel(); + /** * @return Map of display types for the library paired code to populate the library with data in the correct format. */ @@ -57,16 +57,16 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt @Inject public JTreeLibrary(IDatabase database, IPlaylist playlist){ - this.database = database; + super(database); this.playlist = playlist; - this.setLayout(new BorderLayout(0, 0)); + libraryPanel.setLayout(new BorderLayout(0, 0)); libraryTree.setRootVisible(false); libraryTree.setToggleClickCount(1); libraryTree.setCellRenderer(new LibraryTreeCellRenderer()); libraryTree.addMouseListener(new LibraryMouseAdapter()); libraryTree.setScrollsOnExpand(false); final JScrollPane libraryPane = new JScrollPane(libraryTree); - this.add(libraryPane, BorderLayout.CENTER); + libraryPanel.add(libraryPane, BorderLayout.CENTER); libraryDisplayVariants.keySet().forEach(libraryDisplayType::addItem); libraryDisplayType.setSelectedIndex(ConfigManager.getLastDisplayTypeIndex()); @@ -83,10 +83,17 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt otherContainer.setLayout(new BoxLayout(otherContainer, BoxLayout.PAGE_AXIS)); otherContainer.add(libraryDisplayType); otherContainer.add(librarySearchField); - this.add(otherContainer, BorderLayout.NORTH); + libraryPanel.add(otherContainer, BorderLayout.NORTH); getAlbumArt(); } + /** + * @return JPanel showing this library. + */ + public JPanel getLibraryPanel(){ + return libraryPanel; + } + /** * Add an item to libraryView. * @@ -170,7 +177,7 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt addNodeToTreeModel(model, parentNode, updatingNode); libraryTree.setModel(model); List dirs = ConfigManager.getLibraryDirectories().stream().map(File::toPath).collect(Collectors.toList()); - Thread updaterThread = new Thread(() -> ILibrary.processSongsWithCallback(database, dirs, this), "updater"); + Thread updaterThread = new Thread(() -> database.processSongsWithCallback(this, dirs), "updater"); updaterThread.start(); } } @@ -205,8 +212,8 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt * @param message Error message. */ @Override - public void libraryUpdated(String message) { - JOptionPane.showMessageDialog(this, message, "Update Error", JOptionPane.ERROR_MESSAGE); + public void libraryUpdateFailed(String message) { + JOptionPane.showMessageDialog(libraryPanel, message, "Update Error", JOptionPane.ERROR_MESSAGE); libraryUpdated(); } diff --git a/src/main/java/musicplayer/model/Album.java b/src/main/java/musicplayer/model/Album.java index 43979c2..3f2b37a 100644 --- a/src/main/java/musicplayer/model/Album.java +++ b/src/main/java/musicplayer/model/Album.java @@ -90,6 +90,7 @@ public class Album implements Comparable, IDBType, HasSongs { * Get album art for a given album. * @return Album art. */ + @Transient public Optional getAlbumArt(){ Tag audioTags; File targetFile = songs.iterator().next().getSongFile(); diff --git a/src/main/java/musicplayer/model/ExtractedMetadata.java b/src/main/java/musicplayer/model/ExtractedMetadata.java index d0c73f6..6a46f89 100644 --- a/src/main/java/musicplayer/model/ExtractedMetadata.java +++ b/src/main/java/musicplayer/model/ExtractedMetadata.java @@ -1,16 +1,24 @@ package musicplayer.model; +import org.jaudiotagger.audio.AudioFileIO; +import org.jaudiotagger.audio.exceptions.CannotReadException; +import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException; +import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; import org.jaudiotagger.tag.FieldKey; import org.jaudiotagger.tag.Tag; +import org.jaudiotagger.tag.TagException; import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Optional; /** * Internal class representing metadata extracted from a music file. */ public class ExtractedMetadata { private final Tag audioTags; - private String songFile; + private final String songFile; /** * @param audioTags jaudiotagger tag data. @@ -37,4 +45,19 @@ public class ExtractedMetadata { @Override public String toString() { return getTitle()+ " - " + getArtist(); } + + /** + * Extract music metadata from the target file. + * + * @param targetFile Path to file to extract metadata from. + * @return Metadata contained in targetFile. + */ + public static Optional autoParse(Path targetFile) { + Tag audioTags = null; + try { + audioTags = AudioFileIO.read(targetFile.toFile()).getTag(); + } catch (CannotReadException | IOException | ReadOnlyFileException | TagException | InvalidAudioFrameException ignored) { + } + return audioTags == null ? Optional.empty() : Optional.of(new ExtractedMetadata(audioTags, targetFile.toFile())); + } } diff --git a/src/main/java/musicplayer/player/GStreamerPlayer.java b/src/main/java/musicplayer/player/GStreamerPlayer.java index 00d911e..20c4b76 100644 --- a/src/main/java/musicplayer/player/GStreamerPlayer.java +++ b/src/main/java/musicplayer/player/GStreamerPlayer.java @@ -150,7 +150,7 @@ public class GStreamerPlayer implements IPlayer{ if (this.thread != null) { playBin.stop(); internalThread.stop(); - thread = null; + thread.interrupt(); resetSeek(); playlist.setStopped(); } diff --git a/src/main/java/musicplayer/player/VLCPlayer.java b/src/main/java/musicplayer/player/VLCPlayer.java index 11eac9a..5e1a025 100644 --- a/src/main/java/musicplayer/player/VLCPlayer.java +++ b/src/main/java/musicplayer/player/VLCPlayer.java @@ -18,8 +18,8 @@ import java.util.Optional; public class VLCPlayer implements IPlayer { - private ArrayList libvlcArgs = new ArrayList<>(Collections.singletonList("--vout=dummy")); - private MediaPlayer mediaPlayer; + private final ArrayList libvlcArgs = new ArrayList<>(Collections.singletonList("--vout=dummy")); + private final MediaPlayer mediaPlayer; private Song currentSong; private final PlayerCallbackInterface callbackInterface; diff --git a/src/main/java/musicplayer/playlist/IPlaylist.java b/src/main/java/musicplayer/playlist/IPlaylist.java index 40573df..acf90dc 100644 --- a/src/main/java/musicplayer/playlist/IPlaylist.java +++ b/src/main/java/musicplayer/playlist/IPlaylist.java @@ -1,8 +1,8 @@ package musicplayer.playlist; -import musicplayer.library.ILibrary; import musicplayer.model.Album; import musicplayer.model.Artist; +import musicplayer.model.ExtractedMetadata; import musicplayer.model.Song; import java.io.*; @@ -38,12 +38,12 @@ public interface IPlaylist { * @throws IOException */ static void writePlaylistToFile(List playlist, File targetFile) throws IOException { - if(!targetFile.exists()) - targetFile.createNewFile(); - try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "utf-8"))) { - writer.write("#EXTM3U\n"); - for(Song song : playlist){ - writer.write(song.getM3UFormatString()); + if(targetFile.exists() || targetFile.createNewFile()) { + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "utf-8"))) { + writer.write("#EXTM3U\n"); + for (Song song : playlist) { + writer.write(song.getM3UFormatString()); + } } } } @@ -67,7 +67,7 @@ public interface IPlaylist { songPaths.set(i, Paths.get(targetFile.getParent(), songPaths.get(i).toString())); } } - songPaths.stream().map(ILibrary::autoParse).filter(Optional::isPresent).map(Optional::get) + songPaths.stream().map(ExtractedMetadata::autoParse).filter(Optional::isPresent).map(Optional::get) .forEach(x -> result.add(new Song(x.getTrackNumber(), x.getDiscNumber(), x.getTitle(), new Artist(x.getArtist()), new Album(x.getAlbum()), x.getGenre(), x.getSongFile()))); } } catch (IOException ignored) { } diff --git a/src/main/java/musicplayer/playlist/JTablePlaylist.java b/src/main/java/musicplayer/playlist/JTablePlaylist.java index 3f87cf4..4881aa4 100644 --- a/src/main/java/musicplayer/playlist/JTablePlaylist.java +++ b/src/main/java/musicplayer/playlist/JTablePlaylist.java @@ -133,7 +133,7 @@ public class JTablePlaylist extends JScrollPane implements IPlaylist { private class PlaylistTableModel extends AbstractTableModel { - private List songList = new ArrayList<>(); + private final List songList = new ArrayList<>(); private int playingRow = -1; // -1 means no song is currently playing. public PlaylistTableModel(){ } diff --git a/src/main/java/musicplayer/swingui/ControlBar.java b/src/main/java/musicplayer/swingui/ControlBar.java index 908d691..45db814 100644 --- a/src/main/java/musicplayer/swingui/ControlBar.java +++ b/src/main/java/musicplayer/swingui/ControlBar.java @@ -7,8 +7,8 @@ import musicplayer.util.Icons; import javax.swing.*; import java.awt.*; -public class ControlBar extends JPanel { - IPlayer player; +class ControlBar extends JPanel { + private final IPlayer player; public ControlBar(IPlayer player){ this.player = player; diff --git a/src/main/java/musicplayer/swingui/PlayerGUI.java b/src/main/java/musicplayer/swingui/PlayerGUI.java index 41839da..e9ed4b0 100644 --- a/src/main/java/musicplayer/swingui/PlayerGUI.java +++ b/src/main/java/musicplayer/swingui/PlayerGUI.java @@ -4,6 +4,7 @@ import com.google.inject.Inject; import musicplayer.StartPlayingException; import musicplayer.callbacks.PlayerCallbackInterface; import musicplayer.library.ILibrary; +import musicplayer.library.JTreeLibrary; import musicplayer.player.IPlayer; import musicplayer.playlist.IPlaylist; import musicplayer.util.ConfigManager; @@ -115,7 +116,7 @@ public class PlayerGUI extends JPanel implements PlayerCallbackInterface { libraryAndPlaylistPane.setDividerLocation(240); libraryAndPlaylistPane.setRightComponent((JScrollPane) playlist); this.add(libraryAndPlaylistPane, BorderLayout.CENTER); - libraryAndPlaylistPane.setLeftComponent((JPanel) library); + libraryAndPlaylistPane.setLeftComponent(((JTreeLibrary)library).getLibraryPanel()); final JPanel controlPane = new JPanel(); controlPane.setLayout(new BorderLayout(0, 0)); this.add(controlPane, BorderLayout.SOUTH); diff --git a/src/test/java/musicplayer/db/HibernateDatabaseTest.java b/src/test/java/musicplayer/db/HibernateDatabaseTest.java index d5c43ea..bcc93a4 100644 --- a/src/test/java/musicplayer/db/HibernateDatabaseTest.java +++ b/src/test/java/musicplayer/db/HibernateDatabaseTest.java @@ -1,9 +1,7 @@ package musicplayer.db; -import musicplayer.model.Album; -import musicplayer.model.Artist; -import musicplayer.model.ExtractedMetadata; -import musicplayer.model.Song; +import musicplayer.library.ILibrary; +import musicplayer.model.*; import org.jaudiotagger.tag.FieldKey; import org.jaudiotagger.tag.Tag; import org.jaudiotagger.tag.id3.ID3v23Tag; @@ -11,6 +9,8 @@ import org.junit.Before; import org.junit.Test; import java.io.File; +import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; import static org.junit.Assert.assertEquals; @@ -216,4 +216,47 @@ public class HibernateDatabaseTest { assertTrue(db.contains(song2)); assertFalse(db.contains(song3)); } + + @Test + public void testProcessSongsWithCallback() throws Exception { + Path dir = new File(HibernateDatabaseTest.class.getResource("/sample.mp3").getFile()).getParentFile().toPath(); + List dirList = new ArrayList<>(); + dirList.add(dir); + FakeLibrary library = new FakeLibrary(database); + database.processSongsWithCallback(library, dirList); + assertEquals(1, database.listAllT(Song.class).get().size()); + assertTrue(library.success); + assertFalse(library.failed); + } + + private class FakeLibrary extends ILibrary{ + + public boolean success = false; + public boolean failed = false; + + public FakeLibrary(IDatabase database) { + super(database); + } + + @Override + public void showSongs() {} + + @Override + public > void showGroupedSongs(Class grouping) {} + + @Override + public void updateLibrary() {} + + @Override + public void refreshLibrary() {} + + @Override + public void libraryUpdateFailed(String message) { failed = true;} + + @Override + public void libraryUpdated() { success = true;} + + @Override + public void currentlyUpdating(String name) {} + } } \ No newline at end of file diff --git a/src/test/java/musicplayer/library/ILibraryTest.java b/src/test/java/musicplayer/library/ILibraryTest.java deleted file mode 100644 index 637fcdf..0000000 --- a/src/test/java/musicplayer/library/ILibraryTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package musicplayer.library; - -import musicplayer.callbacks.LibraryCallbackInterface; -import musicplayer.db.HibernateDatabase; -import musicplayer.db.IDatabase; -import musicplayer.model.ExtractedMetadata; -import musicplayer.model.Song; -import org.jaudiotagger.audio.AudioFileIO; -import org.jaudiotagger.tag.FieldKey; -import org.jaudiotagger.tag.Tag; -import org.junit.Test; - -import java.io.File; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -import static org.junit.Assert.*; - -public class ILibraryTest { - - @Test - public void testAutoParse() throws Exception { - File inputFile = new File(ILibraryTest.class.getResource("/sample.mp3").getFile()); - Optional data = ILibrary.autoParse(inputFile.toPath()); - Tag audioTags = AudioFileIO.read(inputFile).getTag(); - assertTrue(data.isPresent()); - ExtractedMetadata internal = data.get(); - assertEquals(audioTags.getFirst(FieldKey.ARTIST), internal.getArtist()); - assertEquals(audioTags.getFirst(FieldKey.GENRE), internal.getGenre()); - assertEquals(audioTags.getFirst(FieldKey.TITLE), internal.getTitle()); - assertEquals(audioTags.getFirst(FieldKey.ALBUM), internal.getAlbum()); - assertEquals(audioTags.getFirst(FieldKey.TRACK), internal.getTrackNumber()); - assertEquals(audioTags.getFirst(FieldKey.DISC_NO), internal.getDiscNumber()); - assertEquals(inputFile.getPath(), internal.getSongFile()); - } - - @Test - public void testAutoParseInvalidFile() throws Exception { - File inputFile = new File(""); - Optional data = ILibrary.autoParse(inputFile.toPath()); - assertFalse(data.isPresent()); - } - - @Test - public void testProcessSongsWithCallback() throws Exception { - CallbackInterface callbackInterface = new CallbackInterface(); - Path dir = new File(ILibraryTest.class.getResource("/sample.mp3").getFile()).getParentFile().toPath(); - List dirList = new ArrayList<>(); - dirList.add(dir); - IDatabase database = new HibernateDatabase(true); - ILibrary.processSongsWithCallback(database, dirList, callbackInterface); - assertEquals(1, database.listAllT(Song.class).get().size()); - assertTrue(callbackInterface.status); - assertFalse(callbackInterface.error); - } - - private class CallbackInterface implements LibraryCallbackInterface{ - public boolean status = false; - public String update = ""; - public boolean error = false; - - @Override - public void libraryUpdated(String message) { - error = true; - } - - @Override - public void libraryUpdated() { - status = true; - } - - @Override - public void currentlyUpdating(String name) { - update = name; - } - } -} \ No newline at end of file diff --git a/src/test/java/musicplayer/library/JTreeLibraryTest.java b/src/test/java/musicplayer/library/JTreeLibraryTest.java index a2ce336..d9fb6e7 100644 --- a/src/test/java/musicplayer/library/JTreeLibraryTest.java +++ b/src/test/java/musicplayer/library/JTreeLibraryTest.java @@ -4,6 +4,7 @@ import musicplayer.db.HibernateDatabase; import musicplayer.db.IDatabase; import musicplayer.model.Album; import musicplayer.model.Artist; +import musicplayer.model.ExtractedMetadata; import musicplayer.model.Song; import musicplayer.playlist.IPlaylist; import musicplayer.playlist.JTablePlaylist; @@ -12,18 +13,21 @@ import org.junit.Test; import javax.swing.tree.DefaultMutableTreeNode; import java.io.File; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; import static org.junit.Assert.*; public class JTreeLibraryTest { - JTreeLibrary library; - Song sampleSong = new Song(ILibrary.autoParse( + private JTreeLibrary library; + private final Song sampleSong = new Song(ExtractedMetadata.autoParse( new File(JTreeLibraryTest.class.getResource("/sample.mp3").getFile()).toPath()).get()); - IDatabase database; + private IDatabase database; @Before - public void setUp() throws Exception { + public void setUp() { database = new HibernateDatabase(true); IPlaylist playlist = new JTablePlaylist(); library = new JTreeLibrary(database, playlist); @@ -85,6 +89,6 @@ public class JTreeLibraryTest { public void testCurrentlyUpdating() throws Exception { String updateText = "Test String"; library.currentlyUpdating("Test String"); - assertEquals(updateText, library.updatingNode.getUserObject()); + assertEquals(updateText, JTreeLibrary.updatingNode.getUserObject()); } } \ No newline at end of file diff --git a/src/test/java/musicplayer/model/ExtractedMetadataTest.java b/src/test/java/musicplayer/model/ExtractedMetadataTest.java new file mode 100644 index 0000000..66a3b48 --- /dev/null +++ b/src/test/java/musicplayer/model/ExtractedMetadataTest.java @@ -0,0 +1,38 @@ +package musicplayer.model; + +import org.jaudiotagger.audio.AudioFileIO; +import org.jaudiotagger.tag.FieldKey; +import org.jaudiotagger.tag.Tag; +import org.junit.Test; + +import java.io.File; +import java.util.Optional; + +import static org.junit.Assert.*; + +public class ExtractedMetadataTest { + + @Test + public void testAutoParse() throws Exception { + File inputFile = new File(ExtractedMetadataTest.class.getResource("/sample.mp3").getFile()); + Optional data = ExtractedMetadata.autoParse(inputFile.toPath()); + Tag audioTags = AudioFileIO.read(inputFile).getTag(); + assertTrue(data.isPresent()); + ExtractedMetadata internal = data.get(); + assertEquals(audioTags.getFirst(FieldKey.ARTIST), internal.getArtist()); + assertEquals(audioTags.getFirst(FieldKey.GENRE), internal.getGenre()); + assertEquals(audioTags.getFirst(FieldKey.TITLE), internal.getTitle()); + assertEquals(audioTags.getFirst(FieldKey.ALBUM), internal.getAlbum()); + assertEquals(audioTags.getFirst(FieldKey.TRACK), internal.getTrackNumber()); + assertEquals(audioTags.getFirst(FieldKey.DISC_NO), internal.getDiscNumber()); + assertEquals(inputFile.getPath(), internal.getSongFile()); + } + + @Test + public void testAutoParseInvalidFile() throws Exception { + File inputFile = new File(""); + Optional data = ExtractedMetadata.autoParse(inputFile.toPath()); + assertFalse(data.isPresent()); + } +} + diff --git a/src/test/java/musicplayer/player/GStreamerPlayerTest.java b/src/test/java/musicplayer/player/GStreamerPlayerTest.java index 5640629..beed1aa 100644 --- a/src/test/java/musicplayer/player/GStreamerPlayerTest.java +++ b/src/test/java/musicplayer/player/GStreamerPlayerTest.java @@ -1,6 +1,7 @@ package musicplayer.player; import musicplayer.library.ILibrary; +import musicplayer.model.ExtractedMetadata; import musicplayer.model.Song; import musicplayer.playlist.IPlaylist; import musicplayer.playlist.JTablePlaylist; @@ -15,13 +16,13 @@ import static org.junit.Assert.*; public class GStreamerPlayerTest { - GStreamerPlayer player; - IPlaylist playlist; - Song sampleSong = new Song(ILibrary.autoParse( + private GStreamerPlayer player; + private IPlaylist playlist; + private final Song sampleSong = new Song(ExtractedMetadata.autoParse( new File(GStreamerPlayerTest.class.getResource("/sample.mp3").getFile()).toPath()).get()); @Before - public void setUp() throws Exception { + public void setUp() { playlist = new JTablePlaylist(); player = new GStreamerPlayer(null, playlist, true); } diff --git a/src/test/java/musicplayer/player/VLCPlayerTest.java b/src/test/java/musicplayer/player/VLCPlayerTest.java index 83fafe2..a624296 100644 --- a/src/test/java/musicplayer/player/VLCPlayerTest.java +++ b/src/test/java/musicplayer/player/VLCPlayerTest.java @@ -1,10 +1,10 @@ package musicplayer.player; import musicplayer.library.ILibrary; +import musicplayer.model.ExtractedMetadata; import musicplayer.model.Song; import musicplayer.playlist.IPlaylist; import musicplayer.playlist.JTablePlaylist; -import musicplayer.util.ConfigManager; import org.junit.Before; import org.junit.Test; @@ -15,13 +15,13 @@ import static org.junit.Assert.*; public class VLCPlayerTest { - VLCPlayer player; - IPlaylist playlist; - Song sampleSong = new Song(ILibrary.autoParse( + private VLCPlayer player; + private IPlaylist playlist; + private final Song sampleSong = new Song(ExtractedMetadata.autoParse( new File(VLCPlayerTest.class.getResource("/sample.mp3").getFile()).toPath()).get()); @Before - public void setUp() throws Exception { + public void setUp() { playlist = new JTablePlaylist(); player = new VLCPlayer(null, playlist, true); } diff --git a/src/test/java/musicplayer/playlist/IPlaylistTest.java b/src/test/java/musicplayer/playlist/IPlaylistTest.java index 783dc9e..d6c62bb 100644 --- a/src/test/java/musicplayer/playlist/IPlaylistTest.java +++ b/src/test/java/musicplayer/playlist/IPlaylistTest.java @@ -1,6 +1,6 @@ package musicplayer.playlist; -import musicplayer.library.ILibrary; +import musicplayer.model.ExtractedMetadata; import musicplayer.model.Song; import org.junit.Rule; import org.junit.Test; @@ -19,12 +19,13 @@ public class IPlaylistTest { @Test public void testWriteAndReadPlaylist() throws Exception { - Song sampleSong = new Song(ILibrary.autoParse( + Song sampleSong = new Song(ExtractedMetadata.autoParse( new File(IPlaylistTest.class.getResource("/sample.mp3").getFile()).toPath()).get()); File playlistFile = folder.newFile(); List playlist = new ArrayList<>(); playlist.add(sampleSong); IPlaylist.writePlaylistToFile(playlist, playlistFile); + assertTrue(playlistFile.exists()); List result = IPlaylist.readPlaylistFromFile(playlistFile); assertEquals(sampleSong, result.get(0)); } diff --git a/src/test/java/musicplayer/playlist/JTablePlaylistTest.java b/src/test/java/musicplayer/playlist/JTablePlaylistTest.java index ec61154..a32f9b0 100644 --- a/src/test/java/musicplayer/playlist/JTablePlaylistTest.java +++ b/src/test/java/musicplayer/playlist/JTablePlaylistTest.java @@ -14,8 +14,8 @@ import static org.junit.Assert.*; public class JTablePlaylistTest { - JTablePlaylist playlist; - JTable innerTable; + private JTablePlaylist playlist; + private JTable innerTable; @Before public void setUp() throws Exception {