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.Line;
  27 import javax.sound.sampled.LineUnavailableException;
  28 import javax.sound.sampled.Mixer;
  29 import javax.sound.sampled.SourceDataLine;
  30 
  31 /**
  32  * @test
  33  * @bug 4680710
  34  * @summary SourceDataLine.write() behavior is not correct for not open or not
  35  *          started lines
  36  */
  37 public class SDLwrite {
  38 
  39     static final int STATUS_PASSED = 0;
  40     static final int STATUS_FAILED = 2;
  41 
  42     public static void main(String argv[]) throws Exception {
  43         int testExitStatus = run(argv, System.out);
  44         if (testExitStatus != STATUS_PASSED) {
  45             throw new Exception("test FAILED!");
  46         }
  47     }
  48 
  49     public static int run(String argv[], java.io.PrintStream out) {
  50         int testResult = STATUS_PASSED;
  51 
  52         out.println
  53             ("\n==> Test for SourceDataLine.write() method for not open and not started line:");
  54 
  55         Mixer.Info[] installedMixersInfo = AudioSystem.getMixerInfo();
  56 
  57         if ( installedMixersInfo == null ) {
  58             out.println("## AudioSystem.getMixerInfo() returned unexpected result:");
  59             out.println("#  expected: an array of Mixer.Info objects (may be array of length 0);");
  60             out.println("#  produced: null;");
  61             return STATUS_FAILED;
  62         }
  63 
  64         if ( installedMixersInfo.length == 0 ) {
  65             // there are no mixers installed on the system - so this testcase can not be tested
  66             return STATUS_PASSED;
  67         }
  68 
  69         Mixer testedMixer = null;
  70         for (int i=0; i < installedMixersInfo.length; i++) {
  71             try {
  72                 testedMixer = AudioSystem.getMixer(installedMixersInfo[i]);
  73             } catch (SecurityException securityException) {
  74                 // installed Mixer is unavailable because of security restrictions
  75                 continue;
  76             } catch (Throwable thrown) {
  77                 out.println("## AudioSystem.getMixer() threw unexpected exception:");
  78                 thrown.printStackTrace(out);
  79                 testResult = STATUS_FAILED;
  80                 continue;
  81             }
  82 
  83             try {
  84                 testedMixer.open();
  85             } catch (LineUnavailableException lineUnavailableException) {
  86                 // testedMixer is not available due to resource restrictions
  87                 continue;
  88             } catch (SecurityException securityException) {
  89                 // testedMixer is not available due to security restrictions
  90                 continue;
  91             } catch (Throwable thrown) {
  92                 out.println("## Mixer.open() threw unexpected exception:");
  93                 out.println("#  Mixer = " + testedMixer);
  94                 thrown.printStackTrace(out);
  95                 testResult = STATUS_FAILED;
  96                 continue;
  97             }
  98             Line.Info supportedSourceLineInfo[] = null;
  99             try {
 100                 supportedSourceLineInfo = testedMixer.getSourceLineInfo();
 101             } catch (Throwable thrown) {
 102                 out.println("## Mixer.getSourceLineInfo() threw unexpected exception:");
 103                 out.println("#  Mixer = " + testedMixer);
 104                 thrown.printStackTrace(out);
 105                 testResult = STATUS_FAILED;
 106                 testedMixer.close();
 107                 continue;
 108             }
 109             if ( supportedSourceLineInfo == null ) {
 110                 out.println("## Mixer.getSourceLineInfo() returned null array");
 111                 out.println("#  Mixer = " + testedMixer);
 112                 testResult = STATUS_FAILED;
 113                 testedMixer.close();
 114                 continue;
 115             }
 116             out.println("\n>>>  testedMixer["+i+"] = " + testedMixer);
 117             out.println("\n>>  supportedSourceLineInfo.length = " + supportedSourceLineInfo.length);
 118 
 119             for (int j=0; j < supportedSourceLineInfo.length; j++) {
 120                 Line.Info testSourceLineInfo = supportedSourceLineInfo[j];
 121 
 122                 Line testSourceLine = null;
 123                 try {
 124                     testSourceLine = testedMixer.getLine(testSourceLineInfo);
 125                 } catch (LineUnavailableException lineUnavailableException) {
 126                     // line is not available due to resource restrictions
 127                     continue;
 128                 } catch (SecurityException securityException) {
 129                     // line is not available due to security restrictions
 130                     continue;
 131                 } catch (Throwable thrown) {
 132                     out.println("## Mixer.getLine(Line.Info) threw unexpected Exception:");
 133                     out.println("#  Mixer = " + testedMixer);
 134                     out.println("#  Line.Info = " + testSourceLineInfo);
 135                     thrown.printStackTrace(out);
 136                     testResult = STATUS_FAILED;
 137                     continue;
 138                 }
 139 
 140                 out.println("\n>  testSourceLineInfo["+j+"] = " + testSourceLineInfo);
 141                 out.println(">  testSourceLine = " + testSourceLine);
 142                 if ( ! (testSourceLine instanceof SourceDataLine) ) {
 143                     out.println(">  testSourceLine is not SourceDataLine");
 144                     continue;
 145                 }
 146 
 147                 SourceDataLine testedSourceLine = (SourceDataLine)testSourceLine;
 148                 AudioFormat lineAudioFormat = testedSourceLine.getFormat();
 149 
 150                 int bufferSizeToWrite = 1;
 151                 if ( lineAudioFormat.getSampleSizeInBits() != AudioSystem.NOT_SPECIFIED ) {
 152                     bufferSizeToWrite = lineAudioFormat.getSampleSizeInBits()/8;
 153                     if ( lineAudioFormat.getSampleSizeInBits()%8 != 0 ) {
 154                         bufferSizeToWrite++;
 155                     }
 156                 }
 157                 if ( lineAudioFormat.getFrameSize() != AudioSystem.NOT_SPECIFIED ) {
 158                     bufferSizeToWrite = lineAudioFormat.getFrameSize();
 159                 }
 160                 byte[] dataToWrite = new byte[bufferSizeToWrite];
 161                 for (int k=0; k < bufferSizeToWrite; k++) {
 162                     dataToWrite[k] = (byte)1;
 163                 }
 164                 int offsetToWrite = 0;
 165 
 166                 out.println
 167                     ("\n>  check SourceDataLine.write() for not open line with correct length of data:");
 168                 int writtenBytes = -1;
 169                 try {
 170                     writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);
 171                     out.println(">  Bytes written: number of written bytes = " + writtenBytes);
 172                 } catch (Throwable thrown) {
 173                     out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");
 174                     out.println("#  Unexpected Exception is thrown");
 175                     out.println("#  Mixer = " + testedMixer);
 176                     out.println("#  SourceDataLine = " + testedSourceLine);
 177                     thrown.printStackTrace(out);
 178                     testResult = STATUS_FAILED;
 179                 }
 180 
 181                 out.println
 182                     ("\n>  check SourceDataLine.write() for not open line with incorrect length of data:");
 183                 writtenBytes = -1;
 184                 bufferSizeToWrite--;
 185                 try {
 186                     writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);
 187                     out.println(">  Bytes written: number of written bytes = " + writtenBytes);
 188                 } catch (IllegalArgumentException illegalArgumentException) {
 189                     out.println(">  Permissible IllegalArgumentException for the present instance is thrown:");
 190                     illegalArgumentException.printStackTrace(out);
 191                 } catch (Throwable thrown) {
 192                     out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");
 193                     out.println("#  Unexpected Exception is thrown");
 194                     out.println("#  Mixer = " + testedMixer);
 195                     out.println("#  SourceDataLine = " + testedSourceLine);
 196                     thrown.printStackTrace(out);
 197                     testResult = STATUS_FAILED;
 198                 }
 199 
 200                 out.println
 201                     ("\n>  open tested line:");
 202                 bufferSizeToWrite++;
 203                 try {
 204                     testedSourceLine.open(lineAudioFormat, bufferSizeToWrite);
 205                     out.println(">  OK - line is opened");
 206                 } catch (LineUnavailableException lineUnavailableException) {
 207                     out.println(">  Line is not available due to resource restrictions:");
 208                     lineUnavailableException.printStackTrace(out);
 209                     continue;
 210                 } catch (SecurityException securityException) {
 211                     out.println("> Line is not available due to security restrictions:");
 212                     securityException.printStackTrace(out);
 213                     continue;
 214                 } catch (Throwable thrown) {
 215                     out.println("## SourceDataLine.open(AudioFormat format) failed:");
 216                     out.println("#  Unexpected Exception is thrown");
 217                     out.println("#  Mixer = " + testedMixer);
 218                     out.println("#  SourceDataLine = " + testedSourceLine);
 219                     thrown.printStackTrace(out);
 220                     testResult = STATUS_FAILED;
 221                     continue;
 222                 }
 223 
 224                 out.println
 225                     ("\n>  check SourceDataLine.write() for not started line with correct length of data:");
 226                 writtenBytes = -1;
 227                 try {
 228                     writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);
 229                     out.println(">  Bytes written: number of written bytes = " + writtenBytes);
 230                 } catch (Throwable thrown) {
 231                     out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");
 232                     out.println("#  Unexpected Exception is thrown");
 233                     out.println("#  Mixer = " + testedMixer);
 234                     out.println("#  SourceDataLine = " + testedSourceLine);
 235                     thrown.printStackTrace(out);
 236                     testResult = STATUS_FAILED;
 237                 }
 238 
 239                 out.println
 240                     ("\n>  check SourceDataLine.write() for not started line with incorrect length of data:");
 241                 writtenBytes = -1;
 242                 bufferSizeToWrite--;
 243                 try {
 244                     writtenBytes = testedSourceLine.write(dataToWrite, offsetToWrite, bufferSizeToWrite);
 245                     out.println(">  Bytes written: number of written bytes = " + writtenBytes);
 246                 } catch (IllegalArgumentException illegalArgumentException) {
 247                     out.println(">  Permissible IllegalArgumentException for the present instance is thrown:");
 248                     illegalArgumentException.printStackTrace(out);
 249                 } catch (Throwable thrown) {
 250                     out.println("## SourceDataLine.write(byte[] b, int off, int len) failed:");
 251                     out.println("#  Unexpected Exception is thrown");
 252                     out.println("#  Mixer = " + testedMixer);
 253                     out.println("#  SourceDataLine = " + testedSourceLine);
 254                     thrown.printStackTrace(out);
 255                     testResult = STATUS_FAILED;
 256                 }
 257                 testedSourceLine.close();
 258 
 259             }  // for (int j=0; j < supportedSourceLineInfo.length; j++)
 260             testedMixer.close();
 261 
 262         }  // for (int i=0; i < installedMixersInfo.length; i++)
 263 
 264         if ( testResult == STATUS_FAILED ) {
 265             out.println("\n==> test FAILED!");
 266         } else {
 267             out.println("\n==> test PASSED!");
 268         }
 269         return testResult;
 270     }
 271 }