Path: blob/master/test/jdk/javax/sound/midi/Devices/Reopen.java
41152 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 javax.sound.midi.MidiDevice;24import javax.sound.midi.MidiSystem;25import javax.sound.midi.Sequencer;26import javax.sound.midi.Synthesizer;2728/**29* @test30* @bug 491466731* @summary Closing and reopening MIDI IN device on Linux throws32* MidiUnavailableException33*/34public class Reopen {3536private static boolean isTestExecuted;37private static boolean isTestPassed;3839/*40* run manually:41* java Reopen 100 in for 100 iterations on the MIDI IN device42* java Reopen 16 out for 16 iterations on the MIDI OUT device43*/44public static void main(String[] args) throws Exception {45if (args.length == 0) {46doAllTests();47} else if (args.length == 2) {48int numIterations = Integer.parseInt(args[0]);49if (args[1].equals("in")) {50doTest(numIterations, true);51} else {52doTest(numIterations, false);53}54} else {55out("usage: java Reopen <iterations> in|out");56}57}5859private static void doAllTests() throws Exception {60out("#4914667: Closing and reopening MIDI IN device on Linux throws MidiUnavailableException");61boolean success = true;62try {63success &= doTest(20, true); // MIDI IN64success &= doTest(20, false); // MIDI OUT65isTestExecuted = true;66} catch (Exception e) {67out(e);68isTestExecuted = false;69}70isTestPassed = success;71if (isTestExecuted) {72if (isTestPassed) {73out("Test PASSED.");74} else {75throw new Exception("Test FAILED.");76}77} else {78out("Test NOT FAILED");79}80}8182private static boolean doTest(int numIterations, boolean input) throws Exception {83MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();84MidiDevice outDevice = null;85MidiDevice inDevice = null;86for (int i = 0; i < infos.length; i++) {87MidiDevice device = MidiSystem.getMidiDevice(infos[i]);88if (! (device instanceof Sequencer) &&89! (device instanceof Synthesizer)) {90if (device.getMaxReceivers() != 0) {91outDevice = device;92}93if (device.getMaxTransmitters() != 0) {94inDevice = device;95}96}97}98MidiDevice testDevice = null;99if (input) {100testDevice = inDevice;101} else {102testDevice = outDevice;103}104if (testDevice == null) {105out("Cannot test: device not available.");106return true;107}108out("Using Device: " + testDevice);109110for (int i = 0; i < numIterations; i++) {111out("@@@ ITERATION: " + i);112testDevice.open();113// This sleep ensures that the thread of MidiInDevice is started.114sleep(50);115testDevice.close();116}117return true;118}119120private static void sleep(int milliseconds) {121try {122Thread.sleep(milliseconds);123} catch (InterruptedException e) {124}125}126127private static void out(Throwable t) {128t.printStackTrace(System.out);129}130131private static void out(String message) {132System.out.println(message);133}134}135136137