Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/midi/MidiSystem/DefaultDevices.java
38853 views
1
/*
2
* Copyright (c) 2003, 2020, 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 java.util.List;
25
26
import javax.sound.midi.MidiDevice;
27
import javax.sound.midi.MidiSystem;
28
import javax.sound.midi.MidiUnavailableException;
29
import javax.sound.midi.Receiver;
30
import javax.sound.midi.Sequencer;
31
import javax.sound.midi.Synthesizer;
32
import javax.sound.midi.Transmitter;
33
import javax.sound.midi.spi.MidiDeviceProvider;
34
35
import com.sun.media.sound.JDK13Services;
36
37
/**
38
* @test
39
* @bug 4776511
40
* @bug 4934509
41
* @bug 4938236
42
* @run main/timeout=600 DefaultDevices
43
* @summary RFE: Setting the default MixerProvider
44
*/
45
/** Test the retrieving of MidiDevices with default device properties.
46
* This is a part of the test for 4776511.
47
* The test also functions as a unit test for 4934509: SPEC: Document
48
* explicitely MidiSystem.getReceiver's behavior
49
* and a regession test for 4938236: Crash when opening synthesizer implicitly
50
* The test has been updated to reflect a fix for 6411624: MidiSystem.getSequencer()
51
* doesn't throw MidiUnavaivableException if no audio card installed (see also
52
* 6422786: regression test javax/sound/midi/MidiSystem/DefaultDevices.java fails)
53
*/
54
public class DefaultDevices {
55
56
private static final String ERROR_PROVIDER_CLASS_NAME = "abc";
57
private static final String ERROR_INSTANCE_NAME = "def";
58
59
private static final Class RECEIVER_CLASS = javax.sound.midi.Receiver.class;
60
private static final Class TRANSMITTER_CLASS = javax.sound.midi.Transmitter.class;
61
private static final Class SEQUENCER_CLASS = javax.sound.midi.Sequencer.class;
62
private static final Class SYNTHESIZER_CLASS = javax.sound.midi.Synthesizer.class;
63
64
public static void main(String[] args) throws Exception {
65
boolean allOk = true;
66
MidiDevice.Info[] infos;
67
68
out("\nTesting MidiDevices retrieved via MidiSystem");
69
infos = MidiSystem.getMidiDeviceInfo();
70
allOk &= testDevices(infos, null);
71
72
out("\nTesting MidiDevices retrieved from MidiDeviceProviders");
73
List providers = JDK13Services.getProviders(MidiDeviceProvider.class);
74
for (int i = 0; i < providers.size(); i++) {
75
MidiDeviceProvider provider = (MidiDeviceProvider)providers.get(i);
76
infos = provider.getDeviceInfo();
77
allOk &= testDevices(infos, provider.getClass().getName());
78
}
79
80
if (!allOk) {
81
throw new Exception("Test failed");
82
} else {
83
out("Test passed");
84
}
85
}
86
87
private static boolean testDevices(MidiDevice.Info[] infos,
88
String providerClassName) {
89
boolean allOk = true;
90
91
for (int i = 0; i < infos.length; i++) {
92
MidiDevice device = null;
93
try {
94
device = MidiSystem.getMidiDevice(infos[i]);
95
} catch (MidiUnavailableException e) {
96
out("Exception thrown; Test NOT failed.");
97
e.printStackTrace(System.out);
98
out("");
99
}
100
out("\nTesting device: " + device);
101
if (device instanceof Sequencer) {
102
allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, true, true);
103
// incorrect cases
104
allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);
105
allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, false);
106
allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);
107
}
108
if (device instanceof Synthesizer) {
109
allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, true, true);
110
allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, true);
111
// incorrect cases
112
allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);
113
allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);
114
}
115
if (device instanceof Receiver) {
116
allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, true, true);
117
// incorrect cases
118
allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, false, false);
119
allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);
120
allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);
121
}
122
if (device instanceof Transmitter) {
123
allOk &= testDevice(device, TRANSMITTER_CLASS, providerClassName, true, true);
124
// incorrect cases
125
allOk &= testDevice(device, RECEIVER_CLASS, providerClassName, false, false);
126
allOk &= testDevice(device, SYNTHESIZER_CLASS, providerClassName, false, false);
127
allOk &= testDevice(device, SEQUENCER_CLASS, providerClassName, false, false);
128
}
129
}
130
return allOk;
131
}
132
133
private static boolean testDevice(MidiDevice device, Class type,
134
String providerClassName, boolean testWrong, boolean expectedResult) {
135
boolean allOk = true;
136
String instanceName = device.getDeviceInfo().getName();
137
138
// no error
139
allOk &= testDevice(device, type, providerClassName,
140
instanceName, expectedResult);
141
142
if (testWrong) {
143
// erroneous provider class name, correct instance name
144
allOk &= testDevice(device, type, ERROR_PROVIDER_CLASS_NAME,
145
instanceName, expectedResult);
146
147
// correct provider class name, erroneous instance name
148
// we presume that provider provides only one class of requested type
149
allOk &= testDevice(device, type, providerClassName,
150
ERROR_INSTANCE_NAME, expectedResult);
151
}
152
153
return allOk;
154
}
155
156
private static boolean testDevice(MidiDevice device, Class type,
157
String providerClassName, String instanceName,
158
boolean expectedResult) {
159
boolean allOk = true;
160
161
try {
162
String propertyName = type.getName();
163
String propertyValue = (providerClassName != null) ? providerClassName: "" ;
164
propertyValue += "#" + instanceName;
165
out("property: " + propertyName + "="+ propertyValue);
166
System.setProperty(propertyName, propertyValue);
167
Object reference = null;
168
Object result = null;
169
if (type == SEQUENCER_CLASS) {
170
reference = device;
171
result = MidiSystem.getSequencer();
172
} else if (type == SYNTHESIZER_CLASS) {
173
reference = device;
174
result = MidiSystem.getSynthesizer();
175
} else if (type == RECEIVER_CLASS) {
176
reference = device.getReceiver();
177
result = MidiSystem.getReceiver();
178
} else if (type == TRANSMITTER_CLASS) {
179
reference = device.getTransmitter();
180
result = MidiSystem.getTransmitter();
181
}
182
out("result: " + result);
183
boolean rightDevice = (reference.getClass() == result.getClass());
184
if (rightDevice != expectedResult) {
185
out("\nERROR: type " + type + " failed:"
186
+ " class should" + (expectedResult ? "" : " NOT") + " be '"
187
+ reference.getClass()
188
+ "' but is '" + result.getClass() + "'!\n");
189
allOk = false;
190
}
191
if (expectedResult
192
&& reference instanceof MidiDevice
193
&& result instanceof MidiDevice) {
194
MidiDevice referenceDevice = (MidiDevice)reference;
195
MidiDevice resultDevice = (MidiDevice)result;
196
if (!referenceDevice.getDeviceInfo().getName().equals(
197
resultDevice.getDeviceInfo().getName())) {
198
out("\nERROR: type " + type + " failed: name should be '"
199
+ referenceDevice.getDeviceInfo().getName()
200
+ "' but is '"
201
+ resultDevice.getDeviceInfo().getName() + "'!\n");
202
allOk = false;
203
}
204
}
205
if (result instanceof Receiver) {
206
((Receiver)result).close();
207
} else if (result instanceof Transmitter) {
208
((Transmitter)result).close();
209
} else if (result instanceof Synthesizer) {
210
((Synthesizer)result).close();
211
} else if (result instanceof Sequencer) {
212
((Sequencer)result).close();
213
}
214
} catch (Exception e) {
215
out("Exception thrown; Test NOT failed.");
216
e.printStackTrace(System.out);
217
out("");
218
}
219
return allOk;
220
}
221
222
private static void out(String message) {
223
System.out.println(message);
224
}
225
}
226
227