Skip to content

Commit 9bc6bd6

Browse files
committed
8320943: Files/probeContentType/Basic.java fails on latest Windows 11 - content type mismatch
Backport-of: 87516e29dc5015c4cab2c07c5539ad30f2768667
1 parent 5df2a5c commit 9bc6bd6

File tree

1 file changed

+60
-43
lines changed
  • test/jdk/java/nio/file/Files/probeContentType

1 file changed

+60
-43
lines changed

test/jdk/java/nio/file/Files/probeContentType/Basic.java

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,7 @@
2323

2424
/* @test
2525
* @bug 4313887 8129632 8129633 8162624 8146215 8162745 8273655 8274171 8287237 8297609
26+
* @modules java.base/jdk.internal.util
2627
* @summary Unit test for probeContentType method
2728
* @library ../..
2829
* @build Basic SimpleFileTypeDetector
@@ -33,17 +34,18 @@
3334
import java.io.OutputStream;
3435
import java.nio.file.Files;
3536
import java.nio.file.Path;
37+
import java.util.ArrayList;
3638
import java.util.List;
3739
import java.util.stream.Stream;
3840

41+
import jdk.internal.util.OperatingSystem;
42+
import jdk.internal.util.OSVersion;
43+
3944
/**
4045
* Uses Files.probeContentType to probe html file, custom file type, and minimal
4146
* set of file extension to content type mappings.
4247
*/
4348
public class Basic {
44-
private static final boolean IS_UNIX =
45-
! System.getProperty("os.name").startsWith("Windows");
46-
4749
static Path createHtmlFile() throws IOException {
4850
Path file = Files.createTempFile("foo", ".html");
4951
try (OutputStream out = Files.newOutputStream(file)) {
@@ -79,7 +81,7 @@ private static int checkContentTypes(String expected, String actual) {
7981
assert actual != null;
8082

8183
if (!expected.equals(actual)) {
82-
if (IS_UNIX) {
84+
if (!OperatingSystem.isWindows()) {
8385
Path userMimeTypes =
8486
Path.of(System.getProperty("user.home"), ".mime.types");
8587
checkMimeTypesFile(userMimeTypes);
@@ -98,12 +100,12 @@ private static int checkContentTypes(String expected, String actual) {
98100
return 0;
99101
}
100102

101-
static int checkContentTypes(ExType[] exTypes)
103+
static int checkContentTypes(List<ExType> exTypes)
102104
throws IOException {
103105
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();
107109
Path file = Files.createTempFile("foo", "." + extension);
108110
try {
109111
String type = Files.probeContentType(file);
@@ -155,40 +157,55 @@ public static void main(String[] args) throws IOException {
155157
}
156158

157159
// 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+
192209
failures += checkContentTypes(exTypes);
193210

194211
// Verify type is found when the extension is in a fragment component

0 commit comments

Comments
 (0)