Path: blob/master/test/jdk/javax/sound/midi/spi/MidiDeviceProvider/ExpectedNPEOnNull.java
41154 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Objects;2425import javax.sound.midi.MidiDevice;26import javax.sound.midi.MidiSystem;27import javax.sound.midi.spi.MidiDeviceProvider;2829import static java.util.ServiceLoader.load;3031/**32* @test33* @bug 814390934* @author Sergey Bylokhov35*/36public final class ExpectedNPEOnNull {3738public static void main(final String[] args) throws Exception {39testMS();40for (final MidiDeviceProvider mdp : load(MidiDeviceProvider.class)) {41testMDP(mdp);42}43testMDP(customMDP);44}4546/**47* Tests the part of MidiSystem API, which implemented via48* MidiDeviceProvider.49*/50private static void testMS() throws Exception {51// MidiSystem#getMidiDevice(MidiDevice.Info)52try {53MidiSystem.getMidiDevice(null);54throw new RuntimeException("NPE is expected");55} catch (final NullPointerException ignored) {56}57}5859/**60* Tests the MidiDeviceProvider API directly.61*/62private static void testMDP(final MidiDeviceProvider mdp) throws Exception {63// MidiDeviceProvider#isDeviceSupported(Info)64try {65mdp.isDeviceSupported(null);66throw new RuntimeException("NPE is expected");67} catch (final NullPointerException ignored) {68}69// MidiDeviceProvider#getDevice(Info)70try {71mdp.getDevice(null);72throw new RuntimeException("NPE is expected");73} catch (final NullPointerException ignored) {74}75}7677/**78* Tests some default implementation of MidiDeviceProvider API, using the79* custom {@code MidiDeviceProvider}, which support nothing.80*/81static MidiDeviceProvider customMDP = new MidiDeviceProvider() {82@Override83public MidiDevice.Info[] getDeviceInfo() {84return new MidiDevice.Info[0];85}8687@Override88public MidiDevice getDevice(MidiDevice.Info info) {89Objects.requireNonNull(info);90return null;91}92};93}949596