SequencerImplicitSynthOpen.java revision 8729:0242fce0f717
1/*
2 * Copyright (c) 2011, 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/**
25 * @test
26 * @bug 6660470
27 * @summary Tests that sequencer correctly opens/closes (implicitly) devices
28 * @author Alex Menkov
29 */
30
31import java.util.List;
32import javax.sound.midi.MidiDevice;
33import javax.sound.midi.MidiDeviceReceiver;
34import javax.sound.midi.MidiSystem;
35import javax.sound.midi.MidiUnavailableException;
36import javax.sound.midi.Receiver;
37import javax.sound.midi.Sequencer;
38import javax.sound.midi.Transmitter;
39
40public class SequencerImplicitSynthOpen {
41
42    static int TEST_COUNT   = 5;
43
44    public static void main(String[] args) {
45        try {
46            log("getting sequencer...");
47            Sequencer sequencer = MidiSystem.getSequencer();
48            log("  - got " + getDeviceStr(sequencer));
49
50            // obtain connected device (usually synthesizer)
51            MidiDevice synth = getConnectedDevice(sequencer);
52            if (synth == null) {
53                log("could not get connected device, returning");
54                return;
55            }
56
57            log("connected device: " + getDeviceStr(synth));
58
59            int success = 0;
60            for (int i=0; i<TEST_COUNT; i++) {
61                if (test(sequencer)) {
62                    success++;
63                }
64            }
65
66            if (success != TEST_COUNT) {
67                throw new RuntimeException("test FAILS");
68            }
69        } catch (MidiUnavailableException ex) {
70            // this is not a failure
71            log("Could not get Sequencer");
72        }
73        log("test PASSED.");
74    }
75
76    static boolean test(Sequencer sequencer) throws MidiUnavailableException {
77        log("");
78        log("opening sequencer...");
79        sequencer.open();   // opens connected synthesizer implicitly
80        MidiDevice synth = getConnectedDevice(sequencer);
81        log("  connected device: " + getDeviceStr(synth));
82
83        log("closing sequencer...");
84        sequencer.close();  // closes the synth implicitly
85        log("  synth is " + getDeviceStr(synth));
86        MidiDevice synth2 = getConnectedDevice(sequencer);
87        log("  currently connected device: " + getDeviceStr(synth2));
88
89        if (synth != null && synth.isOpen()) {
90            log("FAIL.");
91            return false;
92        }
93        log("OK.");
94        return true;
95    }
96
97    static MidiDevice getConnectedDevice(Sequencer sequencer) {
98        List<Transmitter> trans = sequencer.getTransmitters();
99        log("  sequencer has " + trans.size() + " opened transmitters:");
100        for (Transmitter tr: trans) {
101            Receiver r = tr.getReceiver();
102            log("    " + getClassStr(tr) + " connected to " + getClassStr(r));
103            if (r instanceof MidiDeviceReceiver) {
104                MidiDeviceReceiver recv = (MidiDeviceReceiver)r;
105                MidiDevice dev = recv.getMidiDevice();
106                log("      - receiver of " + getClassStr(dev));
107                return dev;
108            } else {
109                log("      - does NOT implement MidiDeviceReceiver");
110            }
111        }
112        return null;
113    }
114
115    static String getClassStr(Object o) {
116        if (o == null) {
117            return "<null>";
118        }
119        return o.getClass().getName();
120    }
121
122    static String getDeviceStr(MidiDevice dev) {
123        if (dev == null) {
124            return "NULL";
125        }
126        return getClassStr(dev) + ", " + (dev.isOpen() ? "OPENED" : "CLOSED");
127    }
128
129    static void log(String s) {
130        System.out.println(s);
131    }
132
133}
134