Path: blob/master/test/jdk/javax/sound/midi/spi/MidiFileWriter/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.io.File;24import java.io.OutputStream;25import java.util.Objects;2627import javax.sound.midi.MidiSystem;28import javax.sound.midi.Sequence;29import javax.sound.midi.spi.MidiFileWriter;3031import static java.util.ServiceLoader.load;3233/**34* @test35* @bug 814390936* @author Sergey Bylokhov37*/38public final class ExpectedNPEOnNull {3940public static void main(final String[] args) throws Exception {41testMS();42for (final MidiFileWriter mfw : load(MidiFileWriter.class)) {43testMFW(mfw);44}45testMFW(customMFW);46}4748/**49* Tests the part of MidiSystem API, which implemented via MidiFileWriter.50*/51private static void testMS() throws Exception {52// MidiSystem#getMidiFileTypes(Sequence)53try {54MidiSystem.getMidiFileTypes(null);55throw new RuntimeException("NPE is expected");56} catch (final NullPointerException ignored) {57}5859// MidiSystem#isFileTypeSupported(int, Sequence)60for (final int type : MidiSystem.getMidiFileTypes()) {61try {62MidiSystem.isFileTypeSupported(type, null);63throw new RuntimeException("NPE is expected");64} catch (final NullPointerException ignored) {65}66}67// MidiSystem#write(Sequence, int, OutputStream)68for (final int type : MidiSystem.getMidiFileTypes()) {69try {70MidiSystem.write(null, type, new NullOutputStream());71throw new RuntimeException("NPE is expected");72} catch (final NullPointerException ignored) {73}74}75for (final int type : MidiSystem.getMidiFileTypes()) {76try {77MidiSystem.write(new Sequence(0, 0), type, (OutputStream) null);78throw new RuntimeException("NPE is expected");79} catch (final NullPointerException ignored) {80}81}82for (final int type : MidiSystem.getMidiFileTypes()) {83try {84MidiSystem.write(null, type, (OutputStream) null);85throw new RuntimeException("NPE is expected");86} catch (final NullPointerException ignored) {87}88}89// MidiSystem#write(Sequence, int, File)90for (final int type : MidiSystem.getMidiFileTypes()) {91try {92MidiSystem.write(null, type, new File(""));93throw new RuntimeException("NPE is expected");94} catch (final NullPointerException ignored) {95}96}97for (final int type : MidiSystem.getMidiFileTypes()) {98try {99MidiSystem.write(new Sequence(0, 0), type, (File) null);100throw new RuntimeException("NPE is expected");101} catch (final NullPointerException ignored) {102}103}104for (final int type : MidiSystem.getMidiFileTypes()) {105try {106MidiSystem.write(null, type, (File) null);107throw new RuntimeException("NPE is expected");108} catch (final NullPointerException ignored) {109}110}111}112113/**114* Tests the MidiFileWriter API directly.115*/116private static void testMFW(final MidiFileWriter mfw) throws Exception {117// MidiFileWriter#getMidiFileTypes(Sequence)118try {119mfw.getMidiFileTypes(null);120throw new RuntimeException("NPE is expected");121} catch (final NullPointerException ignored) {122}123// MidiFileWriter#isFileTypeSupported(int, Sequence)124for (final int type : MidiSystem.getMidiFileTypes()) {125try {126mfw.isFileTypeSupported(type, null);127throw new RuntimeException("NPE is expected");128} catch (final NullPointerException ignored) {129}130}131// MidiFileWriter#write(Sequence, int, OutputStream)132for (final int type : MidiSystem.getMidiFileTypes()) {133try {134mfw.write(null, type, new NullOutputStream());135throw new RuntimeException("NPE is expected");136} catch (final NullPointerException ignored) {137}138}139for (final int type : MidiSystem.getMidiFileTypes()) {140try {141mfw.write(new Sequence(0, 0), type, (OutputStream) null);142throw new RuntimeException("NPE is expected");143} catch (final NullPointerException ignored) {144}145}146for (final int type : MidiSystem.getMidiFileTypes()) {147try {148mfw.write(null, type, (OutputStream) null);149throw new RuntimeException("NPE is expected");150} catch (final NullPointerException ignored) {151}152}153// MidiFileWriter#write(Sequence, int, File)154for (final int type : MidiSystem.getMidiFileTypes()) {155try {156mfw.write(null, type, new File(""));157throw new RuntimeException("NPE is expected");158} catch (final NullPointerException ignored) {159}160}161for (final int type : MidiSystem.getMidiFileTypes()) {162try {163mfw.write(new Sequence(0, 0), type, (File) null);164throw new RuntimeException("NPE is expected");165} catch (final NullPointerException ignored) {166}167}168for (final int type : MidiSystem.getMidiFileTypes()) {169try {170mfw.write(null, type, (File) null);171throw new RuntimeException("NPE is expected");172} catch (final NullPointerException ignored) {173}174}175}176/**177* Tests some default implementation of MidiFileWriter API, using the custom178* {@code MidiFileWriter}, which support nothing.179*/180static MidiFileWriter customMFW = new MidiFileWriter() {181@Override182public int[] getMidiFileTypes() {183return new int[0];184}185186@Override187public int[] getMidiFileTypes(Sequence sequence) {188Objects.requireNonNull(sequence);189return new int[0];190}191192@Override193public int write(Sequence in, int fileType, OutputStream out) {194Objects.requireNonNull(in);195Objects.requireNonNull(out);196return 0;197}198199@Override200public int write(Sequence in, int fileType, File out) {201Objects.requireNonNull(in);202Objects.requireNonNull(out);203return 0;204}205};206207private static final class NullOutputStream extends OutputStream {208209@Override210public void write(final int b) {211//do nothing212}213}214}215216217