Browse Source

Merge commit '0e9ae94aef323c9c14556c70347909e388dc0d58' into HEAD

master
Jenkins 2 years ago
parent
commit
e90385101c
  1. 85
      src/main/java/BordComputer.java
  2. 9
      src/main/java/device/cdPlayer/CDPlayer.java
  3. 2
      src/main/java/device/usbPlayer/USB_Stick.java
  4. 5
      src/main/java/device/usbPlayer/UsbPlayer.java
  5. 7
      src/main/java/exception/NoItemEvenAllPreconditionAreMet.java
  6. 154
      src/test/java/BordComputerTest.java
  7. 3
      src/test/java/device/cdPlayer/CDPlayerTest.java

85
src/main/java/BordComputer.java

@ -1,4 +1,12 @@
import device.Device;
import device.cdPlayer.CD;
import device.cdPlayer.CDPlayer;
import device.cdPlayer.CD;
import device.cdPlayer.CDPlayer;
import device.radioPlayer.RadioPlayer;
import device.usbPlayer.USB_Stick;
import device.usbPlayer.UsbPlayer;
import exception.NoItemEvenAllPreconditionAreMet;
import java.io.FileReader;
import java.lang.reflect.InvocationTargetException;
@ -35,4 +43,81 @@ public class BordComputer {
installedDevices[i] = (Device) c.getConstructor().newInstance();
}
}
public String changeDevice() {
Device[] tempDevices = new Device[installedDevices.length];
tempDevices[0] = installedDevices[1];
tempDevices[1] = installedDevices[2];
tempDevices[2] = installedDevices[0];
//replaces the lists
installedDevices = tempDevices;
//setting activeDevice
activeDevice = installedDevices[0];
String infoText="";
if(activeDevice instanceof CDPlayer){
CDPlayer cdPlayer=(CDPlayer) activeDevice;
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.tapOnCdFlap();
cdPlayer.setCD(audioCD1);
cdPlayer.tapOnCdFlap();
cdPlayer.setInfoText();
infoText = cdPlayer.getInfoText();
}else{
infoText=activeDevice.getInfoText()+" ist activate";
}
return infoText;
}
public String play(){
String itemToPlay=null;
try {
itemToPlay=activeDevice.play();
}catch (RuntimeException exception) {
throw new NoItemEvenAllPreconditionAreMet();
}
return itemToPlay;
}
//===== Hilf-Funktionen ======
public Device getInstanceByName(String deviceName){
for (int i = 0; i <installedDevices.length ; i++) {
if(installedDevices[i].getClass().getSimpleName().equals(deviceName)){
return installedDevices[i];
}
}
return null;
}
public void meetAllPreconditionForPlay(){
if(activeDevice instanceof CDPlayer) {
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 cdPlayer = (CDPlayer) activeDevice;
cdPlayer.changePlaySource();
cdPlayer.loadCDWechseler(audioCD1, 0);
cdPlayer.loadCDWechseler(audioCD2, 1);
cdPlayer.setCD(cdPlayer.getCdWechseler().getCDList().get(0));
activeDevice = cdPlayer;
}
if(activeDevice instanceof UsbPlayer) {
USB_Stick USBDrive = new USB_Stick();
UsbPlayer up1 = new UsbPlayer(USBDrive);
activeDevice = up1;
}
}
public void setActiveDevice(Device activeDevice) {
this.activeDevice = activeDevice;
}
}

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

