Browse Source

Merge remote-tracking branch 'origin/master'

feature-pr-USB-NextTest
sahar 3 years ago
parent
commit
494ba5e317
  1. 38
      src/main/java/device/cdPlayer/CDPlayer.java
  2. 108
      src/main/java/device/usbPlayer/UsbPlayer.java
  3. 110
      src/test/java/device/cdPlayer/CDPlayerTest.java
  4. 41
      src/test/java/device/radioPlayer/RadioPlayerTest.java
  5. 98
      src/test/java/device/usbPlayer/UsbPlayerTest.java

38
src/main/java/device/cdPlayer/CDPlayer.java

@ -1,7 +1,7 @@
package device.cdPlayer;
import device.Device;
import device.cdPlayer.exceptions.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -16,8 +16,6 @@ public class CDPlayer implements Device {
private String actualPlayTrack="";
private CD loadedCD;
private String[] cdAlbum=new String[]{};
@Override
public void louder() {
@ -70,10 +68,23 @@ public class CDPlayer implements Device {
@Override
public String play() {
if(isCdFlapOpen()){
throw new CdFlapIsOpenException();
}
else if(!isCdFlapOpen()&&getCdDriveContent()==0){
throw new NoCDException();
}
else if(!isCdFlapOpen()&&getCdDriveContent()==1&&!checkFormat(loadedCD.getFormat())){
throw new FormatNotSupportedException();
}
else if(!isCdFlapOpen()&&getCdDriveContent()==1&&checkFormat(loadedCD.getFormat())){
System.out.println(loadedCD.getFormat()+" "+this.actualPlayTrack+" is playing.");
return "AllConditionsForPlayMet";
}
return null;
}
//Getters und Setters
//Getters, Setters und SupportMethods
public void setCD(CD _loadedCD) {
loadedCD=_loadedCD;
this.setCdDriveContent();
@ -84,4 +95,23 @@ public class CDPlayer implements Device {
CdDriveContent = 1;
}
public void tapOnCdFlap(){
this.CdFlap+=1;
}
private boolean checkFormat(String format){
if(this.supportedFormats.contains(format))
return true;
else return false;
}
private int getCdDriveContent() {
return CdDriveContent;
}
private boolean isCdFlapOpen() {
if(CdFlap==1) {
return true;
}
else
return false;
}
}

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

@ -2,9 +2,50 @@ package device.usbPlayer;
import device.Device;
import java.util.ArrayList;
public class UsbPlayer implements Device {
private int Lautstaerke = 0;
private ArrayList<String> Playlist = new ArrayList<String>();
private String playTrack = "";
private Boolean isConnected;
private Boolean isDefect;
int Lautstaerke = 0;
//*********setters und getters********
public ArrayList<String> getPlaylist() {
return Playlist;
}
public void setPlaylist(ArrayList<String> playlist) {
Playlist = playlist;
}
public Boolean getDefect() {
return isDefect;
}
public void setDefect(Boolean defect) {
isDefect = defect;
}
public Boolean getConnected() {
return isConnected;
}
public void setConnected(Boolean connected) {
isConnected = connected;
}
public String getPlayTrack() {
return playTrack;
}
public void setPlayTrack(String playTrack) {
this.playTrack = playTrack;
}
public void setLautstaerke(int ls) {
if (ls <= 100) {
@ -16,15 +57,35 @@ public class UsbPlayer implements Device {
return Lautstaerke;
}
@Override
public void louder() {
if(Lautstaerke < 100) {
Lautstaerke += 1;
//*********constructure********
public UsbPlayer() {
super();
isDefect = false;
isConnected = true;
Playlist.add("Musik 01.mp3");
Playlist.add("Musik 02.mp3");
Playlist.add("Musik 03.mp3");
playTrack = Playlist.get(0);
}
//**********search method*************
private boolean isAllAudio() {
String song = "";
boolean flag = true;
for (int i = 0; i < Playlist.size(); i++) {
song = Playlist.get(i).substring(Playlist.get(i).length() - 4);
if (song.equals(".mp3") | song.equals(".mp4") | song.equals(".wav")) {
flag = true;
} else {
Lautstaerke = 100;
flag = false;
break;
}
}
return flag;
}
//**********search method*************
@Override
public void quieter() {
if(Lautstaerke > 0) {
@ -34,6 +95,15 @@ public class UsbPlayer implements Device {
}
}
@Override
public void louder() {
if(Lautstaerke < 100) {
Lautstaerke += 1;
} else {
Lautstaerke = 100;
}
}
@Override
public int getVolume() {
return 0;
@ -67,7 +137,29 @@ public class UsbPlayer implements Device {
@Override
public String play() {
return null;
String result = "USB : ";
if (isConnected == true) {
result += "connected,";
if (isDefect == false) {
result += "working";
if (this.Playlist.size() != 0) {
result += ",have a list";
if (this.isAllAudio()) {
result += ",Audio";
if (!playTrack.equals(""))
result += ",point to a track.";
else
result += ",doesn't point to a track.";
} else {
result += ",not Audio.";
}
} else result += ",have no list.";
} else {
result += "not working.";
}
} else result = "not connected.";
return result;
}
}

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

@ -1,6 +1,6 @@
package device.cdPlayer;
import org.junit.jupiter.api.Test;
import device.cdPlayer.exceptions.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
@ -11,10 +11,115 @@ import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(MockitoExtension.class)
class CDPlayerTest {
@ParameterizedTest
@MethodSource("playTestData")
void play(String testName, CDPlayer _cdPlayer,Exception exception , String expectedResult) {
if(exception!=null){
Exception newException=assertThrows(exception.getClass(),()->_cdPlayer.play());
assertEquals(expectedResult, newException.getMessage());
}else{
assertThat(_cdPlayer.play()).describedAs(testName).isEqualTo(expectedResult);
}
}
static Stream<Arguments> playTestData () {
//some PlayLists
String[] videoPlayList=new String[]{"Video 01","Video 02","Video 03","Video 04","Video 05"};
String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"};
//some CDs
CD videoCD1=new CD("DVD",videoPlayList);
CD videoCD2=new CD("Blue-ray",videoPlayList);
CD audioCD1=new CD("Audio",audioPlayList);
CD audioCD2=new CD("MP3",audioPlayList);
CD audioCD3=new CD("WMA",audioPlayList);
CD audioCD4=new CD("AAC",audioPlayList);
//some CDPlayers
CDPlayer cdPlayer1=new CDPlayer();
CDPlayer cdPlayer2=new CDPlayer();
cdPlayer2.tapOnCdFlap();
cdPlayer2.tapOnCdFlap();
CDPlayer cdPlayer3=new CDPlayer();
cdPlayer3.tapOnCdFlap();
CDPlayer cdPlayer4=new CDPlayer();
cdPlayer4.tapOnCdFlap();
cdPlayer4.setCD(videoCD1);
//===================================
CDPlayer cdPlayer5=new CDPlayer();
cdPlayer5.tapOnCdFlap();
cdPlayer5.setCD(videoCD1);
cdPlayer5.tapOnCdFlap();
CDPlayer cdPlayer6=new CDPlayer();
cdPlayer6.tapOnCdFlap();
cdPlayer6.setCD(videoCD2);
cdPlayer6.tapOnCdFlap();
//===================================
CDPlayer cdPlayer7=new CDPlayer();
cdPlayer7.tapOnCdFlap();
cdPlayer7.setCD(audioCD1);
cdPlayer7.tapOnCdFlap();
CDPlayer cdPlayer8=new CDPlayer();
cdPlayer8.tapOnCdFlap();
cdPlayer8.setCD(audioCD2);
cdPlayer8.tapOnCdFlap();
CDPlayer cdPlayer9=new CDPlayer();
cdPlayer9.tapOnCdFlap();
cdPlayer9.setCD(audioCD3);
cdPlayer9.tapOnCdFlap();
CDPlayer cdPlayer10=new CDPlayer();
cdPlayer10.tapOnCdFlap();
cdPlayer10.setCD(audioCD4);
cdPlayer10.tapOnCdFlap();
return Stream.of(
Arguments.of("[CdFlap=isClosed , CdDriveContent=>isEmpty] => NoCD", cdPlayer1, new NoCDException(), "CD must be set."),
Arguments.of("[CdFlap has been Opened and Closed, CdDriveContent=>isEmpty] => NoCD", cdPlayer2,new NoCDException(),"CD must be set."),
Arguments.of("[CdFlap=isOpen , CdDriveContent=>isEmpty] => CdFlapIsOpen", cdPlayer3,new CdFlapIsOpenException(),"Cd Flap must be closed."),
Arguments.of("[CdFlap=isOpen , CdDriveContent=>isSet] => CdFlapIsOpen", cdPlayer4,new CdFlapIsOpenException(),"Cd Flap must be closed."),
Arguments.of("[CdFlap=isClosed , CdDriveContent=>isSet, CdFormat=>DVD] => FormatNotSupported",cdPlayer5,new FormatNotSupportedException(),"Cd-Format not supported."),
Arguments.of("[CdFlap=isClosed , CdDriveContent=>isSet, CdFormat=>Blue-ray] => FormatNotSupported",cdPlayer6, new FormatNotSupportedException(),"Cd-Format not supported."),
Arguments.of("[CdFlap=isClosed , CdDriveContent=>isSet, CdFormat=>Audio] => AllConditionsForPlayMet", cdPlayer7,null,"AllConditionsForPlayMet"),
Arguments.of("[CdFlap=isClosed , CdDriveContent=>isSet, CdFormat=>MP3] => AllConditionsForPlayMet", cdPlayer8,null,"AllConditionsForPlayMet"),
Arguments.of("[CdFlap=isClosed , CdDriveContent=>isSet, CdFormat=>WMA] => AllConditionsForPlayMet", cdPlayer9,null,"AllConditionsForPlayMet"),
Arguments.of("[CdFlap=isClosed , CdDriveContent=>isSet, CdFormat=>AAC] => AllConditionsForPlayMet", cdPlayer10,null,"AllConditionsForPlayMet")
);
}
@ParameterizedTest
@MethodSource("louderTestData")
void louderTest(String testName,String testDirection, CDPlayer _cdPlayer,int anzClicks, int expectedResult) {
for (int i = 0; i < anzClicks; i++) {
_cdPlayer.louder();
}
if(testDirection.equals("VolumeIncreased")){
assertThat(_cdPlayer.getVolume()).describedAs(testName).isEqualTo(expectedResult);
}
if(testDirection.equals("VolumeHasChanged")){
assertThat(_cdPlayer.getVolume()).describedAs(testName).isNotEqualTo(expectedResult);
}if(testDirection.equals("VolumeNotDecreased")){
assertThat(_cdPlayer.getVolume()).describedAs(testName).isNotEqualTo(expectedResult);
}
}
static Stream<Arguments> louderTestData () {
CDPlayer cdPlayer=new CDPlayer();
return Stream.of(
Arguments.of("[getVolume() after 20x louder() ] => VolumeIncreased","VolumeIncreased", cdPlayer, 20, 20),
Arguments.of("[getVolume() after 2x louder() ] => VolumeHasChanged","VolumeHasChanged" ,cdPlayer,2,0),
Arguments.of("[getVolume() after 5x louder() ] => VolumeDecreased","VolumeNotDecreased", cdPlayer,5,-5)
);
}
@ParameterizedTest
@MethodSource("quieterTestData")
void quieterTest(String testName,String testDirection, CDPlayer _cdPlayer,int anzClicks, int expectedResult) {
@ -48,5 +153,4 @@ class CDPlayerTest {
);
}
}

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

@ -2,7 +2,13 @@ package device.radioPlayer;
import device.radioPlayer.RadioPlayer;
import device.usbPlayer.UsbPlayer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
@ -38,27 +44,36 @@ class RadioPlayerTest {
assertThat(rp.getLautstaerke()).isEqualTo(0);
}
/*
Jenkins config test
@Test
void getVolume() {
}
*/
@Test
void next() {
rp.next();
int currentIndex=rp.Playlist.indexOf(rp.playTrack);
assertThat(rp.playTrack).isEqualTo(rp.Playlist.get((currentIndex)%rp.Playlist.size()));
}
@Test
void prev() {
rp.prev();
int currentIndex=rp.Playlist.indexOf(rp.playTrack);
assertThat(rp.playTrack).isEqualTo(rp.Playlist.get((currentIndex)%rp.Playlist.size()));
@ParameterizedTest
@MethodSource("prevNextOptions")
void prevNextTest(String testName, RadioPlayer testRp, String expectedResult) {
String station = testRp.playTrack;
assertThat(station).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> prevNextOptions() {
RadioPlayer rp1 = new RadioPlayer();
RadioPlayer rp2 = new RadioPlayer();
RadioPlayer rp3 = new RadioPlayer();
rp1.next();
rp2.next();
rp2.prev();
rp3.prev();
return Stream.of(
Arguments.of("Next Station Test", rp1, "Radio Teddy"),
Arguments.of("Prev Station Test", rp2, "Radio YouFM"),
Arguments.of("Prev works on first Station ", rp3, "Radio MegaHits")
);
}
/*
@Test
void getInfoText() {
}

98
src/test/java/device/usbPlayer/UsbPlayerTest.java

@ -1,55 +1,89 @@
package device.usbPlayer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
class UsbPlayerTest {
UsbPlayer up = new UsbPlayer();
@Test
void louder_test() {
up.setLautstaerke(40);
up.louder();
assertThat(up.getLautstaerke()).isEqualTo(41);
@ParameterizedTest
@MethodSource("PlayOptions")
void playTest(String testName, UsbPlayer inputPlay, String expectedResult) {
String playState = inputPlay.play();
assertThat(playState).describedAs(testName).isEqualTo(expectedResult);
}
@Test
void quieter_test() {
up.setLautstaerke(40);
up.quieter();
assertThat(up.getLautstaerke()).isEqualTo(39);
}
/*
@Test
void getVolume() {
}
static Stream<Arguments> PlayOptions() {
UsbPlayer up1 = new UsbPlayer();
UsbPlayer up8 = new UsbPlayer();
up8.setPlayTrack("");
UsbPlayer up2 = new UsbPlayer();
ArrayList<String> l = new ArrayList<String>();
l.add("a.mp3");
l.add("b.mp3");
l.add("c.gif");
up2.setPlaylist(l);
UsbPlayer up3 = new UsbPlayer();
ArrayList<String> Pl = new ArrayList<String>();
up3.setPlaylist(Pl);
UsbPlayer up6 = new UsbPlayer();
up6.setDefect(true);
UsbPlayer up7 = new UsbPlayer();
up7.setConnected(false);
@Test
void next() {
}
@Test
void prev() {
return Stream.of(
Arguments.of("All right : ", up1, "USB : connected,working,have a list,Audio,point to a track."),
Arguments.of("No first Track : ", up8, "USB : connected,working,have a list,Audio,doesn't point to a track."),
Arguments.of("No Audio Files : ", up2, "USB : connected,working,have a list,not Audio."),
Arguments.of("No list of tracks : ", up3, "USB : connected,working,have no list."),
Arguments.of("connected , not working : ", up6, "USB : connected,not working."),
Arguments.of("not connected : ", up7, "not connected."));
}
@Test
void getInfoText() {
@ParameterizedTest
@MethodSource("louderOptions")
void louderTest(String testName, UsbPlayer inputPlay, int expectedResult) {
int volumeNum = inputPlay.getLautstaerke();
assertThat(volumeNum).describedAs(testName).isEqualTo(expectedResult);
}
@Test
void getOptions() {
static Stream<Arguments> louderOptions() {
UsbPlayer up11 = new UsbPlayer();
up11.louder();
UsbPlayer up22 = new UsbPlayer();
up22.setLautstaerke(100);
up22.louder();
return Stream.of(
Arguments.of("Volume is : 1 ", up11, 1),
Arguments.of("Volume is : 100 ", up22, 100)
);
}
@Test
void chooseOption() {
@ParameterizedTest
@MethodSource("quieterOptions")
void quieterTest(String testName, UsbPlayer inputPlay, int expectedResult) {
int volumeNum = inputPlay.getLautstaerke();
assertThat(volumeNum).describedAs(testName).isEqualTo(expectedResult);
}
@Test
void play() {
static Stream<Arguments> quieterOptions() {
UsbPlayer up1 = new UsbPlayer();
up1.quieter();
UsbPlayer up2 = new UsbPlayer();
up2.setLautstaerke(10);
up2.quieter();
return Stream.of(
Arguments.of("Volume must stay 0 ", up1, 0),
Arguments.of("Volume must be 9 ", up2, 9)
);
}
*/
}
Loading…
Cancel
Save