Browse Source

Merge branch 'master' into feature-pr-USB-otherTests

feature-pr-USB-otherTests
sahar 2 years ago
parent
commit
33d834e702
  1. 7
      src/main/java/device/Device.java
  2. 9
      src/main/java/device/cdPlayer/CD.java
  3. 24
      src/main/java/device/cdPlayer/CDPlayer.java
  4. 9
      src/main/java/device/cdPlayer/exceptions/ReturnValueNullException.java
  5. 60
      src/main/java/device/radioPlayer/RadioPlayer.java
  6. 10
      src/main/java/device/usbPlayer/UsbPlayer.java
  7. 84
      src/test/java/device/cdPlayer/CDPlayerTest.java
  8. 92
      src/test/java/device/radioPlayer/RadioPlayerTest.java

7
src/main/java/device/Device.java

@ -7,6 +7,7 @@ public interface Device {
int getVolume();
void next();
void prev();
String play();
//get deviceName, Album and current Item
String getInfoText();
//get a list of public methods
@ -15,6 +16,8 @@ public interface Device {
String chooseItem(int itemNr);
// get the actual album
String[] getItemList();
String play();
//mute the Volume and returns an Info.
String mute();
//unmute the Volume and returns an Info.
String unmute();
}

9
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;
}
}

24
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<String> supportedFormats = new ArrayList<String>(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
@ -90,6 +94,16 @@ public class CDPlayer implements Device {
return loadedCD.getPlayList();
}
@Override
public String mute() {
return null;
}
@Override
public String unmute() {
return null;
}
@Override
public String play() {
if(isCdFlapOpen()){
@ -147,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;
}
}

9
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.");
}
}

60
src/main/java/device/radioPlayer/RadioPlayer.java

