22 Commits

Author SHA1 Message Date
JPANZO 7c7ae70a2b Method loadCDWechseler of CDPlayer tested on MaxCapacityExceeded:Max=10 -> shouldThrowAnException. 3 years ago
JPANZO e6741fd092 Method loadCDWechseler of CDPlayer tested on leadingFirstCD -> CDListOfCDWechselerShouldHaveLength1. 3 years ago
JPANZO 77ce33d615 Method loadCDWechseler of CDPlayer tested on WrongCDFormat -> shouldThrowAnException. 3 years ago
JPANZO 29aa121c19 Method loadCDWechseler of CDPlayer tested on notRunning=CDWechseler -> shouldThrowAnException. 3 years ago
JPANZO 7faf0d78e2 Method changePlaySource of CDPlayer tested on activeSource=CDWechseler -> shouldReturnCDDrive. 3 years ago
JPANZO 43f556fa6f Method changePlaySource of CDPlayer tested on activeSource=CDDrive -> shouldReturnCDWechseler. 3 years ago
JPANZO 34b14f3e87 Method chooseItem of CDPlayer tested on validInput->shouldReturnMsgWithinTheItemNr. 3 years ago
JPANZO 1fb89934b1 Method chooseItem of CDPlayer tested on invalidInput->negativeNumber. 3 years ago
JPANZO aac81d6b22 Method chooseItem of CDPlayer tested on invalidInput->greater than PlayListSize. 3 years ago
JPANZO 87a0723a68 Method chooseItem of CDPlayer tested on notSetCD. 3 years ago
Jenkins 603b2e0029 Merge commit '33d834e7022e8fbebbe8f15124da3e92400910f6' into HEAD 3 years ago
sahar 33d834e702 Merge branch 'master' into feature-pr-USB-otherTests 3 years ago
sahar 002db4c71f USB_StickTest.getAlbumTest Second Case 3 years ago
Jenkins 1b557c8ad3 Merge commit 'dff29bea43d8fa550cb061fe11dbc1d095eecd6d' into HEAD 3 years ago
sahar 88078ff752 USB_StickTest.ChangeAlbumTest First Case 3 years ago
sahar d1b481b3d0 USB_StickTest.getAlbumTest First Case 3 years ago
sahar 85e3d47217 Changes in the Structure if device.usbPlayer , USB_Stick.java was added and changes in the UsbPlayer.java 3 years ago
sahar 308057ae4b UsbPlayerTest.setPlayTrackTest Third Case 3 years ago
sahar 648baabab0 UsbPlayerTest.setPlayTrackTest Second Case 3 years ago
sahar 95977c9ae7 UsbPlayerTest.setPlayTrackTest first Case 3 years ago
sahar 945bb15abf UsbPlayerTest.GoToLastTest third Case 3 years ago
sahar 9bf794bbea UsbPlayerTest.GoToLastTest fiSecond Case 3 years ago
  1. 46
      src/main/java/device/cdPlayer/CDPlayer.java
  2. 26
      src/main/java/device/cdPlayer/CDWechseler.java
  3. 8
      src/main/java/device/cdPlayer/exceptions/CDWechselerNotRunningException.java
  4. 7
      src/main/java/device/cdPlayer/exceptions/ItemNumberNotFoundException.java
  5. 7
      src/main/java/device/cdPlayer/exceptions/MaxCapacityExceededException.java
  6. 7
      src/main/java/device/cdPlayer/exceptions/NegativeInputException.java
  7. 8
      src/main/java/device/usbPlayer/NoFolderFound.java
  8. 60
      src/main/java/device/usbPlayer/USB_Stick.java
  9. 42
      src/main/java/device/usbPlayer/UsbPlayer.java
  10. 104
      src/test/java/device/cdPlayer/CDPlayerTest.java
  11. 71
      src/test/java/device/usbPlayer/USB_StickTest.java
  12. 39
      src/test/java/device/usbPlayer/UsbPlayerTest.java

46
src/main/java/device/cdPlayer/CDPlayer.java

@ -18,7 +18,10 @@ public class CDPlayer implements Device {
private List<String> supportedFormats = new ArrayList<String>(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
@Override
public void louder() {
@ -48,9 +51,7 @@ public class CDPlayer implements Device {
List<String> 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 +87,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 && itemNr<loadedCD.getPlayList().length)){
throw new ItemNumberNotFoundException();
}
return loadedCD.getPlayList()[itemNr];
}
@Override
@ -121,6 +132,28 @@ public class CDPlayer implements Device {
}
return null;
}
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(CD cd,int index){
if(cdWechseler==null||!cdWechseler.isRunning()){
throw new CDWechselerNotRunningException();
}
if(!(this.supportedFormats.contains(cd.getFormat()))){
throw new FormatNotSupportedException();
}
if(index>10){
throw new MaxCapacityExceededException();
}
cdWechseler.loadOneCD(cd,index);
System.out.println(index);
}
//Getters, Setters und SupportMethods
public void setCD(CD _loadedCD) {
@ -165,8 +198,11 @@ 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;
}
}

