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