From 15385d3b4c6d0d5ce3c74027b5901d880af92a5d Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 21:00:21 +0100 Subject: [PATCH 1/3] Method addToFavoriteSongs of CDPlayer tested on addingOneSong -> favoriteSongsShouldReturnsThatAddedSong. --- src/main/java/device/cdPlayer/CDPlayer.java | 9 ++++++++ .../java/device/cdPlayer/CDPlayerTest.java | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 8d4a53b..7742fcb 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -23,6 +23,8 @@ public class CDPlayer implements Device { private CDWechseler cdWechseler=null; private String activeSource="CDDrive";//or CDWechseler + private ListfavoriteSongs=new ArrayList<>(); + @Override public void louder() { if(volume < 100) { @@ -164,6 +166,9 @@ public class CDPlayer implements Device { int nextIndex=(indexOfLoadedCD+1)%CDList.size(); this.loadedCD=CDList.get(nextIndex); } + public void addToFavoriteSongs(String song){ + favoriteSongs.add(song); + } //Getters, Setters und SupportMethods public void setCD(CD _loadedCD) { @@ -215,4 +220,8 @@ public class CDPlayer implements Device { public CDWechseler getCdWechseler() { return cdWechseler; } + + public List getFavoriteSongs() { + return favoriteSongs; + } } diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index a3e3ebf..2468c0c 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -491,6 +491,27 @@ class CDPlayerTest { ); } + @ParameterizedTest + @MethodSource("addToFavoriteSongsData") + void addToFavoriteSongs(String testName,String cases, CDPlayer _cdPlayer,Exception exception) { + String myFavoriteSong=_cdPlayer.getCD().getPlayList()[0]; + _cdPlayer.addToFavoriteSongs(myFavoriteSong); + assertThat(_cdPlayer.getFavoriteSongs().get(0)).describedAs(testName).isEqualTo(myFavoriteSong); + + } + static Stream addToFavoriteSongsData () { + String[] audioPlayList = new String[]{"Audio 01", "Audio 02", "Audio 03", "Audio 04", "Audio 05"}; + CD audioCD1 = new CD("Audio", "PopMusic", audioPlayList, "XAc20044"); + CD audioCD2=new CD("Audio","RockMusic",audioPlayList,"BLXPP78"); + CDPlayer cdPlayer1 = new CDPlayer(); + cdPlayer1.changePlaySource(); + cdPlayer1.loadCDWechseler(audioCD1, 0); + cdPlayer1.loadCDWechseler(audioCD2, 1); + + return Stream.of( + Arguments.of("[addToFavoriteSongs() by addingOneSong] => favoriteSongsShouldReturnsThatAddedSong", "Case1", cdPlayer1, null) + ); + } } \ No newline at end of file From 9e5960da8785a44239d489fa1b86a4f739423d8f Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 21:14:48 +0100 Subject: [PATCH 2/3] Method addToFavoriteSongs of CDPlayer tested on addingDuplicatedSong -> SongShouldNotBeAdded. --- src/main/java/device/cdPlayer/CDPlayer.java | 4 +++- .../java/device/cdPlayer/CDPlayerTest.java | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 7742fcb..b6ccdab 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -167,7 +167,9 @@ public class CDPlayer implements Device { this.loadedCD=CDList.get(nextIndex); } public void addToFavoriteSongs(String song){ - favoriteSongs.add(song); + if(!(favoriteSongs.contains(song))) { + favoriteSongs.add(song); + } } //Getters, Setters und SupportMethods diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 2468c0c..9f53724 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -494,10 +494,18 @@ class CDPlayerTest { @ParameterizedTest @MethodSource("addToFavoriteSongsData") void addToFavoriteSongs(String testName,String cases, CDPlayer _cdPlayer,Exception exception) { - String myFavoriteSong=_cdPlayer.getCD().getPlayList()[0]; - _cdPlayer.addToFavoriteSongs(myFavoriteSong); - assertThat(_cdPlayer.getFavoriteSongs().get(0)).describedAs(testName).isEqualTo(myFavoriteSong); - + if(cases.equals("Case1")) { + String myFavoriteSong = _cdPlayer.getCD().getPlayList()[0]; + _cdPlayer.addToFavoriteSongs(myFavoriteSong); + assertThat(_cdPlayer.getFavoriteSongs().get(0)).describedAs(testName).isEqualTo(myFavoriteSong); + } + if(cases.equals("Case2")) { + String myFavoriteSong1 = _cdPlayer.getCD().getPlayList()[0]; + String myFavoriteSong2 = _cdPlayer.getCD().getPlayList()[0]; + _cdPlayer.addToFavoriteSongs(myFavoriteSong1); + _cdPlayer.addToFavoriteSongs(myFavoriteSong2); + assertThat(_cdPlayer.getFavoriteSongs().size()>1).describedAs(testName).isNotEqualTo(true); + } } static Stream addToFavoriteSongsData () { String[] audioPlayList = new String[]{"Audio 01", "Audio 02", "Audio 03", "Audio 04", "Audio 05"}; @@ -510,7 +518,8 @@ class CDPlayerTest { cdPlayer1.loadCDWechseler(audioCD2, 1); return Stream.of( - Arguments.of("[addToFavoriteSongs() by addingOneSong] => favoriteSongsShouldReturnsThatAddedSong", "Case1", cdPlayer1, null) + Arguments.of("[addToFavoriteSongs() by addingOneSong] => favoriteSongsShouldReturnsThatAddedSong", "Case1", cdPlayer1, null), + Arguments.of("[addToFavoriteSongs() by addingDuplicatedSong] => SongShouldNotBeAdded", "Case2", cdPlayer1, null) ); } From 067a5c3d70fb8706e80667a50cd73ba67ae1289d Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 21:31:27 +0100 Subject: [PATCH 3/3] Method addToFavoriteSongs of CDPlayer tested on NotSelectingCDWechseler -> shouldThrowAnException. --- src/main/java/device/cdPlayer/CDPlayer.java | 3 +++ src/test/java/device/cdPlayer/CDPlayerTest.java | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index b6ccdab..304d03d 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -167,6 +167,9 @@ public class CDPlayer implements Device { this.loadedCD=CDList.get(nextIndex); } public void addToFavoriteSongs(String song){ + if(activeSource.equals("CDDrive")){ + throw new CDWechselerNotRunningException(); + } if(!(favoriteSongs.contains(song))) { favoriteSongs.add(song); } diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 9f53724..c3cda36 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -506,6 +506,11 @@ class CDPlayerTest { _cdPlayer.addToFavoriteSongs(myFavoriteSong2); assertThat(_cdPlayer.getFavoriteSongs().size()>1).describedAs(testName).isNotEqualTo(true); } + if(cases.equals("Case3")) { + String myFavoriteSong1 = _cdPlayer.getCD().getPlayList()[0]; + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.addToFavoriteSongs(myFavoriteSong1)); + assertEquals(CDWechselerNotRunningException.class, newException.getClass()); + } } static Stream addToFavoriteSongsData () { String[] audioPlayList = new String[]{"Audio 01", "Audio 02", "Audio 03", "Audio 04", "Audio 05"}; @@ -517,9 +522,12 @@ class CDPlayerTest { cdPlayer1.loadCDWechseler(audioCD1, 0); cdPlayer1.loadCDWechseler(audioCD2, 1); + CDPlayer cdPlayer2 = new CDPlayer(); + cdPlayer2.setCD(audioCD2); return Stream.of( Arguments.of("[addToFavoriteSongs() by addingOneSong] => favoriteSongsShouldReturnsThatAddedSong", "Case1", cdPlayer1, null), - Arguments.of("[addToFavoriteSongs() by addingDuplicatedSong] => SongShouldNotBeAdded", "Case2", cdPlayer1, null) + Arguments.of("[addToFavoriteSongs() by addingDuplicatedSong] => SongShouldNotBeAdded", "Case2", cdPlayer1, null), + Arguments.of("[addToFavoriteSongs() by NotSelectingCDWechseler] => shouldThrowAnException", "Case3", cdPlayer2, new CDWechselerNotRunningException()) ); }