jdk/test/javax/sound/midi/Sequencer/SequencerImplicitSynthOpen.java
author serb
Mon, 31 Oct 2016 14:50:09 +0300
changeset 41905 e8e5df013c6e
parent 23010 6dadb192ad81
permissions -rw-r--r--
8167615: Opensource unit/regression tests for JavaSound Reviewed-by: amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21596
diff changeset
     2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
     4
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
     7
 * published by the Free Software Foundation.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
     8
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    13
 * accompanied this code).
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    14
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    18
 *
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    21
 * questions.
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    22
 */
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 8528
diff changeset
    23
8528
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    24
/**
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    25
 * @test
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    26
 * @bug 6660470
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    27
 * @summary Tests that sequencer correctly opens/closes (implicitly) devices
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    28
 * @author Alex Menkov
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    29
 */
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    30
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    31
import java.util.List;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    32
import javax.sound.midi.MidiDevice;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    33
import javax.sound.midi.MidiDeviceReceiver;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    34
import javax.sound.midi.MidiSystem;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    35
import javax.sound.midi.MidiUnavailableException;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    36
import javax.sound.midi.Receiver;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    37
import javax.sound.midi.Sequencer;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    38
import javax.sound.midi.Transmitter;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    39
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    40
public class SequencerImplicitSynthOpen {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    41
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    42
    static int TEST_COUNT   = 5;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    43
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    44
    public static void main(String[] args) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    45
        try {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    46
            log("getting sequencer...");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    47
            Sequencer sequencer = MidiSystem.getSequencer();
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    48
            log("  - got " + getDeviceStr(sequencer));
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    49
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    50
            // obtain connected device (usually synthesizer)
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    51
            MidiDevice synth = getConnectedDevice(sequencer);
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    52
            if (synth == null) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    53
                log("could not get connected device, returning");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    54
                return;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    55
            }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    56
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    57
            log("connected device: " + getDeviceStr(synth));
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    58
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    59
            int success = 0;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    60
            for (int i=0; i<TEST_COUNT; i++) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    61
                if (test(sequencer)) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    62
                    success++;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    63
                }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    64
            }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    65
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    66
            if (success != TEST_COUNT) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    67
                throw new RuntimeException("test FAILS");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    68
            }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    69
        } catch (MidiUnavailableException ex) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    70
            // this is not a failure
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    71
            log("Could not get Sequencer");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    72
        }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    73
        log("test PASSED.");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    74
    }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    75
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    76
    static boolean test(Sequencer sequencer) throws MidiUnavailableException {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    77
        log("");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    78
        log("opening sequencer...");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    79
        sequencer.open();   // opens connected synthesizer implicitly
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    80
        MidiDevice synth = getConnectedDevice(sequencer);
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    81
        log("  connected device: " + getDeviceStr(synth));
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    82
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    83
        log("closing sequencer...");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    84
        sequencer.close();  // closes the synth implicitly
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    85
        log("  synth is " + getDeviceStr(synth));
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    86
        MidiDevice synth2 = getConnectedDevice(sequencer);
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    87
        log("  currently connected device: " + getDeviceStr(synth2));
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    88
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    89
        if (synth != null && synth.isOpen()) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    90
            log("FAIL.");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    91
            return false;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    92
        }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    93
        log("OK.");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    94
        return true;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    95
    }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    96
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    97
    static MidiDevice getConnectedDevice(Sequencer sequencer) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    98
        List<Transmitter> trans = sequencer.getTransmitters();
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
    99
        log("  sequencer has " + trans.size() + " opened transmitters:");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   100
        for (Transmitter tr: trans) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   101
            Receiver r = tr.getReceiver();
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   102
            log("    " + getClassStr(tr) + " connected to " + getClassStr(r));
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   103
            if (r instanceof MidiDeviceReceiver) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   104
                MidiDeviceReceiver recv = (MidiDeviceReceiver)r;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   105
                MidiDevice dev = recv.getMidiDevice();
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   106
                log("      - receiver of " + getClassStr(dev));
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   107
                return dev;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   108
            } else {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   109
                log("      - does NOT implement MidiDeviceReceiver");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   110
            }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   111
        }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   112
        return null;
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   113
    }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   114
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   115
    static String getClassStr(Object o) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   116
        if (o == null) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   117
            return "<null>";
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   118
        }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   119
        return o.getClass().getName();
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   120
    }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   121
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   122
    static String getDeviceStr(MidiDevice dev) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   123
        if (dev == null) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   124
            return "NULL";
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   125
        }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   126
        return getClassStr(dev) + ", " + (dev.isOpen() ? "OPENED" : "CLOSED");
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   127
    }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   128
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   129
    static void log(String s) {
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   130
        System.out.println(s);
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   131
    }
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   132
f496950c5d98 6660470: RealTimeSequencer incorrectly opens (implicitly) synthesizer
amenkov
parents:
diff changeset
   133
}