jdk/test/javax/sound/midi/Transmitter/bug6415669.java
author serb
Mon, 31 Oct 2016 14:50:09 +0300
changeset 41905 e8e5df013c6e
permissions -rw-r--r--
8167615: Opensource unit/regression tests for JavaSound Reviewed-by: amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41905
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     1
/*
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     2
 * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     4
 *
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     8
 *
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    13
 * accompanied this code).
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    14
 *
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    18
 *
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    21
 * questions.
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    22
 */
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    23
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    24
import javax.sound.midi.MidiSystem;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    25
import javax.sound.midi.MidiUnavailableException;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    26
import javax.sound.midi.Transmitter;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    27
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    28
/**
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    29
 * @test
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    30
 * @bug 6415669
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    31
 * @summary Tests that terminating thread which got transmitter doesn't cause
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    32
 *          JVM crash (windows)
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    33
 * @run main bug6415669
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    34
 */
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    35
public class bug6415669 {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    36
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    37
    public static void main(String args[]) throws Exception {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    38
        String osStr = System.getProperty("os.name");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    39
        boolean isWin = osStr.toLowerCase().startsWith("windows");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    40
        log("OS: " + osStr);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    41
        log("Arch: " + System.getProperty("os.arch"));
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    42
        if (!isWin) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    43
            log("The test is for Windows only");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    44
            return;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    45
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    46
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    47
        bug6415669 This = new bug6415669();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    48
        if (This.test()) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    49
            log("Test sucessfully passed.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    50
        } else {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    51
            log("Test FAILED!");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    52
            throw new RuntimeException("Test FAILED!");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    53
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    54
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    55
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    56
    volatile Transmitter transmitter = null;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    57
    Thread openThread = null;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    58
    boolean test() {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    59
        openThread = new Thread(new Runnable() {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    60
            public void run() {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    61
                try {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    62
                    log("openThread: getting transmitter...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    63
                    transmitter = MidiSystem.getTransmitter();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    64
                    log("openThread:   - OK: " + transmitter);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    65
                } catch (MidiUnavailableException ex) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    66
                    log("openThread:   - Exception: ");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    67
                    ex.printStackTrace(System.out);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    68
                    log("openThread: skipping...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    69
                }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    70
                log("openThread: exiting...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    71
            }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    72
        });
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    73
        log("starting openThread...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    74
        openThread.start();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    75
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    76
        while (openThread.isAlive())
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    77
            delay(500);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    78
        // make additional delay
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    79
        delay(500);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    80
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    81
        if (transmitter == null) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    82
            return true;   // midi is not available, just ignore
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    83
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    84
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    85
        log("closing transmitter");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    86
        transmitter.close();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    87
        log("  - OK");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    88
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    89
        return true;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    90
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    91
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    92
    // helper routines
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    93
    static long startTime = currentTimeMillis();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    94
    static long currentTimeMillis() {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    95
        //return System.nanoTime() / 1000000L;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    96
        return System.currentTimeMillis();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    97
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    98
    static void log(String s) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    99
        long time = currentTimeMillis() - startTime;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   100
        long ms = time % 1000;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   101
        time /= 1000;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   102
        long sec = time % 60;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   103
        time /= 60;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   104
        long min = time % 60;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   105
        time /= 60;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   106
        System.out.println(""
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   107
                + (time < 10 ? "0" : "") + time
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   108
                + ":" + (min < 10 ? "0" : "") + min
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   109
                + ":" + (sec < 10 ? "0" : "") + sec
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   110
                + "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   111
                + " (" + Thread.currentThread().getName() + ") " + s);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   112
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   113
    static void delay(int millis) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   114
        try {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   115
            Thread.sleep(millis);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   116
        } catch (InterruptedException e) {}
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   117
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   118
}