Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import static org.junit.Assert.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.PrintStream; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import org.junit.After; import org.junit.Before; import org.junit.Test; public class

image text in transcribed

import static org.junit.Assert.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.PrintStream; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import org.junit.After; import org.junit.Before; import org.junit.Test; public class ListLabTest { private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); @Before public void setUpStreams() { System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); } @Test public void SongInstanceVariablesTest() { Song testSong = new Song("Hurt Me Soul","Lupe Fiasco","Food & Liquor"); @SuppressWarnings("rawtypes") Class c = testSong.getClass(); try { c.getDeclaredField("title"); c.getDeclaredField("artist"); c.getDeclaredField("album"); assertEquals("You must make your instance variables private.",true,Modifier.isPrivate(c.getDeclaredField("title").getModifiers())); assertEquals("You must make your instance variables private.",true,Modifier.isPrivate(c.getDeclaredField("artist").getModifiers())); assertEquals("You must make your instance variables private.",true,Modifier.isPrivate(c.getDeclaredField("album").getModifiers())); } catch(NoSuchFieldException e) { fail("Could not find the " + e.getLocalizedMessage().toString() + " instance variable"); } catch(Exception e) { fail("Something weird went wrong"); } } @Test public void SongConstructorTest() { Song testSong = new Song("What Do I Do","A. Savage","Thawing Dawn"); @SuppressWarnings("rawtypes") Class c = testSong.getClass(); try { Field title = c.getDeclaredField("title"); title.setAccessible(true); String titleValue = (String) title.get(testSong); assertEquals("When checking the value of title we","What Do I Do",titleValue); Field artist = c.getDeclaredField("artist"); artist.setAccessible(true); String artistValue = (String) artist.get(testSong); assertEquals("When checking the value of title we","A. Savage",artistValue); Field album = c.getDeclaredField("album"); album.setAccessible(true); String albumValue = (String) album.get(testSong); assertEquals("When checking the value of title we","Thawing Dawn",albumValue); } catch (Exception e) { fail(e.toString()); } } @Test public void SongGettersTest() { Song testSong = new Song("In One Ear","Cage The Elephant","Cage the Elephant"); assertEquals("When checking the value of title we","In One Ear",testSong.getTitle()); assertEquals("When checking the value of artist we","Cage The Elephant",testSong.getArtist()); assertEquals("When checking the value of album we","Cage the Elephant",testSong.getAlbum()); } @Test public void SongSettersTest() { Song testSong = new Song("In One Ear","Cage The Elephant","Cage the Elephant"); testSong.setTitle("Birthday");testSong.setArtist("The Beatles");testSong.setAlbum("The Beatles (White Album)"); assertEquals("When checking the value of title we","Birthday",testSong.getTitle()); assertEquals("When checking the value of artist we","The Beatles",testSong.getArtist()); assertEquals("When checking the value of album we","The Beatles (White Album)",testSong.getAlbum()); } @Test public void SongToStringTest() { Song testSong = new Song("Distopian Dream Girl","Built to Spill","There's Nothing Wrong With Love"); assertEquals("When checking the return value of toString we","Distopian Dream Girl - Built to Spill - There's Nothing Wrong With Love",testSong.toString()); } @Test public void SongEqualsTest() { Song testSongOne = new Song("Clampdown","The Clash","London Calling"); Song testSongTwo = new Song("Walking Alone","Green Day","Nimrod"); Song testSongThree = new Song("Clampdown","The Clash","London Calling"); assertEquals("When checking the return value of equals given the same song object we",true,testSongOne.equals(testSongOne)); assertEquals("When checking the return value of equals given another song object with different values we",false,testSongOne.equals(testSongTwo)); assertEquals("When checking the return value of equals given another song object with the same values we",true,testSongOne.equals(testSongThree)); } @Test public void SongCompareToTest() { Song testSongOne = new Song("Frozen","Surfer Blood","Snowdonia"); Song testSongTwo = new Song("Jinx","Green Day","Nimrod"); Song testSongThree = new Song("Haushinka","Green Day","Nimrod"); Song testSongFour = new Song("Haushinka","Green Day","Nimrod"); Song testSongFive = new Song("St. Jimmy","Green Day","American Idiot"); //SurferBlood.GreenDay -1 assertEquals("When checking the return value of compareTo (comparing artist S to artist G) we",true,testSongOne.compareTo(testSongFive) = 1); //Nimrod.AmericanIdiot -1 assertEquals("When checking the return value of compareTo (matching artist, comparing album N to album A) we",true,testSongTwo.compareTo(testSongFive) = 1); //Jinx.Haushinka -1 assertEquals("When checking the return value of compareTo (matching artist & album, comparing title J to title H) we",true,testSongTwo.compareTo(testSongThree) = 1); //Haushinka.Haushinka 0 assertEquals("When checking the return value of compareTo (matching artist & album & title) we",0,testSongThree.compareTo(testSongFour)); } @Test public void MusicLibraryInstanceVariablesTest() { MusicLibrary testLibrary = new MusicLibrary(); @SuppressWarnings("rawtypes") Class c = testLibrary.getClass(); try { c.getDeclaredField("library"); assertEquals("You must make your instance variable private.",true,Modifier.isPrivate(c.getDeclaredField("library").getModifiers())); } catch(NoSuchFieldException e) { fail("Could not find the " + e.getLocalizedMessage().toString() + " instance variable"); } catch(Exception e) { fail("Something weird went wrong"); } } @Test public void MusicLibraryConstructorTest() { MusicLibrary testLibrary = new MusicLibrary(); @SuppressWarnings("rawtypes") Class c = testLibrary.getClass(); try { Field title = c.getDeclaredField("library"); title.setAccessible(true); @SuppressWarnings("unchecked") ArrayList libraryValue = (ArrayList) title.get(testLibrary); assertNotNull(libraryValue); assertEquals(0,libraryValue.size()); } catch (Exception e) { fail(e.toString()); } } @Test public void MusicLibraryAddSongTest() { MusicLibrary testLibrary = new MusicLibrary(); Song testSong = new Song("Sexy Sadie","The Beatles","The Beatles (White Album)"); testLibrary.addSong(testSong); @SuppressWarnings("rawtypes") Class c = testLibrary.getClass(); try { Field title = c.getDeclaredField("library"); title.setAccessible(true); @SuppressWarnings("unchecked") ArrayList libraryValue = (ArrayList) title.get(testLibrary); //Once we have extracted the raw data ArrayList, validate the data assertTrue("We expected " + testSong.toString() + " to be the only song in the library ArrayList",libraryValue.get(0).equals(testSong)); } catch (Exception e) { fail(e.toString()); } } @Test @SuppressWarnings("unchecked") public void MusicLibraryRemoveSongTest() { MusicLibrary testLibrary = new MusicLibrary(); Song testSong = new Song("Vaporize","Broken Bells","Broken Bells"); @SuppressWarnings("rawtypes") Class c = testLibrary.getClass(); try { //Extract the data ArrayList from the Object Field title = c.getDeclaredField("library"); title.setAccessible(true); ArrayList libraryValue = (ArrayList) title.get(testLibrary); //Add values directly to the ArrayList libraryValue.add(testSong); //Call the method we want to test testLibrary.removeSong(testSong); //Once we have extracted the raw data ArrayList, validate the data assertEquals(0,libraryValue.size()); } catch (Exception e) { fail(e.toString()); } } @Test //This test expects addSong to add songs at position 0 in the arrayList public void MusicLibraryListSongsTest() { MusicLibrary testLibrary = new MusicLibrary(); Song testSong = new Song("Sometime Around Midnight","The Airborne Toxic Event","The Airborne Toxic Event"); Song testSong1 = new Song("Dunes","Alabama Shakes","Sound & Color"); Song testSong2 = new Song("Staring Out The Window At Your Old Apartment","Jeff Rosenstock","WORRY"); Song testSong3 = new Song("We The People....","A Tribe Called Quest","We got it from Here... Thank You 4 Your service"); Song[] testSongs = {testSong,testSong1,testSong2,testSong3}; testLibrary.addSong(testSong); testLibrary.addSong(testSong1); testLibrary.addSong(testSong2); testLibrary.addSong(testSong3); String input = ""; InputStream in = new ByteArrayInputStream(input.getBytes()); System.setIn(in); testLibrary.listSongs(); String rawOutput = outContent.toString(); String[] outputLines = rawOutput.split(System.lineSeparator()); for(int i = 0; i  libraryValue = (ArrayList) title.get(testLibrary); //Once we have extracted the raw data ArrayList, validate the data boolean unchanged = true; for(int j = 0; j   List Lab Write a Java class called Song to represent a song in a music library. The instance variables of the Song class are String objects named title, artist, and album. These instance variables are t

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions