diff --git a/src/main/java/device/cdPlayer/CD.java b/src/main/java/device/cdPlayer/CD.java index e1f61b3..4934ac2 100644 --- a/src/main/java/device/cdPlayer/CD.java +++ b/src/main/java/device/cdPlayer/CD.java @@ -2,6 +2,7 @@ package device.cdPlayer; public class CD { private String format; + private String albumName; private String[] playList; public CD() {} @@ -30,4 +31,12 @@ public class CD { public void setPlayList(String[] playList) { this.playList = playList; } + + public String getAlbumName() { + return albumName; + } + + public void setAlbumName(String albumName) { + this.albumName = albumName; + } } diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index f5f93cc..57101c8 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -14,6 +14,7 @@ public class CDPlayer implements Device { private int CdDriveContent=0; private int CdFlap=0; private int volume = 0; + private String infoText=null; private List supportedFormats = new ArrayList(Arrays.asList(new String[] {"Audio","MP3","WMA","AAC"})); private String actualPlayTrack=""; @@ -66,7 +67,10 @@ public class CDPlayer implements Device { @Override public String getInfoText() { - return null; + if(infoText==null){ + throw new ReturnValueNullException(); + } + return infoText; } @Override @@ -157,4 +161,12 @@ public class CDPlayer implements Device { return actualPlayTrack; } + public void setInfoText() { + this.infoText=this.getClass().getSimpleName()+": Item->"+actualPlayTrack+" from Album->"+this.loadedCD.getAlbumName()+" running."; + } + + + public CD getCD() { + return this.loadedCD; + } } diff --git a/src/main/java/device/cdPlayer/exceptions/ReturnValueNullException.java b/src/main/java/device/cdPlayer/exceptions/ReturnValueNullException.java new file mode 100644 index 0000000..15f8ca0 --- /dev/null +++ b/src/main/java/device/cdPlayer/exceptions/ReturnValueNullException.java @@ -0,0 +1,9 @@ +package device.cdPlayer.exceptions; + +public class ReturnValueNullException extends RuntimeException{ + + public ReturnValueNullException() { + super("Method should not return a Null-Value."); + } + +} diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 3bcc1db..e8d8f77 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import java.lang.reflect.Method; @@ -17,6 +18,8 @@ import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + @ExtendWith(MockitoExtension.class) class CDPlayerTest { @@ -272,4 +275,56 @@ class CDPlayerTest { assertThat(volumeIstInValueRange).isEqualTo(true); } + @ParameterizedTest + @MethodSource("getInfoTestData") + void getInfoTest(String testName,String cases, CDPlayer _cdPlayer,Exception exception , String expectedResult){ + + if(cases.equals("Case1")) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.getInfoText()); + assertEquals(ReturnValueNullException.class, newException.getClass()); + } + if(cases.equals("Case2")) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.getInfoText()); + assertThat(newException.getMessage()).describedAs(testName).isEqualTo(expectedResult); + } + if(cases.equals("Case3")) { + String[] album= _cdPlayer.getItemList(); + boolean containsInfoOfActualPlayTrack=false; + for (int i = 0; i < album.length; i++) { + if(_cdPlayer.getInfoText().contains(expectedResult)) + containsInfoOfActualPlayTrack=true; + } + assertThat(containsInfoOfActualPlayTrack).describedAs(testName).isEqualTo(true); + } + if(cases.equals("Case4")) { + String albumName= _cdPlayer.getCD().getAlbumName(); + assertThat(_cdPlayer.getInfoText().contains(albumName)).describedAs(testName).isEqualTo(true); + } + if(cases.equals("Case5")) { + assertThat(_cdPlayer.getInfoText().contains("CDPlayer")).describedAs(testName).isEqualTo(true); + } + + } + static Stream getInfoTestData () { + CDPlayer cdPlayer1 = new CDPlayer(); + + String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"}; + //some CDs + CD audioCD1=new CD("Audio",audioPlayList); + audioCD1.setAlbumName("Love Songs"); + CDPlayer cdPlayer2 = new CDPlayer(); + cdPlayer2.tapOnCdFlap(); + cdPlayer2.setCD(audioCD1); + cdPlayer2.tapOnCdFlap(); + cdPlayer2.setInfoText(); + + return Stream.of( + Arguments.of("[getInfoText() by infoText=null ] => shouldThrowReturnValueNullException","Case1", cdPlayer1,new ReturnValueNullException(),""), + Arguments.of("[getInfoText() by infoText=null ] => ExceptionShouldReturnAMessage","Case2",cdPlayer1,new ReturnValueNullException(),"Method should not return a Null-Value."), + Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoOfActualPlayTrack","Case3",cdPlayer2,null,""), + Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoAboutAlbum","Case4",cdPlayer2,null,""), + Arguments.of("[getInfoText() by infoText=Message ] => MessageShouldContainInfoAboutDevice","Case5",cdPlayer2,null,"") + ); + } + } \ No newline at end of file