jdk/test/javax/sound/sampled/FileReader/ReadersExceptions.java
changeset 32121 93ad23865c9e
parent 21245 786207514920
equal deleted inserted replaced
32120:06c83c5f2912 32121:93ad23865c9e
     1 /*
     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 
       
    25 import java.io.ByteArrayInputStream;
    24 import java.io.ByteArrayInputStream;
    26 import java.io.IOException;
    25 import java.io.IOException;
    27 import java.io.InputStream;
    26 import java.io.InputStream;
    28 
    27 
    29 import javax.sound.sampled.AudioSystem;
    28 import javax.sound.sampled.AudioSystem;
    30 import javax.sound.sampled.UnsupportedAudioFileException;
    29 import javax.sound.sampled.UnsupportedAudioFileException;
       
    30 import javax.sound.sampled.spi.AudioFileReader;
       
    31 
       
    32 import static java.util.ServiceLoader.load;
    31 
    33 
    32 /**
    34 /**
    33  * @test
    35  * @test
    34  * @bug 7058662 7058666 7058672
    36  * @bug 7058662 7058666 7058672 8130305
    35  * @author Sergey Bylokhov
    37  * @author Sergey Bylokhov
    36  */
    38  */
    37 public final class ReadersExceptions {
    39 public final class ReadersExceptions {
    38 
    40 
    39     // empty channels
    41     // empty channels
   109              0, 0, // sampleSizeInBits
   111              0, 0, // sampleSizeInBits
   110              0x64, 0x61, 0x74, 0x61, // WaveFileFormat.DATA_MAGIC
   112              0x64, 0x61, 0x74, 0x61, // WaveFileFormat.DATA_MAGIC
   111              0, 0, 0, 0, // dataLength
   113              0, 0, 0, 0, // dataLength
   112             };
   114             };
   113 
   115 
       
   116     static byte[][] data = {wrongAIFFCh, wrongAIFFSSL, wrongAIFFSSH, wrongAUCh,
       
   117                             wrongWAVCh, wrongWAVSSB};
       
   118 
   114     public static void main(final String[] args) throws IOException {
   119     public static void main(final String[] args) throws IOException {
   115         test(wrongAIFFCh);
   120         for (final byte[] bytes : data) {
   116         test(wrongAIFFSSL);
   121             testAS(bytes);
   117         test(wrongAIFFSSH);
   122             testAFR(bytes);
   118         test(wrongAUCh);
   123         }
   119         test(wrongWAVCh);
       
   120         test(wrongWAVSSB);
       
   121     }
   124     }
   122 
   125 
   123     private static void test(final byte[] buffer) throws IOException {
   126     private static void testAS(final byte[] buffer) throws IOException {
       
   127         // AudioSystem API
   124         final InputStream is = new ByteArrayInputStream(buffer);
   128         final InputStream is = new ByteArrayInputStream(buffer);
   125         try {
   129         try {
   126             AudioSystem.getAudioFileFormat(is);
   130             AudioSystem.getAudioFileFormat(is);
   127         } catch (UnsupportedAudioFileException ignored) {
   131         } catch (UnsupportedAudioFileException ignored) {
   128             // Expected.
   132             // Expected.
   129             return;
   133             return;
   130         }
   134         }
   131         throw new RuntimeException("Test Failed");
   135         throw new RuntimeException("Test Failed");
   132     }
   136     }
       
   137 
       
   138     private static void testAFR(final byte[] buffer) throws IOException {
       
   139         // AudioFileReader API
       
   140         final InputStream is = new ByteArrayInputStream(buffer);
       
   141         for (final AudioFileReader afr : load(AudioFileReader.class)) {
       
   142             for (int i = 0; i < 10; ++i) {
       
   143                 try {
       
   144                     afr.getAudioFileFormat(is);
       
   145                     throw new RuntimeException("UAFE expected");
       
   146                 } catch (final UnsupportedAudioFileException ignored) {
       
   147                     // Expected.
       
   148                 }
       
   149             }
       
   150         }
       
   151     }
   133 }
   152 }