Path: blob/master/test/jdk/javax/sound/midi/Sequencer/SequencerImplicitSynthOpen.java
41155 views
/*1* Copyright (c) 2011, 2013, 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*/2223/**24* @test25* @bug 666047026* @summary Tests that sequencer correctly opens/closes (implicitly) devices27* @author Alex Menkov28*/2930import java.util.List;31import javax.sound.midi.MidiDevice;32import javax.sound.midi.MidiDeviceReceiver;33import javax.sound.midi.MidiSystem;34import javax.sound.midi.MidiUnavailableException;35import javax.sound.midi.Receiver;36import javax.sound.midi.Sequencer;37import javax.sound.midi.Transmitter;3839public class SequencerImplicitSynthOpen {4041static int TEST_COUNT = 5;4243public static void main(String[] args) {44try {45log("getting sequencer...");46Sequencer sequencer = MidiSystem.getSequencer();47log(" - got " + getDeviceStr(sequencer));4849// obtain connected device (usually synthesizer)50MidiDevice synth = getConnectedDevice(sequencer);51if (synth == null) {52log("could not get connected device, returning");53return;54}5556log("connected device: " + getDeviceStr(synth));5758int success = 0;59for (int i=0; i<TEST_COUNT; i++) {60if (test(sequencer)) {61success++;62}63}6465if (success != TEST_COUNT) {66throw new RuntimeException("test FAILS");67}68} catch (MidiUnavailableException ex) {69// this is not a failure70log("Could not get Sequencer");71}72log("test PASSED.");73}7475static boolean test(Sequencer sequencer) throws MidiUnavailableException {76log("");77log("opening sequencer...");78sequencer.open(); // opens connected synthesizer implicitly79MidiDevice synth = getConnectedDevice(sequencer);80log(" connected device: " + getDeviceStr(synth));8182log("closing sequencer...");83sequencer.close(); // closes the synth implicitly84log(" synth is " + getDeviceStr(synth));85MidiDevice synth2 = getConnectedDevice(sequencer);86log(" currently connected device: " + getDeviceStr(synth2));8788if (synth != null && synth.isOpen()) {89log("FAIL.");90return false;91}92log("OK.");93return true;94}9596static MidiDevice getConnectedDevice(Sequencer sequencer) {97List<Transmitter> trans = sequencer.getTransmitters();98log(" sequencer has " + trans.size() + " opened transmitters:");99for (Transmitter tr: trans) {100Receiver r = tr.getReceiver();101log(" " + getClassStr(tr) + " connected to " + getClassStr(r));102if (r instanceof MidiDeviceReceiver) {103MidiDeviceReceiver recv = (MidiDeviceReceiver)r;104MidiDevice dev = recv.getMidiDevice();105log(" - receiver of " + getClassStr(dev));106return dev;107} else {108log(" - does NOT implement MidiDeviceReceiver");109}110}111return null;112}113114static String getClassStr(Object o) {115if (o == null) {116return "<null>";117}118return o.getClass().getName();119}120121static String getDeviceStr(MidiDevice dev) {122if (dev == null) {123return "NULL";124}125return getClassStr(dev) + ", " + (dev.isOpen() ? "OPENED" : "CLOSED");126}127128static void log(String s) {129System.out.println(s);130}131132}133134135