From 9913b1755461ee46d2d19b83468d184f51d3c0a8 Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Thu, 17 Feb 2022 19:32:22 +0100 Subject: [PATCH 1/3] added fifth test case for readConfig() & productive code and added first test case for setDevices() & productive code --- Geraete.config | 2 +- src/main/java/BordComputer.java | 13 ++++++++ src/test/java/BordComputerTest.java | 46 ++++++++++++++++++++++------- 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/Geraete.config b/Geraete.config index a98abfd..170bbd6 100644 --- a/Geraete.config +++ b/Geraete.config @@ -1,4 +1,4 @@ -CD=device.cdPlayer.CdPlayer +CD=device.cdPlayer.CDPlayer Radio=device.radioPlayer.RadioPlayer USB=device.usbPlayer.UsbPlayer diff --git a/src/main/java/BordComputer.java b/src/main/java/BordComputer.java index e0af6dc..dbf777c 100644 --- a/src/main/java/BordComputer.java +++ b/src/main/java/BordComputer.java @@ -1,10 +1,15 @@ +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() { @@ -15,8 +20,16 @@ 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(); } + activeDevice =installedDevices[0]; + } + + public void setDevices() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException { + Class c; + c= Class.forName(deviceNames[0]); + installedDevices[0] = (Device) c.getConstructor().newInstance(); } } diff --git a/src/test/java/BordComputerTest.java b/src/test/java/BordComputerTest.java index 3992b59..834a958 100644 --- a/src/test/java/BordComputerTest.java +++ b/src/test/java/BordComputerTest.java @@ -1,3 +1,4 @@ +import device.Device; import device.radioPlayer.RadioPlayer; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -5,6 +6,7 @@ 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 +19,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); @@ -44,10 +41,37 @@ class BordComputerTest { 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) { + Device dev = testBc.installedDevices[0]; + assertThat(dev).describedAs(testName).isNotEqualTo(null); + } + + 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) + + ); + } + 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; + } } \ No newline at end of file From 4bd3d5acd261a64d00699d9f8f0038cfe624a412 Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Thu, 17 Feb 2022 19:40:52 +0100 Subject: [PATCH 2/3] added second and third test case for setDevices() & productive code --- src/main/java/BordComputer.java | 6 ++++-- src/test/java/BordComputerTest.java | 12 ++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/BordComputer.java b/src/main/java/BordComputer.java index dbf777c..5f92057 100644 --- a/src/main/java/BordComputer.java +++ b/src/main/java/BordComputer.java @@ -29,7 +29,9 @@ public class BordComputer { public void setDevices() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException { Class c; - c= Class.forName(deviceNames[0]); - installedDevices[0] = (Device) c.getConstructor().newInstance(); + 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 834a958..8a38b9b 100644 --- a/src/test/java/BordComputerTest.java +++ b/src/test/java/BordComputerTest.java @@ -50,8 +50,11 @@ class BordComputerTest { @ParameterizedTest @MethodSource("setDevicesOptions") - void setDevicesTest(String testName, BordComputer testBc) { - Device dev = testBc.installedDevices[0]; + 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); } @@ -60,8 +63,9 @@ class BordComputerTest { bc.readConfig(); bc.setDevices(); return Stream.of( - Arguments.of("Test if installedDevices[0] is not null", bc) - + 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") ); } public int fileReaderCount() { From ab054d5dfe53df9290ccd26a825841ff2f353e41 Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Thu, 17 Feb 2022 20:11:30 +0100 Subject: [PATCH 3/3] revised all test for setDevices now testing if every installed devices has the right instance & added productive code --- src/main/java/BordComputer.java | 9 +++++---- src/test/java/BordComputerTest.java | 27 +++++++++++++++------------ 2 files changed, 20 insertions(+), 16 deletions(-) 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