@ -22,6 +22,7 @@ public class RadioPlayer implements Device {
LocalDateTime now = LocalDateTime.now();
int hour = now.getHour();
int Lautstaerke = 0;
int savedVolume;
String playedStation = "";
public String getYouFMInfoByTime(int x) {
@ -158,12 +159,50 @@ public class RadioPlayer implements Device {
@Override
public String chooseItem(int itemNr) {
return null;
if (regionPlaylist.contains(playedStation)) {
if (itemNr > regionPlaylist.size()) {
playedStation = regionPlaylist.get(regionPlaylist.size() - 1);
return ("Radio is playing station: 0" + regionPlaylist.size() + " " + this.playedStation + " from regional playlist");
} else if (itemNr < 1) {
playedStation = regionPlaylist.get(0);
return ("Radio is playing station: 01 " + this.playedStation + " from regional playlist");
} else {
playedStation = regionPlaylist.get(itemNr - 1);
return ("Radio is playing station: 0" + (regionPlaylist.indexOf(playedStation) + 1) + " " + this.playedStation + " from regional playlist");
}
} else {
if (itemNr > savedPlaylist.size()) {
playedStation = savedPlaylist.get(savedPlaylist.size() - 1);
return ("Radio is playing station: 0" + savedPlaylist.size() + " " + this.playedStation + " from saved playlist");
} else if (itemNr < 1) {
playedStation = savedPlaylist.get(0);
return ("Radio is playing station: 01 " + this.playedStation + " from saved playlist");
} else {
playedStation = savedPlaylist.get(itemNr - 1);
return ("Radio is playing station: 0" + (savedPlaylist.indexOf(playedStation) + 1) + " " + this.playedStation + " from saved playlist");
}
}
}
@Override
public String[] getItemList() {
return null;
if (regionPlaylist.contains(playedStation))
return regionPlaylist.toArray(new String[0]);
else
return savedPlaylist.toArray(new String[0]);
}
@Override
public String mute() {
savedVolume = getVolume();
setLautstaerke(0);
return "RadioPlayer is muted now";
}
@Override
public String unmute() {
setLautstaerke(savedVolume);
return ("RadioPlayer is unmuted Volume is set to " + getVolume());
}
@ -208,7 +247,7 @@ public class RadioPlayer implements Device {
}
public String changeToSavedPlaylist() {
if(regionPlaylist.contains(playedStation)) {
if (regionPlaylist.contains(playedStation)) {
playedStation = savedPlaylist.get(0);
regionPlaylist.clear();
return "Playlist switched now playing station: 0" + (savedPlaylist.indexOf(playedStation) + 1) + " " + this.playedStation + " from saved playlist";
@ -218,16 +257,15 @@ public class RadioPlayer implements Device {
public String changeOrderInSavedPlaylist(int nr) {
String station = playedStation;
if((nr-1) > savedPlaylist.size()) {
if ((nr - 1) > savedPlaylist.size()) {
savedPlaylist.remove(playedStation);
savedPlaylist.add(savedPlaylist.size(),station);
return"Station " + playedStation + " is now on place 0" + (savedPlaylist.size()+1) + "in saved playlist";
}else if(nr < 0) {
savedPlaylist.add(savedPlaylist.size(), station);
return "Station " + playedStation + " is now on place 0" + (savedPlaylist.size() + 1) + "in saved playlist";
} else if (nr < 0) {
savedPlaylist.remove(playedStation);
savedPlaylist.add(0,station);
return"Station " + playedStation + " is now on place 01 in saved playlist";
}
else {
savedPlaylist.add(0, station);
return "Station " + playedStation + " is now on place 01 in saved playlist";
} else {
savedPlaylist.remove(playedStation);
savedPlaylist.add(nr - 1, station);
return "Station " + playedStation + " is now on place 0" + nr + "in saved playlist";

10
src/main/java/device/usbPlayer/UsbPlayer.java

@ -172,6 +172,16 @@ public class UsbPlayer implements Device {
return null;
}
@Override
public String mute() {
return null;
}
@Override
public String unmute() {
return null;
}
@Override
public String play() {

84
src/test/java/device/cdPlayer/CDPlayerTest.java

@ -1,16 +1,25 @@
package device.cdPlayer;
import device.Device;
import device.cdPlayer.exceptions.*;
import org.junit.jupiter.api.Test;
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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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 {
@ -231,22 +240,91 @@ class CDPlayerTest {
@ParameterizedTest
@MethodSource("getOptionsTestData")
void getOptionsTest(String testName,String testDirection, CDPlayer _cdPlayer, String expectedResult){
void getOptionsTest(String testName,String testDirection, CDPlayer _cdPlayer){
if(testDirection.equals("ReturnValueNotNull")) {
assertThat(_cdPlayer.getOptions()).describedAs(testName).isNotEqualTo(null);
}
if(testDirection.equals("ArrayLengthGreaterThen0")) {
assertThat(_cdPlayer.getOptions().length).describedAs(testName).isNotEqualTo(0);
}
if(testDirection.equals("ArrayContainsInterfaceMethods")) {
Method[]interfaceMethods=Device.class.getDeclaredMethods();
List<String> deviceMethods = new ArrayList<String>(Arrays.asList(_cdPlayer.getOptions()));
for (int i = 0; i < interfaceMethods.length; i++) {
assertThat(deviceMethods.contains(interfaceMethods[i].getName())).describedAs(testName).isEqualTo(true);
}
}
}
static Stream<Arguments> getOptionsTestData () {
CDPlayer cdPlayer1 = new CDPlayer();
return Stream.of(
Arguments.of("[getOptions(): shouldNotReturnNull] => ReturnValueNotNull", "ReturnValueNotNull", cdPlayer1,null),
Arguments.of("[getOptions(): shouldReturnAnArrayWithLengthGreaterThen0] => ArrayLengthGreaterThen0", "ArrayLengthGreaterThen0", cdPlayer1,null)
Arguments.of("[getOptions(): shouldNotReturnNull] => ReturnValueNotNull", "ReturnValueNotNull", cdPlayer1),
Arguments.of("[getOptions(): shouldReturnAnArrayWithLengthGreaterThen0] => ArrayLengthGreaterThen0", "ArrayLengthGreaterThen0", cdPlayer1),
Arguments.of("[getOptions(): shouldReturnAnArrayWithInAllInterfaceMethods] => ArrayContainsInterfaceMethods", "ArrayContainsInterfaceMethods", cdPlayer1)
);
}
@Test
void getVolumeReturnValueBetween0And100(){
CDPlayer cdPlayer1 = new CDPlayer();
boolean volumeIstInValueRange=false;
int volume=cdPlayer1.getVolume();
if(volume>=0 && volume <=100){
volumeIstInValueRange=true;
}
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<Arguments> 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,"")
);
}
}

92
src/test/java/device/radioPlayer/RadioPlayerTest.java

@ -171,11 +171,42 @@ class RadioPlayerTest {
@Test
void getOptions() {
}
@Test
void chooseOption() {
}
*/
@ParameterizedTest
@MethodSource("chooseItemOptions")
void testChooseItem(String testName, RadioPlayer testRp, String expectedResult) {
String playedStation = testRp.playedStation;
assertThat(playedStation).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> chooseItemOptions() {
RadioPlayer rp = new RadioPlayer();
rp.chooseItem(2);
RadioPlayer rp1 = new RadioPlayer();
rp1.chooseItem(5);
RadioPlayer rp2 = new RadioPlayer();
rp2.chooseItem(-1);
RadioPlayer rp3 = new RadioPlayer();
rp3.changeRegion("BY");
rp3.chooseItem(2);
RadioPlayer rp4 = new RadioPlayer();
rp4.changeRegion("BY");
rp4.chooseItem(5);
RadioPlayer rp5 = new RadioPlayer();
rp5.changeRegion("BY");
rp5.chooseItem(-2);
return Stream.of(
Arguments.of("Test select station in saved playlist to play with nr isn`t bigger than playlist size", rp, "Teddy"),
Arguments.of("Test select station in saved playlist to play with nr is bigger than playlist size. Last station in playlist gets played", rp1, "MegaHits"),
Arguments.of("Test select station in saved playlist to play with nr is lower than 0. First station in playlist gets played", rp2, "YouFM"),
Arguments.of("Test select station in regional playlist to play with nr isn`t bigger than playlist size", rp3, "Bayern 1"),
Arguments.of("Test select station in regional playlist to play with nr is bigger than playlist size. Last station in playlist gets played", rp4, "Hit Radio N1"),
Arguments.of("Test select station in regional playlist to play with nr is lower than 0. Last station in playlist gets played", rp5, "Antenne Bayern")
);
}
@ParameterizedTest
@MethodSource("testPlayOptions")
void testPlay(String testName, RadioPlayer testRp, String expectedResult) {
@ -289,4 +320,57 @@ class RadioPlayerTest {
Arguments.of("Test for change order in saved playlist with nr is than smaller than 0 at front of playlist", rp2, 0, "Teddy")
);
}
@ParameterizedTest
@MethodSource("getItemListOptions")
void testGetItemList(String testName, RadioPlayer testRp, String testTyp, String[] expectedResult) {
if (testTyp.equals("region")) {
testRp.changeRegion("BY");
}
String[] playList = testRp.getItemList();
assertThat(playList).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> getItemListOptions() {
RadioPlayer rp = new RadioPlayer();
RadioPlayer rp1 = new RadioPlayer();
rp1.changeRegion("BY");
return Stream.of(
Arguments.of("Test for return saved playlist", rp, "saved", rp.savedPlaylist.toArray(new String[0])),
Arguments.of("Test for return regional playlist", rp1, "region", rp1.regionPlaylist.toArray(new String[0]))
);
}
@ParameterizedTest
@MethodSource("muteOptions")
void testMute(String testName, String testTyp, RadioPlayer testRp, int expectedResult) {
int volume;
if (testTyp.equals("actual")) volume = testRp.getVolume();
else volume = testRp.savedVolume;
assertThat(volume).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> muteOptions() {
RadioPlayer rp = new RadioPlayer();
rp.setLautstaerke(25);
rp.mute();
RadioPlayer rp1 = new RadioPlayer();
rp1.setLautstaerke(30);
rp1.mute();
return Stream.of(
Arguments.of("Test for mute RadioPlayer if volume is actually 0", "actual", rp, 0),
Arguments.of("Test for mute RadioPlayer if volume is saved before muting", "saved", rp1, 30)
);
}
@Test
void TestUnmute() {
RadioPlayer rp = new RadioPlayer();
rp.setLautstaerke(40);
rp.mute();
rp.unmute();
assertThat(rp.getVolume()).describedAs("Test if unmute is setting the saved volume").isEqualTo(rp.savedVolume);
}
}
Loading…
Cancel
Save