From 5c1b1bd8a2dc375b236154b5d673134314c214da Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Thu, 17 Feb 2022 17:57:06 +0100 Subject: [PATCH 1/3] added BordComputer & BordComputerTest added first test for readConfig & productive code --- src/main/java/BordComputer.java | 19 +++++++++++++ src/test/java/BordComputerTest.java | 41 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/main/java/BordComputer.java create mode 100644 src/test/java/BordComputerTest.java diff --git a/src/main/java/BordComputer.java b/src/main/java/BordComputer.java new file mode 100644 index 0000000..3b01ee2 --- /dev/null +++ b/src/main/java/BordComputer.java @@ -0,0 +1,19 @@ +import java.io.FileReader; +import java.util.Properties; + +public class BordComputer { + + String[] deviceNames; + + public void readConfig() { + + try (FileReader reader = new FileReader("Geraete.config")) { + Properties properties = new Properties(); + properties.load(reader); + String[] values = properties.values().toArray(new String[0]); + deviceNames = new String[values.length]; + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/BordComputerTest.java b/src/test/java/BordComputerTest.java new file mode 100644 index 0000000..2659039 --- /dev/null +++ b/src/test/java/BordComputerTest.java @@ -0,0 +1,41 @@ +import device.radioPlayer.RadioPlayer; +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.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) { + 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); + } + } + + static Stream readConfigOptions() { + BordComputer bc1 = new BordComputer(); + bc1.readConfig(); + + return Stream.of( + Arguments.of("Check if file reader is getting every item in Geraete.config", "count", bc1) + ); + } + +} \ No newline at end of file From 0fd42ce65a7d3500c1529625353ac0ae3c72f0db Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Thu, 17 Feb 2022 18:10:59 +0100 Subject: [PATCH 2/3] added second test for readConfig & productive code --- src/main/java/BordComputer.java | 1 + src/test/java/BordComputerTest.java | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/BordComputer.java b/src/main/java/BordComputer.java index 3b01ee2..5b44487 100644 --- a/src/main/java/BordComputer.java +++ b/src/main/java/BordComputer.java @@ -12,6 +12,7 @@ public class BordComputer { properties.load(reader); String[] values = properties.values().toArray(new String[0]); deviceNames = new String[values.length]; + System.arraycopy(values, 0, deviceNames, 0, properties.size()); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/BordComputerTest.java b/src/test/java/BordComputerTest.java index 2659039..034584d 100644 --- a/src/test/java/BordComputerTest.java +++ b/src/test/java/BordComputerTest.java @@ -15,7 +15,7 @@ class BordComputerTest { @ParameterizedTest @MethodSource("readConfigOptions") - void readConfigTest(String testName, String testTyp, BordComputer testBc) { + void readConfigTest(String testName, String testTyp, BordComputer testBc, String expectedResult) { if (testTyp.equals("count")) { int count = 0; try (FileReader reader = new FileReader("Geraete.config")) { @@ -27,6 +27,9 @@ class BordComputerTest { } assertThat(count).describedAs(testName).isEqualTo(testBc.deviceNames.length); } + if (testTyp.equals("item1")) { + assertThat(testBc.deviceNames[0]).describedAs(testName).isEqualTo(expectedResult); + } } static Stream readConfigOptions() { @@ -34,7 +37,8 @@ class BordComputerTest { 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 every item in Geraete.config", "count", bc1, ""), + Arguments.of("Check if file reader is getting the first element", "item1", bc1, "device.cdPlayer.CdPlayer") ); } From b6a718f728bf18ae29019f809fe35852c7a1acc5 Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Thu, 17 Feb 2022 18:28:22 +0100 Subject: [PATCH 3/3] added third & fourth test for readConfig & productive code --- src/main/java/BordComputer.java | 2 ++ src/test/java/BordComputerTest.java | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/BordComputer.java b/src/main/java/BordComputer.java index 5b44487..e0af6dc 100644 --- a/src/main/java/BordComputer.java +++ b/src/main/java/BordComputer.java @@ -1,4 +1,5 @@ import java.io.FileReader; +import java.util.Arrays; import java.util.Properties; public class BordComputer { @@ -11,6 +12,7 @@ public class BordComputer { Properties properties = new Properties(); properties.load(reader); String[] values = properties.values().toArray(new String[0]); + Arrays.sort(values); deviceNames = new String[values.length]; System.arraycopy(values, 0, deviceNames, 0, properties.size()); } catch (Exception e) { diff --git a/src/test/java/BordComputerTest.java b/src/test/java/BordComputerTest.java index 034584d..3992b59 100644 --- a/src/test/java/BordComputerTest.java +++ b/src/test/java/BordComputerTest.java @@ -30,6 +30,12 @@ class BordComputerTest { 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 readConfigOptions() { @@ -38,7 +44,9 @@ 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") ); }