diff --git a/src/main/java/musicplayer/library/JTreeLibrary.java b/src/main/java/musicplayer/library/JTreeLibrary.java index 44e5052..cdbe2fa 100644 --- a/src/main/java/musicplayer/library/JTreeLibrary.java +++ b/src/main/java/musicplayer/library/JTreeLibrary.java @@ -30,10 +30,10 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt private IDatabase database; private IPlaylist playlist; - private static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); + static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); private final AtomicBoolean libraryUpdating = new AtomicBoolean(false); private final JComboBox libraryDisplayType = new JComboBox<>(); - private final JTree libraryTree = new JTree(); + final JTree libraryTree = new JTree(); private final Map libraryDisplayVariants = createDisplayVariantMap(); /** diff --git a/src/test/java/musicplayer/library/JTreeLibraryTest.java b/src/test/java/musicplayer/library/JTreeLibraryTest.java new file mode 100644 index 0000000..a2ce336 --- /dev/null +++ b/src/test/java/musicplayer/library/JTreeLibraryTest.java @@ -0,0 +1,90 @@ +package musicplayer.library; + +import musicplayer.db.HibernateDatabase; +import musicplayer.db.IDatabase; +import musicplayer.model.Album; +import musicplayer.model.Artist; +import musicplayer.model.Song; +import musicplayer.playlist.IPlaylist; +import musicplayer.playlist.JTablePlaylist; +import org.junit.Before; +import org.junit.Test; + +import javax.swing.tree.DefaultMutableTreeNode; +import java.io.File; + +import static org.junit.Assert.*; + +public class JTreeLibraryTest { + + JTreeLibrary library; + Song sampleSong = new Song(ILibrary.autoParse( + new File(JTreeLibraryTest.class.getResource("/sample.mp3").getFile()).toPath()).get()); + IDatabase database; + + @Before + public void setUp() throws Exception { + database = new HibernateDatabase(true); + IPlaylist playlist = new JTablePlaylist(); + library = new JTreeLibrary(database, playlist); + } + + @Test + public void testShowSongsEmpty() throws Exception { + library.showSongs(); + DefaultMutableTreeNode root = (DefaultMutableTreeNode)library.libraryTree.getModel().getRoot(); + assertEquals(1, root.getChildCount()); + // If empty, tree will only have a string with an error message in + assertEquals(String.class, ((DefaultMutableTreeNode)root.getFirstChild()).getUserObject().getClass()); + } + + @Test + public void testShowSongs() throws Exception { + database.addSong(sampleSong); + library.showSongs(); + DefaultMutableTreeNode root = (DefaultMutableTreeNode)library.libraryTree.getModel().getRoot(); + assertEquals(1, root.getChildCount()); + assertEquals(sampleSong, ((DefaultMutableTreeNode)root.getFirstChild()).getUserObject()); + } + + + @Test + public void testShowGroupedSongsEmpty() throws Exception { + library.showGroupedSongs(Artist.class); + DefaultMutableTreeNode root = (DefaultMutableTreeNode)library.libraryTree.getModel().getRoot(); + assertEquals(1, root.getChildCount()); + // If empty, tree will only have a string with an error message in + assertEquals(String.class, ((DefaultMutableTreeNode)root.getFirstChild()).getUserObject().getClass()); + } + + @Test + public void testShowGroupedSongsArtist() throws Exception { + database.addSong(sampleSong); + library.showGroupedSongs(Artist.class); + DefaultMutableTreeNode root = (DefaultMutableTreeNode)library.libraryTree.getModel().getRoot(); + assertEquals(1, root.getChildCount()); + DefaultMutableTreeNode artistNode = (DefaultMutableTreeNode)root.getFirstChild(); + assertEquals(1, artistNode.getChildCount()); + assertEquals(sampleSong.getArtist(), artistNode.getUserObject()); + assertEquals(sampleSong, ((DefaultMutableTreeNode)artistNode.getFirstChild()).getUserObject()); + } + + @Test + public void testShowGroupedSongsAlbum() throws Exception { + database.addSong(sampleSong); + library.showGroupedSongs(Album.class); + DefaultMutableTreeNode root = (DefaultMutableTreeNode)library.libraryTree.getModel().getRoot(); + assertEquals(1, root.getChildCount()); + DefaultMutableTreeNode albumNode = (DefaultMutableTreeNode)root.getFirstChild(); + assertEquals(1, albumNode.getChildCount()); + assertEquals(sampleSong.getAlbum(), albumNode.getUserObject()); + assertEquals(sampleSong, ((DefaultMutableTreeNode)albumNode.getFirstChild()).getUserObject()); + } + + @Test + public void testCurrentlyUpdating() throws Exception { + String updateText = "Test String"; + library.currentlyUpdating("Test String"); + assertEquals(updateText, library.updatingNode.getUserObject()); + } +} \ No newline at end of file