1 /*
   2  * Copyright (c) 2002, 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.AudioFileFormat;
  25 import javax.sound.sampled.AudioFormat;
  26 
  27 /**
  28  * @test
  29  * @bug 4672864
  30  * @summary AudioFileFormat.toString() throws unexpected NullPointerException
  31  */
  32 public class AudioFileFormatToString {
  33 
  34     static final int STATUS_PASSED = 0;
  35     static final int STATUS_FAILED = 2;
  36     static final int STATUS_TEMP = 95;
  37 
  38     public static void main(String argv[]) throws Exception {
  39         int testExitStatus = run(argv, System.out);
  40         if (testExitStatus != STATUS_PASSED) {
  41             throw new Exception("Test FAILED " + testExitStatus);
  42         }
  43         System.out.println("Test passed.");
  44     }
  45 
  46     public static int run(String argv[], java.io.PrintStream out) {
  47         int testResult = STATUS_PASSED;
  48 
  49         out.println("\n==> Test for AudioFileFormat class:");
  50 
  51         AudioFormat testAudioFormat =
  52          new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,  // AudioFormat.Encoding
  53                          (float) 44100.0, // float SampleRate
  54                          (int) 8, // int sampleSizeInBits
  55                          (int) 2, // int channels
  56                          (int) 2,    // int frameSize
  57                          (float) 110.0,    // float frameRate
  58                          true    // boolean bigEndian
  59                          );
  60         AudioFormat nullAudioFormat = null;
  61 
  62         AudioFileFormat.Type testAudioFileFormatType = AudioFileFormat.Type.WAVE;
  63         AudioFileFormat.Type nullAudioFileFormatType = null;
  64 
  65         AudioFileFormat testedAudioFileFormat = null;
  66         out.println("\n>> public AudioFileFormat constructor for AudioFileFormat.Type = null: ");
  67         try {
  68          testedAudioFileFormat =
  69              new AudioFileFormat(nullAudioFileFormatType,  // AudioFileFormat.Type
  70                                  testAudioFormat, // AudioFormat
  71                                  (int) 1024 // int frameLength
  72                                  );
  73          out.println(">  No any Exception was thrown!");
  74          out.println(">  testedAudioFileFormat.getType():");
  75          try {
  76              AudioFileFormat.Type producedType = testedAudioFileFormat.getType();
  77              out.println(">   PASSED: producedType = " + producedType);
  78          } catch (Throwable thrown) {
  79              out.println("##  FAILED: unexpected Exception was thrown:");
  80              thrown.printStackTrace(out);
  81              testResult = STATUS_FAILED;
  82          }
  83          out.println(">  testedAudioFileFormat.toString():");
  84          try {
  85              String producedString = testedAudioFileFormat.toString();
  86              out.println(">   PASSED: producedString = " + producedString);
  87          } catch (Throwable thrown) {
  88              out.println("##  FAILED: unexpected Exception was thrown:");
  89              thrown.printStackTrace(out);
  90              testResult = STATUS_FAILED;
  91          }
  92         } catch (IllegalArgumentException illegArgExcept) {
  93          out.println(">   PASSED: expected IllegalArgumentException was thrown:");
  94          illegArgExcept.printStackTrace(out);
  95         } catch (NullPointerException nullPE) {
  96          out.println(">   PASSED: expected NullPointerException was thrown:");
  97          nullPE.printStackTrace(out);
  98         } catch (Throwable thrown) {
  99          out.println("##  FAILED: unexpected Exception was thrown:");
 100          thrown.printStackTrace(out);
 101          testResult = STATUS_FAILED;
 102         }
 103 
 104         out.println("\n>> public AudioFileFormat constructor for AudioFormat = null: ");
 105         try {
 106          testedAudioFileFormat =
 107              new AudioFileFormat(testAudioFileFormatType,  // AudioFileFormat.Type
 108                                  nullAudioFormat, // AudioFormat
 109                                  (int) 1024 // int frameLength
 110                                  );
 111          out.println(">  No any Exception was thrown!");
 112          out.println(">  testedAudioFileFormat.getFormat():");
 113          try {
 114              AudioFormat producedFormat = testedAudioFileFormat.getFormat();
 115              out.println(">   PASSED: producedFormat = " + producedFormat);
 116          } catch (Throwable thrown) {
 117              out.println("##  FAILED: unexpected Exception was thrown:");
 118              thrown.printStackTrace(out);
 119              testResult = STATUS_FAILED;
 120          }
 121          out.println(">  testedAudioFileFormat.toString():");
 122          try {
 123              String producedString = testedAudioFileFormat.toString();
 124              out.println(">   PASSED: producedString = " + producedString);
 125          } catch (Throwable thrown) {
 126              out.println("##  FAILED: unexpected Exception was thrown:");
 127              thrown.printStackTrace(out);
 128              testResult = STATUS_FAILED;
 129          }
 130         } catch (IllegalArgumentException illegArgExcept) {
 131             out.println(">   PASSED: expected IllegalArgumentException was thrown:");
 132             illegArgExcept.printStackTrace(out);
 133         } catch (NullPointerException nullPE) {
 134             out.println(">   PASSED: expected NullPointerException was thrown:");
 135             nullPE.printStackTrace(out);
 136         } catch (Throwable thrown) {
 137             out.println("##  FAILED: unexpected Exception was thrown:");
 138             thrown.printStackTrace(out);
 139             testResult = STATUS_FAILED;
 140         }
 141 
 142         out.println("\n>> protected AudioFileFormat constructor for AudioFileFormat.Type = null: ");
 143         try {
 144          testedAudioFileFormat =
 145              new TestAudioFileFormat(nullAudioFileFormatType,  // AudioFileFormat.Type
 146                                      (int) 1024, // byteLength
 147                                      testAudioFormat, // AudioFormat
 148                                      (int) 1024 // int frameLength
 149                                      );
 150          out.println(">  No any Exception was thrown!");
 151          out.println(">  testedAudioFileFormat.getType():");
 152          try {
 153              AudioFileFormat.Type producedType = testedAudioFileFormat.getType();
 154              out.println(">   PASSED: producedType = " + producedType);
 155          } catch (Throwable thrown) {
 156              out.println("##  FAILED: unexpected Exception was thrown:");
 157              thrown.printStackTrace(out);
 158              testResult = STATUS_FAILED;
 159          }
 160          out.println(">  testedAudioFileFormat.toString():");
 161          try {
 162              String producedString = testedAudioFileFormat.toString();
 163              out.println(">   PASSED: producedString = " + producedString);
 164          } catch (Throwable thrown) {
 165              out.println("##  FAILED: unexpected Exception was thrown:");
 166              thrown.printStackTrace(out);
 167              testResult = STATUS_FAILED;
 168          }
 169         } catch (IllegalArgumentException illegArgExcept) {
 170          out.println(">   PASSED: expected IllegalArgumentException was thrown:");
 171          illegArgExcept.printStackTrace(out);
 172         } catch (NullPointerException nullPE) {
 173          out.println(">   PASSED: expected NullPointerException was thrown:");
 174          nullPE.printStackTrace(out);
 175         } catch (Throwable thrown) {
 176          out.println("##  FAILED: unexpected Exception was thrown:");
 177          thrown.printStackTrace(out);
 178          testResult = STATUS_FAILED;
 179         }
 180 
 181         out.println("\n>> protected AudioFileFormat constructor for AudioFormat = null: ");
 182         try {
 183          testedAudioFileFormat =
 184              new TestAudioFileFormat(testAudioFileFormatType,  // AudioFileFormat.Type
 185                                      (int) 1024, // byteLength
 186                                      nullAudioFormat, // AudioFormat
 187                                      (int) 1024 // int frameLength
 188                                      );
 189          out.println(">  No any Exception was thrown!");
 190          out.println(">  testedAudioFileFormat.getFormat():");
 191          try {
 192              AudioFormat producedFormat = testedAudioFileFormat.getFormat();
 193              out.println(">   PASSED: producedFormat = " + producedFormat);
 194          } catch (Throwable thrown) {
 195              out.println("##  FAILED: unexpected Exception was thrown:");
 196              thrown.printStackTrace(out);
 197              testResult = STATUS_FAILED;
 198          }
 199          out.println(">  testedAudioFileFormat.toString():");
 200          try {
 201              String producedString = testedAudioFileFormat.toString();
 202              out.println(">   PASSED: producedString = " + producedString);
 203          } catch (Throwable thrown) {
 204              out.println("##  FAILED: unexpected Exception was thrown:");
 205              thrown.printStackTrace(out);
 206              testResult = STATUS_FAILED;
 207          }
 208         } catch (IllegalArgumentException illegArgExcept) {
 209          out.println(">   PASSED: expected IllegalArgumentException was thrown:");
 210          illegArgExcept.printStackTrace(out);
 211         } catch (NullPointerException nullPE) {
 212          out.println(">   PASSED: expected NullPointerException was thrown:");
 213          nullPE.printStackTrace(out);
 214         } catch (Throwable thrown) {
 215          out.println("##  FAILED: unexpected Exception was thrown:");
 216          thrown.printStackTrace(out);
 217          testResult = STATUS_FAILED;
 218         }
 219 
 220         if (testResult == STATUS_FAILED) {
 221             out.println("\n==> test FAILED!");
 222         } else {
 223             out.println("\n==> test PASSED!");
 224         }
 225         return testResult;
 226     }
 227 }
 228 
 229 class TestAudioFileFormat extends AudioFileFormat {
 230 
 231     TestAudioFileFormat(AudioFileFormat.Type type, int byteLength,
 232                         AudioFormat format, int frameLength) {
 233         super(type, byteLength, format, frameLength);
 234     }
 235 }