26
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 CD[]CDList=new CD[10];
private boolean running=false;
public boolean isRunning() {
return running;
}
public void activate() {
this.running = true;
}
public CD[] getCDList() {
return CDList;
}
public void loadOneCD(CD cd,int index){
CDList[index]=cd;
}
}

8
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.");
}
}

7
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.");
}
}

7
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.");
}
}

7
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.");
}
}

8
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);
}
}

60
src/main/java/device/usbPlayer/USB_Stick.java

@ -0,0 +1,60 @@
package device.usbPlayer;
import java.util.ArrayList;
import device.usbPlayer.NoFolderFound;
public class USB_Stick {
public ArrayList<ArrayList<String>> Folders = new ArrayList<>();
int currentAlbum ;
USB_Stick() {
ArrayList<String> a1 = new ArrayList<String>();
a1.add("Musik 01.mp3");
a1.add("Musik 02.mp3");
a1.add("Musik 03.mp3");
Folders.add(0,a1);
ArrayList<String> a2 = new ArrayList<String>();
a2.add("File 01.txt");
a2.add("File 02.txt");
a2.add("File 03.txt");
Folders.add(1,a2);
ArrayList<String> a3 = new ArrayList<String>();
a3.add("Photo 01.gif");
a3.add("Photo 02.gif");
a3.add("Photo 03.gif");
Folders.add(2,a3);
ArrayList<String> a4 = new ArrayList<String>();
a4.add("Music 01.mp3");
a4.add("Music 02.mp3");
a4.add("Music 03.mp3");
Folders.add(3,a4);
currentAlbum = 0;
}
USB_Stick(String a){
}
public ArrayList<String> 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(){
++currentAlbum;
}
}

42
src/main/java/device/usbPlayer/UsbPlayer.java

