From e1507efe44de0fe47690f4c96c28964bbd24879d Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Tue, 22 Mar 2016 05:15:39 +0000 Subject: [PATCH] Added tests for JTablePlaylist. --- .../java/musicplayer/playlist/IPlaylist.java | 2 +- .../playlist/JTablePlaylistTest.java | 210 ++++++++++++++++++ 2 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 src/test/java/musicplayer/playlist/JTablePlaylistTest.java diff --git a/src/main/java/musicplayer/playlist/IPlaylist.java b/src/main/java/musicplayer/playlist/IPlaylist.java index fbd724b..6f32d6b 100644 --- a/src/main/java/musicplayer/playlist/IPlaylist.java +++ b/src/main/java/musicplayer/playlist/IPlaylist.java @@ -16,7 +16,7 @@ public interface IPlaylist { int getIndex(Song song); void delete(Song song); void delete(int index); - void delete(int[] indices); + void delete(int[] index); void deleteAll(); boolean isEmpty(); void setPlayingSong(Song song); diff --git a/src/test/java/musicplayer/playlist/JTablePlaylistTest.java b/src/test/java/musicplayer/playlist/JTablePlaylistTest.java new file mode 100644 index 0000000..bc968e0 --- /dev/null +++ b/src/test/java/musicplayer/playlist/JTablePlaylistTest.java @@ -0,0 +1,210 @@ +package musicplayer.playlist; + +import musicplayer.model.Album; +import musicplayer.model.Artist; +import musicplayer.model.Song; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class JTablePlaylistTest { + + JTablePlaylist playlist; + + @Before + public void setUp() throws Exception { + playlist = new JTablePlaylist(); + } + + @Test + public void testAddSong() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + playlist.addSong(testSong); + assertEquals(0, playlist.getIndex(testSong)); + } + + @Test + public void testGetFirst() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + Song otherSong = new Song("2", "2", "test 2", new Artist("test artist 2"), new Album("test album 2"), "test genre 2", ""); + playlist.addSong(testSong); + playlist.addSong(otherSong); + assertEquals(playlist.getFirst().get(), testSong); + } + + @Test + public void testGetFirstWhileEmpty() throws Exception { + assertFalse(playlist.getFirst().isPresent()); + } + + @Test + public void testGetNext() throws Exception { + Song altSong = new Song("3", "3", "test 3", new Artist("test artist 3"), new Album("test album 3"), "test genre 3", ""); + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + Song otherSong = new Song("2", "2", "test 2", new Artist("test artist 2"), new Album("test album 2"), "test genre 2", ""); + playlist.addSong(altSong); + playlist.addSong(testSong); + playlist.addSong(otherSong); + assertEquals(playlist.getNext(testSong).get(), otherSong); + } + + @Test + public void testGetNextWhileEmpty() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + assertFalse(playlist.getNext(testSong).isPresent()); + } + + @Test + public void testGetNextNull() throws Exception { + assertFalse(playlist.getNext(null).isPresent()); + } + + @Test + public void testGetPrevious() throws Exception { + Song altSong = new Song("3", "3", "test 3", new Artist("test artist 3"), new Album("test album 3"), "test genre 3", ""); + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + Song otherSong = new Song("2", "2", "test 2", new Artist("test artist 2"), new Album("test album 2"), "test genre 2", ""); + playlist.addSong(altSong); + playlist.addSong(testSong); + playlist.addSong(otherSong); + assertEquals(playlist.getPrevious(testSong).get(), altSong); + } + + @Test + public void testGetPreviousWhileEmpty() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + assertFalse(playlist.getPrevious(testSong).isPresent()); + } + + @Test + public void testGetPreviousNull() throws Exception { + assertFalse(playlist.getPrevious(null).isPresent()); + } + + @Test + public void testGet() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + Song otherSong = new Song("2", "2", "test 2", new Artist("test artist 2"), new Album("test album 2"), "test genre 2", ""); + playlist.addSong(testSong); + playlist.addSong(otherSong); + assertEquals(testSong, playlist.get(0).get()); + } + + @Test + public void testGetWhileEmpty() throws Exception { + assertFalse(playlist.get(0).isPresent()); + } + + @Test + public void testGetInvalidIndex() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + playlist.addSong(testSong); + assertFalse(playlist.get(-1).isPresent()); + } + + @Test + public void testSetAndGetActive() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + playlist.addSong(testSong); + playlist.setPlayingSong(testSong); + assertEquals(testSong, playlist.getActive().get()); + } + + @Test + public void testSetAndGetActiveNotInPlaylist() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + playlist.setPlayingSong(testSong); + assertFalse(playlist.getActive().isPresent()); + } + + @Test + public void testGetSongList() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + Song otherSong = new Song("2", "2", "test 2", new Artist("test artist 2"), new Album("test album 2"), "test genre 2", ""); + Song altSong = new Song("3", "3", "test 3", new Artist("test artist 3"), new Album("test album 3"), "test genre 3", ""); + playlist.addSong(altSong); + playlist.addSong(testSong); + playlist.addSong(otherSong); + assertEquals(3, playlist.getSongList().size()); + assertTrue(playlist.getSongList().contains(testSong)); + assertTrue(playlist.getSongList().contains(otherSong)); + assertTrue(playlist.getSongList().contains(altSong)); + } + + @Test + public void testGetIndex() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + Song otherSong = new Song("2", "2", "test 2", new Artist("test artist 2"), new Album("test album 2"), "test genre 2", ""); + Song altSong = new Song("3", "3", "test 3", new Artist("test artist 3"), new Album("test album 3"), "test genre 3", ""); + playlist.addSong(altSong); + playlist.addSong(testSong); + playlist.addSong(otherSong); + assertEquals(0, playlist.getIndex(altSong)); + assertEquals(1, playlist.getIndex(testSong)); + assertEquals(2, playlist.getIndex(otherSong)); + } + + @Test + public void testDelete() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + Song otherSong = new Song("2", "2", "test 2", new Artist("test artist 2"), new Album("test album 2"), "test genre 2", ""); + Song altSong = new Song("3", "3", "test 3", new Artist("test artist 3"), new Album("test album 3"), "test genre 3", ""); + playlist.addSong(altSong); + playlist.addSong(testSong); + playlist.addSong(otherSong); + playlist.delete(testSong); + assertFalse(playlist.getSongList().contains(testSong)); + assertEquals(-1, playlist.getIndex(testSong)); + } + + @Test + public void testDelete1() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + Song otherSong = new Song("2", "2", "test 2", new Artist("test artist 2"), new Album("test album 2"), "test genre 2", ""); + Song altSong = new Song("3", "3", "test 3", new Artist("test artist 3"), new Album("test album 3"), "test genre 3", ""); + playlist.addSong(altSong); + playlist.addSong(testSong); + playlist.addSong(otherSong); + playlist.delete(playlist.getIndex(testSong)); + assertFalse(playlist.getSongList().contains(testSong)); + assertEquals(0, playlist.getIndex(altSong)); + assertEquals(-1, playlist.getIndex(testSong)); + assertEquals(1, playlist.getIndex(otherSong)); + } + + @Test + public void testDelete2() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + Song otherSong = new Song("2", "2", "test 2", new Artist("test artist 2"), new Album("test album 2"), "test genre 2", ""); + Song altSong = new Song("3", "3", "test 3", new Artist("test artist 3"), new Album("test album 3"), "test genre 3", ""); + playlist.addSong(altSong); + playlist.addSong(testSong); + playlist.addSong(otherSong); + int[] indices = {0, 2}; + playlist.delete(indices); + assertFalse(playlist.getSongList().contains(altSong)); + assertTrue(playlist.getSongList().contains(testSong)); + assertFalse(playlist.getSongList().contains(otherSong)); + assertEquals(-1, playlist.getIndex(altSong)); + assertEquals(0, playlist.getIndex(testSong)); + assertEquals(-1, playlist.getIndex(otherSong)); + } + + @Test + public void testDeleteAll() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + Song otherSong = new Song("2", "2", "test 2", new Artist("test artist 2"), new Album("test album 2"), "test genre 2", ""); + Song altSong = new Song("3", "3", "test 3", new Artist("test artist 3"), new Album("test album 3"), "test genre 3", ""); + playlist.addSong(altSong); + playlist.addSong(testSong); + playlist.addSong(otherSong); + playlist.deleteAll(); + assertTrue(playlist.isEmpty()); + } + + @Test + public void testIsEmpty() throws Exception { + assertTrue(playlist.isEmpty()); + } +} \ No newline at end of file