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.ByteArrayInputStream; 25 import java.io.File; 26 import java.io.OutputStream; 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.Objects; 30 31 import javax.sound.sampled.AudioFileFormat; 32 import javax.sound.sampled.AudioFormat; 33 import javax.sound.sampled.AudioInputStream; 34 import javax.sound.sampled.AudioSystem; 35 import javax.sound.sampled.spi.AudioFileWriter; 36 37 import static java.util.ServiceLoader.load; 38 import static javax.sound.sampled.AudioFileFormat.Type; 39 import static javax.sound.sampled.AudioFormat.*; 40 41 /** 42 * @test 43 * @bug 8135100 44 * @author Sergey Bylokhov 45 */ 46 public final class ExpectedNPEOnNull { 47 48 /** 49 * We will try to use all encoding, in this case all our providers will be 50 * covered by supported/unsupported encoding. 51 */ 52 private static final Encoding[] encodings = {Encoding.PCM_SIGNED, 53 Encoding.PCM_UNSIGNED, 54 Encoding.PCM_FLOAT, 55 Encoding.ULAW, Encoding.ALAW, 56 new Encoding("test")}; 57 58 /** 59 * We will try to use all types, in this case all our providers will be 60 * covered by supported/unsupported types. 61 */ 62 private static final Type[] types = {Type.WAVE, Type.AU, Type.AIFF, 63 Type.AIFC, Type.SND, 64 new Type("MIDI", "mid"), 65 new Type("test", "test")}; 66 67 /** 68 * We will try to use all supported AudioInputStream, in this case all our 69 * providers will be covered by supported/unsupported streams. 70 */ 71 private static final List<AudioInputStream> aiss = new ArrayList<>(); 72 73 static { 74 for (final Encoding encoding : encodings) { 75 for (final Type type : types) { 76 aiss.add(getAIS(type, encoding)); 77 } 78 } 79 } 80 81 public static void main(final String[] args) throws Exception { 82 testAS(); 83 for (final AudioFileWriter afw : load(AudioFileWriter.class)) { 84 testAFW(afw); 85 } 86 testAFW(customAFW); 87 } 88 89 /** 90 * Tests the part of AudioSystem API, which implemented via AudioFileWriter. 91 */ 92 private static void testAS() throws Exception { 93 94 // AudioSystem#getAudioFileTypes(AudioInputStream) 95 try { 96 AudioSystem.getAudioFileTypes(null); 97 throw new RuntimeException("NPE is expected"); 98 } catch (final NullPointerException ignored) { 99 } 100 101 // AudioSystem#isFileTypeSupported(Type) 102 try { 103 AudioSystem.isFileTypeSupported(null); 104 throw new RuntimeException("NPE is expected"); 105 } catch (final NullPointerException ignored) { 106 } 107 108 // AudioSystem#isFileTypeSupported(Type, AudioInputStream) 109 for (final Type type : types) { 110 try { 111 AudioSystem.isFileTypeSupported(type, null); 112 throw new RuntimeException("NPE is expected"); 113 } catch (final NullPointerException ignored) { 114 } 115 } 116 for (final AudioInputStream stream : aiss) { 117 try { 118 AudioSystem.isFileTypeSupported(null, stream); 119 throw new RuntimeException("NPE is expected"); 120 } catch (final NullPointerException ignored) { 121 } 122 } 123 124 // AudioSystem#write(AudioInputStream, Type, OutputStream) 125 for (final Type type : types) { 126 try { 127 AudioSystem.write(null, type, new NullOutputStream()); 128 throw new RuntimeException("NPE is expected"); 129 } catch (final NullPointerException ignored) { 130 } 131 } 132 for (final AudioInputStream stream : aiss) { 133 try { 134 AudioSystem.write(stream, null, new NullOutputStream()); 135 throw new RuntimeException("NPE is expected"); 136 } catch (final NullPointerException ignored) { 137 } 138 } 139 for (final Type type : types) { 140 for (final AudioInputStream stream : aiss) { 141 try { 142 AudioSystem.write(stream, type, (OutputStream) null); 143 throw new RuntimeException("NPE is expected"); 144 } catch (final NullPointerException ignored) { 145 } 146 } 147 } 148 149 // AudioSystem#write(AudioInputStream, Type, File) 150 for (final Type type : types) { 151 try { 152 AudioSystem.write(null, type, new File("test.sound")); 153 throw new RuntimeException("NPE is expected"); 154 } catch (final NullPointerException ignored) { 155 } 156 } 157 158 for (final AudioInputStream stream : aiss) { 159 try { 160 AudioSystem.write(stream, null, new File("test.sound")); 161 throw new RuntimeException("NPE is expected"); 162 } catch (final NullPointerException ignored) { 163 } 164 } 165 for (final AudioInputStream stream : aiss) { 166 for (final Type type : types) { 167 try { 168 AudioSystem.write(stream, type, (File) null); 169 throw new RuntimeException("NPE is expected"); 170 } catch (final NullPointerException ignored) { 171 } 172 } 173 } 174 } 175 176 /** 177 * Tests the AudioFileWriter API directly. 178 */ 179 private static void testAFW(final AudioFileWriter afw) throws Exception { 180 181 // AudioFileWriter#isFileTypeSupported(Type) 182 try { 183 afw.isFileTypeSupported(null); 184 throw new RuntimeException("NPE is expected: " + afw); 185 } catch (final NullPointerException ignored) { 186 } 187 188 // AudioFileWriter#getAudioFileTypes(AudioInputStream) 189 try { 190 afw.getAudioFileTypes(null); 191 throw new RuntimeException("NPE is expected: " + afw); 192 } catch (final NullPointerException ignored) { 193 } 194 195 // AudioFileWriter#isFileTypeSupported(Type, AudioInputStream) 196 for (final Type type : types) { 197 try { 198 afw.isFileTypeSupported(type, null); 199 throw new RuntimeException("NPE is expected: " + afw); 200 } catch (final NullPointerException ignored) { 201 } 202 } 203 for (final AudioInputStream stream : aiss) { 204 try { 205 afw.isFileTypeSupported(null, stream); 206 throw new RuntimeException("NPE is expected: " + afw); 207 } catch (final NullPointerException ignored) { 208 } 209 } 210 211 // AudioFileWriter#write(AudioInputStream, Type, OutputStream) 212 for (final Type type : types) { 213 try { 214 afw.write(null, type, new NullOutputStream()); 215 throw new RuntimeException("NPE is expected: " + afw); 216 } catch (final NullPointerException ignored) { 217 } 218 } 219 for (final AudioInputStream stream : aiss) { 220 try { 221 afw.write(stream, null, new NullOutputStream()); 222 throw new RuntimeException("NPE is expected: " + afw); 223 } catch (final NullPointerException ignored) { 224 } 225 } 226 for (final AudioInputStream stream : aiss) { 227 for (final Type type : types) { 228 try { 229 afw.write(stream, type, (OutputStream) null); 230 throw new RuntimeException("NPE is expected: " + afw); 231 } catch (final NullPointerException ignored) { 232 } 233 } 234 } 235 236 // AudioFileWriter#write(AudioInputStream, Type, File) 237 for (final Type type : types) { 238 try { 239 afw.write(null, type, new File("test.sound")); 240 throw new RuntimeException("NPE is expected: " + afw); 241 } catch (final NullPointerException ignored) { 242 } 243 } 244 for (final AudioInputStream stream : aiss) { 245 try { 246 afw.write(stream, null, new File("test.sound")); 247 throw new RuntimeException("NPE is expected: " + afw); 248 } catch (final NullPointerException ignored) { 249 } 250 } 251 for (final AudioInputStream stream : aiss) { 252 for (final Type type : types) { 253 try { 254 afw.write(stream, type, (File) null); 255 throw new RuntimeException("NPE is expected: " + afw); 256 } catch (final NullPointerException ignored) { 257 } 258 } 259 } 260 } 261 262 /** 263 * Tests some default implementation of AudioFileWriter API, using the 264 * custom {@code AudioFileWriter}, which support nothing. 265 */ 266 static final AudioFileWriter customAFW = new AudioFileWriter() { 267 @Override 268 public Type[] getAudioFileTypes() { 269 return new Type[0]; 270 } 271 272 @Override 273 public Type[] getAudioFileTypes(final AudioInputStream stream) { 274 Objects.requireNonNull(stream); 275 return new Type[0]; 276 } 277 278 @Override 279 public int write(AudioInputStream stream, Type fileType, 280 OutputStream out) { 281 Objects.requireNonNull(stream); 282 Objects.requireNonNull(fileType); 283 Objects.requireNonNull(out); 284 return 0; 285 } 286 287 @Override 288 public int write(AudioInputStream stream, Type fileType, File out) { 289 Objects.requireNonNull(stream); 290 Objects.requireNonNull(fileType); 291 Objects.requireNonNull(out); 292 return 0; 293 } 294 }; 295 296 private static AudioInputStream getAIS(final Type type, Encoding encoding) { 297 AudioFormat af = new AudioFormat(encoding, 44100.0f, 16, 2, 1, 1, true); 298 AudioFileFormat aif = new AudioFileFormat(type, af, 0); 299 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[1024]); 300 return new AudioInputStream(bais, aif.getFormat(), 0); 301 } 302 303 private static final class NullOutputStream extends OutputStream { 304 305 @Override 306 public void write(final int b) { 307 //do nothing 308 } 309 } 310 }