Path: blob/master/test/jdk/javax/sound/midi/MidiSystem/DefaultDevices.java
41154 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.Sequencer;30import javax.sound.midi.Synthesizer;31import javax.sound.midi.Transmitter;32import javax.sound.midi.spi.MidiDeviceProvider;3334import com.sun.media.sound.JDK13Services;3536/**37* @test38* @bug 477651139* @bug 493450940* @bug 493823641* @modules java.desktop/com.sun.media.sound42* @run main/timeout=600 DefaultDevices43* @summary RFE: Setting the default MixerProvider44*/45/** Test the retrieving of MidiDevices with default device properties.46* This is a part of the test for 4776511.47* The test also functions as a unit test for 4934509: SPEC: Document48* explicitely MidiSystem.getReceiver's behavior49* and a regession test for 4938236: Crash when opening synthesizer implicitly50* The test has been updated to reflect a fix for 6411624: MidiSystem.getSequencer()51* doesn't throw MidiUnavaivableException if no audio card installed (see also52* 6422786: regression test javax/sound/midi/MidiSystem/DefaultDevices.java fails)53*/54public class DefaultDevices {5556private static final String ERROR_PROVIDER_CLASS_NAME = "abc";57private static final String ERROR_INSTANCE_NAME = "def";5859private static final Class RECEIVER_CLASS = javax.sound.midi.Receiver.class;60private static final Class TRANSMITTER_CLASS = javax.sound.midi.Transmitter.class;61private static final Class SEQUENCER_CLASS = javax.sound.midi.Sequencer.class;62private static final Class SYNTHESIZER_CLASS = javax.sound.midi.Synthesizer.class;6364public static void main(String[] args) throws Exception {65boolean allOk = true;66MidiDevice.Info[] infos;6768out("\nTesting MidiDevices retrieved via MidiSystem");69infos = MidiSystem.getMidiDeviceInfo();70allOk &= testDevices(infos, null);7172out("\nTesting MidiDevices retrieved from MidiDeviceProviders");73List providers = JDK13Services.getProviders(MidiDeviceProvider.class);74for (int i = 0; i < providers.size(); i++) {75MidiDeviceProvider provider = (MidiDeviceProvider)providers.get(i);76infos = provider.getDeviceInfo();77allOk &= testDevices(infos, provider.getClass().getName());78}7980if (!allOk) {81throw new Exception("Test failed");82} else {83out("Test passed");84}85}8687private static boolean testDevices(MidiDevice.Info[] infos,88String providerClassName) {89boolean allOk = true;9091for (int i = 0; i < infos.length; i++) {92MidiDevice device = null;93try {94device = MidiSystem.getMidiDevice(infos[i]);95} catch (MidiUnavailableException e) {96out("Exception thrown; Test NOT failed.");97e.printStackTrace(System.out);98out("");99}100out("\nTesting device: " + device);101if (device instanceof Sequencer) {102allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, true, true);103// incorrect cases104allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);105allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, false);106allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);107}108if (device instanceof Synthesizer) {109allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, true, true);110allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, true);111// incorrect cases112allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);113allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);114}115if (device instanceof Receiver) {116allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, true, true);117// incorrect cases118allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);119allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);120allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);121}122if (device instanceof Transmitter) {123allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, true, true);124// incorrect cases125allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, false);126allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);127allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);128}129}130return allOk;131}132133private static boolean testDevice(MidiDevice device, Class type,134String providerClassName, boolean testWrong, boolean expectedResult) {135boolean allOk = true;136String instanceName = device.getDeviceInfo().getName();137138// no error139allOk &= testDevice(device, type, providerClassName,140instanceName, expectedResult);141142if (testWrong) {143// erroneous provider class name, correct instance name144allOk &= testDevice(device, type, ERROR_PROVIDER_CLASS_NAME,145instanceName, expectedResult);146147// correct provider class name, erroneous instance name148// we presume that provider provides only one class of requested type149allOk &= testDevice(device, type, providerClassName,150ERROR_INSTANCE_NAME, expectedResult);151}152153return allOk;154}155156private static boolean testDevice(MidiDevice device, Class type,157String providerClassName, String instanceName,158boolean expectedResult) {159boolean allOk = true;160161try {162String propertyName = type.getName();163String propertyValue = (providerClassName != null) ? providerClassName: "" ;164propertyValue += "#" + instanceName;165out("property: " + propertyName + "="+ propertyValue);166System.setProperty(propertyName, propertyValue);167Object reference = null;168Object result = null;169if (type == SEQUENCER_CLASS) {170reference = device;171result = MidiSystem.getSequencer();172} else if (type == SYNTHESIZER_CLASS) {173reference = device;174result = MidiSystem.getSynthesizer();175} else if (type == RECEIVER_CLASS) {176reference = device.getReceiver();177result = MidiSystem.getReceiver();178} else if (type == TRANSMITTER_CLASS) {179reference = device.getTransmitter();180result = MidiSystem.getTransmitter();181}182out("result: " + result);183boolean rightDevice = (reference.getClass() == result.getClass());184if (rightDevice != expectedResult) {185out("\nERROR: type " + type + " failed:"186+ " class should" + (expectedResult ? "" : " NOT") + " be '"187+ reference.getClass()188+ "' but is '" + result.getClass() + "'!\n");189allOk = false;190}191if (expectedResult192&& reference instanceof MidiDevice193&& result instanceof MidiDevice) {194MidiDevice referenceDevice = (MidiDevice)reference;195MidiDevice resultDevice = (MidiDevice)result;196if (!referenceDevice.getDeviceInfo().getName().equals(197resultDevice.getDeviceInfo().getName())) {198out("\nERROR: type " + type + " failed: name should be '"199+ referenceDevice.getDeviceInfo().getName()200+ "' but is '"201+ resultDevice.getDeviceInfo().getName() + "'!\n");202allOk = false;203}204}205if (result instanceof Receiver) {206((Receiver)result).close();207} else if (result instanceof Transmitter) {208((Transmitter)result).close();209} else if (result instanceof Synthesizer) {210((Synthesizer)result).close();211} else if (result instanceof Sequencer) {212((Sequencer)result).close();213}214} catch (Exception e) {215out("Exception thrown; Test NOT failed.");216e.printStackTrace(System.out);217out("");218}219return allOk;220}221222private static void out(String message) {223System.out.println(message);224}225}226227228