Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileWriter/ExpectedNPEOnNull.java
41159 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.ByteArrayInputStream;24import java.io.File;25import java.io.OutputStream;26import java.util.ArrayList;27import java.util.List;28import java.util.Objects;2930import javax.sound.sampled.AudioFileFormat;31import javax.sound.sampled.AudioFormat;32import javax.sound.sampled.AudioInputStream;33import javax.sound.sampled.AudioSystem;34import javax.sound.sampled.spi.AudioFileWriter;3536import static java.util.ServiceLoader.load;37import static javax.sound.sampled.AudioFileFormat.Type;38import static javax.sound.sampled.AudioFormat.*;3940/**41* @test42* @bug 813510043* @author Sergey Bylokhov44*/45public final class ExpectedNPEOnNull {4647/**48* We will try to use all encoding, in this case all our providers will be49* covered by supported/unsupported encoding.50*/51private static final Encoding[] encodings = {Encoding.PCM_SIGNED,52Encoding.PCM_UNSIGNED,53Encoding.PCM_FLOAT,54Encoding.ULAW, Encoding.ALAW,55new Encoding("test")};5657/**58* We will try to use all types, in this case all our providers will be59* covered by supported/unsupported types.60*/61private static final Type[] types = {Type.WAVE, Type.AU, Type.AIFF,62Type.AIFC, Type.SND,63new Type("MIDI", "mid"),64new Type("test", "test")};6566/**67* We will try to use all supported AudioInputStream, in this case all our68* providers will be covered by supported/unsupported streams.69*/70private static final List<AudioInputStream> aiss = new ArrayList<>();7172static {73for (final Encoding encoding : encodings) {74for (final Type type : types) {75aiss.add(getAIS(type, encoding));76}77}78}7980public static void main(final String[] args) throws Exception {81testAS();82for (final AudioFileWriter afw : load(AudioFileWriter.class)) {83testAFW(afw);84}85testAFW(customAFW);86}8788/**89* Tests the part of AudioSystem API, which implemented via AudioFileWriter.90*/91private static void testAS() throws Exception {9293// AudioSystem#getAudioFileTypes(AudioInputStream)94try {95AudioSystem.getAudioFileTypes(null);96throw new RuntimeException("NPE is expected");97} catch (final NullPointerException ignored) {98}99100// AudioSystem#isFileTypeSupported(Type)101try {102AudioSystem.isFileTypeSupported(null);103throw new RuntimeException("NPE is expected");104} catch (final NullPointerException ignored) {105}106107// AudioSystem#isFileTypeSupported(Type, AudioInputStream)108for (final Type type : types) {109try {110AudioSystem.isFileTypeSupported(type, null);111throw new RuntimeException("NPE is expected");112} catch (final NullPointerException ignored) {113}114}115for (final AudioInputStream stream : aiss) {116try {117AudioSystem.isFileTypeSupported(null, stream);118throw new RuntimeException("NPE is expected");119} catch (final NullPointerException ignored) {120}121}122123// AudioSystem#write(AudioInputStream, Type, OutputStream)124for (final Type type : types) {125try {126AudioSystem.write(null, type, new NullOutputStream());127throw new RuntimeException("NPE is expected");128} catch (final NullPointerException ignored) {129}130}131for (final AudioInputStream stream : aiss) {132try {133AudioSystem.write(stream, null, new NullOutputStream());134throw new RuntimeException("NPE is expected");135} catch (final NullPointerException ignored) {136}137}138for (final Type type : types) {139for (final AudioInputStream stream : aiss) {140try {141AudioSystem.write(stream, type, (OutputStream) null);142throw new RuntimeException("NPE is expected");143} catch (final NullPointerException ignored) {144}145}146}147148// AudioSystem#write(AudioInputStream, Type, File)149for (final Type type : types) {150try {151AudioSystem.write(null, type, new File("test.sound"));152throw new RuntimeException("NPE is expected");153} catch (final NullPointerException ignored) {154}155}156157for (final AudioInputStream stream : aiss) {158try {159AudioSystem.write(stream, null, new File("test.sound"));160throw new RuntimeException("NPE is expected");161} catch (final NullPointerException ignored) {162}163}164for (final AudioInputStream stream : aiss) {165for (final Type type : types) {166try {167AudioSystem.write(stream, type, (File) null);168throw new RuntimeException("NPE is expected");169} catch (final NullPointerException ignored) {170}171}172}173}174175/**176* Tests the AudioFileWriter API directly.177*/178private static void testAFW(final AudioFileWriter afw) throws Exception {179180// AudioFileWriter#isFileTypeSupported(Type)181try {182afw.isFileTypeSupported(null);183throw new RuntimeException("NPE is expected: " + afw);184} catch (final NullPointerException ignored) {185}186187// AudioFileWriter#getAudioFileTypes(AudioInputStream)188try {189afw.getAudioFileTypes(null);190throw new RuntimeException("NPE is expected: " + afw);191} catch (final NullPointerException ignored) {192}193194// AudioFileWriter#isFileTypeSupported(Type, AudioInputStream)195for (final Type type : types) {196try {197afw.isFileTypeSupported(type, null);198throw new RuntimeException("NPE is expected: " + afw);199} catch (final NullPointerException ignored) {200}201}202for (final AudioInputStream stream : aiss) {203try {204afw.isFileTypeSupported(null, stream);205throw new RuntimeException("NPE is expected: " + afw);206} catch (final NullPointerException ignored) {207}208}209210// AudioFileWriter#write(AudioInputStream, Type, OutputStream)211for (final Type type : types) {212try {213afw.write(null, type, new NullOutputStream());214throw new RuntimeException("NPE is expected: " + afw);215} catch (final NullPointerException ignored) {216}217}218for (final AudioInputStream stream : aiss) {219try {220afw.write(stream, null, new NullOutputStream());221throw new RuntimeException("NPE is expected: " + afw);222} catch (final NullPointerException ignored) {223}224}225for (final AudioInputStream stream : aiss) {226for (final Type type : types) {227try {228afw.write(stream, type, (OutputStream) null);229throw new RuntimeException("NPE is expected: " + afw);230} catch (final NullPointerException ignored) {231}232}233}234235// AudioFileWriter#write(AudioInputStream, Type, File)236for (final Type type : types) {237try {238afw.write(null, type, new File("test.sound"));239throw new RuntimeException("NPE is expected: " + afw);240} catch (final NullPointerException ignored) {241}242}243for (final AudioInputStream stream : aiss) {244try {245afw.write(stream, null, new File("test.sound"));246throw new RuntimeException("NPE is expected: " + afw);247} catch (final NullPointerException ignored) {248}249}250for (final AudioInputStream stream : aiss) {251for (final Type type : types) {252try {253afw.write(stream, type, (File) null);254throw new RuntimeException("NPE is expected: " + afw);255} catch (final NullPointerException ignored) {256}257}258}259}260261/**262* Tests some default implementation of AudioFileWriter API, using the263* custom {@code AudioFileWriter}, which support nothing.264*/265static final AudioFileWriter customAFW = new AudioFileWriter() {266@Override267public Type[] getAudioFileTypes() {268return new Type[0];269}270271@Override272public Type[] getAudioFileTypes(final AudioInputStream stream) {273Objects.requireNonNull(stream);274return new Type[0];275}276277@Override278public int write(AudioInputStream stream, Type fileType,279OutputStream out) {280Objects.requireNonNull(stream);281Objects.requireNonNull(fileType);282Objects.requireNonNull(out);283return 0;284}285286@Override287public int write(AudioInputStream stream, Type fileType, File out) {288Objects.requireNonNull(stream);289Objects.requireNonNull(fileType);290Objects.requireNonNull(out);291return 0;292}293};294295private static AudioInputStream getAIS(final Type type, Encoding encoding) {296AudioFormat af = new AudioFormat(encoding, 44100.0f, 16, 2, 1, 1, true);297AudioFileFormat aif = new AudioFileFormat(type, af, 0);298ByteArrayInputStream bais = new ByteArrayInputStream(new byte[1024]);299return new AudioInputStream(bais, aif.getFormat(), 0);300}301302private static final class NullOutputStream extends OutputStream {303304@Override305public void write(final int b) {306//do nothing307}308}309}310311312