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 
  24 import java.io.File;
  25 import java.io.OutputStream;
  26 import java.util.Objects;
  27 
  28 import javax.sound.midi.MidiSystem;
  29 import javax.sound.midi.Sequence;
  30 import javax.sound.midi.spi.MidiFileWriter;
  31 
  32 import static java.util.ServiceLoader.load;
  33 
  34 /**
  35  * @test
  36  * @bug 8143909
  37  * @author Sergey Bylokhov
  38  */
  39 public final class ExpectedNPEOnNull {
  40 
  41     public static void main(final String[] args) throws Exception {
  42         testMS();
  43         for (final MidiFileWriter mfw : load(MidiFileWriter.class)) {
  44             testMFW(mfw);
  45         }
  46         testMFW(customMFW);
  47     }
  48 
  49     /**
  50      * Tests the part of MidiSystem API, which implemented via MidiFileWriter.
  51      */
  52     private static void testMS() throws Exception {
  53         // MidiSystem#getMidiFileTypes(Sequence)
  54         try {
  55             MidiSystem.getMidiFileTypes(null);
  56             throw new RuntimeException("NPE is expected");
  57         } catch (final NullPointerException ignored) {
  58         }
  59 
  60         // MidiSystem#isFileTypeSupported(int, Sequence)
  61         for (final int type : MidiSystem.getMidiFileTypes()) {
  62             try {
  63                 MidiSystem.isFileTypeSupported(type, null);
  64                 throw new RuntimeException("NPE is expected");
  65             } catch (final NullPointerException ignored) {
  66             }
  67         }
  68         // MidiSystem#write(Sequence, int, OutputStream)
  69         for (final int type : MidiSystem.getMidiFileTypes()) {
  70             try {
  71                 MidiSystem.write(null, type, new NullOutputStream());
  72                 throw new RuntimeException("NPE is expected");
  73             } catch (final NullPointerException ignored) {
  74             }
  75         }
  76         for (final int type : MidiSystem.getMidiFileTypes()) {
  77             try {
  78                 MidiSystem.write(new Sequence(0, 0), type, (OutputStream) null);
  79                 throw new RuntimeException("NPE is expected");
  80             } catch (final NullPointerException ignored) {
  81             }
  82         }
  83         for (final int type : MidiSystem.getMidiFileTypes()) {
  84             try {
  85                 MidiSystem.write(null, type, (OutputStream) null);
  86                 throw new RuntimeException("NPE is expected");
  87             } catch (final NullPointerException ignored) {
  88             }
  89         }
  90         // MidiSystem#write(Sequence, int, File)
  91         for (final int type : MidiSystem.getMidiFileTypes()) {
  92             try {
  93                 MidiSystem.write(null, type, new File(""));
  94                 throw new RuntimeException("NPE is expected");
  95             } catch (final NullPointerException ignored) {
  96             }
  97         }
  98         for (final int type : MidiSystem.getMidiFileTypes()) {
  99             try {
 100                 MidiSystem.write(new Sequence(0, 0), type, (File) null);
 101                 throw new RuntimeException("NPE is expected");
 102             } catch (final NullPointerException ignored) {
 103             }
 104         }
 105         for (final int type : MidiSystem.getMidiFileTypes()) {
 106             try {
 107                 MidiSystem.write(null, type, (File) null);
 108                 throw new RuntimeException("NPE is expected");
 109             } catch (final NullPointerException ignored) {
 110             }
 111         }
 112     }
 113 
 114     /**
 115      * Tests the MidiFileWriter API directly.
 116      */
 117     private static void testMFW(final MidiFileWriter mfw) throws Exception {
 118         // MidiFileWriter#getMidiFileTypes(Sequence)
 119         try {
 120             mfw.getMidiFileTypes(null);
 121             throw new RuntimeException("NPE is expected");
 122         } catch (final NullPointerException ignored) {
 123         }
 124         // MidiFileWriter#isFileTypeSupported(int, Sequence)
 125         for (final int type : MidiSystem.getMidiFileTypes()) {
 126             try {
 127                 mfw.isFileTypeSupported(type, null);
 128                 throw new RuntimeException("NPE is expected");
 129             } catch (final NullPointerException ignored) {
 130             }
 131         }
 132         // MidiFileWriter#write(Sequence, int, OutputStream)
 133         for (final int type : MidiSystem.getMidiFileTypes()) {
 134             try {
 135                 mfw.write(null, type, new NullOutputStream());
 136                 throw new RuntimeException("NPE is expected");
 137             } catch (final NullPointerException ignored) {
 138             }
 139         }
 140         for (final int type : MidiSystem.getMidiFileTypes()) {
 141             try {
 142                 mfw.write(new Sequence(0, 0), type, (OutputStream) null);
 143                 throw new RuntimeException("NPE is expected");
 144             } catch (final NullPointerException ignored) {
 145             }
 146         }
 147         for (final int type : MidiSystem.getMidiFileTypes()) {
 148             try {
 149                 mfw.write(null, type, (OutputStream) null);
 150                 throw new RuntimeException("NPE is expected");
 151             } catch (final NullPointerException ignored) {
 152             }
 153         }
 154         // MidiFileWriter#write(Sequence, int, File)
 155         for (final int type : MidiSystem.getMidiFileTypes()) {
 156             try {
 157                 mfw.write(null, type, new File(""));
 158                 throw new RuntimeException("NPE is expected");
 159             } catch (final NullPointerException ignored) {
 160             }
 161         }
 162         for (final int type : MidiSystem.getMidiFileTypes()) {
 163             try {
 164                 mfw.write(new Sequence(0, 0), type, (File) null);
 165                 throw new RuntimeException("NPE is expected");
 166             } catch (final NullPointerException ignored) {
 167             }
 168         }
 169         for (final int type : MidiSystem.getMidiFileTypes()) {
 170             try {
 171                 mfw.write(null, type, (File) null);
 172                 throw new RuntimeException("NPE is expected");
 173             } catch (final NullPointerException ignored) {
 174             }
 175         }
 176     }
 177     /**
 178      * Tests some default implementation of MidiFileWriter API, using the custom
 179      * {@code MidiFileWriter}, which support nothing.
 180      */
 181     static MidiFileWriter customMFW = new MidiFileWriter() {
 182         @Override
 183         public int[] getMidiFileTypes() {
 184             return new int[0];
 185         }
 186 
 187         @Override
 188         public int[] getMidiFileTypes(Sequence sequence) {
 189             Objects.requireNonNull(sequence);
 190             return new int[0];
 191         }
 192 
 193         @Override
 194         public int write(Sequence in, int fileType, OutputStream out) {
 195             Objects.requireNonNull(in);
 196             Objects.requireNonNull(out);
 197             return 0;
 198         }
 199 
 200         @Override
 201         public int write(Sequence in, int fileType, File out) {
 202             Objects.requireNonNull(in);
 203             Objects.requireNonNull(out);
 204             return 0;
 205         }
 206     };
 207 
 208     private static final class NullOutputStream extends OutputStream {
 209 
 210         @Override
 211         public void write(final int b) {
 212             //do nothing
 213         }
 214     }
 215 }