Skip to content

Commit 6e8eaf7

Browse files
committed
adding some test cases for the event queue
1 parent 152b276 commit 6e8eaf7

3 files changed

Lines changed: 120 additions & 16 deletions

File tree

event-queue/src/main/java/com/iluwatar/event/queue/App.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@
2424
package com.iluwatar.event.queue;
2525

2626
import java.io.BufferedReader;
27-
import java.io.File;
2827
import java.io.IOException;
2928
import java.io.InputStreamReader;
3029

31-
import javax.sound.sampled.AudioInputStream;
32-
import javax.sound.sampled.AudioSystem;
3330
import javax.sound.sampled.UnsupportedAudioFileException;
3431

3532
/**
@@ -51,17 +48,12 @@ public class App {
5148
* @throws UnsupportedAudioFileException when the loaded audio file is unsupported
5249
*/
5350
public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
54-
Audio.playSound(getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
55-
Audio.playSound(getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
51+
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
52+
Audio.playSound(Audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
5653

5754
System.out.println("Press Enter key to stop the program...");
5855
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
5956
br.read();
6057
Audio.stopService();
6158
}
62-
63-
public static AudioInputStream getAudioStream(String filePath)
64-
throws UnsupportedAudioFileException, IOException {
65-
return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
66-
}
6759
}

event-queue/src/main/java/com/iluwatar/event/queue/Audio.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323

2424
package com.iluwatar.event.queue;
2525

26+
import java.io.File;
2627
import java.io.IOException;
28+
import java.lang.Thread.State;
2729

2830
import javax.sound.sampled.AudioInputStream;
2931
import javax.sound.sampled.AudioSystem;
3032
import javax.sound.sampled.Clip;
3133
import javax.sound.sampled.LineUnavailableException;
34+
import javax.sound.sampled.UnsupportedAudioFileException;
3235

3336
/**
3437
* This class implements the Event Queue pattern.
@@ -55,6 +58,17 @@ public static void stopService() {
5558
updateThread.interrupt();
5659
}
5760
}
61+
62+
/**
63+
* This method stops the Update Method's thread.
64+
* @return boolean
65+
*/
66+
public static boolean isServiceRunning() {
67+
if (updateThread != null) {
68+
return updateThread.isAlive();
69+
}
70+
return false;
71+
}
5872

5973
/**
6074
* Starts the thread for the Update Method pattern if it was not started previously.
@@ -86,17 +100,17 @@ public static void playSound(AudioInputStream stream, float volume) {
86100
init();
87101
// Walk the pending requests.
88102
for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) {
89-
if (pendingAudio[i].stream == stream) {
103+
if (getPendingAudio()[i].stream == stream) {
90104
// Use the larger of the two volumes.
91-
pendingAudio[i].volume = Math.max(volume, pendingAudio[i].volume);
105+
getPendingAudio()[i].volume = Math.max(volume, getPendingAudio()[i].volume);
92106

93107
// Don't need to enqueue.
94108
return;
95109
}
96110
}
97-
pendingAudio[tailIndex] = new PlayMessage();
98-
pendingAudio[tailIndex].stream = stream;
99-
pendingAudio[tailIndex].volume = volume;
111+
getPendingAudio()[tailIndex] = new PlayMessage();
112+
getPendingAudio()[tailIndex].stream = stream;
113+
getPendingAudio()[tailIndex].volume = volume;
100114
tailIndex = (tailIndex + 1) % MAX_PENDING;
101115
}
102116

@@ -112,7 +126,7 @@ public static void update() {
112126
Clip clip = null;
113127
try {
114128
clip = AudioSystem.getClip();
115-
clip.open(pendingAudio[headIndex].stream);
129+
clip.open(getPendingAudio()[headIndex].stream);
116130
clip.start();
117131
headIndex++;
118132
} catch (LineUnavailableException e) {
@@ -123,4 +137,25 @@ public static void update() {
123137
e.printStackTrace();
124138
}
125139
}
140+
141+
/**
142+
* Returns the AudioInputStream of a file
143+
* @param filePath is the path of the audio file
144+
* @return AudioInputStream
145+
* @throws UnsupportedAudioFileException when the audio file is not supported
146+
* @throws IOException when the file is not readable
147+
*/
148+
public static AudioInputStream getAudioStream(String filePath)
149+
throws UnsupportedAudioFileException, IOException {
150+
return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
151+
}
152+
153+
/**
154+
* Returns with the message array of the queue
155+
* @return PlayMessage[]
156+
*/
157+
public static PlayMessage[] getPendingAudio() {
158+
return pendingAudio;
159+
}
160+
126161
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.event.queue;
25+
import static org.junit.Assert.*;
26+
27+
import java.io.IOException;
28+
29+
import javax.sound.sampled.UnsupportedAudioFileException;
30+
31+
import org.junit.Test;
32+
33+
/**
34+
* Testing the Audio service of the Queue
35+
* @author mkuprivecz
36+
*
37+
*/
38+
public class AudioTest {
39+
40+
/**
41+
* Test here that the playSound method works correctly
42+
* @throws UnsupportedAudioFileException when the audio file is not supported
43+
* @throws IOException when the file is not readable
44+
* @throws InterruptedException when the test is interrupted externally
45+
*/
46+
@Test
47+
public void testPlaySound() throws UnsupportedAudioFileException, IOException, InterruptedException {
48+
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
49+
// test that service is started
50+
assertTrue(Audio.isServiceRunning());
51+
// adding a small pause to be sure that the sound is ended
52+
Thread.sleep(5000);
53+
// test that service is finished
54+
assertFalse(!Audio.isServiceRunning());
55+
}
56+
57+
/**
58+
* Test here that the Queue
59+
* @throws UnsupportedAudioFileException when the audio file is not supported
60+
* @throws IOException when the file is not readable
61+
* @throws InterruptedException when the test is interrupted externally
62+
*/
63+
@Test
64+
public void testQueue() throws UnsupportedAudioFileException, IOException, InterruptedException {
65+
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
66+
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
67+
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
68+
assertTrue(Audio.getPendingAudio().length > 0);
69+
// test that service is started
70+
assertTrue(Audio.isServiceRunning());
71+
// adding a small pause to be sure that the sound is ended
72+
Thread.sleep(10000);
73+
// test that service is finished
74+
assertFalse(!Audio.isServiceRunning());
75+
}
76+
77+
}

0 commit comments

Comments
 (0)