From 7c7ae70a2b179b0cce12df32f2f06dd9664d16e4 Mon Sep 17 00:00:00 2001 From: JPANZO Date: Thu, 17 Feb 2022 16:13:17 +0100 Subject: [PATCH] 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()) ); }