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 javax.sound.midi.MidiDevice;
  25 import javax.sound.midi.MidiSystem;
  26 import javax.sound.midi.MidiUnavailableException;
  27 import javax.sound.midi.Receiver;
  28 import javax.sound.midi.Transmitter;
  29 
  30 /**
  31  * @test
  32  * @bug 4616517
  33  * @summary Receiver.send() does not work properly
  34  */
  35 public class ReceiverTransmitterAvailable {
  36 
  37     private static boolean isTestExecuted;
  38     private static boolean isTestPassed;
  39 
  40     public static void main(String[] args) throws Exception {
  41         out("#4616517: Receiver.send() does not work properly");
  42         doAllTests();
  43         if (isTestExecuted) {
  44             if (isTestPassed) {
  45                 out("Test PASSED.");
  46             } else {
  47                 throw new Exception("Test FAILED.");
  48             }
  49         } else {
  50             out("Test NOT FAILED");
  51         }
  52     }
  53 
  54     private static void doAllTests() {
  55         boolean problemOccured = false;
  56         boolean succeeded = true;
  57         MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
  58         for (int i = 0; i < infos.length; i++) {
  59             MidiDevice device = null;
  60             try {
  61                 device = MidiSystem.getMidiDevice(infos[i]);
  62                 succeeded &= doTest(device);
  63             } catch (MidiUnavailableException e) {
  64                 out("exception occured; cannot test");
  65                 problemOccured = true;
  66             }
  67         }
  68         if (infos.length == 0) {
  69             out("Soundcard does not exist or sound drivers not installed!");
  70             out("This test requires sound drivers for execution.");
  71         }
  72         isTestExecuted = !problemOccured;
  73         isTestPassed = succeeded;
  74     }
  75 
  76     private static boolean doTest(MidiDevice device) {
  77         boolean succeeded = true;
  78         out("Testing: " + device);
  79         boolean expectingReceivers = (device.getMaxReceivers() != 0);
  80         boolean expectingTransmitters = (device.getMaxTransmitters() != 0);
  81         try {
  82             Receiver rec = device.getReceiver();
  83             rec.close();
  84             if (! expectingReceivers) {
  85                 out("no exception on getting Receiver");
  86                 succeeded = false;
  87             }
  88         } catch (MidiUnavailableException e) {
  89             if (expectingReceivers) {
  90                 out("Exception on getting Receiver: " + e);
  91                 succeeded = false;
  92             }
  93         }
  94         try {
  95             Transmitter trans = device.getTransmitter();
  96             trans.close();
  97             if (! expectingTransmitters) {
  98                 out("no exception on getting Transmitter");
  99                 succeeded = false;
 100             }
 101         } catch (MidiUnavailableException e) {
 102             if (expectingTransmitters) {
 103                 out("Exception on getting Transmitter: " + e);
 104                 succeeded = false;
 105             }
 106         }
 107         return succeeded;
 108     }
 109 
 110     private static void out(String message) {
 111         System.out.println(message);
 112     }
 113 }