1
1
/*
2
- * Copyright (c) 2008, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2008, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
23
23
24
24
/* @test
25
25
* @bug 4313887 8129632 8129633 8162624 8146215 8162745 8273655 8274171 8287237 8297609
26
+ * @modules java.base/jdk.internal.util
26
27
* @summary Unit test for probeContentType method
27
28
* @library ../..
28
29
* @build Basic SimpleFileTypeDetector
33
34
import java .io .OutputStream ;
34
35
import java .nio .file .Files ;
35
36
import java .nio .file .Path ;
37
+ import java .util .ArrayList ;
36
38
import java .util .List ;
37
39
import java .util .stream .Stream ;
38
40
41
+ import jdk .internal .util .OperatingSystem ;
42
+ import jdk .internal .util .OSVersion ;
43
+
39
44
/**
40
45
* Uses Files.probeContentType to probe html file, custom file type, and minimal
41
46
* set of file extension to content type mappings.
42
47
*/
43
48
public class Basic {
44
- private static final boolean IS_UNIX =
45
- ! System .getProperty ("os.name" ).startsWith ("Windows" );
46
-
47
49
static Path createHtmlFile () throws IOException {
48
50
Path file = Files .createTempFile ("foo" , ".html" );
49
51
try (OutputStream out = Files .newOutputStream (file )) {
@@ -79,7 +81,7 @@ private static int checkContentTypes(String expected, String actual) {
79
81
assert actual != null ;
80
82
81
83
if (!expected .equals (actual )) {
82
- if (IS_UNIX ) {
84
+ if (! OperatingSystem . isWindows () ) {
83
85
Path userMimeTypes =
84
86
Path .of (System .getProperty ("user.home" ), ".mime.types" );
85
87
checkMimeTypesFile (userMimeTypes );
@@ -98,12 +100,12 @@ private static int checkContentTypes(String expected, String actual) {
98
100
return 0 ;
99
101
}
100
102
101
- static int checkContentTypes (ExType [] exTypes )
103
+ static int checkContentTypes (List < ExType > exTypes )
102
104
throws IOException {
103
105
int failures = 0 ;
104
- for (int i = 0 ; i < exTypes . length ; i ++ ) {
105
- String extension = exTypes [ i ] .extension ();
106
- List <String > expectedTypes = exTypes [ i ] .expectedTypes ();
106
+ for (ExType exType : exTypes ) {
107
+ String extension = exType .extension ();
108
+ List <String > expectedTypes = exType .expectedTypes ();
107
109
Path file = Files .createTempFile ("foo" , "." + extension );
108
110
try {
109
111
String type = Files .probeContentType (file );
@@ -155,40 +157,55 @@ public static void main(String[] args) throws IOException {
155
157
}
156
158
157
159
// Verify that certain extensions are mapped to the correct type.
158
- var exTypes = new ExType [] {
159
- new ExType ("adoc" , List .of ("text/plain" )),
160
- new ExType ("bz2" , List .of ("application/bz2" , "application/x-bzip2" , "application/x-bzip" )),
161
- new ExType ("css" , List .of ("text/css" )),
162
- new ExType ("csv" , List .of ("text/csv" )),
163
- new ExType ("doc" , List .of ("application/msword" )),
164
- new ExType ("docx" , List .of ("application/vnd.openxmlformats-officedocument.wordprocessingml.document" )),
165
- new ExType ("gz" , List .of ("application/gzip" , "application/x-gzip" )),
166
- new ExType ("jar" , List .of ("application/java-archive" , "application/x-java-archive" , "application/jar" )),
167
- new ExType ("jpg" , List .of ("image/jpeg" )),
168
- new ExType ("js" , List .of ("text/plain" , "text/javascript" , "application/javascript" )),
169
- new ExType ("json" , List .of ("application/json" )),
170
- new ExType ("markdown" , List .of ("text/markdown" )),
171
- new ExType ("md" , List .of ("text/markdown" , "application/x-genesis-rom" )),
172
- new ExType ("mp3" , List .of ("audio/mpeg" )),
173
- new ExType ("mp4" , List .of ("video/mp4" )),
174
- new ExType ("odp" , List .of ("application/vnd.oasis.opendocument.presentation" )),
175
- new ExType ("ods" , List .of ("application/vnd.oasis.opendocument.spreadsheet" )),
176
- new ExType ("odt" , List .of ("application/vnd.oasis.opendocument.text" )),
177
- new ExType ("pdf" , List .of ("application/pdf" )),
178
- new ExType ("php" , List .of ("text/plain" , "text/php" , "application/x-php" )),
179
- new ExType ("png" , List .of ("image/png" )),
180
- new ExType ("ppt" , List .of ("application/vnd.ms-powerpoint" )),
181
- new ExType ("pptx" ,List .of ("application/vnd.openxmlformats-officedocument.presentationml.presentation" )),
182
- new ExType ("py" , List .of ("text/plain" , "text/x-python" , "text/x-python-script" )),
183
- new ExType ("rar" , List .of ("application/rar" , "application/vnd.rar" , "application/x-rar" , "application/x-rar-compressed" )),
184
- new ExType ("rtf" , List .of ("application/rtf" , "text/rtf" )),
185
- new ExType ("webm" , List .of ("video/webm" )),
186
- new ExType ("webp" , List .of ("image/webp" )),
187
- new ExType ("xls" , List .of ("application/vnd.ms-excel" )),
188
- new ExType ("xlsx" , List .of ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" )),
189
- new ExType ("7z" , List .of ("application/x-7z-compressed" )),
190
- new ExType ("wasm" , List .of ("application/wasm" )),
191
- };
160
+ List <ExType > exTypes = new ArrayList <ExType >();
161
+
162
+ // extensions with consistent content type
163
+ exTypes .add (new ExType ("adoc" , List .of ("text/plain" )));
164
+ exTypes .add (new ExType ("css" , List .of ("text/css" )));
165
+ exTypes .add (new ExType ("doc" , List .of ("application/msword" )));
166
+ exTypes .add (new ExType ("docx" , List .of ("application/vnd.openxmlformats-officedocument.wordprocessingml.document" )));
167
+ exTypes .add (new ExType ("gz" , List .of ("application/gzip" , "application/x-gzip" )));
168
+ exTypes .add (new ExType ("jar" , List .of ("application/java-archive" , "application/x-java-archive" , "application/jar" )));
169
+ exTypes .add (new ExType ("jpg" , List .of ("image/jpeg" )));
170
+ exTypes .add (new ExType ("js" , List .of ("text/plain" , "text/javascript" , "application/javascript" )));
171
+ exTypes .add (new ExType ("json" , List .of ("application/json" )));
172
+ exTypes .add (new ExType ("markdown" , List .of ("text/markdown" )));
173
+ exTypes .add (new ExType ("md" , List .of ("text/markdown" , "application/x-genesis-rom" )));
174
+ exTypes .add (new ExType ("mp3" , List .of ("audio/mpeg" )));
175
+ exTypes .add (new ExType ("mp4" , List .of ("video/mp4" )));
176
+ exTypes .add (new ExType ("odp" , List .of ("application/vnd.oasis.opendocument.presentation" )));
177
+ exTypes .add (new ExType ("ods" , List .of ("application/vnd.oasis.opendocument.spreadsheet" )));
178
+ exTypes .add (new ExType ("odt" , List .of ("application/vnd.oasis.opendocument.text" )));
179
+ exTypes .add (new ExType ("pdf" , List .of ("application/pdf" )));
180
+ exTypes .add (new ExType ("php" , List .of ("text/plain" , "text/php" , "application/x-php" )));
181
+ exTypes .add (new ExType ("png" , List .of ("image/png" )));
182
+ exTypes .add (new ExType ("ppt" , List .of ("application/vnd.ms-powerpoint" )));
183
+ exTypes .add (new ExType ("pptx" , List .of ("application/vnd.openxmlformats-officedocument.presentationml.presentation" )));
184
+ exTypes .add (new ExType ("py" , List .of ("text/plain" , "text/x-python" , "text/x-python-script" )));
185
+ exTypes .add (new ExType ("webm" , List .of ("video/webm" )));
186
+ exTypes .add (new ExType ("webp" , List .of ("image/webp" )));
187
+ exTypes .add (new ExType ("xls" , List .of ("application/vnd.ms-excel" )));
188
+ exTypes .add (new ExType ("xlsx" , List .of ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" )));
189
+ exTypes .add (new ExType ("wasm" , List .of ("application/wasm" )));
190
+
191
+ // extensions with content type that differs on Windows 11+
192
+ if (OperatingSystem .isWindows () &&
193
+ (System .getProperty ("os.name" ).endsWith ("11" ) ||
194
+ new OSVersion (10 , 0 ).compareTo (OSVersion .current ()) > 0 )) {
195
+ System .out .println ("Windows 11+ detected: using different types" );
196
+ exTypes .add (new ExType ("bz2" , List .of ("application/bz2" , "application/x-bzip2" , "application/x-bzip" , "application/x-compressed" )));
197
+ exTypes .add (new ExType ("csv" , List .of ("text/csv" , "application/vnd.ms-excel" )));
198
+ exTypes .add (new ExType ("rar" , List .of ("application/rar" , "application/vnd.rar" , "application/x-rar" , "application/x-rar-compressed" , "application/x-compressed" )));
199
+ exTypes .add (new ExType ("rtf" , List .of ("application/rtf" , "text/rtf" , "application/msword" )));
200
+ exTypes .add (new ExType ("7z" , List .of ("application/x-7z-compressed" , "application/x-compressed" )));
201
+ } else {
202
+ exTypes .add (new ExType ("bz2" , List .of ("application/bz2" , "application/x-bzip2" , "application/x-bzip" )));
203
+ exTypes .add (new ExType ("csv" , List .of ("text/csv" )));
204
+ exTypes .add (new ExType ("rar" , List .of ("application/rar" , "application/vnd.rar" , "application/x-rar" , "application/x-rar-compressed" )));
205
+ exTypes .add (new ExType ("rtf" , List .of ("application/rtf" , "text/rtf" )));
206
+ exTypes .add (new ExType ("7z" , List .of ("application/x-7z-compressed" )));
207
+ }
208
+
192
209
failures += checkContentTypes (exTypes );
193
210
194
211
// Verify type is found when the extension is in a fragment component
0 commit comments