From 87a0723a68e55d3920d8dcbf5586968ef4abe092 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 02:52:46 +0100 Subject: [PATCH 01/10] Method chooseItem of CDPlayer tested on notSetCD. --- src/main/java/device/cdPlayer/CDPlayer.java | 5 ++++- src/test/java/device/cdPlayer/CDPlayerTest.java | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 57101c8..d70ffca 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -18,7 +18,7 @@ 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; @Override public void louder() { @@ -86,6 +86,9 @@ public class CDPlayer implements Device { @Override public String chooseItem(int itemNr) { + if(this.loadedCD==null){ + throw new ReturnValueNullException(); + } return null; } diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index e8d8f77..b820e8f 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -327,4 +327,17 @@ class CDPlayerTest { ); } -} \ No newline at end of file + @ParameterizedTest + @MethodSource("chooseItemTestData") + void chooseItemTest(String testName,String cases, CDPlayer _cdPlayer,Exception exception , String expectedResult) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.chooseItem(1)); + assertEquals(ReturnValueNullException.class, newException.getClass()); + } + static Stream chooseItemTestData () { + CDPlayer cdPlayer1 = new CDPlayer(); + return Stream.of( + Arguments.of("[chooseItem() by notSetCD ] => shouldThrowReturnValueNullException","Case1", cdPlayer1,new ReturnValueNullException(),"") + ); + } + + } \ No newline at end of file From aac81d6b2272f4a06cd72f444fd13eee407a2d7a Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 03:13:09 +0100 Subject: [PATCH 02/10] Method chooseItem of CDPlayer tested on invalidInput->greater than PlayListSize. --- src/main/java/device/cdPlayer/CDPlayer.java | 3 +++ .../ItemNumberNotFoundException.java | 7 +++++++ .../java/device/cdPlayer/CDPlayerTest.java | 21 ++++++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 src/main/java/device/cdPlayer/exceptions/ItemNumberNotFoundException.java diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index d70ffca..561e456 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -89,6 +89,9 @@ public class CDPlayer implements Device { if(this.loadedCD==null){ throw new ReturnValueNullException(); } + if(!(itemNr>=0 && itemNr _cdPlayer.chooseItem(1)); - assertEquals(ReturnValueNullException.class, newException.getClass()); + 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()); + } } 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 notSetCD ] => shouldThrowReturnValueNullException","Case1", cdPlayer1,new ReturnValueNullException(),""), + Arguments.of("[chooseItem() by invalidInput ] => shouldThrowItemNumberNotFoundException","Case2", cdPlayer2,new ItemNumberNotFoundException(),"") ); } From 1fb89934b18e65d097cfcecc3c86516826b8dd32 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 03:25:32 +0100 Subject: [PATCH 03/10] Method chooseItem of CDPlayer tested on invalidInput->negativeNumber. --- src/main/java/device/cdPlayer/CDPlayer.java | 4 ++++ .../device/cdPlayer/exceptions/NegativeInputException.java | 7 +++++++ src/test/java/device/cdPlayer/CDPlayerTest.java | 7 ++++++- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/main/java/device/cdPlayer/exceptions/NegativeInputException.java diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 561e456..755566d 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -89,9 +89,13 @@ public class CDPlayer implements Device { if(this.loadedCD==null){ throw new ReturnValueNullException(); } + if(itemNr<0){ + throw new NegativeInputException(); + } if(!(itemNr>=0 && itemNr _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()); + } } static Stream chooseItemTestData () { CDPlayer cdPlayer1 = new CDPlayer(); @@ -351,7 +355,8 @@ class CDPlayerTest { 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 ] => shouldThrowItemNumberNotFoundException","Case2", cdPlayer2,new ItemNumberNotFoundException(),""), + Arguments.of("[chooseItem() by invalidInput:negativeNumber] => shouldThrowItemNumberNotFoundException","Case3", cdPlayer2,new NegativeInputException(),"") ); } From 34b14f3e87bd16d5cf39b293969c10615dcdc3b5 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 03:46:21 +0100 Subject: [PATCH 04/10] Method chooseItem of CDPlayer tested on validInput->shouldReturnMsgWithinTheItemNr. --- src/main/java/device/cdPlayer/CDPlayer.java | 2 +- src/test/java/device/cdPlayer/CDPlayerTest.java | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 755566d..99b3b81 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -96,7 +96,7 @@ public class CDPlayer implements Device { throw new ItemNumberNotFoundException(); } - return null; + return loadedCD.getPlayList()[itemNr]; } @Override diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 98c8920..3e68055 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -329,7 +329,7 @@ class CDPlayerTest { @ParameterizedTest @MethodSource("chooseItemTestData") - void chooseItemTest(String testName,String cases, CDPlayer _cdPlayer,Exception exception , String expectedResult) { + 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()); @@ -342,6 +342,9 @@ class CDPlayerTest { 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(); @@ -354,9 +357,10 @@ class CDPlayerTest { 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 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) ); } From 43f556fa6f01e1104ae72e6e8cc9eabaa27649f2 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 13:50:38 +0100 Subject: [PATCH 05/10] Method changePlaySource of CDPlayer tested on activeSource=CDDrive -> shouldReturnCDWechseler. --- src/main/java/device/cdPlayer/CDPlayer.java | 13 ++++++++++--- .../java/device/cdPlayer/CDWechseler.java | 19 +++++++++++++++++++ .../java/device/cdPlayer/CDPlayerTest.java | 16 ++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/main/java/device/cdPlayer/CDWechseler.java diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 99b3b81..827d104 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -20,6 +20,9 @@ public class CDPlayer implements Device { private String actualPlayTrack=""; private CD loadedCD=null; + private CDWechseler cdWechseler; + private String activeSource="CDDrive";//or CDWechseler + @Override public void louder() { if(volume < 100) { @@ -48,9 +51,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 @@ -131,6 +132,10 @@ public class CDPlayer implements Device { } return null; } + public String changePlaySource(){ + this.activeSource="CDWechseler"; + return activeSource; + } //Getters, Setters und SupportMethods public void setCD(CD _loadedCD) { @@ -175,8 +180,10 @@ public class CDPlayer implements Device { this.infoText=this.getClass().getSimpleName()+": Item->"+actualPlayTrack+" from Album->"+this.loadedCD.getAlbumName()+" running."; } - public CD getCD() { return this.loadedCD; } + + + } diff --git a/src/main/java/device/cdPlayer/CDWechseler.java b/src/main/java/device/cdPlayer/CDWechseler.java new file mode 100644 index 0000000..1f408b1 --- /dev/null +++ b/src/main/java/device/cdPlayer/CDWechseler.java @@ -0,0 +1,19 @@ +package device.cdPlayer; + +import java.util.List; + +public class CDWechseler { + private ListCDList; + private boolean running=false; + + + public boolean isRunning() { + return running; + } + + public void activate() { + this.running = true; + } + + +} diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 3e68055..64ea048 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -364,4 +364,20 @@ class CDPlayerTest { ); } + @ParameterizedTest + @MethodSource("changePlaySourceData") + void changePlaySourceTest(String testName,String cases, CDPlayer _cdPlayer) { + String returnVal=_cdPlayer.changePlaySource(); + assertThat(returnVal).describedAs(testName).isEqualTo("CDWechseler"); + } + static Stream changePlaySourceData () { + CDPlayer cdPlayer1 = new CDPlayer(); + + return Stream.of( + Arguments.of("[changePlaySource() by activeSource=CDDrive ] => shouldReturnCDWechseler","Case1", cdPlayer1) + ); + } + + + } \ No newline at end of file From 7faf0d78e227e462194b3bbcd6208688642b911c Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 14:08:31 +0100 Subject: [PATCH 06/10] Method changePlaySource of CDPlayer tested on activeSource=CDWechseler -> shouldReturnCDDrive. --- src/main/java/device/cdPlayer/CDPlayer.java | 5 ++++- src/test/java/device/cdPlayer/CDPlayerTest.java | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 827d104..89f6ad9 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -133,7 +133,10 @@ public class CDPlayer implements Device { return null; } public String changePlaySource(){ - this.activeSource="CDWechseler"; + if(activeSource.equals("CDDrive")) { + this.activeSource = "CDWechseler"; + }else this.activeSource="CDDrive"; + return activeSource; } diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 64ea048..f0ef16a 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -367,14 +367,25 @@ class CDPlayerTest { @ParameterizedTest @MethodSource("changePlaySourceData") void changePlaySourceTest(String testName,String cases, CDPlayer _cdPlayer) { - String returnVal=_cdPlayer.changePlaySource(); - assertThat(returnVal).describedAs(testName).isEqualTo("CDWechseler"); + + 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=CDDrive ] => shouldReturnCDWechseler","Case1", cdPlayer1), + Arguments.of("[changePlaySource() by activeSource=CDWechseler ] => shouldReturnCDDrive","Case2", cdPlayer2) ); } From 29aa121c19fe04d6bf3716be1168a4042243769d Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 14:41:34 +0100 Subject: [PATCH 07/10] Method loadCDWechseler of CDPlayer tested on notRunning=CDWechseler -> shouldThrowAnException. --- src/main/java/device/cdPlayer/CDPlayer.java | 7 ++++++- .../CDWechselerNotRunningException.java | 8 ++++++++ src/test/java/device/cdPlayer/CDPlayerTest.java | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/main/java/device/cdPlayer/exceptions/CDWechselerNotRunningException.java diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 89f6ad9..16f9d66 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -20,7 +20,7 @@ public class CDPlayer implements Device { private String actualPlayTrack=""; private CD loadedCD=null; - private CDWechseler cdWechseler; + private CDWechseler cdWechseler=null; private String activeSource="CDDrive";//or CDWechseler @Override @@ -139,6 +139,11 @@ public class CDPlayer implements Device { return activeSource; } + public void loadCDWechseler(ListcdList){ + if(cdWechseler==null||!cdWechseler.isRunning()){ + throw new CDWechselerNotRunningException(); + } + } //Getters, Setters und SupportMethods public void setCD(CD _loadedCD) { 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/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index f0ef16a..d159604 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -389,6 +389,22 @@ class CDPlayerTest { ); } + @ParameterizedTest + @MethodSource("loadCDWechselerData") + void loadCDWechselerTest(String testName,String cases, CDPlayer _cdPlayer,ListcdList,Exception exception) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cdList)); + assertEquals(CDWechselerNotRunningException.class, newException.getClass()); + } + static Stream loadCDWechselerData () { + CDPlayer cdPlayer1 = new CDPlayer(); + String[] audioPlayList=new String[]{"Audio 01","Audio 02","Audio 03","Audio 04","Audio 05"}; + CD audioCD1=new CD("Audio",audioPlayList); + ListCdList1=Arrays.asList(audioCD1); + + return Stream.of( + Arguments.of("[loadCDWechseler() by notRunning=CDWechseler ] => shouldThrowAnException", "Case1", cdPlayer1,CdList1,new CDWechselerNotRunningException()) + ); + } } \ No newline at end of file From 77ce33d615fbc124bb040a12b6997fbb6a57cfe9 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 15:00:52 +0100 Subject: [PATCH 08/10] Method loadCDWechseler of CDPlayer tested on WrongCDFormat -> shouldThrowAnException. --- src/main/java/device/cdPlayer/CDPlayer.java | 7 ++++++- .../java/device/cdPlayer/CDPlayerTest.java | 21 +++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 16f9d66..da243d6 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -135,14 +135,19 @@ public class CDPlayer implements Device { public String changePlaySource(){ if(activeSource.equals("CDDrive")) { this.activeSource = "CDWechseler"; + this.cdWechseler=new CDWechseler(); + this.cdWechseler.activate(); }else this.activeSource="CDDrive"; return activeSource; } - public void loadCDWechseler(ListcdList){ + public void loadCDWechseler(CD cd){ if(cdWechseler==null||!cdWechseler.isRunning()){ throw new CDWechselerNotRunningException(); } + if(!(this.supportedFormats.contains(cd.getFormat()))){ + throw new FormatNotSupportedException(); + } } //Getters, Setters und SupportMethods diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index d159604..d093081 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -391,18 +391,27 @@ class CDPlayerTest { @ParameterizedTest @MethodSource("loadCDWechselerData") - void loadCDWechselerTest(String testName,String cases, CDPlayer _cdPlayer,ListcdList,Exception exception) { - Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cdList)); - assertEquals(CDWechselerNotRunningException.class, newException.getClass()); + void loadCDWechselerTest(String testName,String cases, CDPlayer _cdPlayer,CD cd,Exception exception) { + if(cases.equals("Case1")) { + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cd)); + assertEquals(CDWechselerNotRunningException.class, newException.getClass()); + } + if(cases.equals("Case2")) { + _cdPlayer.changePlaySource(); + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cd)); + assertEquals(FormatNotSupportedException.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); - ListCdList1=Arrays.asList(audioCD1); + CD audioCD1=new CD("Audio",audioPlayList); + CD DVD=new CD("DVD",videoPlayList); return Stream.of( - Arguments.of("[loadCDWechseler() by notRunning=CDWechseler ] => shouldThrowAnException", "Case1", cdPlayer1,CdList1,new CDWechselerNotRunningException()) + Arguments.of("[loadCDWechseler() by notRunning=CDWechseler ] => shouldThrowAnException", "Case1", cdPlayer1,audioCD1,new CDWechselerNotRunningException()), + Arguments.of("[loadCDWechseler() by WrongCDFormat ] => shouldThrowAnException", "Case2", cdPlayer1,DVD,new FormatNotSupportedException()) ); } From e6741fd092fecad0d9e5864e8af3d27ba7f571d8 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 15:31:27 +0100 Subject: [PATCH 09/10] Method loadCDWechseler of CDPlayer tested on leadingFirstCD -> CDListOfCDWechselerShouldHaveLength1. --- src/main/java/device/cdPlayer/CDPlayer.java | 6 ++++-- src/main/java/device/cdPlayer/CDWechseler.java | 9 ++++++++- src/test/java/device/cdPlayer/CDPlayerTest.java | 9 ++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index da243d6..7e78148 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -148,6 +148,7 @@ public class CDPlayer implements Device { if(!(this.supportedFormats.contains(cd.getFormat()))){ throw new FormatNotSupportedException(); } + cdWechseler.loadOneCD(cd); } //Getters, Setters und SupportMethods @@ -197,6 +198,7 @@ public class CDPlayer implements Device { return this.loadedCD; } - - + public CDWechseler getCdWechseler() { + return cdWechseler; + } } diff --git a/src/main/java/device/cdPlayer/CDWechseler.java b/src/main/java/device/cdPlayer/CDWechseler.java index 1f408b1..40caf90 100644 --- a/src/main/java/device/cdPlayer/CDWechseler.java +++ b/src/main/java/device/cdPlayer/CDWechseler.java @@ -1,9 +1,10 @@ package device.cdPlayer; +import java.util.ArrayList; import java.util.List; public class CDWechseler { - private ListCDList; + private ListCDList=new ArrayList<>(); private boolean running=false; @@ -15,5 +16,11 @@ public class CDWechseler { this.running = true; } + public List getCDList() { + return CDList; + } + public void loadOneCD(CD cd){ + CDList.add(cd); + } } diff --git a/src/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index d093081..81f79d6 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -401,6 +401,12 @@ class CDPlayerTest { Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cd)); assertEquals(FormatNotSupportedException.class, newException.getClass()); } + if(cases.equals("Case3")) { + _cdPlayer.changePlaySource(); + _cdPlayer.loadCDWechseler(cd); + int length=_cdPlayer.getCdWechseler().getCDList().size(); + assertThat(length).describedAs(testName).isEqualTo(1); + } } static Stream loadCDWechselerData () { CDPlayer cdPlayer1 = new CDPlayer(); @@ -411,7 +417,8 @@ class CDPlayerTest { 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 WrongCDFormat ] => shouldThrowAnException", "Case2", cdPlayer1,DVD,new FormatNotSupportedException()), + Arguments.of("[loadCDWechseler() by leadingFirstCD ] => CDListOfCDWechselerShouldHaveLength1", "Case3", cdPlayer1,audioCD1,null) ); } From 7c7ae70a2b179b0cce12df32f2f06dd9664d16e4 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 16:13:17 +0100 Subject: [PATCH 10/10] Method loadCDWechseler of CDPlayer tested on MaxCapacityExceeded:Max=10 -> shouldThrowAnException. --- src/main/java/device/cdPlayer/CDPlayer.java | 8 ++++++-- src/main/java/device/cdPlayer/CDWechseler.java | 8 ++++---- .../MaxCapacityExceededException.java | 7 +++++++ .../java/device/cdPlayer/CDPlayerTest.java | 18 ++++++++++++------ 4 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 src/main/java/device/cdPlayer/exceptions/MaxCapacityExceededException.java diff --git a/src/main/java/device/cdPlayer/CDPlayer.java b/src/main/java/device/cdPlayer/CDPlayer.java index 7e78148..a60009a 100644 --- a/src/main/java/device/cdPlayer/CDPlayer.java +++ b/src/main/java/device/cdPlayer/CDPlayer.java @@ -141,14 +141,18 @@ public class CDPlayer implements Device { return activeSource; } - public void loadCDWechseler(CD cd){ + public void loadCDWechseler(CD cd,int index){ if(cdWechseler==null||!cdWechseler.isRunning()){ throw new CDWechselerNotRunningException(); } if(!(this.supportedFormats.contains(cd.getFormat()))){ throw new FormatNotSupportedException(); } - cdWechseler.loadOneCD(cd); + if(index>10){ + throw new MaxCapacityExceededException(); + } + cdWechseler.loadOneCD(cd,index); + System.out.println(index); } //Getters, Setters und SupportMethods diff --git a/src/main/java/device/cdPlayer/CDWechseler.java b/src/main/java/device/cdPlayer/CDWechseler.java index 40caf90..0ae54e5 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 ListCDList=new ArrayList<>(); + private CD[]CDList=new CD[10]; private boolean running=false; @@ -16,11 +16,11 @@ public class CDWechseler { this.running = true; } - public List getCDList() { + public CD[] getCDList() { return CDList; } - public void loadOneCD(CD cd){ - CDList.add(cd); + public void loadOneCD(CD cd,int index){ + CDList[index]=cd; } } 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/test/java/device/cdPlayer/CDPlayerTest.java b/src/test/java/device/cdPlayer/CDPlayerTest.java index 81f79d6..1eb3043 100644 --- a/src/test/java/device/cdPlayer/CDPlayerTest.java +++ b/src/test/java/device/cdPlayer/CDPlayerTest.java @@ -393,19 +393,24 @@ class CDPlayerTest { @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)); + 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)); + Exception newException = assertThrows(exception.getClass(), () -> _cdPlayer.loadCDWechseler(cd,0)); assertEquals(FormatNotSupportedException.class, newException.getClass()); } if(cases.equals("Case3")) { _cdPlayer.changePlaySource(); - _cdPlayer.loadCDWechseler(cd); - int length=_cdPlayer.getCdWechseler().getCDList().size(); - assertThat(length).describedAs(testName).isEqualTo(1); + _cdPlayer.loadCDWechseler(cd,0); + CD firstCD=_cdPlayer.getCdWechseler().getCDList()[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 () { @@ -418,7 +423,8 @@ class CDPlayerTest { 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 leadingFirstCD ] => CDListOfCDWechselerShouldHaveLength1", "Case3", cdPlayer1,audioCD1,null), + Arguments.of("[loadCDWechseler() by MaxCapacityExceeded:Max=10 ] => shouldThrowAnException", "Case4", cdPlayer1,audioCD1,new MaxCapacityExceededException()) ); }