1 /*
   2  * Copyright (c) 2010, 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 /**
  25  * @test
  26  * @bug 4933700
  27  * @summary Tests that default devices return MidiDeviceTransmitter/Receiver and returned objects return correct MidiDevice
  28  * @compile -source 1.7 TestAllDevices.java
  29  * @run main TestAllDevices
  30  * @author Alex Menkov
  31  */
  32 
  33 import javax.sound.midi.MidiDevice;
  34 import javax.sound.midi.MidiDeviceReceiver;
  35 import javax.sound.midi.MidiDeviceTransmitter;
  36 import javax.sound.midi.MidiSystem;
  37 import javax.sound.midi.MidiUnavailableException;
  38 import javax.sound.midi.Receiver;
  39 import javax.sound.midi.Transmitter;
  40 
  41 
  42 public class TestAllDevices {
  43 
  44     static boolean failed = false;
  45 
  46     public static void main(String[] args) throws Exception {
  47         out("default receiver:");
  48         try {
  49             Receiver recv = MidiSystem.getReceiver();
  50             out("  receiver: " + recv);
  51             if (recv instanceof MidiDeviceReceiver) {
  52                 out("    OK");
  53             } else {
  54                 out("    ERROR: not an instance of MidiDeviceReceiver");
  55                 failed = true;
  56             }
  57         } catch (MidiUnavailableException ex) {
  58             // this is not an error
  59             out("  receiver: MidiUnavailableException (test NOT failed)");
  60         }
  61 
  62         out("default transmitter:");
  63         try {
  64             Transmitter tran = MidiSystem.getTransmitter();
  65             out("  transmitter: " + tran);
  66             if (tran instanceof MidiDeviceTransmitter) {
  67                 out("    OK");
  68             } else {
  69                 out("    ERROR: not an instance of MidiDeviceTransmitter");
  70                 failed = true;
  71             }
  72         } catch (MidiUnavailableException ex) {
  73             // this is not an error
  74             out("  transmitter: MidiUnavailableException (test NOT failed)");
  75         }
  76 
  77         MidiDevice.Info[] infos = MidiSystem .getMidiDeviceInfo();
  78         for (MidiDevice.Info info: infos) {
  79             out(info.toString() + ":");
  80             try {
  81                 MidiDevice dev = MidiSystem.getMidiDevice(info);
  82                 dev.open();
  83 
  84                 try {
  85                     Receiver recv = dev.getReceiver();
  86                     out("  receiver: " + recv);
  87                     if (recv instanceof MidiDeviceReceiver) {
  88                         MidiDeviceReceiver devRecv = (MidiDeviceReceiver)recv;
  89                         MidiDevice retDev = devRecv.getMidiDevice();
  90                         if (retDev == dev) {
  91                             out("    OK");
  92                         } else {
  93                             out("    ERROR: getMidiDevice returned incorrect device: " + retDev);
  94                             failed = true;
  95                         }
  96                     } else {
  97                         out("    ERROR: not an instance of MidiDeviceReceiver");
  98                         failed = true;
  99                     }
 100                 } catch (MidiUnavailableException ex) {
 101                     // this is not an error
 102                     out("  receiver: MidiUnavailableException (test NOT failed)");
 103                 }
 104 
 105                 try {
 106                     Transmitter tran = dev.getTransmitter();
 107                     out("  transmitter: " + tran);
 108                     if (tran instanceof MidiDeviceTransmitter) {
 109                         MidiDeviceTransmitter devTran = (MidiDeviceTransmitter)tran;
 110                         MidiDevice retDev = devTran.getMidiDevice();
 111                         if (retDev == dev) {
 112                             out("    OK");
 113                         } else {
 114                             out("    ERROR: getMidiDevice retur4ned incorrect device: " + retDev);
 115                             failed = true;
 116                         }
 117                     } else {
 118                         out("    ERROR: not an instance of MidiDeviceTransmitter");
 119                         failed = true;
 120                     }
 121                 } catch (MidiUnavailableException ex) {
 122                     // this is not an error
 123                     out("  transmitter: MidiUnavailableException (test NOT failed)");
 124                 }
 125 
 126                 dev.close();
 127             } catch (MidiUnavailableException ex) {
 128                 out("  device: MidiUnavailableException (test NOT failed)");
 129             }
 130         }
 131 
 132         if (failed) {
 133             throw new Exception("Test failed.");
 134         }
 135     }
 136 
 137     static void out(String s) {
 138         System.out.println(s);
 139     }
 140 
 141 }