From 96c9e5c3b65061e1c0becf339d6ca4da8ebda9b2 Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Thu, 17 Feb 2022 00:56:03 +0100 Subject: [PATCH 1/3] added first test case for RadioPlayer getOptions() --- .../device/radioPlayer/RadioPlayerTest.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/test/java/device/radioPlayer/RadioPlayerTest.java b/src/test/java/device/radioPlayer/RadioPlayerTest.java index 7e2f5d0..b7cae0c 100644 --- a/src/test/java/device/radioPlayer/RadioPlayerTest.java +++ b/src/test/java/device/radioPlayer/RadioPlayerTest.java @@ -367,7 +367,7 @@ class RadioPlayerTest { ); } @Test - void TestUnmute() { + void testUnmute() { RadioPlayer rp = new RadioPlayer(); rp.setLautstaerke(40); rp.mute(); @@ -418,4 +418,20 @@ class RadioPlayerTest { Arguments.of("Test for getInfoText not contains station info if station not has one", "noStationInfo", rp7, false) ); } + @ParameterizedTest + @MethodSource("getOptionsOptions") + void testGetOptions(String testName, RadioPlayer testRp) { + + assertThat(testRp.getOptions()).describedAs(testName).isNotEqualTo(null); + } + + static Stream getOptionsOptions() { + RadioPlayer rp = new RadioPlayer(); + + + return Stream.of( + Arguments.of("returnValue of getOptions is not null", rp) + + ); + } } \ No newline at end of file From f6361c27b82eb63c2c57f2b6e5eae88a1c705811 Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Thu, 17 Feb 2022 01:09:58 +0100 Subject: [PATCH 2/3] added second test case for RadioPlayer getOptions() & added productive code --- src/main/java/device/radioPlayer/RadioPlayer.java | 7 ++++++- src/test/java/device/radioPlayer/RadioPlayerTest.java | 11 +++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/device/radioPlayer/RadioPlayer.java b/src/main/java/device/radioPlayer/RadioPlayer.java index 7dcc37a..669d9c1 100644 --- a/src/main/java/device/radioPlayer/RadioPlayer.java +++ b/src/main/java/device/radioPlayer/RadioPlayer.java @@ -2,8 +2,10 @@ package device.radioPlayer; import device.Device; +import java.lang.reflect.Method; import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Arrays; public class RadioPlayer implements Device { @@ -161,7 +163,10 @@ public class RadioPlayer implements Device { @Override public String[] getOptions() { - return new String[0]; + Method[] methods = this.getClass().getDeclaredMethods(); + String[] returnMethods = Arrays.stream(methods).map(Method::getName).toArray(String[]::new); + + return returnMethods; } @Override diff --git a/src/test/java/device/radioPlayer/RadioPlayerTest.java b/src/test/java/device/radioPlayer/RadioPlayerTest.java index b7cae0c..23078bb 100644 --- a/src/test/java/device/radioPlayer/RadioPlayerTest.java +++ b/src/test/java/device/radioPlayer/RadioPlayerTest.java @@ -420,18 +420,17 @@ class RadioPlayerTest { } @ParameterizedTest @MethodSource("getOptionsOptions") - void testGetOptions(String testName, RadioPlayer testRp) { - - assertThat(testRp.getOptions()).describedAs(testName).isNotEqualTo(null); + void testGetOptions(String testName,String testTyp, RadioPlayer testRp) { + if(testTyp.equals("notNull")) assertThat(testRp.getOptions()).describedAs(testName).isNotEqualTo(null); + if(testTyp.equals("arrayLengthGreater0")) assertThat(testRp.getOptions().length).describedAs(testName).isNotEqualTo(0); } static Stream getOptionsOptions() { RadioPlayer rp = new RadioPlayer(); - return Stream.of( - Arguments.of("returnValue of getOptions is not null", rp) - + Arguments.of("returnValue of getOptions is not null", "notNull", rp), + Arguments.of("should not return an empty array", "arrayLengthGreater0", rp) ); } } \ No newline at end of file From 9ca1d2a492283b9860b6d2a40adeb4596db5d13c Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Thu, 17 Feb 2022 01:19:02 +0100 Subject: [PATCH 3/3] added third test case for RadioPlayer getOptions() --- .../java/device/radioPlayer/RadioPlayer.java | 5 ++- .../device/radioPlayer/RadioPlayerTest.java | 40 ++++++++++++++----- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/main/java/device/radioPlayer/RadioPlayer.java b/src/main/java/device/radioPlayer/RadioPlayer.java index 669d9c1..9461dd9 100644 --- a/src/main/java/device/radioPlayer/RadioPlayer.java +++ b/src/main/java/device/radioPlayer/RadioPlayer.java @@ -151,13 +151,14 @@ public class RadioPlayer implements Device { @Override public String getInfoText() { - if(showStationInfo().equals("")) return buildInfoText(); + if (showStationInfo().equals("")) return buildInfoText(); return (buildInfoText() + "\n Now playing: " + showStationInfo()); } private String buildInfoText() { String infoText = playedStation; - if(regionPlaylist.contains(playedStation)) return ("Regional playlist: " + (regionPlaylist.indexOf(playedStation) + 1) + infoText); + if (regionPlaylist.contains(playedStation)) + return ("Regional playlist: " + (regionPlaylist.indexOf(playedStation) + 1) + infoText); else return ("Saved playlist: " + (savedPlaylist.indexOf(playedStation) + 1) + infoText); } diff --git a/src/test/java/device/radioPlayer/RadioPlayerTest.java b/src/test/java/device/radioPlayer/RadioPlayerTest.java index 23078bb..2121aa9 100644 --- a/src/test/java/device/radioPlayer/RadioPlayerTest.java +++ b/src/test/java/device/radioPlayer/RadioPlayerTest.java @@ -1,12 +1,17 @@ package device.radioPlayer; +import device.Device; import org.assertj.core.util.Strings; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -366,6 +371,7 @@ class RadioPlayerTest { Arguments.of("Test for mute RadioPlayer if volume is saved before muting", "saved", rp1, 30) ); } + @Test void testUnmute() { RadioPlayer rp = new RadioPlayer(); @@ -379,13 +385,15 @@ class RadioPlayerTest { @MethodSource("getInfoTextOptions") void testGetInfoText(String testName, String testTyp, RadioPlayer testRp, boolean expectedResult) { Boolean bool; - if(testTyp.equals("null")) bool = Strings.isNullOrEmpty(testRp.getInfoText()); - else if(testTyp.contains("station")) bool = testRp.getInfoText().contains(testRp.playedStation); - else if(testTyp.contains("region")) bool = testRp.getInfoText().contains("Regional playlist"); - else if(testTyp.contains("saved")) bool = testRp.getInfoText().contains("Saved playlist"); - else if(testTyp.contains("savedNum")) bool = testRp.getInfoText().contains(("0") + testRp.savedPlaylist.indexOf(testRp.playedStation) + 1); - else if(testTyp.contains("regionNum")) bool = testRp.getInfoText().contains(("0") + testRp.regionPlaylist.indexOf(testRp.playedStation) + 1); - else if(testTyp.contains("stationInfo")) bool = testRp.getInfoText().contains("Now playing"); + if (testTyp.equals("null")) bool = Strings.isNullOrEmpty(testRp.getInfoText()); + else if (testTyp.contains("station")) bool = testRp.getInfoText().contains(testRp.playedStation); + else if (testTyp.contains("region")) bool = testRp.getInfoText().contains("Regional playlist"); + else if (testTyp.contains("saved")) bool = testRp.getInfoText().contains("Saved playlist"); + else if (testTyp.contains("savedNum")) + bool = testRp.getInfoText().contains(("0") + testRp.savedPlaylist.indexOf(testRp.playedStation) + 1); + else if (testTyp.contains("regionNum")) + bool = testRp.getInfoText().contains(("0") + testRp.regionPlaylist.indexOf(testRp.playedStation) + 1); + else if (testTyp.contains("stationInfo")) bool = testRp.getInfoText().contains("Now playing"); else bool = testRp.getInfoText().contains("Now playing"); assertThat(bool).describedAs(testName).isEqualTo(expectedResult); @@ -418,11 +426,20 @@ class RadioPlayerTest { Arguments.of("Test for getInfoText not contains station info if station not has one", "noStationInfo", rp7, false) ); } + @ParameterizedTest @MethodSource("getOptionsOptions") - void testGetOptions(String testName,String testTyp, RadioPlayer testRp) { - if(testTyp.equals("notNull")) assertThat(testRp.getOptions()).describedAs(testName).isNotEqualTo(null); - if(testTyp.equals("arrayLengthGreater0")) assertThat(testRp.getOptions().length).describedAs(testName).isNotEqualTo(0); + void testGetOptions(String testName, String testTyp, RadioPlayer testRp) { + if (testTyp.equals("notNull")) assertThat(testRp.getOptions()).describedAs(testName).isNotEqualTo(null); + if (testTyp.equals("arrayLengthGreater0")) + assertThat(testRp.getOptions().length).describedAs(testName).isNotEqualTo(0); + else { + Method[] interfaceMethods = Device.class.getDeclaredMethods(); + List deviceMethods = new ArrayList<>(Arrays.asList(testRp.getOptions())); + for (Method interfaceMethod : interfaceMethods) { + assertThat(deviceMethods.contains(interfaceMethod.getName())).describedAs(testName).isEqualTo(true); + } + } } static Stream getOptionsOptions() { @@ -430,7 +447,8 @@ class RadioPlayerTest { return Stream.of( Arguments.of("returnValue of getOptions is not null", "notNull", rp), - Arguments.of("should not return an empty array", "arrayLengthGreater0", rp) + Arguments.of("should not return an empty array", "arrayLengthGreater0", rp), + Arguments.of("Test if all methods declared in interface device are available", "device", rp) ); } } \ No newline at end of file