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 new file mode 100644 index 0000000..7563899 --- /dev/null +++ b/src/main/java/BordComputer.java @@ -0,0 +1,38 @@ +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() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + + try (FileReader reader = new FileReader("Geraete.config")) { + 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()); + installedDevices = new Device[values.length]; + } catch (Exception e) { + e.printStackTrace(); + } + setDevices(); + activeDevice = installedDevices[0]; + } + + public void setDevices() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException { + Class c; + for (int i = 0; i < deviceNames.length; i++) { + c = Class.forName(deviceNames[i]); + installedDevices[i] = (Device) c.getConstructor().newInstance(); + } + } +} diff --git a/src/main/java/device/cdPlayer/CD.java b/src/main/java/device/cdPlayer/CD.java index 4934ac2..ae8f7e8 100644 --- a/src/main/java/device/cdPlayer/CD.java +++ b/src/main/java/device/cdPlayer/CD.java @@ -4,6 +4,7 @@ public class CD { private String format; private String albumName; private String[] playList; + private String id; public CD() {} @@ -16,6 +17,13 @@ public class CD { this.playList = playList; } + public CD(String format, String albumName, String[] playList, String id) { + this.format = format; + this.albumName = albumName; + this.playList = playList; + this.id = id; + } + public String getFormat() { return format; } @@ -39,4 +47,12 @@ public class CD { public void setAlbumName(String albumName) { this.albumName = albumName; } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } } diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 57101c8..ca99dfb 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -18,7 +18,15 @@ public class CDPlayer implements Device { private List supportedFormats = new ArrayList(Arrays.asList(new String[] {"Audio","MP3","WMA","AAC"})); private String actualPlayTrack=""; - private CD loadedCD; + private CD loadedCD=null; + + private CDWechseler cdWechseler=null; + private String activeSource="CDDrive";//or CDWechseler + + private ListfavoriteSongs=new ArrayList<>(); + + private boolean muted=false; + private int oldVolume=0; @Override public void louder() { @@ -48,9 +56,7 @@ public class CDPlayer implements Device { List playList = Arrays.asList(loadedCD.getPlayList()); int indexOfActualPlayTrack=playList.indexOf(actualPlayTrack); int indexOfNextPlayTrack=(indexOfActualPlayTrack+1)%loadedCD.getPlayList().length; - //System.out.println("actualIndex= "+indexOfActualPlayTrack); this.actualPlayTrack=playList.get(indexOfNextPlayTrack ); - //System.out.println("nextSong= "+actualPlayTrack); } @Override @@ -86,7 +92,17 @@ public class CDPlayer implements Device { @Override public String chooseItem(int itemNr) { - return null; + if(this.loadedCD==null){ + throw new ReturnValueNullException(); + } + if(itemNr<0){ + throw new NegativeInputException(); + } + if(!(itemNr>=0 && itemNr10){ + throw new MaxCapacityExceededException(); + } + cdWechseler.loadOneCD(cd,index); + this.loadedCD=cdWechseler.getCDList().get(0); + } + public void changeToNextCD(){ + if(activeSource.equals("CDDrive")){ + throw new ChangeCDOnlyOnCDWechselerException(); + } + + List CDList = cdWechseler.getCDList(); + int indexOfLoadedCD=CDList.indexOf(loadedCD); + int nextIndex=(indexOfLoadedCD+1)%CDList.size(); + this.loadedCD=CDList.get(nextIndex); + } + public void addToFavoriteSongs(String song){ + if(activeSource.equals("CDDrive")){ + throw new CDWechselerNotRunningException(); + } + if(!(favoriteSongs.contains(song))) { + favoriteSongs.add(song); + } + } //Getters, Setters und SupportMethods public void setCD(CD _loadedCD) { @@ -165,8 +227,24 @@ public class CDPlayer implements Device { this.infoText=this.getClass().getSimpleName()+": Item->"+actualPlayTrack+" from Album->"+this.loadedCD.getAlbumName()+" running."; } - public CD getCD() { return this.loadedCD; } + + public CDWechseler getCdWechseler() { + return cdWechseler; + } + + public List getFavoriteSongs() { + return favoriteSongs; + } + + public boolean isMuted() { + return muted; + } + + public int getOldVolume() { + return oldVolume; + } + } diff --git a/src/main/java/device/cdPlayer/CDWechseler.java b/src/main/java/device/cdPlayer/CDWechseler.java new file mode 100644 index 0000000..fca17d6 --- /dev/null +++ b/src/main/java/device/cdPlayer/CDWechseler.java @@ -0,0 +1,26 @@ +package device.cdPlayer; + +import java.util.ArrayList; +import java.util.List; + +public class CDWechseler { + private List CDList=new ArrayList<>(); + private boolean running=false; + + + public boolean isRunning() { + return running; + } + + public void activate() { + this.running = true; + } + + public List getCDList() { + return CDList; + } + + public void loadOneCD(CD cd, int index){ + CDList.add(cd); + } +} diff --git a/src/main/java/device/cdPlayer/exceptions/CDWechselerNotRunningException.java b/src/main/java/device/cdPlayer/exceptions/CDWechselerNotRunningException.java new file mode 100644 index 0000000..a2bb44d --- /dev/null +++ b/src/main/java/device/cdPlayer/exceptions/CDWechselerNotRunningException.java @@ -0,0 +1,8 @@ +package device.cdPlayer.exceptions; + +public class CDWechselerNotRunningException extends RuntimeException { + + public CDWechselerNotRunningException() { + super("CDWechseler must be active for using."); + } +} diff --git a/src/main/java/device/cdPlayer/exceptions/ChangeCDOnlyOnCDWechselerException.java b/src/main/java/device/cdPlayer/exceptions/ChangeCDOnlyOnCDWechselerException.java new file mode 100644 index 0000000..ab81555 --- /dev/null +++ b/src/main/java/device/cdPlayer/exceptions/ChangeCDOnlyOnCDWechselerException.java @@ -0,0 +1,7 @@ +package device.cdPlayer.exceptions; + +public class ChangeCDOnlyOnCDWechselerException extends RuntimeException{ + public ChangeCDOnlyOnCDWechselerException(){ + super("Change to next CD olny when CDWechseler is selected"); + } +} diff --git a/src/main/java/device/cdPlayer/exceptions/ItemNumberNotFoundException.java b/src/main/java/device/cdPlayer/exceptions/ItemNumberNotFoundException.java new file mode 100644 index 0000000..6491fa9 --- /dev/null +++ b/src/main/java/device/cdPlayer/exceptions/ItemNumberNotFoundException.java @@ -0,0 +1,7 @@ +package device.cdPlayer.exceptions; + +public class ItemNumberNotFoundException extends RuntimeException { + public ItemNumberNotFoundException() { + super("Given ItemNumber ist not in the Album."); + } +} diff --git a/src/main/java/device/cdPlayer/exceptions/MaxCapacityExceededException.java b/src/main/java/device/cdPlayer/exceptions/MaxCapacityExceededException.java new file mode 100644 index 0000000..163b156 --- /dev/null +++ b/src/main/java/device/cdPlayer/exceptions/MaxCapacityExceededException.java @@ -0,0 +1,7 @@ +package device.cdPlayer.exceptions; + +public class MaxCapacityExceededException extends RuntimeException{ + public MaxCapacityExceededException(){ + super("Max capacity of 10-CDs is already exceeded."); + } +} diff --git a/src/main/java/device/cdPlayer/exceptions/NegativeInputException.java b/src/main/java/device/cdPlayer/exceptions/NegativeInputException.java new file mode 100644 index 0000000..67ec2ca --- /dev/null +++ b/src/main/java/device/cdPlayer/exceptions/NegativeInputException.java @@ -0,0 +1,7 @@ +package device.cdPlayer.exceptions; + +public class NegativeInputException extends RuntimeException{ + public NegativeInputException() { + super("InputNumber must be positive."); + } +} diff --git a/src/main/java/device/radioPlayer/RadioPlayer.java b/src/main/java/device/radioPlayer/RadioPlayer.java index 2b91e3e..0b341d4 100644 --- a/src/main/java/device/radioPlayer/RadioPlayer.java +++ b/src/main/java/device/radioPlayer/RadioPlayer.java @@ -1,14 +1,13 @@ 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 { - private int x; - public RadioPlayer() { super(); savedPlaylist.add("YouFM"); @@ -17,13 +16,13 @@ public class RadioPlayer implements Device { playedStation = savedPlaylist.get(0); } - ArrayList savedPlaylist = new ArrayList(); - ArrayList regionPlaylist = new ArrayList(); + ArrayList savedPlaylist = new ArrayList<>(); + ArrayList regionPlaylist = new ArrayList<>(); LocalDateTime now = LocalDateTime.now(); int hour = now.getHour(); - int Lautstaerke = 0; + int volume = 0; int savedVolume; - String playedStation = ""; + String playedStation; public String getYouFMInfoByTime(int x) { if (x >= 5 && x < 10) return YouFMInfo[0]; @@ -61,8 +60,8 @@ public class RadioPlayer implements Device { String[] AntenneBYInfo = {"ANTENNE BAYERN Guten Morgen Bayern", "ANTENNE BAYERN bei der Arbeit", "ANTENNE BAYERN am Nachmittag", "ANTENNE BAYERN am Abend", "ANTENNE BAYERN Hit-Nacht"}; - public void setLautstaerke(int lautstaerke) { - Lautstaerke = lautstaerke; + public void setVolume(int volume) { + this.volume = volume; } public void changeRegion(String region) { @@ -96,21 +95,21 @@ public class RadioPlayer implements Device { @Override public void louder() { - if (Lautstaerke < 100) { - Lautstaerke += 1; - } else Lautstaerke = 100; + if (volume < 100) { + volume += 1; + } else volume = 100; } @Override public void quieter() { - if (Lautstaerke > 0) { - Lautstaerke -= 1; - } else Lautstaerke = 0; + if (volume > 0) { + volume -= 1; + } else volume = 0; } @Override public int getVolume() { - return Lautstaerke; + return volume; } @Override @@ -149,12 +148,22 @@ public class RadioPlayer implements Device { @Override public String getInfoText() { - return null; + 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); + else return ("Saved playlist: " + (savedPlaylist.indexOf(playedStation) + 1) + infoText); } @Override public String[] getOptions() { - return new String[0]; + Method[] methods = this.getClass().getDeclaredMethods(); + + return Arrays.stream(methods).map(Method::getName).toArray(String[]::new); } @Override @@ -195,17 +204,16 @@ public class RadioPlayer implements Device { @Override public String mute() { savedVolume = getVolume(); - setLautstaerke(0); + setVolume(0); return "RadioPlayer is muted now"; } @Override public String unmute() { - setLautstaerke(savedVolume); + setVolume(savedVolume); return ("RadioPlayer is unmuted Volume is set to " + getVolume()); } - @Override public String play() { if (regionPlaylist.contains(playedStation)) @@ -225,8 +233,6 @@ public class RadioPlayer implements Device { default: return ""; } - - } public String saveStation() { @@ -236,7 +242,6 @@ public class RadioPlayer implements Device { return "Station " + playedStation + " is saved in your Station list"; } - public String deleteStation() { String station = playedStation; if ((savedPlaylist.size()) > 1) { diff --git a/src/test/java/BordComputerTest.java b/src/test/java/BordComputerTest.java new file mode 100644 index 0000000..6772d99 --- /dev/null +++ b/src/test/java/BordComputerTest.java @@ -0,0 +1,84 @@ +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; + +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +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, String expectedResult) { + if (testTyp.equals("count")) { + 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); + } + 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() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + BordComputer bc1 = new BordComputer(); + 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 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("Test if installed devices has the length", "count1", bc1, "") + ); + } + + @ParameterizedTest + @MethodSource("setDevicesOptions") + 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(); + return Stream.of( + 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")) { + Properties properties = new Properties(); + properties.load(reader); + count = properties.size(); + } catch (IOException e) { + e.printStackTrace(); + } + return count; + } +} \ No newline at end of file diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index e8d8f77..06cdff8 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -327,4 +327,282 @@ class CDPlayerTest { ); } -} \ No newline at end of file + @ParameterizedTest + @MethodSource("chooseItemTestData") + void chooseItemTest(String testName,String cases, CDPlayer _cdPlayer,Exception exception ) { + if(cases.equals("Case1")) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.chooseItem(1)); + assertEquals(ReturnValueNullException.class, newException.getClass()); + } + if(cases.equals("Case2")) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.chooseItem(200)); + assertEquals(ItemNumberNotFoundException.class, newException.getClass()); + } + if(cases.equals("Case3")) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.chooseItem(-1)); + assertEquals(NegativeInputException.class, newException.getClass()); + } + if(cases.equals("Case4")) { + assertThat(_cdPlayer.chooseItem(1).contains("2")).describedAs(testName).isEqualTo(true); + } + } + static Stream chooseItemTestData () { + CDPlayer cdPlayer1 = new CDPlayer(); + + String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"}; + CD audioCD1=new CD("Audio",audioPlayList); + CDPlayer cdPlayer2 = new CDPlayer(); + cdPlayer2.tapOnCdFlap(); + cdPlayer2.setCD(audioCD1); + cdPlayer2.tapOnCdFlap(); + + return Stream.of( + Arguments.of("[chooseItem() by notSetCD ] => shouldThrowReturnValueNullException","Case1", cdPlayer1,new ReturnValueNullException()), + Arguments.of("[chooseItem() by invalidInput ] => shouldThrowItemNumberNotFoundException","Case2", cdPlayer2,new ItemNumberNotFoundException()), + Arguments.of("[chooseItem() by invalidInput:negativeNumber] => shouldThrowItemNumberNotFoundException","Case3", cdPlayer2,new NegativeInputException()), + Arguments.of("[chooseItem() by validInput] => shouldReturnMsgWithinTheItemNr","Case4", cdPlayer2,null) + ); + } + + @ParameterizedTest + @MethodSource("changePlaySourceData") + void changePlaySourceTest(String testName,String cases, CDPlayer _cdPlayer) { + + if(cases.equals("Case1")) { + String returnVal = _cdPlayer.changePlaySource(); + assertThat(returnVal).describedAs(testName).isEqualTo("CDWechseler"); + } + if(cases.equals("Case2")) { + String returnVal = _cdPlayer.changePlaySource(); + assertThat(returnVal).describedAs(testName).isEqualTo("CDDrive"); + } + } + static Stream changePlaySourceData () { + CDPlayer cdPlayer1 = new CDPlayer(); + + CDPlayer cdPlayer2 = new CDPlayer(); + cdPlayer2.changePlaySource(); + + return Stream.of( + Arguments.of("[changePlaySource() by activeSource=CDDrive ] => shouldReturnCDWechseler","Case1", cdPlayer1), + Arguments.of("[changePlaySource() by activeSource=CDWechseler ] => shouldReturnCDDrive","Case2", cdPlayer2) + ); + } + + @ParameterizedTest + @MethodSource("loadCDWechselerData") + void loadCDWechselerTest(String testName,String cases, CDPlayer _cdPlayer,CD cd,Exception exception) { + if(cases.equals("Case1")) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cd,0)); + assertEquals(CDWechselerNotRunningException.class, newException.getClass()); + } + if(cases.equals("Case2")) { + _cdPlayer.changePlaySource(); + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cd,0)); + assertEquals(FormatNotSupportedException.class, newException.getClass()); + } + if(cases.equals("Case3")) { + _cdPlayer.changePlaySource(); + _cdPlayer.loadCDWechseler(cd,0); + CD firstCD=_cdPlayer.getCdWechseler().getCDList().get(0); + assertThat(firstCD).describedAs(testName).isNotEqualTo(null); + } + if(cases.equals("Case4")) { + _cdPlayer.changePlaySource(); + Exception newException=assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cd, 12)); + assertEquals(MaxCapacityExceededException.class, newException.getClass()); + } + } + static Stream loadCDWechselerData () { + CDPlayer cdPlayer1 = new CDPlayer(); + String[] videoPlayList=new String[]{"Video 01","Video 02","Video 03","Video 04","Video 05"}; + String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"}; + + CD audioCD1=new CD("Audio",audioPlayList); + CD DVD=new CD("DVD",videoPlayList); + return Stream.of( + Arguments.of("[loadCDWechseler() by notRunning=CDWechseler ] => shouldThrowAnException", "Case1", cdPlayer1,audioCD1,new CDWechselerNotRunningException()), + Arguments.of("[loadCDWechseler() by WrongCDFormat ] => shouldThrowAnException", "Case2", cdPlayer1,DVD,new FormatNotSupportedException()), + Arguments.of("[loadCDWechseler() by leadingFirstCD ] => CDListOfCDWechselerShouldHaveLength1", "Case3", cdPlayer1,audioCD1,null), + Arguments.of("[loadCDWechseler() by MaxCapacityExceeded:Max=10 ] => shouldThrowAnException", "Case4", cdPlayer1,audioCD1,new MaxCapacityExceededException()) + ); + } + + @ParameterizedTest + @MethodSource("changeToNextCDData") + void changeToNextCDTest(String testName,String cases, CDPlayer _cdPlayer,Exception exception) { + if(cases.equals("Case1")) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.changeToNextCD()); + assertEquals(ChangeCDOnlyOnCDWechselerException.class, newException.getClass()); + } + if(cases.equals("Case2")) { + CD actualCD=_cdPlayer.getCD(); + _cdPlayer.changeToNextCD(); + CD nextCD=_cdPlayer.getCD(); + assertThat(nextCD).describedAs(testName).isNotEqualTo(null); + } + if(cases.equals("Case3")) { + CD actualCD=_cdPlayer.getCD(); + _cdPlayer.changeToNextCD(); + CD nextCD=_cdPlayer.getCD(); + assertThat(nextCD.getId()==actualCD.getId()).describedAs(testName).isNotEqualTo(true); + } + if(cases.equals("Case4")) { + CD actualCdBefore=_cdPlayer.getCD(); + for (int i = 0; i <3 ; i++) { + _cdPlayer.changeToNextCD(); + } + CD actualCdAfter=_cdPlayer.getCD(); + assertThat(actualCdBefore.getId()==actualCdAfter.getId()).describedAs(testName).isEqualTo(true); + } + } + static Stream changeToNextCDData () { + String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"}; + CD audioCD1=new CD("Audio","PopMusic",audioPlayList,"XAc20044"); + CD audioCD2=new CD("Audio","RockMusic",audioPlayList,"BLXPP78"); + CD audioCD3=new CD("Audio","SalsaMusic",audioPlayList,"KKRP118"); + + CDPlayer cdPlayer1 = new CDPlayer(); + + CDPlayer cdPlayer2 = new CDPlayer(); + cdPlayer2.changePlaySource(); + cdPlayer2.loadCDWechseler(audioCD1,0); + cdPlayer2.loadCDWechseler(audioCD2,1); + cdPlayer2.loadCDWechseler(audioCD3,2); + + + CDPlayer cdPlayer3 = new CDPlayer(); + cdPlayer3.changePlaySource(); + cdPlayer3.loadCDWechseler(audioCD1,0); + cdPlayer3.loadCDWechseler(audioCD2,1); + cdPlayer3.loadCDWechseler(audioCD3,2); + + CDPlayer cdPlayer4 = new CDPlayer(); + cdPlayer4.changePlaySource(); + cdPlayer4.loadCDWechseler(audioCD1,0); + cdPlayer4.loadCDWechseler(audioCD2,1); + cdPlayer4.loadCDWechseler(audioCD3,2); + + return Stream.of( + Arguments.of("[changeToNextCD() by CDWechselerNotSelected ] => shouldThrowAnException", "Case1", cdPlayer1,new ChangeCDOnlyOnCDWechselerException()), + Arguments.of("[changeToNextCD() checking the effect ] => loadedCDShouldNotReturnNull", "Case2", cdPlayer2,null), + Arguments.of("[changeToNextCD() checking the effect 2 ] => loadedCDShouldNotHaveTheSameIdAsPrev", "Case3", cdPlayer3,null), + Arguments.of("[3x changeToNextCD() changing from last to first CD] => FirstCDShouldBeTaken", "Case4", cdPlayer4,null) + ); + } + + @ParameterizedTest + @MethodSource("addToFavoriteSongsData") + void addToFavoriteSongsTest(String testName,String cases, CDPlayer _cdPlayer,Exception exception) { + if(cases.equals("Case1")) { + String myFavoriteSong = _cdPlayer.getCD().getPlayList()[0]; + _cdPlayer.addToFavoriteSongs(myFavoriteSong); + assertThat(_cdPlayer.getFavoriteSongs().get(0)).describedAs(testName).isEqualTo(myFavoriteSong); + } + if(cases.equals("Case2")) { + String myFavoriteSong1 = _cdPlayer.getCD().getPlayList()[0]; + String myFavoriteSong2 = _cdPlayer.getCD().getPlayList()[0]; + _cdPlayer.addToFavoriteSongs(myFavoriteSong1); + _cdPlayer.addToFavoriteSongs(myFavoriteSong2); + assertThat(_cdPlayer.getFavoriteSongs().size()>1).describedAs(testName).isNotEqualTo(true); + } + if(cases.equals("Case3")) { + String myFavoriteSong1 = _cdPlayer.getCD().getPlayList()[0]; + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.addToFavoriteSongs(myFavoriteSong1)); + assertEquals(CDWechselerNotRunningException.class, newException.getClass()); + } + } + static Stream addToFavoriteSongsData () { + String[] audioPlayList = new String[]{"Audio 01", "Audio 02", "Audio 03", "Audio 04", "Audio 05"}; + CD audioCD1 = new CD("Audio", "PopMusic", audioPlayList, "XAc20044"); + CD audioCD2=new CD("Audio","RockMusic",audioPlayList,"BLXPP78"); + + CDPlayer cdPlayer1 = new CDPlayer(); + cdPlayer1.changePlaySource(); + cdPlayer1.loadCDWechseler(audioCD1, 0); + cdPlayer1.loadCDWechseler(audioCD2, 1); + + CDPlayer cdPlayer2 = new CDPlayer(); + cdPlayer2.setCD(audioCD2); + return Stream.of( + Arguments.of("[addToFavoriteSongs() by addingOneSong] => favoriteSongsShouldReturnsThatAddedSong", "Case1", cdPlayer1, null), + Arguments.of("[addToFavoriteSongs() by addingDuplicatedSong] => SongShouldNotBeAdded", "Case2", cdPlayer1, null), + Arguments.of("[addToFavoriteSongs() by NotSelectingCDWechseler] => shouldThrowAnException", "Case3", cdPlayer2, new CDWechselerNotRunningException()) + ); + } + + @ParameterizedTest + @MethodSource("muteTestData") + void muteTest(String testName,String cases, CDPlayer _cdPlayer,String expectedResult) { + if(cases.equals("Case1")) { + assertThat(_cdPlayer.mute()).describedAs(testName).isEqualTo(expectedResult); + } + if(cases.equals("Case2")) { + _cdPlayer.mute(); + assertThat(_cdPlayer.isMuted()).describedAs(testName).isEqualTo(true); + } + if(cases.equals("Case3")) { + _cdPlayer.louder(); + _cdPlayer.louder(); + _cdPlayer.mute(); + assertThat(_cdPlayer.getVolume()).describedAs(testName).isEqualTo(0); + } + if(cases.equals("Case4")) { + for (int i = 0; i < 5; i++) { + _cdPlayer.louder(); + } + _cdPlayer.mute(); + assertThat(_cdPlayer.getOldVolume()).describedAs(testName).isEqualTo(5); + } + } + static Stream muteTestData () { + CDPlayer cdPlayer1 = new CDPlayer(); + return Stream.of( + Arguments.of("[mute() by checkingTheReturnValue ] => ShouldReturnAString", "Case1", cdPlayer1, "Volume muted."), + Arguments.of("[mute() by checkingTheEffectAfterRun ] => VariableMutedShouldBeTrue", "Case2", cdPlayer1, "Volume muted."), + Arguments.of("[mute() after 2x louder() by checkingTheVolume] => VolumeShouldBeSetTo0", "Case3", cdPlayer1, "Volume muted."), + Arguments.of("[mute() after 5x louder() by checkingTheSavedOldVolume] => getOldVolumeShouldReturns_5", "Case4", cdPlayer1, "Volume muted.") + ); + } + + @ParameterizedTest + @MethodSource("unmuteTestData") + void unmuteTest(String testName,String cases, CDPlayer _cdPlayer,String expectedResult) { + if(cases.equals("Case1")) { + assertThat(_cdPlayer.unmute()).describedAs(testName).isEqualTo(expectedResult); + } + if(cases.equals("Case2")) { + _cdPlayer.unmute(); + assertThat(_cdPlayer.isMuted()).describedAs(testName).isEqualTo(false); + } + if(cases.equals("Case3")) { + for (int i = 0; i < 5; i++) { + _cdPlayer.louder(); + } + _cdPlayer.mute(); + _cdPlayer.unmute(); + assertThat(_cdPlayer.isMuted()).describedAs(testName).isEqualTo(false); + assertThat(_cdPlayer.getVolume()).describedAs(testName).isEqualTo(5); + } + if(cases.equals("Case4")) { + for (int i = 0; i < 6; i++) { + _cdPlayer.louder(); + } + _cdPlayer.mute(); + _cdPlayer.unmute(); + assertThat(_cdPlayer.isMuted()).describedAs(testName).isEqualTo(false); + assertThat(_cdPlayer.getOldVolume()).describedAs(testName).isEqualTo(0); + } + } + static Stream unmuteTestData () { + CDPlayer cdPlayer1 = new CDPlayer(); + return Stream.of( + Arguments.of("[unmute() by checkingTheReturnValue ] => ShouldReturnAString", "Case1", cdPlayer1, "Volume unmute."), + Arguments.of("[unmute() by checkingTheEffectAfterRun ] => VariableMutedShouldBeFalse", "Case2", cdPlayer1, "Volume unmute."), + Arguments.of("[unmute() by 5x louder()-> mute()] => getVolumeShouldReturn_5", "Case3", cdPlayer1, "Volume unmute."), + Arguments.of("[unmute() by 6x louder()-> mute()] => getOldVolumeShouldReturn_0", "Case4", cdPlayer1, "Volume unmute.") + ); + } + + + } \ No newline at end of file diff --git a/src/test/java/device/radioPlayer/RadioPlayerTest.java b/src/test/java/device/radioPlayer/RadioPlayerTest.java index ac5eefc..680109c 100644 --- a/src/test/java/device/radioPlayer/RadioPlayerTest.java +++ b/src/test/java/device/radioPlayer/RadioPlayerTest.java @@ -1,18 +1,22 @@ 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; class RadioPlayerTest { - @ParameterizedTest @MethodSource("quieterLouderOptions") void quieterLouderTest(String testName, RadioPlayer testRp, int expectedResult) { @@ -25,13 +29,13 @@ class RadioPlayerTest { RadioPlayer rp2 = new RadioPlayer(); RadioPlayer rp3 = new RadioPlayer(); RadioPlayer rp4 = new RadioPlayer(); - rp1.setLautstaerke(0); + rp1.setVolume(0); rp1.louder(); - rp2.setLautstaerke(100); + rp2.setVolume(100); rp2.louder(); - rp3.setLautstaerke(1); + rp3.setVolume(1); rp3.quieter(); - rp4.setLautstaerke(0); + rp4.setVolume(0); rp4.quieter(); return Stream.of( Arguments.of("Test for one step louder", rp1, 1), @@ -55,7 +59,6 @@ class RadioPlayerTest { rp1.changeRegion("BY"); rp2.changeRegion("HE"); rp3.changeRegion("BW"); - return Stream.of( Arguments.of("Test change Region to Bayern", rp1, "Antenne Bayern"), Arguments.of("Test change Region to Hessen", rp2, "Hit Radio FFH"), @@ -63,7 +66,6 @@ class RadioPlayerTest { ); } - @ParameterizedTest @MethodSource("prevNextOptions") void prevNextTest(String testName, RadioPlayer testRp, String expectedResult) { @@ -89,7 +91,6 @@ class RadioPlayerTest { rp6.changeRegion("BY"); rp6.saveStation(); rp6.next(); - return Stream.of( Arguments.of("Next Station Test for saved Stations", rp1, "Teddy"), Arguments.of("Prev Station Test for saved Stations", rp2, "YouFM"), @@ -100,40 +101,38 @@ class RadioPlayerTest { ); } - @Test - void YouFMInfoByTimeTest() { - RadioPlayer rp = new RadioPlayer(); - assertThat(rp.getYouFMInfoByTime(8)).describedAs("YouFM info by Time at 8.00").isEqualTo("YOUFM Good Morning Show"); - } - - @Test - void YouFMInfoByTime2Test() { - RadioPlayer rp = new RadioPlayer(); - assertThat(rp.getYouFMInfoByTime(3)).describedAs("YouFM info by Time at 3.00").isEqualTo("YOUFM Junge Nacht der ARD"); - } - - @Test - void AntenneBYInfoByTimeTest() { - RadioPlayer rp = new RadioPlayer(); - assertThat(rp.getAntenneBYInfoByTime(8)).describedAs("AntenneBY info by Time at 8.00").isEqualTo("ANTENNE BAYERN Guten Morgen Bayern"); - } - - @Test - void AntenneBYInfoByTime2Test() { - RadioPlayer rp = new RadioPlayer(); - assertThat(rp.getAntenneBYInfoByTime(3)).describedAs("AntenneBY info by Time at 3.00").isEqualTo("ANTENNE BAYERN Hit-Nacht"); - } - - @Test - void BR3InfoByTimeTest() { - RadioPlayer rp = new RadioPlayer(); - assertThat(rp.getBR3InfoByTime(5)).describedAs("BR3 info by Time at 5.00").isEqualTo("Sebastian Winkler und die Frühaufdreher"); + @ParameterizedTest + @MethodSource("infoByTimeOptions") + void infoByTimeInfo(String testName, RadioPlayer testRp, String testTyp, int hour, String expectedResult) { + if(testTyp.equals("YouFM")) assertThat(testRp.getYouFMInfoByTime(hour)).describedAs(testName).isEqualTo(expectedResult); + if(testTyp.equals("BR3")) assertThat(testRp.getBR3InfoByTime(hour)).describedAs(testName).isEqualTo(expectedResult); + if(testTyp.equals("AntenneBY")) assertThat(testRp.getAntenneBYInfoByTime(hour)).describedAs(testName).isEqualTo(expectedResult); } - @Test - void BR3InfoByTime2Test() { + static Stream infoByTimeOptions() { RadioPlayer rp = new RadioPlayer(); - assertThat(rp.getBR3InfoByTime(21)).describedAs("BR3 info by Time at 21.00").isEqualTo("Matuschke - der etwas andere Abend"); + return Stream.of( + Arguments.of("Station info YouFm at 5:00 am", rp, "YouFM", 5, rp.YouFMInfo[0]), + Arguments.of("Station info YouFm at 10:00 am", rp, "YouFM", 10, rp.YouFMInfo[1]), + Arguments.of("Station info YouFm at 2:00 pm", rp, "YouFM", 14, rp.YouFMInfo[2]), + Arguments.of("Station info YouFm at 6:00 pm", rp, "YouFM", 18, rp.YouFMInfo[3]), + Arguments.of("Station info YouFm at 8:00 pm", rp, "YouFM", 20, rp.YouFMInfo[4]), + Arguments.of("Station info YouFm at 10:00 pm", rp, "YouFM", 22, rp.YouFMInfo[5]), + Arguments.of("Station info YouFm at 3:00 am", rp, "YouFM", 3, rp.YouFMInfo[6]), + Arguments.of("Station info BR3 at 5:00 am", rp, "BR3", 5, rp.BR3Info[0]), + Arguments.of("Station info BR3 at 9:00 am", rp, "BR3", 9, rp.BR3Info[1]), + Arguments.of("Station info BR3 at 12:00 am", rp, "BR3", 12, rp.BR3Info[2]), + Arguments.of("Station info BR3 at 1:00 pm", rp, "BR3", 13, rp.BR3Info[3]), + Arguments.of("Station info BR3 at 4:00 pm", rp, "BR3", 16, rp.BR3Info[4]), + Arguments.of("Station info BR3 at 7:00 pm", rp, "BR3", 19, rp.BR3Info[5]), + Arguments.of("Station info BR3 at 9:00 pm", rp, "BR3", 21, rp.BR3Info[6]), + Arguments.of("Station info BR3 at 3:00 am", rp, "BR3", 3, rp.BR3Info[7]), + Arguments.of("Station info AntenneBY at 5:00 am", rp, "AntenneBY", 5, rp.AntenneBYInfo[0]), + Arguments.of("Station info AntenneBY at 9:00 am", rp, "AntenneBY", 9, rp.AntenneBYInfo[1]), + Arguments.of("Station info AntenneBY at 12:00 am", rp, "AntenneBY", 12, rp.AntenneBYInfo[2]), + Arguments.of("Station info AntenneBY at 3:00 pm", rp, "AntenneBY", 15, rp.AntenneBYInfo[3]), + Arguments.of("Station info AntenneBY at 3:00 am", rp, "AntenneBY", 3, rp.AntenneBYInfo[4]) + ); } @ParameterizedTest @@ -153,7 +152,6 @@ class RadioPlayerTest { rp3.next(); RadioPlayer rp4 = new RadioPlayer(); rp4.next(); - return Stream.of( Arguments.of("Show Station Info for first saved Radio", rp1, rp1.getYouFMInfoByTime(rp1.hour)), Arguments.of("Show Station Info for regional Station", rp2, rp2.getAntenneBYInfoByTime(rp2.hour)), @@ -162,17 +160,6 @@ class RadioPlayerTest { ); } - /* - - @Test - void getInfoText() { - } - - @Test - void getOptions() { - } - */ - @ParameterizedTest @MethodSource("chooseItemOptions") void testChooseItem(String testName, RadioPlayer testRp, String expectedResult) { @@ -196,7 +183,6 @@ class RadioPlayerTest { RadioPlayer rp5 = new RadioPlayer(); rp5.changeRegion("BY"); rp5.chooseItem(-2); - return Stream.of( Arguments.of("Test select station in saved playlist to play with nr isn`t bigger than playlist size", rp, "Teddy"), Arguments.of("Test select station in saved playlist to play with nr is bigger than playlist size. Last station in playlist gets played", rp1, "MegaHits"), @@ -244,7 +230,6 @@ class RadioPlayerTest { rp2.saveStation(); rp3.changeRegion("BY"); rp3.saveStation(); - return Stream.of( Arguments.of("Test for saving station in saved playlist", rp1, "normal", "Antenne Bayern"), Arguments.of("Test for not saving station which is already in saved playlist", rp2, "normal", "MegaHits"), @@ -269,7 +254,6 @@ class RadioPlayerTest { rp1.deleteStation(); rp1.deleteStation(); rp1.deleteStation(); - return Stream.of( Arguments.of("Test for delete station in saved playlist", rp, "delete", false), Arguments.of("Test for not delete station in saved playlist if this is the last station", rp1, "noDelete", true) @@ -289,7 +273,6 @@ class RadioPlayerTest { RadioPlayer rp1 = new RadioPlayer(); rp1.changeRegion("BY"); rp1.changeToSavedPlaylist(); - return Stream.of( Arguments.of("Test for change to saved playlist when you already in saved playlist", rp, "YouFM"), Arguments.of("Test for change to saved playlist when in regional playlist", rp1, "YouFM") @@ -313,7 +296,6 @@ class RadioPlayerTest { RadioPlayer rp2 = new RadioPlayer(); rp2.next(); rp2.changeOrderInSavedPlaylist(-1); - return Stream.of( Arguments.of("Test for change order in saved playlist with nr isn`t bigger than playlist size", rp, 0, "Teddy"), Arguments.of("Test for change order in saved playlist with nr is than bigger playlist size put at the end of playlist", rp1, 2, "YouFM"), @@ -336,7 +318,6 @@ class RadioPlayerTest { RadioPlayer rp = new RadioPlayer(); RadioPlayer rp1 = new RadioPlayer(); rp1.changeRegion("BY"); - return Stream.of( Arguments.of("Test for return saved playlist", rp, "saved", rp.savedPlaylist.toArray(new String[0])), Arguments.of("Test for return regional playlist", rp1, "region", rp1.regionPlaylist.toArray(new String[0])) @@ -354,23 +335,90 @@ class RadioPlayerTest { static Stream muteOptions() { RadioPlayer rp = new RadioPlayer(); - rp.setLautstaerke(25); + rp.setVolume(25); rp.mute(); RadioPlayer rp1 = new RadioPlayer(); - rp1.setLautstaerke(30); + rp1.setVolume(30); rp1.mute(); - return Stream.of( Arguments.of("Test for mute RadioPlayer if volume is actually 0", "actual", rp, 0), Arguments.of("Test for mute RadioPlayer if volume is saved before muting", "saved", rp1, 30) ); } + @Test - void TestUnmute() { + void testUnmute() { RadioPlayer rp = new RadioPlayer(); - rp.setLautstaerke(40); + rp.setVolume(40); rp.mute(); rp.unmute(); assertThat(rp.getVolume()).describedAs("Test if unmute is setting the saved volume").isEqualTo(rp.savedVolume); } + + @ParameterizedTest + @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"); + else bool = testRp.getInfoText().contains("Now playing"); + + assertThat(bool).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream getInfoTextOptions() { + RadioPlayer rp = new RadioPlayer(); + RadioPlayer rp2 = new RadioPlayer(); + rp2.changeRegion("BY"); + RadioPlayer rp4 = new RadioPlayer(); + rp4.next(); + RadioPlayer rp5 = new RadioPlayer(); + rp5.changeRegion("HE"); + rp5.next(); + RadioPlayer rp6 = new RadioPlayer(); + rp6.changeRegion("BY"); + RadioPlayer rp7 = new RadioPlayer(); + rp7.next(); + return Stream.of( + Arguments.of("Test for getInfoText is not empty or null", "null", rp, false), + Arguments.of("Test for getInfoText contains played station", "station", rp, true), + Arguments.of("Test for getInfoText contains regional playlist if played station is in it", "region", rp2, true), + Arguments.of("Test for getInfoText contains saved playlist if played station is in it", "saved", rp, true), + Arguments.of("Test for getInfoText contains station number in saved playlist", "savedNum", rp4, true), + Arguments.of("Test for getInfoText contains station number in regional playlist", "regionNum", rp5, true), + Arguments.of("Test for getInfoText contains station info if station has one", "stationInfo", rp6, true), + 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); + 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() { + RadioPlayer rp = new RadioPlayer(); + 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("Test if all methods declared in interface device are available", "device", rp) + ); + } } \ No newline at end of file