Path: blob/master/test/jdk/javax/sound/sampled/Lines/SDLwrite.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.Line;26import javax.sound.sampled.LineUnavailableException;27import javax.sound.sampled.Mixer;28import javax.sound.sampled.SourceDataLine;2930/**31* @test32* @bug 468071033* @summary SourceDataLine.write() behavior is not correct for not open or not34* started lines35*/36public class SDLwrite {3738static final int STATUS_PASSED = 0;39static final int STATUS_FAILED = 2;4041public static void main(String argv[]) throws Exception {42int testExitStatus = run(argv, System.out);43if (testExitStatus != STATUS_PASSED) {44throw new Exception("test FAILED!");45}46}4748public static int run(String argv[], java.io.PrintStream out) {49int testResult = STATUS_PASSED;5051out.println52("\n==> Test for SourceDataLine.write() method for not open and not started line:");5354Mixer.Info[] installedMixersInfo = AudioSystem.getMixerInfo();5556if ( installedMixersInfo == null ) {57out.println("## AudioSystem.getMixerInfo() returned unexpected result:");58out.println("# expected: an array of Mixer.Info objects (may be array of length 0);");59out.println("# produced: null;");60return STATUS_FAILED;61}6263if ( installedMixersInfo.length == 0 ) {64// there are no mixers installed on the system - so this testcase can not be tested65return STATUS_PASSED;66}6768Mixer testedMixer = null;69for (int i=0; i < installedMixersInfo.length; i++) {70try {71testedMixer = AudioSystem.getMixer(installedMixersInfo[i]);72} catch (SecurityException securityException) {73// installed Mixer is unavailable because of security restrictions74continue;75} catch (Throwable thrown) {76out.println("## AudioSystem.getMixer() threw unexpected exception:");77thrown.printStackTrace(out);78testResult = STATUS_FAILED;79continue;80}8182try {83testedMixer.open();84} catch (LineUnavailableException lineUnavailableException) {85// testedMixer is not available due to resource restrictions86continue;87} catch (SecurityException securityException) {88// testedMixer is not available due to security restrictions89continue;90} catch (Throwable thrown) {91out.println("## Mixer.open() threw unexpected exception:");92out.println("# Mixer = " + testedMixer);93thrown.printStackTrace(out);94testResult = STATUS_FAILED;95continue;96}97Line.Info supportedSourceLineInfo[] = null;98try {99supportedSourceLineInfo = testedMixer.getSourceLineInfo();100} catch (Throwable thrown) {101out.println("## Mixer.getSourceLineInfo() threw unexpected exception:");102out.println("# Mixer = " + testedMixer);103thrown.printStackTrace(out);104testResult = STATUS_FAILED;105testedMixer.close();106continue;107}108if ( supportedSourceLineInfo == null ) {109out.println("## Mixer.getSourceLineInfo() returned null array");110out.println("# Mixer = " + testedMixer);111testResult = STATUS_FAILED;112testedMixer.close();113continue;114}115out.println("\n>>> testedMixer["+i+"] = " + testedMixer);116out.println("\n>> supportedSourceLineInfo.length = " + supportedSourceLineInfo.length);117118for (int j=0; j < supportedSourceLineInfo.length; j++) {119Line.Info testSourceLineInfo = supportedSourceLineInfo[j];120121Line testSourceLine = null;122try {123testSourceLine = testedMixer.getLine(testSourceLineInfo);124} catch (LineUnavailableException lineUnavailableException) {125// line is not available due to resource restrictions126continue;127} catch (SecurityException securityException) {128// line is not available due to security restrictions129continue;130} catch (Throwable thrown) {131out.println("## Mixer.getLine(Line.Info) threw unexpected Exception:");132out.println("# Mixer = " + testedMixer);133out.println("# Line.Info = " + testSourceLineInfo);134thrown.printStackTrace(out);135testResult = STATUS_FAILED;136continue;137}138139out.println("\n> testSourceLineInfo["+j+"] = " + testSourceLineInfo);140out.println("> testSourceLine = " + testSourceLine);141if ( ! (testSourceLine instanceof SourceDataLine) ) {142out.println("> testSourceLine is not SourceDataLine");143continue;144}145146SourceDataLine testedSourceLine = (SourceDataLine)testSourceLine;147AudioFormat lineAudioFormat = testedSourceLine.getFormat();148149int bufferSizeToWrite = 1;150if ( lineAudioFormat.getSampleSizeInBits() != AudioSystem.NOT_SPECIFIED ) {151bufferSizeToWrite = lineAudioFormat.getSampleSizeInBits()/8;152if ( lineAudioFormat.getSampleSizeInBits()%8 != 0 ) {153bufferSizeToWrite++;154}155}156if ( lineAudioFormat.getFrameSize() != AudioSystem.NOT_SPECIFIED ) {157bufferSizeToWrite = lineAudioFormat.getFrameSize();158}159byte[] dataToWrite = new byte[bufferSizeToWrite];160for (int k=0; k < bufferSizeToWrite; k++) {161dataToWrite[k] = (byte)1;162}163int offsetToWrite = 0;164165out.println166("\n> check SourceDataLine.write() for not open line with correct length of data:");167int writtenBytes = -1;168try {169writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);170out.println("> Bytes written: number of written bytes = " + writtenBytes);171} catch (Throwable thrown) {172out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");173out.println("# Unexpected Exception is thrown");174out.println("# Mixer = " + testedMixer);175out.println("# SourceDataLine = " + testedSourceLine);176thrown.printStackTrace(out);177testResult = STATUS_FAILED;178}179180out.println181("\n> check SourceDataLine.write() for not open line with incorrect length of data:");182writtenBytes = -1;183bufferSizeToWrite--;184try {185writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);186out.println("> Bytes written: number of written bytes = " + writtenBytes);187} catch (IllegalArgumentException illegalArgumentException) {188out.println("> Permissible IllegalArgumentException for the present instance is thrown:");189illegalArgumentException.printStackTrace(out);190} catch (Throwable thrown) {191out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");192out.println("# Unexpected Exception is thrown");193out.println("# Mixer = " + testedMixer);194out.println("# SourceDataLine = " + testedSourceLine);195thrown.printStackTrace(out);196testResult = STATUS_FAILED;197}198199out.println200("\n> open tested line:");201bufferSizeToWrite++;202try {203testedSourceLine.open(lineAudioFormat, bufferSizeToWrite);204out.println("> OK - line is opened");205} catch (LineUnavailableException lineUnavailableException) {206out.println("> Line is not available due to resource restrictions:");207lineUnavailableException.printStackTrace(out);208continue;209} catch (SecurityException securityException) {210out.println("> Line is not available due to security restrictions:");211securityException.printStackTrace(out);212continue;213} catch (Throwable thrown) {214out.println("## SourceDataLine.open(AudioFormat format) failed:");215out.println("# Unexpected Exception is thrown");216out.println("# Mixer = " + testedMixer);217out.println("# SourceDataLine = " + testedSourceLine);218thrown.printStackTrace(out);219testResult = STATUS_FAILED;220continue;221}222223out.println224("\n> check SourceDataLine.write() for not started line with correct length of data:");225writtenBytes = -1;226try {227writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);228out.println("> Bytes written: number of written bytes = " + writtenBytes);229} catch (Throwable thrown) {230out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");231out.println("# Unexpected Exception is thrown");232out.println("# Mixer = " + testedMixer);233out.println("# SourceDataLine = " + testedSourceLine);234thrown.printStackTrace(out);235testResult = STATUS_FAILED;236}237238out.println239("\n> check SourceDataLine.write() for not started line with incorrect length of data:");240writtenBytes = -1;241bufferSizeToWrite--;242try {243writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);244out.println("> Bytes written: number of written bytes = " + writtenBytes);245} catch (IllegalArgumentException illegalArgumentException) {246out.println("> Permissible IllegalArgumentException for the present instance is thrown:");247illegalArgumentException.printStackTrace(out);248} catch (Throwable thrown) {249out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");250out.println("# Unexpected Exception is thrown");251out.println("# Mixer = " + testedMixer);252out.println("# SourceDataLine = " + testedSourceLine);253thrown.printStackTrace(out);254testResult = STATUS_FAILED;255}256testedSourceLine.close();257258} // for (int j=0; j < supportedSourceLineInfo.length; j++)259testedMixer.close();260261} // for (int i=0; i < installedMixersInfo.length; i++)262263if ( testResult == STATUS_FAILED ) {264out.println("\n==> test FAILED!");265} else {266out.println("\n==> test PASSED!");267}268return testResult;269}270}271272273