jdk/test/javax/sound/midi/Synthesizer/bug4685396.java
changeset 41905 e8e5df013c6e
child 44656 72decb1e4935
equal deleted inserted replaced
41904:524d908e49ea 41905:e8e5df013c6e
       
     1 /*
       
     2  * Copyright (c) 2006, 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.Instrument;
       
    25 import javax.sound.midi.MidiSystem;
       
    26 import javax.sound.midi.Soundbank;
       
    27 import javax.sound.midi.Synthesizer;
       
    28 
       
    29 /**
       
    30  * @test
       
    31  * @bug 4685396
       
    32  * @summary Tests that Synthesizer.remapInstrument works
       
    33  * @run main bug4685396
       
    34  * @key headful
       
    35  */
       
    36 public class bug4685396 {
       
    37 
       
    38     static Synthesizer synth = null;
       
    39 
       
    40     public static boolean isInstrumentExist(Instrument inst, Instrument[] insts) {
       
    41         for (int i = 0; i < insts.length; i++) {
       
    42             if (inst.equals(insts[i]))
       
    43                 return true;
       
    44         }
       
    45         return false;
       
    46     }
       
    47 
       
    48     static boolean test(
       
    49             boolean reloadInstr,    // reload all instruments?
       
    50             boolean unloadFrom,     // unload "from" instrument?
       
    51             boolean unloadTo        // unload "to" instrument?
       
    52             ) throws Exception
       
    53     {
       
    54         log("Starting test: reloadInstr=" + reloadInstr
       
    55                 + ", unloadFrom=" + unloadFrom
       
    56                 + ", unloadTo=" + unloadTo
       
    57                 + "");
       
    58 
       
    59         log("  creating synthesizer...");
       
    60         synth = MidiSystem.getSynthesizer();
       
    61         log("  opening synthesizer...");
       
    62         synth.open();
       
    63 
       
    64         Soundbank sbank = synth.getDefaultSoundbank();
       
    65         if (sbank == null)
       
    66             throw new RuntimeException("ERROR: Could not get default soundbank");
       
    67 
       
    68         if (reloadInstr) {
       
    69             synth.unloadAllInstruments(sbank);
       
    70             synth.loadAllInstruments(sbank);
       
    71         }
       
    72 
       
    73         Instrument[] instrs = synth.getLoadedInstruments();
       
    74 
       
    75         log("  " + instrs.length + " instruments loaded.");
       
    76 
       
    77         if (instrs.length < 2)
       
    78             throw new RuntimeException("ERROR: need at least 2 loaded instruments");
       
    79 
       
    80         Instrument from = instrs[0];
       
    81         Instrument to = instrs[instrs.length - 1];
       
    82 
       
    83         if (unloadFrom)
       
    84             synth.unloadInstrument(from);
       
    85         if (unloadTo)
       
    86             synth.unloadInstrument(to);
       
    87 
       
    88         log("  from instrument (" + (unloadFrom ? "UNLOADED" : "LOADED")
       
    89                                 + "): " + from.toString());
       
    90         log("  to instrument (" + (unloadTo ? "UNLOADED" : "LOADED")
       
    91                                 + "): " + to.toString());
       
    92 
       
    93         boolean result = false;
       
    94         boolean excepted = false;
       
    95         try {
       
    96             result = synth.remapInstrument(from, to);
       
    97             log("  remapInstrument(from, to) returns " + result);
       
    98         } catch (IllegalArgumentException ex) {
       
    99             excepted = true;
       
   100             log("  EXCEPTION:");
       
   101             ex.printStackTrace(System.out);
       
   102         }
       
   103 
       
   104         instrs = synth.getLoadedInstruments();
       
   105         log("  " + instrs.length + " instruments remains loaded.");
       
   106 
       
   107         boolean toUnloaded = !isInstrumentExist(to, instrs);
       
   108         boolean fromUnloaded = !isInstrumentExist(from, instrs);
       
   109 
       
   110         log("  from instrument is " + (fromUnloaded ? "UNLOADED" : "LOADED"));
       
   111         log("  to instrument is " + (toUnloaded ? "UNLOADED" : "LOADED"));
       
   112 
       
   113         boolean bOK = true;
       
   114         if (result) {
       
   115             if (unloadTo) {
       
   116                 bOK = false;
       
   117                 log("ERROR: unloaded to, but sucessfull remap");
       
   118             }
       
   119             if (!fromUnloaded) {
       
   120                 bOK = false;
       
   121                 log("ERROR: sucessfull remap, but from hasn't been unloaded");
       
   122             }
       
   123             if (toUnloaded) {
       
   124                 bOK = false;
       
   125                 log("ERROR: to has been unloaded!");
       
   126             }
       
   127         } else {
       
   128             if (!excepted) {
       
   129                 bOK = false;
       
   130                 log("ERROR: remap returns false, exception hasn't been thrown");
       
   131             }
       
   132             if (!unloadTo) {
       
   133                 bOK = false;
       
   134                 log("ERROR: to is loaded, but remap returns false");
       
   135             }
       
   136             if (unloadFrom != fromUnloaded) {
       
   137                 bOK = false;
       
   138                 log("ERROR: remap returns false, but status of from has been changed");
       
   139             }
       
   140         }
       
   141 
       
   142         if (bOK) {
       
   143             log("Test result: OK\n");
       
   144         } else {
       
   145             log("Test result: FAIL\n");
       
   146         }
       
   147 
       
   148         return bOK;
       
   149     }
       
   150 
       
   151     static void cleanup() {
       
   152         if (synth != null) {
       
   153             synth.close();
       
   154             synth = null;
       
   155         }
       
   156     }
       
   157 
       
   158     static boolean runTest(
       
   159             boolean reloadInstr,    // reload all instruments?
       
   160             boolean unloadTo,       // unload "to" instrument?
       
   161             boolean unloadFrom      // unload "from" instrument?
       
   162             )
       
   163     {
       
   164         boolean success = false;
       
   165         try {
       
   166             success = test(reloadInstr, unloadFrom, unloadTo);
       
   167         } catch (Exception ex) {
       
   168             log("Exception: " + ex.toString());
       
   169         }
       
   170         cleanup();
       
   171         return success;
       
   172     }
       
   173 
       
   174     public static void main(String args[]) throws Exception {
       
   175         boolean failed = false;
       
   176         if (!runTest(true, false, false))
       
   177             failed = true;
       
   178         if (!runTest(true, false, true))
       
   179             failed = true;
       
   180         if (!runTest(true, true, false))
       
   181             failed = true;
       
   182         if (!runTest(true, true, true))
       
   183             failed = true;
       
   184 
       
   185         if (failed) {
       
   186             throw new RuntimeException("Test FAILED.");
       
   187         }
       
   188         log("Test sucessfully passed.");
       
   189     }
       
   190 
       
   191 
       
   192     // helper routines
       
   193     static long startTime = currentTimeMillis();
       
   194     static long currentTimeMillis() {
       
   195         //return System.nanoTime() / 1000000L;
       
   196         return System.currentTimeMillis();
       
   197     }
       
   198     static void log(String s) {
       
   199         long time = currentTimeMillis() - startTime;
       
   200         long ms = time % 1000;
       
   201         time /= 1000;
       
   202         long sec = time % 60;
       
   203         time /= 60;
       
   204         long min = time % 60;
       
   205         time /= 60;
       
   206         System.out.println(""
       
   207                 + (time < 10 ? "0" : "") + time
       
   208                 + ":" + (min < 10 ? "0" : "") + min
       
   209                 + ":" + (sec < 10 ? "0" : "") + sec
       
   210                 + "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms
       
   211                 + " (" + Thread.currentThread().getName() + ") " + s);
       
   212     }
       
   213     static void delay(int millis) {
       
   214         try {
       
   215             Thread.sleep(millis);
       
   216         } catch (InterruptedException e) {}
       
   217     }
       
   218 }