Path: blob/master/test/jdk/javax/sound/midi/Devices/ClosedReceiver.java
41152 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.sound.midi.InvalidMidiDataException;24import javax.sound.midi.MidiDevice;25import javax.sound.midi.MidiSystem;26import javax.sound.midi.MidiUnavailableException;27import javax.sound.midi.Receiver;28import javax.sound.midi.Sequencer;29import javax.sound.midi.ShortMessage;30import javax.sound.midi.Synthesizer;3132/**33* @test34* @bug 461651735* @summary Receiver.send() does not work properly36*/37public class ClosedReceiver {3839public static void main(String[] args) throws Exception {40out("#4616517: Receiver.send() does not work properly");41if (!isMidiInstalled()) {42out("Soundcard does not exist or sound drivers not installed!");43out("This test requires sound drivers for execution.");44return;45}4647boolean passed = true;4849passed &= testReceiverSend();50passed &= testClosedReceivers();51if (passed) {52out("Test PASSED.");53} else {54throw new Exception("Test FAILED.");55}56}5758/**59* Execute Receiver.send() and expect that there is no exception.60*/61private static boolean testReceiverSend() {62boolean result = true;6364Receiver receiver;65ShortMessage shMsg = new ShortMessage();6667try {68receiver = MidiSystem.getReceiver();69shMsg.setMessage(ShortMessage.NOTE_ON, 0,60, 93);70try {71receiver.send( shMsg, -1 );72} catch(IllegalStateException ilEx) {73ilEx.printStackTrace(System.out);74out("IllegalStateException was thrown incorrectly!");75result = false;76}77receiver.close();78} catch(MidiUnavailableException e) {79out("Midi unavailable, cannot test.");80} catch(InvalidMidiDataException ine) {81out("InvalidMidiDataException, cannot test.");82}83return result;84}8586private static boolean testClosedReceivers() {87boolean result = true;88Receiver receiver;89Synthesizer synt = null;9091// test Synthesizer's Receiver92try {93synt = MidiSystem.getSynthesizer();94synt.open();95} catch(MidiUnavailableException e) {96out("Midi unavailable, cannot test.");97return result;98}99try {100receiver = synt.getReceiver();101} catch (MidiUnavailableException e) {102out("unable to get Receiver from synthesizer, cannot test.");103return result;104}105result &= testClosedReceiver(receiver);106synt.close();107108// test all MidiDevices' Receivers109110MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();111for (int i = 0; i < devices.length; i++) {112try {113MidiDevice device = MidiSystem.getMidiDevice(devices[i]);114if (device.getMaxReceivers() != 0) {115receiver = device.getReceiver();116result &= testClosedReceiver(receiver);117}118} catch (Exception e) {119out(e);120out("cannot test.");121return result;122}123}124return result;125}126127/**128* Execute send() on a closed Receivers and expect IllegalStateException.129*/130private static boolean testClosedReceiver(Receiver receiver) {131boolean result = true;132out("testing Receiver: " + receiver);133ShortMessage shMsg = new ShortMessage();134try {135shMsg.setMessage(ShortMessage.NOTE_ON, 0,60, 93);136} catch(InvalidMidiDataException e) {137out(e);138out("unable to construct ShortMessage, cannot test.");139return result;140}141142// begin of test143receiver.close();144try {145receiver.send( shMsg, -1 );146out("IllegalStateException was not thrown "147+ "on Receiver.send()!");148result = false;149} catch(IllegalStateException e) {150out("IllegalStateException was thrown. Ok.");151}152return result;153}154155private static void out(Throwable t) {156t.printStackTrace(System.out);157}158159private static void out(String message) {160System.out.println(message);161}162163/**164* Returns true if at least one MIDI (port) device is correctly installed on165* the system.166*/167private static boolean isMidiInstalled() {168boolean result = false;169MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();170for (int i = 0; i < devices.length; i++) {171try {172MidiDevice device = MidiSystem.getMidiDevice(devices[i]);173result = !(device instanceof Sequencer)174&& !(device instanceof Synthesizer);175} catch (Exception e1) {176System.err.println(e1);177}178if (result)179break;180}181return result;182}183}184185186