Path: blob/master/test/jdk/javax/sound/midi/MidiSystem/6411624/bug6411624.java
41161 views
/*1* Copyright (c) 2006, 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.io.IOException;24import java.util.Iterator;25import java.util.List;2627import javax.sound.midi.MidiDevice;28import javax.sound.midi.MidiSystem;29import javax.sound.midi.MidiUnavailableException;30import javax.sound.midi.Receiver;31import javax.sound.midi.Sequencer;32import javax.sound.midi.Synthesizer;33import javax.sound.midi.Transmitter;3435/**36* This test should be run on specific environment (solaris or linux w/o37* audio card installed).38*/39public class bug6411624 {4041public static void main(String args[]) throws Exception {42log("This test should only be run on solaris or linux system");43log("without audio card installed (to test on SunRay set");44log("incorrect $AUDIODEV value).");45readln();4647boolean testRecv = false;48boolean testTrans = false;49boolean testSeq = true;5051// print add info (midi device list)52try {53MidiDevice.Info[] midis = MidiSystem.getMidiDeviceInfo();54log("MidiDevices (total " + midis.length + "):");55for (int i=0; i<midis.length; i++) {56log("" + i + ": " + midis[i].toString());57// MidiDevice dev = MidiSystem.getMidiDevice(midis[i]);58// log(" device: " + dev);59}60} catch (Exception ex) {61log("!!!EXCEPTION:");62ex.printStackTrace();63}6465log("");66log("getting default receiver...");67try {68Receiver rec = MidiSystem.getReceiver();69log(" - OK: " + rec);70testRecv = checkDevice(rec);71rec.close();72} catch (MidiUnavailableException e) {73log("MidiUnavailableException has been thrown - OK");74testRecv = true;75}767778log("");79log("getting default transmitter...");80try {81Transmitter trans = MidiSystem.getTransmitter();82log(" - OK: " + trans);83testTrans = checkDevice(trans);84trans.close();85} catch (MidiUnavailableException e) {86log("MidiUnavailableException has been thrown - OK");87testTrans = true;88}899091// print default synthesizer92log("");93log("getting default synth...");94try {95Synthesizer synth = MidiSystem.getSynthesizer();96log(" - OK: " + synth);97synth.close();98} catch (MidiUnavailableException e) {99log("MidiUnavailableException has been thrown - OK:");100e.printStackTrace();101}102103104log("");105log("getting default sequencer (connected)...");106try {107Sequencer seq = MidiSystem.getSequencer();108log("OK: " + seq);109110// check that returned sequencer doesn't connected to another sequencer111log(" receivers:");112log(" max=" + seq.getMaxReceivers());113List<Receiver> recvList = seq.getReceivers();114log(" count=" + recvList.size());115Iterator<Receiver> recvIter = recvList.iterator();116int i = 0;117while (recvIter.hasNext()) {118Receiver recv = recvIter.next();119log(" " + (++i) + ": " + recv);120}121122log(" transmitters:");123log(" max=" + seq.getMaxTransmitters());124List<Transmitter> transList = seq.getTransmitters();125log(" count=" + transList.size());126Iterator<Transmitter> transIter = transList.iterator();127i = 0;128while (transIter.hasNext()) {129Transmitter trans = transIter.next();130log(" " + (++i) + ": " + trans);131Receiver recv = trans.getReceiver();132log(" recv: " + recv);133if (!checkDevice(recv))134testSeq = false;135}136137log("opening sequencer...");138seq.open();139log("OK.");140141log("closing...");142seq.close();143log("OK.");144} catch (MidiUnavailableException e) {145log("MidiUnavailableException has been thrown - OK:");146e.printStackTrace();147}148149150// debug testing - non-connected sequencer151log("");152log("getting default sequencer (non-connected)...");153try {154Sequencer seq = MidiSystem.getSequencer(false);155log("OK: " + seq);156157log(" receivers:");158log(" max=" + seq.getMaxReceivers());159List<Receiver> recvList = seq.getReceivers();160log(" count=" + recvList.size());161Iterator<Receiver> recvIter = recvList.iterator();162int i = 0;163while (recvIter.hasNext()) {164Receiver recv = recvIter.next();165log(" " + (++i) + ": " + recv);166}167168log(" transmitters:");169log(" max=" + seq.getMaxTransmitters());170List<Transmitter> transList = seq.getTransmitters();171log(" count=" + transList.size());172Iterator<Transmitter> transIter = transList.iterator();173i = 0;174while (transIter.hasNext()) {175Transmitter trans = transIter.next();176log(" " + (++i) + ": " + trans);177Receiver recv = trans.getReceiver();178log(" recv: " + recv);179}180seq.close();181} catch (MidiUnavailableException e) {182log("MidiUnavailableException has been thrown (shouln't?):");183e.printStackTrace();184}185186log("");187log("Test result:");188// print results189if (testRecv && testTrans && testSeq) {190log(" All tests sucessfully passed.");191} else {192log(" Some tests failed:");193log(" receiver test: " + (testRecv ? "OK" : "FAILED"));194log(" transmitter test: " + (testTrans ? "OK" : "FAILED"));195log(" sequencer test: " + (testSeq ? "OK" : "FAILED"));196}197log("\n\n\n");198}199200// check that device is not sequencer201static boolean checkDevice(Object dev) {202String className = dev.getClass().toString().toLowerCase();203boolean result = (className.indexOf("sequencer") < 0);204if (!result)205log("ERROR: inapropriate device");206return result;207}208209// helper routines210static long startTime = currentTimeMillis();211static long currentTimeMillis() {212//return System.nanoTime() / 1000000L;213return System.currentTimeMillis();214}215static void log(String s) {216long time = currentTimeMillis() - startTime;217long ms = time % 1000;218time /= 1000;219long sec = time % 60;220time /= 60;221long min = time % 60;222time /= 60;223System.out.println(""224+ (time < 10 ? "0" : "") + time225+ ":" + (min < 10 ? "0" : "") + min226+ ":" + (sec < 10 ? "0" : "") + sec227+ "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms228+ " (" + Thread.currentThread().getName() + ") " + s);229}230static void delay(int millis) {231try {232Thread.sleep(millis);233} catch (InterruptedException e) {}234}235static void readln() {236log("");237log("Press ENTER to continue...");238try {239while (System.in.read() != 10) ;240} catch (IOException e) { }241log("");242}243}244245246