From 002db4c71f38e21d9528a63b57b96c2cf3ad45ed Mon Sep 17 00:00:00 2001 From: sahar Date: Wed, 16 Feb 2022 22:52:06 +0100 Subject: [PATCH] USB_StickTest.getAlbumTest Second Case --- .../java/device/usbPlayer/NoFolderFound.java | 8 ++++ src/main/java/device/usbPlayer/USB_Stick.java | 14 ++++++- src/main/java/device/usbPlayer/UsbPlayer.java | 6 ++- .../java/device/usbPlayer/USB_StickTest.java | 41 +++++++++++++------ 4 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 src/main/java/device/usbPlayer/NoFolderFound.java diff --git a/src/main/java/device/usbPlayer/NoFolderFound.java b/src/main/java/device/usbPlayer/NoFolderFound.java new file mode 100644 index 0000000..5d22d07 --- /dev/null +++ b/src/main/java/device/usbPlayer/NoFolderFound.java @@ -0,0 +1,8 @@ +package device.usbPlayer; + +public class NoFolderFound extends Exception{ + public NoFolderFound(String message) + { + super(message); + } +} diff --git a/src/main/java/device/usbPlayer/USB_Stick.java b/src/main/java/device/usbPlayer/USB_Stick.java index c3d3557..cb15e82 100644 --- a/src/main/java/device/usbPlayer/USB_Stick.java +++ b/src/main/java/device/usbPlayer/USB_Stick.java @@ -1,6 +1,7 @@ package device.usbPlayer; import java.util.ArrayList; +import device.usbPlayer.NoFolderFound; public class USB_Stick { @@ -34,11 +35,20 @@ public class USB_Stick { currentAlbum = 0; + } + USB_Stick(String a){ } - public ArrayList getAlbum(){ - return Folders.get(currentAlbum); + public ArrayList getAlbum() throws NoFolderFound{ + if(Folders.size()==0) { + System.out.println("list ist leer"); + throw new NoFolderFound("The USB Drive is empty"); + } + else { + System.out.println("nicht leer"); + return Folders.get(currentAlbum); + } } public void ChangeAlbum(){ diff --git a/src/main/java/device/usbPlayer/UsbPlayer.java b/src/main/java/device/usbPlayer/UsbPlayer.java index 7da474a..a21bca5 100644 --- a/src/main/java/device/usbPlayer/UsbPlayer.java +++ b/src/main/java/device/usbPlayer/UsbPlayer.java @@ -69,7 +69,11 @@ public class UsbPlayer implements Device { Playlist.add("Musik 02.mp3"); Playlist.add("Musik 03.mp3"); playTrack = Playlist.get(0);*/ - Playlist = USBDrive.getAlbum(); + try { + Playlist = USBDrive.getAlbum(); + } catch (NoFolderFound e) { + e.printStackTrace(); + } playTrack = Playlist.get(0); } diff --git a/src/test/java/device/usbPlayer/USB_StickTest.java b/src/test/java/device/usbPlayer/USB_StickTest.java index 4c6f671..8d21050 100644 --- a/src/test/java/device/usbPlayer/USB_StickTest.java +++ b/src/test/java/device/usbPlayer/USB_StickTest.java @@ -2,42 +2,59 @@ package device.usbPlayer; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.MethodSource;//hasMessageMatching import java.util.stream.Stream; import java.util.ArrayList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; +import static org.assertj.core.api.AssertionsForClassTypes.catchThrowable; +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; class USB_StickTest { @ParameterizedTest @MethodSource("getAlbumOptions") - void getAlbumTest(String testName, ArrayList album,int TestNr) {//, int expectedResult - //int volumeNum = inputPlay.getLautstaerke(); - if(TestNr == 1) - assertThat(album).describedAs(testName).isNotEmpty(); + void getAlbumTest(String testName, USB_Stick s, int TestNr) throws NoFolderFound { + ArrayList album =new ArrayList(); + + if (TestNr == 1){ + album = s.getAlbum(); + assertThat(album).describedAs(testName).isNotEmpty();} + else { + // when + Throwable thrown = catchThrowable(() -> s.getAlbum()); + + // then + assertThat(thrown) + .isInstanceOf(NoFolderFound.class) + .hasMessageContaining("The USB Drive is empty"); + + } + } - static Stream getAlbumOptions() { + static Stream getAlbumOptions() throws NoFolderFound { USB_Stick stick = new USB_Stick(); - ArrayList AlbumsItems = new ArrayList<>(); - AlbumsItems= stick.getAlbum(); - + USB_Stick emptyStick = new USB_Stick(""); return Stream.of( - Arguments.of("First Album is loaded", stick.getAlbum(),1)//,something + Arguments.of("First Album is loaded", stick, 1), + Arguments.of("No Album is loaded", emptyStick, 2) ); } //ChangeAlbum @ParameterizedTest @MethodSource("ChangeAlbumOptions") - void ChangeAlbumTest(String testName, USB_Stick s) {//, int expectedResult + void ChangeAlbumTest(String testName, USB_Stick s) throws NoFolderFound { ArrayList a = new ArrayList(); a = s.getAlbum(); - assertThat(a).describedAs(testName).contains("File 01.txt","File 02.txt","File 03.txt"); + assertThat(a).describedAs(testName).contains("File 01.txt", "File 02.txt", "File 03.txt"); } static Stream ChangeAlbumOptions() {