Skip to content

Commit 7ed3c3e

Browse files
Amos ShiGoeLin
authored andcommitted
8320943: Files/probeContentType/Basic.java fails on latest Windows 11 - content type mismatch
Backport-of: 27cf2f404857aa8c3b1679d0e67f0a3a37e7604b
1 parent d1bdb25 commit 7ed3c3e

File tree

1 file changed

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

1 file changed

+62
-43
lines changed

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

Lines changed: 62 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,26 +23,29 @@
2323

2424
/* @test
2525
* @bug 4313887 8129632 8129633 8162624 8146215 8162745 8273655 8274171
26+
* @library /test/lib
27+
* @modules java.base/jdk.internal.util
2628
* @summary Unit test for probeContentType method
2729
* @library ../..
28-
* @build Basic SimpleFileTypeDetector
30+
* @build jdk.test.lib.OSVersion jdk.test.lib.Platform Basic SimpleFileTypeDetector
2931
* @run main/othervm Basic
3032
*/
3133

3234
import java.io.*;
3335
import java.nio.file.*;
36+
import java.util.ArrayList;
3437
import java.util.List;
3538
import java.util.Objects;
3639
import java.util.stream.Stream;
3740

41+
import jdk.test.lib.Platform;
42+
import jdk.test.lib.OSVersion;
43+
3844
/**
3945
* Uses Files.probeContentType to probe html file, custom file type, and minimal
4046
* set of file extension to content type mappings.
4147
*/
4248
public class Basic {
43-
private static final boolean IS_UNIX =
44-
! System.getProperty("os.name").startsWith("Windows");
45-
4649
static Path createHtmlFile() throws IOException {
4750
Path file = Files.createTempFile("foo", ".html");
4851
try (OutputStream out = Files.newOutputStream(file)) {
@@ -78,7 +81,7 @@ private static int checkContentTypes(String expected, String actual) {
7881
assert actual != null;
7982

8083
if (!expected.equals(actual)) {
81-
if (IS_UNIX) {
84+
if (!Platform.isWindows()) {
8285
Path userMimeTypes =
8386
Paths.get(System.getProperty("user.home"), ".mime.types");
8487
checkMimeTypesFile(userMimeTypes);
@@ -97,12 +100,12 @@ private static int checkContentTypes(String expected, String actual) {
97100
return 0;
98101
}
99102

100-
static int checkContentTypes(ExType[] exTypes)
103+
static int checkContentTypes(List<ExType> exTypes)
101104
throws IOException {
102105
int failures = 0;
103-
for (int i = 0; i < exTypes.length; i++) {
104-
String extension = exTypes[i].extension();
105-
List<String> expectedTypes = exTypes[i].expectedTypes();
106+
for (ExType exType : exTypes) {
107+
String extension = exType.extension();
108+
List<String> expectedTypes = exType.expectedTypes();
106109
Path file = Files.createTempFile("foo", "." + extension);
107110
try {
108111
String type = Files.probeContentType(file);
@@ -154,39 +157,55 @@ public static void main(String[] args) throws IOException {
154157
}
155158

156159
// Verify that certain extensions are mapped to the correct type.
157-
var exTypes = new ExType[] {
158-
new ExType("adoc", List.of("text/plain")),
159-
new ExType("bz2", List.of("application/bz2", "application/x-bzip2", "application/x-bzip")),
160-
new ExType("css", List.of("text/css")),
161-
new ExType("csv", List.of("text/csv")),
162-
new ExType("doc", List.of("application/msword")),
163-
new ExType("docx", List.of("application/vnd.openxmlformats-officedocument.wordprocessingml.document")),
164-
new ExType("gz", List.of("application/gzip", "application/x-gzip")),
165-
new ExType("jar", List.of("application/java-archive", "application/x-java-archive", "application/jar")),
166-
new ExType("jpg", List.of("image/jpeg")),
167-
new ExType("js", List.of("text/javascript", "application/javascript")),
168-
new ExType("json", List.of("application/json")),
169-
new ExType("markdown", List.of("text/markdown")),
170-
new ExType("md", List.of("text/markdown", "application/x-genesis-rom")),
171-
new ExType("mp3", List.of("audio/mpeg")),
172-
new ExType("mp4", List.of("video/mp4")),
173-
new ExType("odp", List.of("application/vnd.oasis.opendocument.presentation")),
174-
new ExType("ods", List.of("application/vnd.oasis.opendocument.spreadsheet")),
175-
new ExType("odt", List.of("application/vnd.oasis.opendocument.text")),
176-
new ExType("pdf", List.of("application/pdf")),
177-
new ExType("php", List.of("text/plain", "text/php", "application/x-php")),
178-
new ExType("png", List.of("image/png")),
179-
new ExType("ppt", List.of("application/vnd.ms-powerpoint")),
180-
new ExType("pptx",List.of("application/vnd.openxmlformats-officedocument.presentationml.presentation")),
181-
new ExType("py", List.of("text/plain", "text/x-python", "text/x-python-script")),
182-
new ExType("rar", List.of("application/rar", "application/vnd.rar", "application/x-rar", "application/x-rar-compressed")),
183-
new ExType("rtf", List.of("application/rtf", "text/rtf")),
184-
new ExType("webm", List.of("video/webm")),
185-
new ExType("webp", List.of("image/webp")),
186-
new ExType("xls", List.of("application/vnd.ms-excel")),
187-
new ExType("xlsx", List.of("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")),
188-
new ExType("7z", List.of("application/x-7z-compressed")),
189-
};
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"))); Note. "wasm" is added via JDK-8297609 (Java 20) which not exist in current java version yet
190+
191+
// extensions with content type that differs on Windows 11+
192+
if (Platform.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+
190209
failures += checkContentTypes(exTypes);
191210

192211
if (failures > 0) {

0 commit comments

Comments
 (0)