Skip to content

Commit f89296a

Browse files
committed
added more tests to AudioHandlerTest class
1 parent de815d3 commit f89296a

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/com/redomar/game/audio/AudioHandler.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.redomar.game.audio;
22

3+
import com.redomar.game.script.PrintTypes;
4+
import com.redomar.game.script.Printing;
5+
36
import javax.sound.sampled.*;
47
import java.io.File;
58

@@ -8,20 +11,25 @@ public class AudioHandler {
811

912
private Clip clip;
1013
private boolean active = false;
14+
private Printing p = new Printing();
1115

1216
public AudioHandler(String path){
13-
initiate(path);
17+
check(path);
1418
}
1519

1620
public AudioHandler(File file){
21+
check(file.toString());
22+
}
23+
24+
private void check(String path){
1725
try {
18-
if(file.toString() != ""){
19-
initiate(file.toString());
26+
if(path != ""){
27+
initiate(path);
2028
} else {
2129
throw new NullPointerException();
2230
}
2331
} catch (NullPointerException e){
24-
System.err.println("Destination Cannot be empty");
32+
p.print("Destination Cannot be empty", PrintTypes.ERROR);
2533
throw e;
2634
}
2735
}
@@ -44,15 +52,21 @@ private void initiate(String path){
4452
clip.open(decodedAudioInputStream);
4553
} catch (Exception e){
4654
System.err.println(e.getStackTrace());
55+
p.print("Audio Failed to initiate", PrintTypes.ERROR);
4756
}
4857
}
4958

5059
public void play(){
51-
if(clip == null) return;
52-
stop();
53-
clip.setFramePosition(0);
54-
clip.start();
55-
active = true;
60+
try{
61+
if(clip == null) return;
62+
stop();
63+
clip.setFramePosition(0);
64+
clip.start();
65+
active = true;
66+
} catch (Exception e) {
67+
p.print("Audio Failed to play", PrintTypes.ERROR);
68+
throw e;
69+
}
5670
}
5771

5872
public void setVolume(float velocity){

test/com/redomar/game/audio/AudioHandlerTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,15 @@ public void expectReturnExceptionFileEmptyDir(){
2525
AudioHandler audio = new AudioHandler(empty);
2626
}
2727

28+
@Test(expected = NullPointerException.class)
29+
public void expectReturnExceptionFileEmptyPath(){
30+
AudioHandler audio = new AudioHandler("");
31+
}
32+
33+
@Test
34+
public void tryInitiatingAndPlayingNonExistingFile(){
35+
AudioHandler audio = new AudioHandler("//");
36+
audio.play();
37+
}
38+
2839
}

0 commit comments

Comments
 (0)