@ -6,13 +6,14 @@ import java.util.ArrayList;
public class UsbPlayer implements Device {
private ArrayList<String> Playlist = new ArrayList<String>();
private String playTrack = "";
private Boolean isConnected;
private Boolean isDefect;
int Lautstaerke = 0;
public USB_Stick USBDrive = new USB_Stick();
//*********setters und getters********
public ArrayList<String> getPlaylist() {
return Playlist;
}
@ -43,7 +44,9 @@ public class UsbPlayer implements Device {
}
public void setPlayTrack(String playTrack) {
if (this.findSong(playTrack) != -1) {
this.playTrack = playTrack;
} else System.out.println("The Song does not exist in the Albume");
}
@ -62,12 +65,19 @@ public class UsbPlayer implements Device {
super();
isDefect = false;
isConnected = true;
Playlist.add("Musik 01.mp3");
/*Playlist.add("Musik 01.mp3");
Playlist.add("Musik 02.mp3");
Playlist.add("Musik 03.mp3");
playTrack = Playlist.get(0);*/
try {
Playlist = USBDrive.getAlbum();
} catch (NoFolderFound e) {
e.printStackTrace();
}
playTrack = Playlist.get(0);
}
//**********search method*************
public boolean isAllAudio() {
@ -76,7 +86,7 @@ public class UsbPlayer implements Device {
for (int i = 0; i < Playlist.size(); i++) {
song = Playlist.get(i).substring(Playlist.get(i).length() - 4);
if (song.equals(".mp3") | song.equals(".mp4")| song.equals(".m4a")| song.equals(".wav")| song.equals(".wma")| song.equals(".aac") ) {
if (song.equals(".mp3") | song.equals(".mp4") | song.equals(".m4a") | song.equals(".wav") | song.equals(".wma") | song.equals(".aac")) {
flag = true;
} else {
flag = false;
@ -85,10 +95,12 @@ public class UsbPlayer implements Device {
}
return flag;
}
//**********search method*************
@Override
public void quieter() {
if(Lautstaerke > 0) {
if (Lautstaerke > 0) {
Lautstaerke -= 1;
} else {
Lautstaerke = 0;
@ -97,21 +109,21 @@ public class UsbPlayer implements Device {
@Override
public void louder() {
if(Lautstaerke < 100) {
if (Lautstaerke < 100) {
Lautstaerke += 1;
} else {
Lautstaerke = 100;
}
}
public void Louder_10(){
public void Louder_10() {
Lautstaerke += 10;
if (Lautstaerke > 100) {
Lautstaerke = 100;
}
}
public void quieter_10(){
public void quieter_10() {
Lautstaerke -= 10;
if (Lautstaerke < 0) {
Lautstaerke = 0;
@ -198,15 +210,15 @@ public class UsbPlayer implements Device {
}
public int findSong(String songName){
public int findSong(String songName) {
int songIndex =-1, index=-1;
int songIndex = -1, index = -1;
String song;
for (int j = 0; j < getPlaylist().size(); j++) {
song = getPlaylist().get(j);
songIndex=song.toLowerCase().indexOf(songName.toLowerCase());
songIndex = song.toLowerCase().indexOf(songName.toLowerCase());
if (songIndex != -1) {
index = j;
@ -216,12 +228,11 @@ public class UsbPlayer implements Device {
return index;
}
public boolean GoTo(String songName){
if(findSong(songName) != -1){
public boolean GoTo(String songName) {
if (findSong(songName) != -1) {
playTrack = getPlaylist().get(findSong(songName));//songName;
return true;
}
else
} else
//playTrack = songName;
return false;
}
@ -232,6 +243,7 @@ public class UsbPlayer implements Device {
}
public void GoToLast() {
setPlayTrack("Musik 03.mp3");
//go to the last Song
setPlayTrack(this.getPlaylist().get(this.getPlaylist().size() - 1));
}
}

104
src/test/java/device/cdPlayer/CDPlayerTest.java

@ -327,4 +327,106 @@ class CDPlayerTest {
);
}
}
@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<Arguments> 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<Arguments> 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()[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<Arguments> 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())
);
}
}

71
src/test/java/device/usbPlayer/USB_StickTest.java

@ -0,0 +1,71 @@
package device.usbPlayer;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
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, USB_Stick s, int TestNr) throws NoFolderFound {
ArrayList<String> album =new ArrayList<String>();
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<Arguments> getAlbumOptions() throws NoFolderFound {
USB_Stick stick = new USB_Stick();
USB_Stick emptyStick = new USB_Stick("");
return Stream.of(
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) throws NoFolderFound {
ArrayList<String> a = new ArrayList<String>();
a = s.getAlbum();
assertThat(a).describedAs(testName).contains("File 01.txt", "File 02.txt", "File 03.txt");
}
static Stream<Arguments> ChangeAlbumOptions() {
USB_Stick stick = new USB_Stick();
ArrayList<String> AlbumsItems = new ArrayList<>();
stick.ChangeAlbum();
return Stream.of(
Arguments.of("First Album is loaded", stick)//,something
);
}
}

39
src/test/java/device/usbPlayer/UsbPlayerTest.java

@ -348,9 +348,46 @@ class UsbPlayerTest {
UsbPlayer up1 = new UsbPlayer();
up1.GoToLast();
UsbPlayer up2 = new UsbPlayer();
up2.GoToLast();
up2.GoToLast();
UsbPlayer up3 = new UsbPlayer();
up3.next();
up3.GoToLast();
return Stream.of(
Arguments.of("It should go to the Last Song ", up1,up1.getPlaylist().get(up1.getPlaylist().size()-1)),
Arguments.of("It should go to the Last Song even after calling the function twice ", up2,up2.getPlaylist().get(up2.getPlaylist().size()-1)),
Arguments.of("It should go to the Last Song even changing the current song ", up3,up3.getPlaylist().get(up3.getPlaylist().size()-1))
);
}
@ParameterizedTest
@MethodSource("setPlayTrackOptions")
void setPlayTrackTest(String testName, UsbPlayer inputUSB, String expectedResult) {
String firstSong = inputUSB.getPlayTrack();
assertThat(firstSong ).describedAs(testName).isEqualTo(expectedResult);
}
static Stream<Arguments> setPlayTrackOptions() {
//first case
UsbPlayer up1 = new UsbPlayer();
up1.setPlayTrack("Musik 02.mp3");
//Second Case
UsbPlayer up2 = new UsbPlayer();
up2.setPlayTrack("Musik 02.mp3");
//Third Case
UsbPlayer up3 = new UsbPlayer();
up3.setPlayTrack("Musik 04.mp3");
return Stream.of(
Arguments.of("It should go to the first Song ", up1,"Musik 03.mp3")
Arguments.of("It should change the current Song ", up1,"Musik 02.mp3"),
Arguments.of("It should change the current Song if it is in the Albume", up2,"Musik 02.mp3"),
Arguments.of("Tried to change the Song,but the Song name does not exist in the Album(No change)", up3,"Musik 01.mp3")
);
}
Loading…
Cancel
Save