From 9ca1d2a492283b9860b6d2a40adeb4596db5d13c Mon Sep 17 00:00:00 2001 From: alpina0707 Date: Thu, 17 Feb 2022 01:19:02 +0100 Subject: [PATCH] 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