Added tests for JTablePlaylist.

This commit is contained in:
neviyn 2016-03-22 05:15:39 +00:00
parent 091126e2e2
commit e1507efe44
2 changed files with 211 additions and 1 deletions

View File

@ -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);

View File

@ -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());
}
}