@ -28,6 +28,8 @@ public class CDPlayer implements Device {
private boolean muted=false;
private int oldVolume=0;
private String status="";
@Override
public void louder() {
if(volume < 100) {
@ -138,8 +140,8 @@ public class CDPlayer implements Device {
throw new FormatNotSupportedException();
}
else if(!isCdFlapOpen()&&getCdDriveContent()==1&&checkFormat(loadedCD.getFormat())){
System.out.println(loadedCD.getFormat()+" "+this.actualPlayTrack+" is playing.");
return "AllConditionsForPlayMet";
status="AllConditionsForPlayMet";
return (loadedCD.getFormat()+":"+this.actualPlayTrack+" is playing.");
}
return null;
}
@ -247,4 +249,7 @@ public class CDPlayer implements Device {
return oldVolume;
}
public String getStatus() {
return status;
}
}

2
src/main/java/device/usbPlayer/USB_Stick.java

@ -8,7 +8,7 @@ public class USB_Stick {
public ArrayList<ArrayList<String>> Folders = new ArrayList<>();
int currentAlbum ;
USB_Stick() {
public USB_Stick() {
ArrayList<String> a1 = new ArrayList<String>();
a1.add("Musik 01.mp3");
a1.add("Musik 02.mp3");

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

@ -70,10 +70,7 @@ public class UsbPlayer implements Device {
super();
isDefect = false;
isConnected = true;
/*Playlist.add("Musik 01.mp3");
Playlist.add("Musik 02.mp3");
Playlist.add("Musik 03.mp3");
playTrack = Playlist.get(0);*/
try {
Playlist = stick.getAlbum();
} catch (NoFolderFound e) {

7
src/main/java/exception/NoItemEvenAllPreconditionAreMet.java

@ -0,0 +1,7 @@
package exception;
public class NoItemEvenAllPreconditionAreMet extends RuntimeException{
public NoItemEvenAllPreconditionAreMet(){
super("No Item returned even all preconditions are met.");
}
}

154
src/test/java/BordComputerTest.java

@ -1,7 +1,9 @@
import device.Device;
import device.cdPlayer.CDPlayer;
import device.cdPlayer.exceptions.NegativeInputException;
import device.radioPlayer.RadioPlayer;
import device.usbPlayer.UsbPlayer;
import exception.NoItemEvenAllPreconditionAreMet;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@ -81,4 +83,156 @@ class BordComputerTest {
}
return count;
}
////
@ParameterizedTest
@MethodSource("changeDeviceOptions")
void changeDeviceTest(String testName, BordComputer inputBC, Device[] oldDevices,Device prvDevice,String message ,int caseNr) {
Device[] newDevicesList = inputBC.installedDevices.clone();
if (caseNr == 1 ) {
assertThat(newDevicesList).describedAs(testName).isNotEqualTo(oldDevices);
}else if (caseNr == 2) {
Device currentDevice = inputBC.activeDevice;
assertThat(currentDevice).describedAs(testName).isNotEqualTo(prvDevice);
}else if (caseNr == 3) {
assertThat(inputBC.changeDevice()).describedAs(testName).isNotNull();
}else if (caseNr == 4) {
assertThat(inputBC.changeDevice()).describedAs(testName).isNotNull();
}else if (caseNr == 5) {
assertThat(inputBC.changeDevice()).describedAs(testName).isNotNull();
}else if (caseNr == 6) {
assertThat(inputBC.changeDevice()).describedAs(testName).contains("USB Player");
}else if (caseNr == 7) {
assertThat(inputBC.changeDevice()).describedAs(testName).contains("CDPlayer");
}else if (caseNr == 8) {
assertThat(inputBC.changeDevice()).describedAs(testName).contains("playlist");
}
}
static Stream<Arguments> changeDeviceOptions() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
//first case
BordComputer bc = new BordComputer();
bc.readConfig();
bc.setDevices();
Device[] currentDevices = new Device[bc.installedDevices.length];
currentDevices = bc.installedDevices.clone();
String m = bc.changeDevice();
//second case
BordComputer bc1 = new BordComputer();
bc1.readConfig();
bc1.setDevices();
Device prvDevice = bc1.activeDevice;
m =bc1.changeDevice();
//third case
BordComputer bc2 = new BordComputer();
bc2.readConfig();
bc2.setDevices();
m =bc2.changeDevice();
//forth case
BordComputer bc3 = new BordComputer();
bc3.readConfig();
bc3.setDevices();
m =bc3.changeDevice();
m =bc3.changeDevice(); //CDPlayer
//fifth case
BordComputer bc4 = new BordComputer();
bc4.readConfig();
bc4.setDevices();
m =bc4.changeDevice();
m =bc4.changeDevice(); //CDPlayer
m =bc4.changeDevice();
//sixth case
BordComputer bc5 = new BordComputer();
bc5.readConfig();
bc5.setDevices();
m =bc5.changeDevice();
//seventh case
BordComputer bc6 = new BordComputer();
bc6.readConfig();
bc6.setDevices();
m =bc6.changeDevice();
m =bc6.changeDevice();
//eighth case
BordComputer bc7 = new BordComputer();
bc7.readConfig();
bc7.setDevices();
m =bc7.changeDevice();
m =bc7.changeDevice();
m =bc7.changeDevice();
return Stream.of(
Arguments.of("The order of the installedDevices should be changed ", bc,currentDevices,null,"",1),
Arguments.of("The active Device should be changed ", bc1,null,prvDevice,"",2),
Arguments.of("The new Device returns a String ", bc2,null,prvDevice,bc2.activeDevice.toString(),3),
Arguments.of("The new Device returns a String ", bc3,null,prvDevice,null,4),
Arguments.of("The new Device returns a String ", bc4,null,prvDevice,null,5),
Arguments.of("The new Device returns Info ", bc5,null,prvDevice,null,6),
Arguments.of("The new Device returns Info ", bc6,null,prvDevice,null,7),
Arguments.of("The new Device returns Info ", bc6,null,prvDevice,null,8)
);
}
@ParameterizedTest
@MethodSource("playDate")
void playTest(String testName, String testCase,BordComputer bordComputer,String instanceName) {
if(testCase.equals("Case1")) {
bordComputer.meetAllPreconditionForPlay();
assertThat(bordComputer.play()).describedAs(testName).isNotEqualTo(null);
}
if(testCase.equals("Case2")) {
Device radioPlayer=bordComputer.getInstanceByName(instanceName);
bordComputer.setActiveDevice(radioPlayer);
assertThat(bordComputer.play()).describedAs(testName).isNotEqualTo(null);
}
if(testCase.equals("Case3")) {
Device usbPlayer=bordComputer.getInstanceByName(instanceName);
bordComputer.setActiveDevice(usbPlayer);
bordComputer.meetAllPreconditionForPlay();
assertThat(bordComputer.play()).describedAs(testName).isNotEqualTo(null);
}
if(testCase.equals("Case4")) {
Exception newException = assertThrows(NoItemEvenAllPreconditionAreMet.class, () -> bordComputer.play());
assertEquals(NoItemEvenAllPreconditionAreMet.class, newException.getClass());
assertEquals(newException.getMessage(), "No Item returned even all preconditions are met.");
}
if(testCase.equals("Case5")) {
Device usbPlayer=bordComputer.getInstanceByName(instanceName);
bordComputer.setActiveDevice(usbPlayer);
Exception newException = assertThrows(NoItemEvenAllPreconditionAreMet.class, () -> bordComputer.play());
assertEquals(NoItemEvenAllPreconditionAreMet.class, newException.getClass());
assertEquals(newException.getMessage(), "No Item returned even all preconditions are met.");
}
if(testCase.equals("Case6")) {
Device radioPlayer=bordComputer.getInstanceByName(instanceName);
bordComputer.setActiveDevice(radioPlayer);
assertDoesNotThrow(() -> bordComputer.play());
}
}
static Stream<Arguments> playDate()throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
BordComputer bordComputer = new BordComputer();
bordComputer.readConfig();
BordComputer bordComputer2 = new BordComputer();
bordComputer2.readConfig();
return Stream.of(
Arguments.of("[CDPlayer-> play(): after all preconditions are met] => ShouldReturnAnItemToPlay","Case1",bordComputer,"CDPlayer"),
Arguments.of("[RadioPlayer-> play(): after all preconditions are met] => ShouldReturnAnItemToPlay","Case2",bordComputer,"RadioPlayer"),
Arguments.of("[UsbPlayer-> play(): after all preconditions are met] => ShouldReturnAnItemToPlay","Case3",bordComputer,"UsbPlayer"),
Arguments.of("[CDPlayer-> play(): without fulfilling preconditions] => ShouldReturnAnException","Case4",bordComputer2,"CDPlayer"),
Arguments.of("[UsbPlayer-> play(): without fulfilling preconditions] => ShouldReturnAnException","Case5",bordComputer2,"UsbPlayer"),
Arguments.of("[RadioPlayer-> play(): without fulfilling preconditions] => ShouldWorkWell","Case6",bordComputer2,"RadioPlayer")
);
}
}

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

@ -30,7 +30,8 @@ class CDPlayerTest {
Exception newException=assertThrows(exception.getClass(),()->_cdPlayer.play());
assertEquals(expectedResult, newException.getMessage());
}else{
assertThat(_cdPlayer.play()).describedAs(testName).isEqualTo(expectedResult);
_cdPlayer.play();
assertThat(_cdPlayer.getStatus()).describedAs(testName).isEqualTo(expectedResult);
}
}
static Stream<Arguments> playTestData () {

Loading…
Cancel
Save