From 3d0f3997f212333fc70adf6c30f83fbb4abd05d8 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 17:11:12 +0100 Subject: [PATCH 1/4] Method changeToNextCD of CDPlayer tested on CDWechselerNotSelected -> shouldThrowAnException. --- src/main/java/device/cdPlayer/CDPlayer.java | 6 ++++++ .../ChangeCDOnlyOnCDWechselerException.java | 7 +++++++ src/test/java/device/cdPlayer/CDPlayerTest.java | 15 +++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/main/java/device/cdPlayer/exceptions/ChangeCDOnlyOnCDWechselerException.java diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index a60009a..90a1f92 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -154,6 +154,12 @@ public class CDPlayer implements Device { cdWechseler.loadOneCD(cd,index); System.out.println(index); } + public void changeToNextCD(){ + if(activeSource.equals("CDDrive")){ + throw new ChangeCDOnlyOnCDWechselerException(); + } + + } //Getters, Setters und SupportMethods public void setCD(CD _loadedCD) { 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/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 1eb3043..add6d31 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -428,5 +428,20 @@ class CDPlayerTest { ); } + @ParameterizedTest + @MethodSource("changeToNextCDData") + void changeToNextCDTest(String testName,String cases, CDPlayer _cdPlayer,Exception exception) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.changeToNextCD()); + assertEquals(ChangeCDOnlyOnCDWechselerException.class, newException.getClass()); + } + static Stream changeToNextCDData () { + CDPlayer cdPlayer1 = new CDPlayer(); + + return Stream.of( + Arguments.of("[changeToNextCD() by CDWechselerNotSelected ] => shouldThrowAnException", "Case1", cdPlayer1,new ChangeCDOnlyOnCDWechselerException()) + ); + } + + } \ No newline at end of file From f77ed10d417d734210ef49e234b5e5dfa405bb17 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 17:41:24 +0100 Subject: [PATCH 2/4] Method changeToNextCD of CDPlayer tested checking the effect -> loadedCDShouldNotReturnNull. --- src/main/java/device/cdPlayer/CDPlayer.java | 2 +- .../java/device/cdPlayer/CDPlayerTest.java | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 90a1f92..724c2b4 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -158,7 +158,7 @@ public class CDPlayer implements Device { if(activeSource.equals("CDDrive")){ throw new ChangeCDOnlyOnCDWechselerException(); } - + this.loadedCD=cdWechseler.getCDList()[1]; } //Getters, Setters und SupportMethods diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index add6d31..20cbc8d 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -431,14 +431,31 @@ class CDPlayerTest { @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); + } } static Stream changeToNextCDData () { CDPlayer cdPlayer1 = new CDPlayer(); + CDPlayer cdPlayer2 = new CDPlayer(); + cdPlayer2.changePlaySource(); + + String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"}; + CD audioCD1=new CD("Audio",audioPlayList); + cdPlayer2.loadCDWechseler(audioCD1,0); + cdPlayer2.loadCDWechseler(audioCD1,1); + return Stream.of( - Arguments.of("[changeToNextCD() by CDWechselerNotSelected ] => shouldThrowAnException", "Case1", cdPlayer1,new ChangeCDOnlyOnCDWechselerException()) + Arguments.of("[changeToNextCD() by CDWechselerNotSelected ] => shouldThrowAnException", "Case1", cdPlayer1,new ChangeCDOnlyOnCDWechselerException()), + Arguments.of("[changeToNextCD() checking the effect ] => loadedCDShouldNotReturnNull", "Case2", cdPlayer2,null) ); } From b3070125906cd0e55fb274c0abcd18678eb7aa79 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 18:57:02 +0100 Subject: [PATCH 3/4] Method changeToNextCD of CDPlayer tested checking the effect2 -> loadedCDShouldNotHaveTheSameIdAsPrev. --- src/main/java/device/cdPlayer/CD.java | 16 ++++++++++ src/main/java/device/cdPlayer/CDPlayer.java | 2 +- .../java/device/cdPlayer/CDPlayerTest.java | 29 +++++++++++++++---- 3 files changed, 41 insertions(+), 6 deletions(-) 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 724c2b4..ea05053 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -152,7 +152,7 @@ public class CDPlayer implements Device { throw new MaxCapacityExceededException(); } cdWechseler.loadOneCD(cd,index); - System.out.println(index); + this.loadedCD=cdWechseler.getCDList()[0]; } public void changeToNextCD(){ if(activeSource.equals("CDDrive")){ diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 20cbc8d..8bb6f90 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -441,21 +441,40 @@ class CDPlayerTest { CD nextCD=_cdPlayer.getCD(); assertThat(nextCD).describedAs(testName).isNotEqualTo(null); } + if(cases.equals("Case3")) { + CD actualCD=_cdPlayer.getCD(); + System.out.println("loadedCD_id:"+actualCD.getId()); + _cdPlayer.changeToNextCD(); + CD nextCD=_cdPlayer.getCD(); + System.out.println("nextCD_id:"+nextCD.getId()); + /*System.out.println("loadedCD_id:"+_cdPlayer.getCD().getId()); + System.out.println("actualCD_id:"+_cdPlayer.getCdWechseler().getCDList()[0].getId()); + System.out.println("nextCD_id:"+_cdPlayer.getCdWechseler().getCDList()[1].getId());*/ + //System.out.println("nextCD_id:"+nextCD.getId()); + assertThat(nextCD.getId()==actualCD.getId()).describedAs(testName).isNotEqualTo(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"); + CDPlayer cdPlayer1 = new CDPlayer(); CDPlayer cdPlayer2 = new CDPlayer(); cdPlayer2.changePlaySource(); - - String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"}; - CD audioCD1=new CD("Audio",audioPlayList); cdPlayer2.loadCDWechseler(audioCD1,0); - cdPlayer2.loadCDWechseler(audioCD1,1); + cdPlayer2.loadCDWechseler(audioCD2,1); + + CDPlayer cdPlayer3 = new CDPlayer(); + cdPlayer3.changePlaySource(); + cdPlayer3.loadCDWechseler(audioCD1,0); + cdPlayer3.loadCDWechseler(audioCD2,1); 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 ] => loadedCDShouldNotReturnNull", "Case2", cdPlayer2,null), + Arguments.of("[changeToNextCD() checking the effect 2 ] => loadedCDShouldNotHaveTheSameIdAsPrev", "Case3", cdPlayer3,null) ); } From 2e2be94402cadfa3ff2ff9b2a80e2ac4825c823c Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 20:06:25 +0100 Subject: [PATCH 4/4] Method changeToNextCD of CDPlayer tested changing_from_last_to_first CD -> FirstCDShouldBeTaken. --- src/main/java/device/cdPlayer/CDPlayer.java | 8 +++-- .../java/device/cdPlayer/CDWechseler.java | 8 ++--- .../java/device/cdPlayer/CDPlayerTest.java | 29 ++++++++++++++----- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index ea05053..8d4a53b 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -152,13 +152,17 @@ public class CDPlayer implements Device { throw new MaxCapacityExceededException(); } cdWechseler.loadOneCD(cd,index); - this.loadedCD=cdWechseler.getCDList()[0]; + this.loadedCD=cdWechseler.getCDList().get(0); } public void changeToNextCD(){ if(activeSource.equals("CDDrive")){ throw new ChangeCDOnlyOnCDWechselerException(); } - this.loadedCD=cdWechseler.getCDList()[1]; + + List CDList = cdWechseler.getCDList(); + int indexOfLoadedCD=CDList.indexOf(loadedCD); + int nextIndex=(indexOfLoadedCD+1)%CDList.size(); + this.loadedCD=CDList.get(nextIndex); } //Getters, Setters und SupportMethods diff --git a/src/main/java/device/cdPlayer/CDWechseler.java b/src/main/java/device/cdPlayer/CDWechseler.java index 0ae54e5..fca17d6 100644 --- a/src/main/java/device/cdPlayer/CDWechseler.java +++ b/src/main/java/device/cdPlayer/CDWechseler.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.List; public class CDWechseler { - private CD[]CDList=new CD[10]; + private List CDList=new ArrayList<>(); private boolean running=false; @@ -16,11 +16,11 @@ public class CDWechseler { this.running = true; } - public CD[] getCDList() { + public List getCDList() { return CDList; } - public void loadOneCD(CD cd,int index){ - CDList[index]=cd; + public void loadOneCD(CD cd, int index){ + CDList.add(cd); } } diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 8bb6f90..a3e3ebf 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -404,7 +404,7 @@ class CDPlayerTest { if(cases.equals("Case3")) { _cdPlayer.changePlaySource(); _cdPlayer.loadCDWechseler(cd,0); - CD firstCD=_cdPlayer.getCdWechseler().getCDList()[0]; + CD firstCD=_cdPlayer.getCdWechseler().getCDList().get(0); assertThat(firstCD).describedAs(testName).isNotEqualTo(null); } if(cases.equals("Case4")) { @@ -443,21 +443,24 @@ class CDPlayerTest { } if(cases.equals("Case3")) { CD actualCD=_cdPlayer.getCD(); - System.out.println("loadedCD_id:"+actualCD.getId()); _cdPlayer.changeToNextCD(); CD nextCD=_cdPlayer.getCD(); - System.out.println("nextCD_id:"+nextCD.getId()); - /*System.out.println("loadedCD_id:"+_cdPlayer.getCD().getId()); - System.out.println("actualCD_id:"+_cdPlayer.getCdWechseler().getCDList()[0].getId()); - System.out.println("nextCD_id:"+_cdPlayer.getCdWechseler().getCDList()[1].getId());*/ - //System.out.println("nextCD_id:"+nextCD.getId()); 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(); @@ -465,16 +468,26 @@ class CDPlayerTest { 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("[changeToNextCD() checking the effect 2 ] => loadedCDShouldNotHaveTheSameIdAsPrev", "Case3", cdPlayer3,null), + Arguments.of("[3x changeToNextCD() changing from last to first CD] => FirstCDShouldBeTaken", "Case4", cdPlayer4,null) ); }