Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/midi/MidiSystem/DefaultDevices.java
38853 views
/*1* Copyright (c) 2003, 2020, 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* @run main/timeout=600 DefaultDevices42* @summary RFE: Setting the default MixerProvider43*/44/** Test the retrieving of MidiDevices with default device properties.45* This is a part of the test for 4776511.46* The test also functions as a unit test for 4934509: SPEC: Document47* explicitely MidiSystem.getReceiver's behavior48* and a regession test for 4938236: Crash when opening synthesizer implicitly49* The test has been updated to reflect a fix for 6411624: MidiSystem.getSequencer()50* doesn't throw MidiUnavaivableException if no audio card installed (see also51* 6422786: regression test javax/sound/midi/MidiSystem/DefaultDevices.java fails)52*/53public class DefaultDevices {5455private static final String ERROR_PROVIDER_CLASS_NAME = "abc";56private static final String ERROR_INSTANCE_NAME = "def";5758private static final Class RECEIVER_CLASS = javax.sound.midi.Receiver.class;59private static final Class TRANSMITTER_CLASS = javax.sound.midi.Transmitter.class;60private static final Class SEQUENCER_CLASS = javax.sound.midi.Sequencer.class;61private static final Class SYNTHESIZER_CLASS = javax.sound.midi.Synthesizer.class;6263public static void main(String[] args) throws Exception {64boolean allOk = true;65MidiDevice.Info[] infos;6667out("\nTesting MidiDevices retrieved via MidiSystem");68infos = MidiSystem.getMidiDeviceInfo();69allOk &= testDevices(infos, null);7071out("\nTesting MidiDevices retrieved from MidiDeviceProviders");72List providers = JDK13Services.getProviders(MidiDeviceProvider.class);73for (int i = 0; i < providers.size(); i++) {74MidiDeviceProvider provider = (MidiDeviceProvider)providers.get(i);75infos = provider.getDeviceInfo();76allOk &= testDevices(infos, provider.getClass().getName());77}7879if (!allOk) {80throw new Exception("Test failed");81} else {82out("Test passed");83}84}8586private static boolean testDevices(MidiDevice.Info[] infos,87String providerClassName) {88boolean allOk = true;8990for (int i = 0; i < infos.length; i++) {91MidiDevice device = null;92try {93device = MidiSystem.getMidiDevice(infos[i]);94} catch (MidiUnavailableException e) {95out("Exception thrown; Test NOT failed.");96e.printStackTrace(System.out);97out("");98}99out("\nTesting device: " + device);100if (device instanceof Sequencer) {101allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, true, true);102// incorrect cases103allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);104allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, false);105allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);106}107if (device instanceof Synthesizer) {108allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, true, true);109allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, true);110// incorrect cases111allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);112allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);113}114if (device instanceof Receiver) {115allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, true, true);116// incorrect cases117allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);118allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);119allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);120}121if (device instanceof Transmitter) {122allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, true, true);123// incorrect cases124allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, false);125allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);126allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);127}128}129return allOk;130}131132private static boolean testDevice(MidiDevice device, Class type,133String providerClassName, boolean testWrong, boolean expectedResult) {134boolean allOk = true;135String instanceName = device.getDeviceInfo().getName();136137// no error138allOk &= testDevice(device, type, providerClassName,139instanceName, expectedResult);140141if (testWrong) {142// erroneous provider class name, correct instance name143allOk &= testDevice(device, type, ERROR_PROVIDER_CLASS_NAME,144instanceName, expectedResult);145146// correct provider class name, erroneous instance name147// we presume that provider provides only one class of requested type148allOk &= testDevice(device, type, providerClassName,149ERROR_INSTANCE_NAME, expectedResult);150}151152return allOk;153}154155private static boolean testDevice(MidiDevice device, Class type,156String providerClassName, String instanceName,157boolean expectedResult) {158boolean allOk = true;159160try {161String propertyName = type.getName();162String propertyValue = (providerClassName != null) ? providerClassName: "" ;163propertyValue += "#" + instanceName;164out("property: " + propertyName + "="+ propertyValue);165System.setProperty(propertyName, propertyValue);166Object reference = null;167Object result = null;168if (type == SEQUENCER_CLASS) {169reference = device;170result = MidiSystem.getSequencer();171} else if (type == SYNTHESIZER_CLASS) {172reference = device;173result = MidiSystem.getSynthesizer();174} else if (type == RECEIVER_CLASS) {175reference = device.getReceiver();176result = MidiSystem.getReceiver();177} else if (type == TRANSMITTER_CLASS) {178reference = device.getTransmitter();179result = MidiSystem.getTransmitter();180}181out("result: " + result);182boolean rightDevice = (reference.getClass() == result.getClass());183if (rightDevice != expectedResult) {184out("\nERROR: type " + type + " failed:"185+ " class should" + (expectedResult ? "" : " NOT") + " be '"186+ reference.getClass()187+ "' but is '" + result.getClass() + "'!\n");188allOk = false;189}190if (expectedResult191&& reference instanceof MidiDevice192&& result instanceof MidiDevice) {193MidiDevice referenceDevice = (MidiDevice)reference;194MidiDevice resultDevice = (MidiDevice)result;195if (!referenceDevice.getDeviceInfo().getName().equals(196resultDevice.getDeviceInfo().getName())) {197out("\nERROR: type " + type + " failed: name should be '"198+ referenceDevice.getDeviceInfo().getName()199+ "' but is '"200+ resultDevice.getDeviceInfo().getName() + "'!\n");201allOk = false;202}203}204if (result instanceof Receiver) {205((Receiver)result).close();206} else if (result instanceof Transmitter) {207((Transmitter)result).close();208} else if (result instanceof Synthesizer) {209((Synthesizer)result).close();210} else if (result instanceof Sequencer) {211((Sequencer)result).close();212}213} catch (Exception e) {214out("Exception thrown; Test NOT failed.");215e.printStackTrace(System.out);216out("");217}218return allOk;219}220221private static void out(String message) {222System.out.println(message);223}224}225226227