Path: blob/master/test/jdk/javax/sound/midi/Synthesizer/bug4685396.java
41155 views
/*1* Copyright (c) 2006, 2017, 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.Instrument;24import javax.sound.midi.MidiSystem;25import javax.sound.midi.MidiUnavailableException;26import javax.sound.midi.Soundbank;27import javax.sound.midi.Synthesizer;2829/**30* @test31* @bug 468539632* @summary Tests that Synthesizer.remapInstrument works33* @run main bug468539634*/35public class bug4685396 {3637static Synthesizer synth = null;3839public static boolean isInstrumentExist(Instrument inst, Instrument[] insts) {40for (int i = 0; i < insts.length; i++) {41if (inst.equals(insts[i]))42return true;43}44return false;45}4647static boolean test(48boolean reloadInstr, // reload all instruments?49boolean unloadFrom, // unload "from" instrument?50boolean unloadTo // unload "to" instrument?51) throws MidiUnavailableException {52log("Starting test: reloadInstr=" + reloadInstr53+ ", unloadFrom=" + unloadFrom54+ ", unloadTo=" + unloadTo55+ "");5657log(" creating synthesizer...");58synth = MidiSystem.getSynthesizer();59log(" opening synthesizer...");60synth.open();6162Soundbank sbank = synth.getDefaultSoundbank();63if (sbank == null)64throw new RuntimeException("ERROR: Could not get default soundbank");6566if (reloadInstr) {67synth.unloadAllInstruments(sbank);68synth.loadAllInstruments(sbank);69}7071Instrument[] instrs = synth.getLoadedInstruments();7273log(" " + instrs.length + " instruments loaded.");7475if (instrs.length < 2)76throw new RuntimeException("ERROR: need at least 2 loaded instruments");7778Instrument from = instrs[0];79Instrument to = instrs[instrs.length - 1];8081if (unloadFrom)82synth.unloadInstrument(from);83if (unloadTo)84synth.unloadInstrument(to);8586log(" from instrument (" + (unloadFrom ? "UNLOADED" : "LOADED")87+ "): " + from.toString());88log(" to instrument (" + (unloadTo ? "UNLOADED" : "LOADED")89+ "): " + to.toString());9091boolean result = false;92boolean excepted = false;93try {94result = synth.remapInstrument(from, to);95log(" remapInstrument(from, to) returns " + result);96} catch (IllegalArgumentException ex) {97excepted = true;98log(" EXCEPTION:");99ex.printStackTrace(System.out);100}101102instrs = synth.getLoadedInstruments();103log(" " + instrs.length + " instruments remains loaded.");104105boolean toUnloaded = !isInstrumentExist(to, instrs);106boolean fromUnloaded = !isInstrumentExist(from, instrs);107108log(" from instrument is " + (fromUnloaded ? "UNLOADED" : "LOADED"));109log(" to instrument is " + (toUnloaded ? "UNLOADED" : "LOADED"));110111boolean bOK = true;112if (result) {113if (unloadTo) {114bOK = false;115log("ERROR: unloaded to, but sucessfull remap");116}117if (!fromUnloaded) {118bOK = false;119log("ERROR: sucessfull remap, but from hasn't been unloaded");120}121if (toUnloaded) {122bOK = false;123log("ERROR: to has been unloaded!");124}125} else {126if (!excepted) {127bOK = false;128log("ERROR: remap returns false, exception hasn't been thrown");129}130if (!unloadTo) {131bOK = false;132log("ERROR: to is loaded, but remap returns false");133}134if (unloadFrom != fromUnloaded) {135bOK = false;136log("ERROR: remap returns false, but status of from has been changed");137}138}139140if (bOK) {141log("Test result: OK\n");142} else {143log("Test result: FAIL\n");144}145146return bOK;147}148149static void cleanup() {150if (synth != null) {151synth.close();152synth = null;153}154}155156static boolean runTest(157boolean reloadInstr, // reload all instruments?158boolean unloadTo, // unload "to" instrument?159boolean unloadFrom // unload "from" instrument?160)161{162boolean success = false;163try {164success = test(reloadInstr, unloadFrom, unloadTo);165} catch (final MidiUnavailableException ignored) {166// the test is not applicable167success = true;168} catch (Exception ex) {169log("Exception: " + ex.toString());170}171cleanup();172return success;173}174175public static void main(String args[]) {176boolean failed = false;177if (!runTest(true, false, false))178failed = true;179if (!runTest(true, false, true))180failed = true;181if (!runTest(true, true, false))182failed = true;183if (!runTest(true, true, true))184failed = true;185186if (failed) {187throw new RuntimeException("Test FAILED.");188}189log("Test sucessfully passed.");190}191192193// helper routines194static long startTime = currentTimeMillis();195static long currentTimeMillis() {196//return System.nanoTime() / 1000000L;197return System.currentTimeMillis();198}199static void log(String s) {200long time = currentTimeMillis() - startTime;201long ms = time % 1000;202time /= 1000;203long sec = time % 60;204time /= 60;205long min = time % 60;206time /= 60;207System.out.println(""208+ (time < 10 ? "0" : "") + time209+ ":" + (min < 10 ? "0" : "") + min210+ ":" + (sec < 10 ? "0" : "") + sec211+ "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms212+ " (" + Thread.currentThread().getName() + ") " + s);213}214static void delay(int millis) {215try {216Thread.sleep(millis);217} catch (InterruptedException e) {}218}219}220221222