jdk/test/javax/sound/midi/MidiSystem/DefaultDevices.java
changeset 41905 e8e5df013c6e
equal deleted inserted replaced
41904:524d908e49ea 41905:e8e5df013c6e
       
     1 /*
       
     2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.util.List;
       
    25 
       
    26 import javax.sound.midi.MidiDevice;
       
    27 import javax.sound.midi.MidiSystem;
       
    28 import javax.sound.midi.MidiUnavailableException;
       
    29 import javax.sound.midi.Receiver;
       
    30 import javax.sound.midi.Sequencer;
       
    31 import javax.sound.midi.Synthesizer;
       
    32 import javax.sound.midi.Transmitter;
       
    33 import javax.sound.midi.spi.MidiDeviceProvider;
       
    34 
       
    35 import com.sun.media.sound.JDK13Services;
       
    36 
       
    37 /**
       
    38  * @test
       
    39  * @bug 4776511
       
    40  * @bug 4934509
       
    41  * @bug 4938236
       
    42  * @modules java.desktop/com.sun.media.sound
       
    43  * @run main/timeout=600 DefaultDevices
       
    44  * @summary RFE: Setting the default MixerProvider
       
    45  */
       
    46 /** Test the retrieving of MidiDevices with default device properties.
       
    47  * This is a part of the test for 4776511.
       
    48  * The test also functions as a unit test for 4934509: SPEC: Document
       
    49  * explicitely MidiSystem.getReceiver's behavior
       
    50  * and a regession test for 4938236: Crash when opening synthesizer implicitly
       
    51  * The test has been updated to reflect a fix for 6411624: MidiSystem.getSequencer()
       
    52  * doesn't throw MidiUnavaivableException if no audio card installed (see also
       
    53  * 6422786: regression test javax/sound/midi/MidiSystem/DefaultDevices.java fails)
       
    54  */
       
    55 public class DefaultDevices {
       
    56 
       
    57     private static final String ERROR_PROVIDER_CLASS_NAME = "abc";
       
    58     private static final String ERROR_INSTANCE_NAME = "def";
       
    59 
       
    60     private static final Class RECEIVER_CLASS = javax.sound.midi.Receiver.class;
       
    61     private static final Class TRANSMITTER_CLASS = javax.sound.midi.Transmitter.class;
       
    62     private static final Class SEQUENCER_CLASS = javax.sound.midi.Sequencer.class;
       
    63     private static final Class SYNTHESIZER_CLASS = javax.sound.midi.Synthesizer.class;
       
    64 
       
    65     public static void main(String[] args) throws Exception {
       
    66         boolean allOk = true;
       
    67         MidiDevice.Info[] infos;
       
    68 
       
    69         out("\nTesting MidiDevices retrieved via MidiSystem");
       
    70         infos = MidiSystem.getMidiDeviceInfo();
       
    71         allOk &= testDevices(infos, null);
       
    72 
       
    73         out("\nTesting MidiDevices retrieved from MidiDeviceProviders");
       
    74         List providers = JDK13Services.getProviders(MidiDeviceProvider.class);
       
    75         for (int i = 0; i < providers.size(); i++) {
       
    76             MidiDeviceProvider provider = (MidiDeviceProvider)providers.get(i);
       
    77             infos = provider.getDeviceInfo();
       
    78             allOk &= testDevices(infos, provider.getClass().getName());
       
    79         }
       
    80 
       
    81         if (!allOk) {
       
    82             throw new Exception("Test failed");
       
    83         } else {
       
    84             out("Test passed");
       
    85         }
       
    86     }
       
    87 
       
    88     private static boolean testDevices(MidiDevice.Info[] infos,
       
    89             String providerClassName) {
       
    90         boolean allOk = true;
       
    91 
       
    92         for (int i = 0; i < infos.length; i++) {
       
    93             MidiDevice device = null;
       
    94             try {
       
    95                 device = MidiSystem.getMidiDevice(infos[i]);
       
    96             } catch (MidiUnavailableException e) {
       
    97                 out("Exception thrown; Test NOT failed.");
       
    98                 e.printStackTrace(System.out);
       
    99                 out("");
       
   100             }
       
   101             out("\nTesting device: " + device);
       
   102             if (device instanceof Sequencer) {
       
   103                 allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, true, true);
       
   104                 // incorrect cases
       
   105                 allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);
       
   106                 allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, false);
       
   107                 allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);
       
   108             }
       
   109             if (device instanceof Synthesizer) {
       
   110                 allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, true, true);
       
   111                 allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, true);
       
   112                 // incorrect cases
       
   113                 allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);
       
   114                 allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);
       
   115             }
       
   116             if (device instanceof Receiver) {
       
   117                 allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, true, true);
       
   118                 // incorrect cases
       
   119                 allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);
       
   120                 allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);
       
   121                 allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);
       
   122             }
       
   123             if (device instanceof Transmitter) {
       
   124                 allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, true, true);
       
   125                 // incorrect cases
       
   126                 allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, false);
       
   127                 allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);
       
   128                 allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);
       
   129             }
       
   130         }
       
   131         return allOk;
       
   132     }
       
   133 
       
   134     private static boolean testDevice(MidiDevice device, Class type,
       
   135             String providerClassName, boolean testWrong, boolean expectedResult) {
       
   136         boolean allOk = true;
       
   137         String instanceName = device.getDeviceInfo().getName();
       
   138 
       
   139         // no error
       
   140         allOk &= testDevice(device, type, providerClassName,
       
   141                             instanceName, expectedResult);
       
   142 
       
   143         if (testWrong) {
       
   144             // erroneous provider class name, correct instance name
       
   145             allOk &= testDevice(device, type, ERROR_PROVIDER_CLASS_NAME,
       
   146                                 instanceName, expectedResult);
       
   147 
       
   148             // correct provider class name, erroneous instance name
       
   149             // we presume that provider provides only one class of requested type
       
   150             allOk &= testDevice(device, type, providerClassName,
       
   151                                 ERROR_INSTANCE_NAME, expectedResult);
       
   152         }
       
   153 
       
   154         return allOk;
       
   155     }
       
   156 
       
   157     private static boolean testDevice(MidiDevice device, Class type,
       
   158             String providerClassName, String instanceName,
       
   159             boolean expectedResult) {
       
   160         boolean allOk = true;
       
   161 
       
   162         try {
       
   163             String propertyName = type.getName();
       
   164             String propertyValue = (providerClassName != null) ? providerClassName: "" ;
       
   165             propertyValue += "#" + instanceName;
       
   166             out("property: " + propertyName + "="+ propertyValue);
       
   167             System.setProperty(propertyName, propertyValue);
       
   168             Object reference = null;
       
   169             Object result = null;
       
   170             if (type == SEQUENCER_CLASS) {
       
   171                 reference = device;
       
   172                 result = MidiSystem.getSequencer();
       
   173             } else if (type == SYNTHESIZER_CLASS) {
       
   174                 reference = device;
       
   175                 result = MidiSystem.getSynthesizer();
       
   176             } else if (type == RECEIVER_CLASS) {
       
   177                 reference = device.getReceiver();
       
   178                 result = MidiSystem.getReceiver();
       
   179             } else if (type == TRANSMITTER_CLASS) {
       
   180                 reference = device.getTransmitter();
       
   181                 result = MidiSystem.getTransmitter();
       
   182             }
       
   183             out("result: " + result);
       
   184             boolean rightDevice = (reference.getClass() == result.getClass());
       
   185             if (rightDevice != expectedResult) {
       
   186                 out("\nERROR: type " + type + " failed:"
       
   187                         + " class should" + (expectedResult ? "" : " NOT") + " be '"
       
   188                         + reference.getClass()
       
   189                         + "' but is '" + result.getClass() + "'!\n");
       
   190                 allOk = false;
       
   191             }
       
   192             if (expectedResult
       
   193                     && reference instanceof MidiDevice
       
   194                     && result instanceof MidiDevice) {
       
   195                 MidiDevice referenceDevice = (MidiDevice)reference;
       
   196                 MidiDevice resultDevice = (MidiDevice)result;
       
   197                 if (!referenceDevice.getDeviceInfo().getName().equals(
       
   198                                     resultDevice.getDeviceInfo().getName())) {
       
   199                     out("\nERROR: type " + type + " failed: name should be '"
       
   200                             + referenceDevice.getDeviceInfo().getName()
       
   201                             + "' but is '"
       
   202                             + resultDevice.getDeviceInfo().getName() + "'!\n");
       
   203                     allOk = false;
       
   204                 }
       
   205             }
       
   206             if (result instanceof Receiver) {
       
   207                 ((Receiver)result).close();
       
   208             } else if (result instanceof Transmitter) {
       
   209                 ((Transmitter)result).close();
       
   210             } else if (result instanceof Synthesizer) {
       
   211                 ((Synthesizer)result).close();
       
   212             } else if (result instanceof Sequencer) {
       
   213                 ((Sequencer)result).close();
       
   214             }
       
   215         } catch (Exception e) {
       
   216             out("Exception thrown; Test NOT failed.");
       
   217             e.printStackTrace(System.out);
       
   218             out("");
       
   219         }
       
   220         return allOk;
       
   221     }
       
   222 
       
   223     private static void out(String message) {
       
   224         System.out.println(message);
       
   225     }
       
   226 }