From 6e8eaf7593bf7ab21865ed035287bc00aa7262f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mih=C3=A1ly=20Kuprivecz?= Date: Fri, 28 Apr 2017 10:38:40 +0200 Subject: [PATCH] adding some test cases for the event queue --- .../java/com/iluwatar/event/queue/App.java | 12 +-- .../java/com/iluwatar/event/queue/Audio.java | 47 +++++++++-- .../com/iluwatar/event/queue/AudioTest.java | 77 +++++++++++++++++++ 3 files changed, 120 insertions(+), 16 deletions(-) create mode 100644 event-queue/src/test/java/com/iluwatar/event/queue/AudioTest.java diff --git a/event-queue/src/main/java/com/iluwatar/event/queue/App.java b/event-queue/src/main/java/com/iluwatar/event/queue/App.java index a84027ecb..ea107d6ca 100644 --- a/event-queue/src/main/java/com/iluwatar/event/queue/App.java +++ b/event-queue/src/main/java/com/iluwatar/event/queue/App.java @@ -24,12 +24,9 @@ package com.iluwatar.event.queue; import java.io.BufferedReader; -import java.io.File; import java.io.IOException; import java.io.InputStreamReader; -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; import javax.sound.sampled.UnsupportedAudioFileException; /** @@ -51,17 +48,12 @@ public class App { * @throws UnsupportedAudioFileException when the loaded audio file is unsupported */ public static void main(String[] args) throws UnsupportedAudioFileException, IOException { - Audio.playSound(getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); - Audio.playSound(getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f); + Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); + Audio.playSound(Audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f); System.out.println("Press Enter key to stop the program..."); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br.read(); Audio.stopService(); } - - public static AudioInputStream getAudioStream(String filePath) - throws UnsupportedAudioFileException, IOException { - return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); - } } diff --git a/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java b/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java index e00cd375a..ecd535b49 100644 --- a/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java +++ b/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java @@ -23,12 +23,15 @@ package com.iluwatar.event.queue; +import java.io.File; import java.io.IOException; +import java.lang.Thread.State; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; /** * This class implements the Event Queue pattern. @@ -55,6 +58,17 @@ public class Audio { updateThread.interrupt(); } } + + /** + * This method stops the Update Method's thread. + * @return boolean + */ + public static boolean isServiceRunning() { + if (updateThread != null) { + return updateThread.isAlive(); + } + return false; + } /** * Starts the thread for the Update Method pattern if it was not started previously. @@ -86,17 +100,17 @@ public class Audio { init(); // Walk the pending requests. for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) { - if (pendingAudio[i].stream == stream) { + if (getPendingAudio()[i].stream == stream) { // Use the larger of the two volumes. - pendingAudio[i].volume = Math.max(volume, pendingAudio[i].volume); + getPendingAudio()[i].volume = Math.max(volume, getPendingAudio()[i].volume); // Don't need to enqueue. return; } } - pendingAudio[tailIndex] = new PlayMessage(); - pendingAudio[tailIndex].stream = stream; - pendingAudio[tailIndex].volume = volume; + getPendingAudio()[tailIndex] = new PlayMessage(); + getPendingAudio()[tailIndex].stream = stream; + getPendingAudio()[tailIndex].volume = volume; tailIndex = (tailIndex + 1) % MAX_PENDING; } @@ -112,7 +126,7 @@ public class Audio { Clip clip = null; try { clip = AudioSystem.getClip(); - clip.open(pendingAudio[headIndex].stream); + clip.open(getPendingAudio()[headIndex].stream); clip.start(); headIndex++; } catch (LineUnavailableException e) { @@ -123,4 +137,25 @@ public class Audio { e.printStackTrace(); } } + + /** + * Returns the AudioInputStream of a file + * @param filePath is the path of the audio file + * @return AudioInputStream + * @throws UnsupportedAudioFileException when the audio file is not supported + * @throws IOException when the file is not readable + */ + public static AudioInputStream getAudioStream(String filePath) + throws UnsupportedAudioFileException, IOException { + return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile()); + } + + /** + * Returns with the message array of the queue + * @return PlayMessage[] + */ + public static PlayMessage[] getPendingAudio() { + return pendingAudio; + } + } diff --git a/event-queue/src/test/java/com/iluwatar/event/queue/AudioTest.java b/event-queue/src/test/java/com/iluwatar/event/queue/AudioTest.java new file mode 100644 index 000000000..4b3bd19ed --- /dev/null +++ b/event-queue/src/test/java/com/iluwatar/event/queue/AudioTest.java @@ -0,0 +1,77 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.event.queue; +import static org.junit.Assert.*; + +import java.io.IOException; + +import javax.sound.sampled.UnsupportedAudioFileException; + +import org.junit.Test; + +/** + * Testing the Audio service of the Queue + * @author mkuprivecz + * + */ +public class AudioTest { + + /** + * Test here that the playSound method works correctly + * @throws UnsupportedAudioFileException when the audio file is not supported + * @throws IOException when the file is not readable + * @throws InterruptedException when the test is interrupted externally + */ + @Test + public void testPlaySound() throws UnsupportedAudioFileException, IOException, InterruptedException { + Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); + // test that service is started + assertTrue(Audio.isServiceRunning()); + // adding a small pause to be sure that the sound is ended + Thread.sleep(5000); + // test that service is finished + assertFalse(!Audio.isServiceRunning()); + } + + /** + * Test here that the Queue + * @throws UnsupportedAudioFileException when the audio file is not supported + * @throws IOException when the file is not readable + * @throws InterruptedException when the test is interrupted externally + */ + @Test + public void testQueue() throws UnsupportedAudioFileException, IOException, InterruptedException { + Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); + Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); + Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); + assertTrue(Audio.getPendingAudio().length > 0); + // test that service is started + assertTrue(Audio.isServiceRunning()); + // adding a small pause to be sure that the sound is ended + Thread.sleep(10000); + // test that service is finished + assertFalse(!Audio.isServiceRunning()); + } + +}