Path: blob/master/test/jdk/javax/sound/midi/Devices/MidiDeviceGetReceivers.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 java.util.List;2425import javax.sound.midi.MidiDevice;26import javax.sound.midi.MidiSystem;27import javax.sound.midi.MidiUnavailableException;28import javax.sound.midi.Receiver;29import javax.sound.midi.Transmitter;3031/**32* @test33* @bug 493138734* @summary Add methods to MidiDevice to get list of Transmitters and Receivers35*/36public class MidiDeviceGetReceivers {3738private static boolean executed = false;39private static boolean failed = false;4041public static void main(String[] args) throws Exception {42out("unit test 4931387: Add methods to MidiDevice to get list of Transmitters and Receivers");43doAllTests();44if (executed) {45if (failed) throw new Exception("Test FAILED!");46out("Test PASSED.");47} else {48out("Test NOT failed.");49}50}5152private static void doAllTests() {53MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();54for (int i = 0; i < infos.length; i++) {55MidiDevice device = null;56try {57device = MidiSystem.getMidiDevice(infos[i]);58doTest(device);59} catch (MidiUnavailableException e) {60out("Exception occured when retrieving device "+infos[i]+": "+e);61}62}63if (infos.length == 0) {64out("No MIDI devices exist or sound drivers not installed!");65}66}6768private static boolean containsReceiver(MidiDevice dev, Receiver rec) {69List<Receiver> recvs = dev.getReceivers();70return recvs.contains(rec);71}7273private static boolean containsTransmitter(MidiDevice dev, Transmitter tra) {74List<Transmitter> tras = dev.getTransmitters();75return tras.contains(tra);76}7778private static void doTest(MidiDevice device) {79boolean thisFailed = false;80out1("Testing: " + device+"...");81try {82device.open();83} catch (Exception e) {84out2("device.open threw exception: "+e);85out2("cannot test this device.");86return;87}88if (device.getMaxReceivers() != 0) {89// device offers receivers90try {91List<Receiver> origList = device.getReceivers();92Receiver rec = device.getReceiver();93if (!containsReceiver(device, rec)) {94out2("Getting a receiver did not add it to device list!");95thisFailed = true;96}97if (origList.contains(rec)) {98out2("Original unmodifiable list was modified by adding a receiver!");99thisFailed = true;100}101rec.close();102if (containsReceiver(device, rec)) {103out2("Closing a receiver did not remove it from device list!");104thisFailed = true;105}106// add a new receiver so that the device.close will really test107// that the receiver is removed108rec = device.getReceiver();109if (!containsReceiver(device, rec)) {110out2("Getting a receiver again did not add it to device list!");111thisFailed = true;112}113} catch (MidiUnavailableException e) {114out2("Exception on getting Receiver: " + e);115}116}117if (device.getMaxTransmitters() != 0) {118// device offers transmitters119try {120List<Transmitter> origList = device.getTransmitters();121Transmitter tra = device.getTransmitter();122if (!containsTransmitter(device, tra)) {123out2("Getting a transmitter did not add it to device list!");124thisFailed = true;125}126if (origList.contains(tra)) {127out2("Original unmodifiable list was modified by adding a transmitter!");128thisFailed = true;129}130tra.close();131if (containsTransmitter(device, tra)) {132out2("Closing a transmitter did not remove it from device list!");133thisFailed = true;134}135tra = device.getTransmitter();136if (!containsTransmitter(device, tra)) {137out2("Getting a transmitter again did not add it to device list!");138thisFailed = true;139}140} catch (MidiUnavailableException e) {141out("Exception on getting Transmitter: " + e);142}143}144try {145device.close();146if (device.getTransmitters().size() > 0) {147out2(" Device still has transmitters after close() was called!");148thisFailed = true;149}150if (device.getReceivers().size() > 0) {151out2(" Device still has receivers after close() was called!");152thisFailed = true;153}154} catch (Exception e) {155out2("device.close threw exception: "+e);156}157if (!thisFailed) {158out("OK");159} else {160failed = true;161}162executed = true;163}164165static boolean lfMissing = false;166167private static void out(String message) {168lfMissing = true;169System.out.println(message);170}171172/* don't print LF at end */173private static void out1(String message) {174System.out.print(message);175lfMissing = true;176}177178/* print at a new line, indented */179private static void out2(String message) {180if (lfMissing) {181System.out.println();182lfMissing = false;183}184System.out.println(" "+message);185}186}187188189