author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
35683 | 1 |
/* |
45341
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
35683 | 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 java.io.BufferedInputStream; |
|
25 |
import java.io.File; |
|
26 |
import java.io.FileInputStream; |
|
27 |
import java.io.FileOutputStream; |
|
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.OutputStream; |
|
31 |
import java.net.URL; |
|
32 |
import java.nio.file.Files; |
|
45341
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
33 |
import java.nio.file.Paths; |
35683 | 34 |
import java.util.Arrays; |
35 |
||
36 |
import javax.sound.sampled.AudioSystem; |
|
37 |
import javax.sound.sampled.UnsupportedAudioFileException; |
|
38 |
import javax.sound.sampled.spi.AudioFileReader; |
|
39 |
||
40 |
import static java.util.ServiceLoader.load; |
|
41 |
||
42 |
/** |
|
43 |
* @test |
|
44 |
* @bug 8131974 |
|
45 |
* @summary Short files should be reported as unsupported |
|
46 |
*/ |
|
47 |
public final class ShortHeader { |
|
48 |
||
49 |
private static byte[] W = {-12, 3, 45}; |
|
50 |
||
51 |
private static byte[] R = new byte[3]; |
|
52 |
||
53 |
public static void main(final String[] args) throws Exception { |
|
54 |
final File file = Files.createTempFile("audio", "test").toFile(); |
|
45341
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
55 |
try { |
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
56 |
try (final OutputStream fos = new FileOutputStream(file)) { |
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
57 |
fos.write(W); |
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
58 |
} |
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
59 |
testAS(file); |
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
60 |
for (final AudioFileReader afr : load(AudioFileReader.class)) { |
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
61 |
testAFR(afr, file); |
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
62 |
} |
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
63 |
} finally { |
b7cc7f639ecf
8178383: Validation of FileIO in the tests for JavaSound should be stricter
serb
parents:
35683
diff
changeset
|
64 |
Files.delete(Paths.get(file.getAbsolutePath())); |
35683 | 65 |
} |
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Tests the part of AudioSystem API, which implemented via |
|
70 |
* AudioFileReader. |
|
71 |
* |
|
72 |
* @see AudioSystem#getAudioFileFormat(InputStream) |
|
73 |
* @see AudioSystem#getAudioFileFormat(File) |
|
74 |
* @see AudioSystem#getAudioFileFormat(URL) |
|
75 |
* @see AudioSystem#getAudioInputStream(InputStream) |
|
76 |
* @see AudioSystem#getAudioInputStream(File) |
|
77 |
* @see AudioSystem#getAudioInputStream(URL) |
|
78 |
*/ |
|
79 |
private static void testAS(final File file) throws IOException { |
|
80 |
try { |
|
81 |
AudioSystem.getAudioFileFormat(file); |
|
82 |
throw new RuntimeException(); |
|
83 |
} catch (final UnsupportedAudioFileException ignored) { |
|
84 |
} |
|
85 |
try { |
|
86 |
AudioSystem.getAudioFileFormat(file.toURL()); |
|
87 |
throw new RuntimeException(); |
|
88 |
} catch (final UnsupportedAudioFileException ignored) { |
|
89 |
} |
|
90 |
try { |
|
91 |
AudioSystem.getAudioInputStream(file); |
|
92 |
throw new RuntimeException(); |
|
93 |
} catch (final UnsupportedAudioFileException ignored) { |
|
94 |
} |
|
95 |
try { |
|
96 |
AudioSystem.getAudioInputStream(file.toURL()); |
|
97 |
throw new RuntimeException(); |
|
98 |
} catch (final UnsupportedAudioFileException ignored) { |
|
99 |
} |
|
100 |
||
101 |
// AudioSystem.getAudioXXX(stream) should properly reset the stream |
|
102 |
||
103 |
try (FileInputStream fis = new FileInputStream(file); |
|
104 |
InputStream stream = new BufferedInputStream(fis)) { |
|
105 |
||
106 |
try { |
|
107 |
AudioSystem.getAudioFileFormat(stream); |
|
108 |
throw new RuntimeException(); |
|
109 |
} catch (final UnsupportedAudioFileException ignored) { |
|
110 |
} |
|
111 |
try { |
|
112 |
AudioSystem.getAudioInputStream(stream); |
|
113 |
throw new RuntimeException(); |
|
114 |
} catch (final UnsupportedAudioFileException ignored) { |
|
115 |
} |
|
116 |
stream.read(R, 0, R.length); |
|
117 |
} |
|
118 |
||
119 |
if (!Arrays.equals(R, W)) { |
|
120 |
System.err.println("Expected = " + Arrays.toString(W)); |
|
121 |
System.err.println("Actual = " + Arrays.toString(R)); |
|
122 |
throw new RuntimeException(); |
|
123 |
} |
|
124 |
} |
|
125 |
/** |
|
126 |
* Tests the AudioFileReader API directly. |
|
127 |
* |
|
128 |
* @see AudioFileReader#getAudioFileFormat(InputStream) |
|
129 |
* @see AudioFileReader#getAudioFileFormat(File) |
|
130 |
* @see AudioFileReader#getAudioFileFormat(URL) |
|
131 |
* @see AudioFileReader#getAudioInputStream(InputStream) |
|
132 |
* @see AudioFileReader#getAudioInputStream(File) |
|
133 |
* @see AudioFileReader#getAudioInputStream(URL) |
|
134 |
*/ |
|
135 |
private static void testAFR(final AudioFileReader fcp, final File file) |
|
136 |
throws Exception { |
|
137 |
try { |
|
138 |
fcp.getAudioFileFormat(file); |
|
139 |
throw new RuntimeException(); |
|
140 |
} catch (final UnsupportedAudioFileException ignored) { |
|
141 |
} |
|
142 |
try { |
|
143 |
fcp.getAudioFileFormat(file.toURL()); |
|
144 |
throw new RuntimeException(); |
|
145 |
} catch (final UnsupportedAudioFileException ignored) { |
|
146 |
} |
|
147 |
try { |
|
148 |
fcp.getAudioInputStream(file); |
|
149 |
throw new RuntimeException(); |
|
150 |
} catch (final UnsupportedAudioFileException ignored) { |
|
151 |
} |
|
152 |
try { |
|
153 |
fcp.getAudioInputStream(file.toURL()); |
|
154 |
throw new RuntimeException(); |
|
155 |
} catch (final UnsupportedAudioFileException ignored) { |
|
156 |
} |
|
157 |
||
158 |
// AudioFileReader should properly reset the stream |
|
159 |
||
160 |
try (FileInputStream fis = new FileInputStream(file); |
|
161 |
InputStream stream = new BufferedInputStream(fis)) { |
|
162 |
||
163 |
try { |
|
164 |
fcp.getAudioFileFormat(stream); |
|
165 |
throw new RuntimeException(); |
|
166 |
} catch (final UnsupportedAudioFileException ignored) { |
|
167 |
} |
|
168 |
try { |
|
169 |
fcp.getAudioInputStream(stream); |
|
170 |
throw new RuntimeException(); |
|
171 |
} catch (final UnsupportedAudioFileException ignored) { |
|
172 |
} |
|
173 |
stream.read(R, 0, R.length); |
|
174 |
} |
|
175 |
||
176 |
if (!Arrays.equals(R, W)) { |
|
177 |
System.err.println("Expected = " + Arrays.toString(W)); |
|
178 |
System.err.println("Actual = " + Arrays.toString(R)); |
|
179 |
throw new RuntimeException(); |
|
180 |
} |
|
181 |
} |
|
182 |
} |