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.MidiSystem;
  25 import javax.sound.midi.MidiUnavailableException;
  26 import javax.sound.midi.Transmitter;
  27 
  28 /**
  29  * @test
  30  * @bug 6415669
  31  * @summary Tests that terminating thread which got transmitter doesn't cause
  32  *          JVM crash (windows)
  33  * @run main bug6415669
  34  */
  35 public class bug6415669 {
  36 
  37     public static void main(String args[]) throws Exception {
  38         String osStr = System.getProperty("os.name");
  39         boolean isWin = osStr.toLowerCase().startsWith("windows");
  40         log("OS: " + osStr);
  41         log("Arch: " + System.getProperty("os.arch"));
  42         if (!isWin) {
  43             log("The test is for Windows only");
  44             return;
  45         }
  46 
  47         bug6415669 This = new bug6415669();
  48         if (This.test()) {
  49             log("Test sucessfully passed.");
  50         } else {
  51             log("Test FAILED!");
  52             throw new RuntimeException("Test FAILED!");
  53         }
  54     }
  55 
  56     volatile Transmitter transmitter = null;
  57     Thread openThread = null;
  58     boolean test() {
  59         openThread = new Thread(new Runnable() {
  60             public void run() {
  61                 try {
  62                     log("openThread: getting transmitter...");
  63                     transmitter = MidiSystem.getTransmitter();
  64                     log("openThread:   - OK: " + transmitter);
  65                 } catch (MidiUnavailableException ex) {
  66                     log("openThread:   - Exception: ");
  67                     ex.printStackTrace(System.out);
  68                     log("openThread: skipping...");
  69                 }
  70                 log("openThread: exiting...");
  71             }
  72         });
  73         log("starting openThread...");
  74         openThread.start();
  75 
  76         while (openThread.isAlive())
  77             delay(500);
  78         // make additional delay
  79         delay(500);
  80 
  81         if (transmitter == null) {
  82             return true;   // midi is not available, just ignore
  83         }
  84 
  85         log("closing transmitter");
  86         transmitter.close();
  87         log("  - OK");
  88 
  89         return true;
  90     }
  91 
  92     // helper routines
  93     static long startTime = currentTimeMillis();
  94     static long currentTimeMillis() {
  95         //return System.nanoTime() / 1000000L;
  96         return System.currentTimeMillis();
  97     }
  98     static void log(String s) {
  99         long time = currentTimeMillis() - startTime;
 100         long ms = time % 1000;
 101         time /= 1000;
 102         long sec = time % 60;
 103         time /= 60;
 104         long min = time % 60;
 105         time /= 60;
 106         System.out.println(""
 107                 + (time < 10 ? "0" : "") + time
 108                 + ":" + (min < 10 ? "0" : "") + min
 109                 + ":" + (sec < 10 ? "0" : "") + sec
 110                 + "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms
 111                 + " (" + Thread.currentThread().getName() + ") " + s);
 112     }
 113     static void delay(int millis) {
 114         try {
 115             Thread.sleep(millis);
 116         } catch (InterruptedException e) {}
 117     }
 118 }