Project for Continous Integration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
11 KiB

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;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
class BordComputerTest {
@ParameterizedTest
@MethodSource("readConfigOptions")
void readConfigTest(String testName, String testTyp, BordComputer testBc, String expectedResult) {
if (testTyp.equals("count")) {
assertThat(fileReaderCount()).describedAs(testName).isEqualTo(testBc.deviceNames.length);
}
if (testTyp.equals("count1")) {
assertThat(fileReaderCount()).describedAs(testName).isEqualTo(testBc.installedDevices.length);
}
if (testTyp.equals("item1")) {
assertThat(testBc.deviceNames[0]).describedAs(testName).isEqualTo(expectedResult);
}
if (testTyp.equals("item2")) {
assertThat(testBc.deviceNames[1]).describedAs(testName).isEqualTo(expectedResult);
}
if (testTyp.equals("item3")) {
assertThat(testBc.deviceNames[2]).describedAs(testName).isEqualTo(expectedResult);
}
}
static Stream<Arguments> readConfigOptions() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
BordComputer bc1 = new BordComputer();
bc1.readConfig();
return Stream.of(
Arguments.of("Check if file reader is getting every item in Geraete.config", "count", bc1, ""),
Arguments.of("Check if file reader is getting the first element", "item1", bc1, "device.cdPlayer.CDPlayer"),
Arguments.of("Check if file reader is getting the second element", "item2", bc1, "device.radioPlayer.RadioPlayer"),
Arguments.of("Check if file reader is getting the third element", "item3", bc1, "device.usbPlayer.UsbPlayer"),
Arguments.of("Test if installed devices has the length", "count1", bc1, "")
);
}
@ParameterizedTest
@MethodSource("setDevicesOptions")
void setDevicesTest(String testName, BordComputer testBc, String testTyp, Boolean bool) {
Boolean boolActual = null;
if (testTyp.equals("0")) boolActual = (testBc.installedDevices[0] instanceof CDPlayer);
if (testTyp.equals("1")) boolActual = (testBc.installedDevices[1] instanceof RadioPlayer);
if (testTyp.equals("2")) boolActual = (testBc.installedDevices[2] instanceof UsbPlayer);
assertThat(boolActual).describedAs(testName).isEqualTo(bool);
}
static Stream<Arguments> setDevicesOptions() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
BordComputer bc = new BordComputer();
bc.readConfig();
return Stream.of(
Arguments.of("Test if installedDevices[0] is instance of CDPlayer", bc, "0", true),
Arguments.of("Test if installedDevices[1] is instance of RadioPlayer", bc, "1", true),
Arguments.of("Test if installedDevices[2] is instance of UsbPlayer", bc, "2", true)
);
}
public int fileReaderCount() {
int count = 0;
try (FileReader reader = new FileReader("Geraete.config")) {
Properties properties = new Properties();
properties.load(reader);
count = properties.size();
} catch (IOException e) {
e.printStackTrace();
}
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")
);
}
}