1 /*
2  * Copyright (c) 2003, 2016, 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 javax.sound.sampled.AudioFormat;
25 import javax.sound.sampled.AudioSystem;
26 import javax.sound.sampled.Clip;
27 import javax.sound.sampled.DataLine;
28 import javax.sound.sampled.Line;
29 import javax.sound.sampled.LineUnavailableException;
30 import javax.sound.sampled.Mixer;
31 import javax.sound.sampled.SourceDataLine;
32 import javax.sound.sampled.TargetDataLine;
33 
34 /**
35  * @test
36  * @bug 4679187
37  * @summary Clip.open() throws unexpected Exceptions. verifies that clip,
38  *          sourcedataline and targetdataline throw IllegalArgumentExcepotion if
39  *          any field in the format is AudioFormat.NOT_SPECIFIED
40  */
41 public class ClipOpenException {
42     static boolean failed = false;
43 
44     static byte[] audioData = new byte[2048];
45     static AudioFormat[] formats = {
46         new AudioFormat(AudioSystem.NOT_SPECIFIED,
47                         AudioSystem.NOT_SPECIFIED,
48                         AudioSystem.NOT_SPECIFIED,
49                         true, false),
50         new AudioFormat(0, 0, 0, true, false)
51     };
52     static AudioFormat infoFormat = new AudioFormat(44100.0f,
53                                                 16,
54                                                 1,
55                                                 true, false);
56     static DataLine.Info clipInfo = new DataLine.Info(Clip.class, infoFormat);
57     static DataLine.Info sdlInfo = new DataLine.Info(SourceDataLine.class, infoFormat);
58     static DataLine.Info tdlInfo = new DataLine.Info(TargetDataLine.class, infoFormat);
59 
60 
print(String s)61     public static void print(String s) {
62         System.out.print(s);
63     }
println(String s)64     public static void println(String s) {
65         System.out.println(s);
66     }
67 
test(Line line)68     public static void test(Line line) {
69         for (int format = 0; format < formats.length; format++) {
70             try {
71                 println("  Opening the line with format "+(format+1));
72                 if (line instanceof Clip) {
73                     ((Clip) line).open(formats[format], audioData, 0, audioData.length);
74                 } else
75                 if (line instanceof SourceDataLine) {
76                     ((SourceDataLine) line).open(formats[format]);
77                 } else
78                 if (line instanceof TargetDataLine) {
79                     ((TargetDataLine) line).open(formats[format]);
80                 } else {
81                     println("    Unknown type of line: "+line.getClass());
82                     return;
83                 }
84                 println("    No exception! not OK.");
85                 failed = true;
86             } catch (IllegalArgumentException iae) {
87                 println("    IllegalArgumentException: "+iae.getMessage());
88                 println("    OK");
89             } catch (LineUnavailableException lue) {
90                 println("    LineUnavailableException: "+lue.getMessage());
91                 println("    Probably incorrect, but may happen if the test system is correctly set up.");
92             } catch (Exception e) {
93                 println("    Unexpected Exception: "+e.toString());
94                 println("    NOT OK!");
95                 failed = true;
96             }
97             println("    Closing line.");
98             line.close();
99         }
100     }
101 
main(String[] args)102     public static void main(String[] args) throws Exception {
103         Mixer.Info[] mixers = AudioSystem.getMixerInfo();
104         int succMixers = 0;
105         println("Using formats: ");
106         for (int i = 0 ; i<formats.length; i++) {
107                 println(""+(i+1)+". "+formats[i]);
108         }
109         for (int i=0; i<mixers.length; i++) {
110             boolean succ = false;
111             try {
112                 Mixer mixer = AudioSystem.getMixer(mixers[i]);
113                 println("Mixer "+mixer.getMixerInfo()+":");
114                 if (mixer.isLineSupported(clipInfo)) {
115                     println("Getting clip from mixer...");
116                     Clip clip = (Clip) mixer.getLine(clipInfo);
117                     succ = true;
118                     test(clip);
119                 }
120                 if (mixer.isLineSupported(sdlInfo)) {
121                     println("Getting source data line from mixer...");
122                     SourceDataLine sdl = (SourceDataLine) mixer.getLine(sdlInfo);
123                     succ = true;
124                     test(sdl);
125                 }
126                 if (mixer.isLineSupported(tdlInfo)) {
127                     println("Getting target data line from mixer...");
128                     TargetDataLine tdl = (TargetDataLine) mixer.getLine(tdlInfo);
129                     succ = true;
130                     test(tdl);
131                 }
132             } catch (Exception e) {
133                 e.printStackTrace();
134             }
135             if (succ) {
136                 succMixers++;
137             }
138         }
139         if (succMixers == 0) {
140             println("No mixers available! ");
141             println("Cannot run test. NOT FAILED");
142         }
143         else if (failed) {
144             throw new Exception("Test FAILED");
145         }
146         println("Test passed.");
147     }
148 }
149