Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/midi/Synthesizer/bug4685396.java
38854 views
/*1* Copyright (c) 2006, 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.Instrument;24import javax.sound.midi.MidiSystem;25import javax.sound.midi.Soundbank;26import javax.sound.midi.Synthesizer;2728/**29* @test30* @bug 468539631* @summary Tests that Synthesizer.remapInstrument works32* @run main bug468539633* @key headful34*/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 Exception52{53log("Starting test: reloadInstr=" + reloadInstr54+ ", unloadFrom=" + unloadFrom55+ ", unloadTo=" + unloadTo56+ "");5758log(" creating synthesizer...");59synth = MidiSystem.getSynthesizer();60log(" opening synthesizer...");61synth.open();6263Soundbank sbank = synth.getDefaultSoundbank();64if (sbank == null)65throw new RuntimeException("ERROR: Could not get default soundbank");6667if (reloadInstr) {68synth.unloadAllInstruments(sbank);69synth.loadAllInstruments(sbank);70}7172Instrument[] instrs = synth.getLoadedInstruments();7374log(" " + instrs.length + " instruments loaded.");7576if (instrs.length < 2)77throw new RuntimeException("ERROR: need at least 2 loaded instruments");7879Instrument from = instrs[0];80Instrument to = instrs[instrs.length - 1];8182if (unloadFrom)83synth.unloadInstrument(from);84if (unloadTo)85synth.unloadInstrument(to);8687log(" from instrument (" + (unloadFrom ? "UNLOADED" : "LOADED")88+ "): " + from.toString());89log(" to instrument (" + (unloadTo ? "UNLOADED" : "LOADED")90+ "): " + to.toString());9192boolean result = false;93boolean excepted = false;94try {95result = synth.remapInstrument(from, to);96log(" remapInstrument(from, to) returns " + result);97} catch (IllegalArgumentException ex) {98excepted = true;99log(" EXCEPTION:");100ex.printStackTrace(System.out);101}102103instrs = synth.getLoadedInstruments();104log(" " + instrs.length + " instruments remains loaded.");105106boolean toUnloaded = !isInstrumentExist(to, instrs);107boolean fromUnloaded = !isInstrumentExist(from, instrs);108109log(" from instrument is " + (fromUnloaded ? "UNLOADED" : "LOADED"));110log(" to instrument is " + (toUnloaded ? "UNLOADED" : "LOADED"));111112boolean bOK = true;113if (result) {114if (unloadTo) {115bOK = false;116log("ERROR: unloaded to, but sucessfull remap");117}118if (!fromUnloaded) {119bOK = false;120log("ERROR: sucessfull remap, but from hasn't been unloaded");121}122if (toUnloaded) {123bOK = false;124log("ERROR: to has been unloaded!");125}126} else {127if (!excepted) {128bOK = false;129log("ERROR: remap returns false, exception hasn't been thrown");130}131if (!unloadTo) {132bOK = false;133log("ERROR: to is loaded, but remap returns false");134}135if (unloadFrom != fromUnloaded) {136bOK = false;137log("ERROR: remap returns false, but status of from has been changed");138}139}140141if (bOK) {142log("Test result: OK\n");143} else {144log("Test result: FAIL\n");145}146147return bOK;148}149150static void cleanup() {151if (synth != null) {152synth.close();153synth = null;154}155}156157static boolean runTest(158boolean reloadInstr, // reload all instruments?159boolean unloadTo, // unload "to" instrument?160boolean unloadFrom // unload "from" instrument?161)162{163boolean success = false;164try {165success = test(reloadInstr, unloadFrom, unloadTo);166} catch (Exception ex) {167log("Exception: " + ex.toString());168}169cleanup();170return success;171}172173public static void main(String args[]) throws Exception {174boolean failed = false;175if (!runTest(true, false, false))176failed = true;177if (!runTest(true, false, true))178failed = true;179if (!runTest(true, true, false))180failed = true;181if (!runTest(true, true, true))182failed = true;183184if (failed) {185throw new RuntimeException("Test FAILED.");186}187log("Test sucessfully passed.");188}189190191// helper routines192static long startTime = currentTimeMillis();193static long currentTimeMillis() {194//return System.nanoTime() / 1000000L;195return System.currentTimeMillis();196}197static void log(String s) {198long time = currentTimeMillis() - startTime;199long ms = time % 1000;200time /= 1000;201long sec = time % 60;202time /= 60;203long min = time % 60;204time /= 60;205System.out.println(""206+ (time < 10 ? "0" : "") + time207+ ":" + (min < 10 ? "0" : "") + min208+ ":" + (sec < 10 ? "0" : "") + sec209+ "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms210+ " (" + Thread.currentThread().getName() + ") " + s);211}212static void delay(int millis) {213try {214Thread.sleep(millis);215} catch (InterruptedException e) {}216}217}218219220