1/*
2 * Copyright (c) 2015, 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
24import java.util.Objects;
25
26import javax.sound.midi.MidiDevice;
27import javax.sound.midi.MidiSystem;
28import javax.sound.midi.spi.MidiDeviceProvider;
29
30import static java.util.ServiceLoader.load;
31
32/**
33 * @test
34 * @bug 8143909
35 * @author Sergey Bylokhov
36 */
37public final class ExpectedNPEOnNull {
38
39    public static void main(final String[] args) throws Exception {
40        testMS();
41        for (final MidiDeviceProvider mdp : load(MidiDeviceProvider.class)) {
42            testMDP(mdp);
43        }
44        testMDP(customMDP);
45    }
46
47    /**
48     * Tests the part of MidiSystem API, which implemented via
49     * MidiDeviceProvider.
50     */
51    private static void testMS() throws Exception {
52        // MidiSystem#getMidiDevice(MidiDevice.Info)
53        try {
54            MidiSystem.getMidiDevice(null);
55            throw new RuntimeException("NPE is expected");
56        } catch (final NullPointerException ignored) {
57        }
58    }
59
60    /**
61     * Tests the MidiDeviceProvider API directly.
62     */
63    private static void testMDP(final MidiDeviceProvider mdp) throws Exception {
64        // MidiDeviceProvider#isDeviceSupported(Info)
65        try {
66            mdp.isDeviceSupported(null);
67            throw new RuntimeException("NPE is expected");
68        } catch (final NullPointerException ignored) {
69        }
70        // MidiDeviceProvider#getDevice(Info)
71        try {
72            mdp.getDevice(null);
73            throw new RuntimeException("NPE is expected");
74        } catch (final NullPointerException ignored) {
75        }
76    }
77
78    /**
79     * Tests some default implementation of MidiDeviceProvider API, using the
80     * custom {@code MidiDeviceProvider}, which support nothing.
81     */
82    static MidiDeviceProvider customMDP = new MidiDeviceProvider() {
83        @Override
84        public MidiDevice.Info[] getDeviceInfo() {
85            return new MidiDevice.Info[0];
86        }
87
88        @Override
89        public MidiDevice getDevice(MidiDevice.Info info) {
90            Objects.requireNonNull(info);
91            return null;
92        }
93    };
94}
95