diff --git a/src/main/java/BordComputer.java b/src/main/java/BordComputer.java index 5f92057..7563899 100644 --- a/src/main/java/BordComputer.java +++ b/src/main/java/BordComputer.java @@ -11,7 +11,7 @@ public class BordComputer { 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(); @@ -20,16 +20,17 @@ public class BordComputer { Arrays.sort(values); deviceNames = new String[values.length]; System.arraycopy(values, 0, deviceNames, 0, properties.size()); - installedDevices=new Device[values.length]; + installedDevices = new Device[values.length]; } catch (Exception e) { e.printStackTrace(); } - activeDevice =installedDevices[0]; + setDevices(); + activeDevice = installedDevices[0]; } public void setDevices() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException { Class c; - for(int i = 0;i < deviceNames.length; i++) { + for (int i = 0; i < deviceNames.length; i++) { c = Class.forName(deviceNames[i]); installedDevices[i] = (Device) c.getConstructor().newInstance(); } diff --git a/src/test/java/BordComputerTest.java b/src/test/java/BordComputerTest.java index 8a38b9b..6772d99 100644 --- a/src/test/java/BordComputerTest.java +++ b/src/test/java/BordComputerTest.java @@ -1,5 +1,7 @@ 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; @@ -35,7 +37,7 @@ class BordComputerTest { } } - static Stream readConfigOptions() { + static Stream readConfigOptions() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { BordComputer bc1 = new BordComputer(); bc1.readConfig(); @@ -50,24 +52,24 @@ class BordComputerTest { @ParameterizedTest @MethodSource("setDevicesOptions") - void setDevicesTest(String testName, BordComputer testBc, String testTyp) { - Device dev = null; - if(testTyp.equals("0")) dev = testBc.installedDevices[0]; - if(testTyp.equals("1")) dev = testBc.installedDevices[1]; - if(testTyp.equals("2")) dev = testBc.installedDevices[2]; - assertThat(dev).describedAs(testName).isNotEqualTo(null); + 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 setDevicesOptions() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { BordComputer bc = new BordComputer(); bc.readConfig(); - bc.setDevices(); return Stream.of( - Arguments.of("Test if installedDevices[0] is not null", bc, "0"), - Arguments.of("Test if installedDevices[1] is not null", bc, "1"), - Arguments.of("Test if installedDevices[2] is not null", bc, "2") + 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")) { @@ -76,6 +78,7 @@ class BordComputerTest { count = properties.size(); } catch (IOException e) { e.printStackTrace(); - } return count; + } + return count; } } \ No newline at end of file