4 Commits

  1. 2
      Geraete.config
  2. 18
      src/main/java/BordComputer.java
  3. 55
      src/test/java/BordComputerTest.java

2
Geraete.config

@ -1,4 +1,4 @@
CD=device.cdPlayer.CdPlayer
CD=device.cdPlayer.CDPlayer
Radio=device.radioPlayer.RadioPlayer
USB=device.usbPlayer.UsbPlayer

18
src/main/java/BordComputer.java

@ -1,12 +1,17 @@
import device.Device;
import java.io.FileReader;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Properties;
public class BordComputer {
String[] deviceNames;
Device[] installedDevices;
Device activeDevice = null;
public void readConfig() {
public void readConfig() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
try (FileReader reader = new FileReader("Geraete.config")) {
Properties properties = new Properties();
@ -15,8 +20,19 @@ public class BordComputer {
Arrays.sort(values);
deviceNames = new String[values.length];
System.arraycopy(values, 0, deviceNames, 0, properties.size());
installedDevices = new Device[values.length];
} catch (Exception e) {
e.printStackTrace();
}
setDevices();
activeDevice = installedDevices[0];
}
public void setDevices() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException {
Class<?> c;
for (int i = 0; i < deviceNames.length; i++) {
c = Class.forName(deviceNames[i]);
installedDevices[i] = (Device) c.getConstructor().newInstance();
}
}
}

55
src/test/java/BordComputerTest.java

@ -1,10 +1,14 @@
import device.Device;
import device.cdPlayer.CDPlayer;
import device.radioPlayer.RadioPlayer;
import device.usbPlayer.UsbPlayer;
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;
@ -17,15 +21,10 @@ class BordComputerTest {
@MethodSource("readConfigOptions")
void readConfigTest(String testName, String testTyp, BordComputer testBc, String expectedResult) {
if (testTyp.equals("count")) {
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();
}
assertThat(count).describedAs(testName).isEqualTo(testBc.deviceNames.length);
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);
@ -38,16 +37,48 @@ class BordComputerTest {
}
}
static Stream<Arguments> readConfigOptions() {
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 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("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;
}
}
Loading…
Cancel
Save