|
1 /* |
|
2 * Copyright (c) 2003, 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.InvalidMidiDataException; |
|
25 import javax.sound.midi.MidiDevice; |
|
26 import javax.sound.midi.MidiSystem; |
|
27 import javax.sound.midi.MidiUnavailableException; |
|
28 import javax.sound.midi.Receiver; |
|
29 import javax.sound.midi.Sequencer; |
|
30 import javax.sound.midi.ShortMessage; |
|
31 import javax.sound.midi.Synthesizer; |
|
32 |
|
33 /** |
|
34 * @test |
|
35 * @bug 4616517 |
|
36 * @summary Receiver.send() does not work properly |
|
37 */ |
|
38 public class ClosedReceiver { |
|
39 |
|
40 public static void main(String[] args) throws Exception { |
|
41 out("#4616517: Receiver.send() does not work properly"); |
|
42 if (!isMidiInstalled()) { |
|
43 out("Soundcard does not exist or sound drivers not installed!"); |
|
44 out("This test requires sound drivers for execution."); |
|
45 return; |
|
46 } |
|
47 |
|
48 boolean passed = true; |
|
49 |
|
50 passed &= testReceiverSend(); |
|
51 passed &= testClosedReceivers(); |
|
52 if (passed) { |
|
53 out("Test PASSED."); |
|
54 } else { |
|
55 throw new Exception("Test FAILED."); |
|
56 } |
|
57 } |
|
58 |
|
59 /** |
|
60 * Execute Receiver.send() and expect that there is no exception. |
|
61 */ |
|
62 private static boolean testReceiverSend() { |
|
63 boolean result = true; |
|
64 |
|
65 Receiver receiver; |
|
66 ShortMessage shMsg = new ShortMessage(); |
|
67 |
|
68 try { |
|
69 receiver = MidiSystem.getReceiver(); |
|
70 shMsg.setMessage(ShortMessage.NOTE_ON, 0,60, 93); |
|
71 try { |
|
72 receiver.send( shMsg, -1 ); |
|
73 } catch(IllegalStateException ilEx) { |
|
74 ilEx.printStackTrace(System.out); |
|
75 out("IllegalStateException was thrown incorrectly!"); |
|
76 result = false; |
|
77 } |
|
78 receiver.close(); |
|
79 } catch(MidiUnavailableException e) { |
|
80 out("Midi unavailable, cannot test."); |
|
81 } catch(InvalidMidiDataException ine) { |
|
82 out("InvalidMidiDataException, cannot test."); |
|
83 } |
|
84 return result; |
|
85 } |
|
86 |
|
87 private static boolean testClosedReceivers() { |
|
88 boolean result = true; |
|
89 Receiver receiver; |
|
90 Synthesizer synt = null; |
|
91 |
|
92 // test Synthesizer's Receiver |
|
93 try { |
|
94 synt = MidiSystem.getSynthesizer(); |
|
95 synt.open(); |
|
96 } catch(MidiUnavailableException e) { |
|
97 out("Midi unavailable, cannot test."); |
|
98 return result; |
|
99 } |
|
100 try { |
|
101 receiver = synt.getReceiver(); |
|
102 } catch (MidiUnavailableException e) { |
|
103 out("unable to get Receiver from synthesizer, cannot test."); |
|
104 return result; |
|
105 } |
|
106 result &= testClosedReceiver(receiver); |
|
107 synt.close(); |
|
108 |
|
109 // test all MidiDevices' Receivers |
|
110 |
|
111 MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); |
|
112 for (int i = 0; i < devices.length; i++) { |
|
113 try { |
|
114 MidiDevice device = MidiSystem.getMidiDevice(devices[i]); |
|
115 if (device.getMaxReceivers() != 0) { |
|
116 receiver = device.getReceiver(); |
|
117 result &= testClosedReceiver(receiver); |
|
118 } |
|
119 } catch (Exception e) { |
|
120 out(e); |
|
121 out("cannot test."); |
|
122 return result; |
|
123 } |
|
124 } |
|
125 return result; |
|
126 } |
|
127 |
|
128 /** |
|
129 * Execute send() on a closed Receivers and expect IllegalStateException. |
|
130 */ |
|
131 private static boolean testClosedReceiver(Receiver receiver) { |
|
132 boolean result = true; |
|
133 out("testing Receiver: " + receiver); |
|
134 ShortMessage shMsg = new ShortMessage(); |
|
135 try { |
|
136 shMsg.setMessage(ShortMessage.NOTE_ON, 0,60, 93); |
|
137 } catch(InvalidMidiDataException e) { |
|
138 out(e); |
|
139 out("unable to construct ShortMessage, cannot test."); |
|
140 return result; |
|
141 } |
|
142 |
|
143 // begin of test |
|
144 receiver.close(); |
|
145 try { |
|
146 receiver.send( shMsg, -1 ); |
|
147 out("IllegalStateException was not thrown " |
|
148 + "on Receiver.send()!"); |
|
149 result = false; |
|
150 } catch(IllegalStateException e) { |
|
151 out("IllegalStateException was thrown. Ok."); |
|
152 } |
|
153 return result; |
|
154 } |
|
155 |
|
156 private static void out(Throwable t) { |
|
157 t.printStackTrace(System.out); |
|
158 } |
|
159 |
|
160 private static void out(String message) { |
|
161 System.out.println(message); |
|
162 } |
|
163 |
|
164 /** |
|
165 * Returns true if at least one MIDI (port) device is correctly installed on |
|
166 * the system. |
|
167 */ |
|
168 private static boolean isMidiInstalled() { |
|
169 boolean result = false; |
|
170 MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo(); |
|
171 for (int i = 0; i < devices.length; i++) { |
|
172 try { |
|
173 MidiDevice device = MidiSystem.getMidiDevice(devices[i]); |
|
174 result = !(device instanceof Sequencer) |
|
175 && !(device instanceof Synthesizer); |
|
176 } catch (Exception e1) { |
|
177 System.err.println(e1); |
|
178 } |
|
179 if (result) |
|
180 break; |
|
181 } |
|
182 return result; |
|
183 } |
|
184 } |