jdk/test/java/nio/file/Files/probeContentType/Basic.java
author bpb
Tue, 09 Aug 2016 07:43:48 -0700
changeset 40249 c164790b79f9
parent 40208 4554a9ae19ef
child 40451 17dd00080a7c
permissions -rw-r--r--
8163431: probeContentType/Basic.java fails after changes for JDK-8146215 Summary: Allow multiple legal MIME type interpretations for certain extensions. Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     1
/*
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
     2
 * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     4
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     8
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    13
 * accompanied this code).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    14
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4056
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4056
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4056
diff changeset
    21
 * questions.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    22
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    23
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    24
/* @test
40208
4554a9ae19ef 8162745: content-types.properties files are missing some modern types
bpb
parents: 40207
diff changeset
    25
 * @bug 4313887 8129632 8129633 8162624 8146215 8162745
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    26
 * @summary Unit test for probeContentType method
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    27
 * @library ../..
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    28
 * @build Basic SimpleFileTypeDetector
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    29
 * @run main/othervm Basic
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    30
 */
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    31
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    32
import java.io.*;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    33
import java.nio.file.*;
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    34
import java.util.stream.Stream;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    35
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    36
/**
31450
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
    37
 * Uses Files.probeContentType to probe html file, custom file type, and minimal
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
    38
 * set of file extension to content type mappings.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    39
 */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    40
public class Basic {
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    41
    private static final boolean IS_UNIX =
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    42
        ! System.getProperty("os.name").startsWith("Windows");
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    43
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    44
    static Path createHtmlFile() throws IOException {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    45
        Path file = Files.createTempFile("foo", ".html");
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    46
        try (OutputStream out = Files.newOutputStream(file)) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    47
            out.write("<html><body>foo</body></html>".getBytes());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    48
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    49
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    50
        return file;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    51
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    52
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
    53
    static Path createGrapeFile() throws IOException {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    54
        return Files.createTempFile("red", ".grape");
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    55
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    56
40207
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    57
    private static void checkMimeTypesFile(Path mimeTypes) {
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    58
        if (!Files.exists(mimeTypes)) {
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    59
            System.out.println(mimeTypes + " does not exist");
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    60
        } else if (!Files.isReadable(mimeTypes)) {
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    61
            System.out.println(mimeTypes + " is not readable");
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    62
        } else {
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    63
            System.out.println(mimeTypes + " contents:");
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    64
            try (Stream<String> lines = Files.lines(mimeTypes)) {
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    65
                lines.forEach(System.out::println);
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    66
                System.out.println("");
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    67
            } catch (IOException ioe) {
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    68
                System.err.printf("Problem reading %s: %s%n",
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    69
                                  mimeTypes, ioe.getMessage());
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    70
            }
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    71
        }
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    72
    }
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    73
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    74
    private static int checkContentTypes(String expected, String actual) {
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    75
        assert expected != null;
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    76
        assert actual != null;
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    77
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    78
        if (!expected.equals(actual)) {
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    79
            if (IS_UNIX) {
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    80
                Path userMimeTypes =
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    81
                    Paths.get(System.getProperty("user.home"), ".mime.types");
40207
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    82
                checkMimeTypesFile(userMimeTypes);
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    83
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    84
                Path etcMimeTypes = Paths.get("/etc/mime.types");
40207
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
    85
                checkMimeTypesFile(etcMimeTypes);
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    86
            }
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    87
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    88
            System.err.println("Expected \"" + expected
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    89
                               + "\" but obtained \""
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    90
                               + actual + "\"");
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    91
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    92
            return 1;
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    93
        }
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    94
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    95
        return 0;
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    96
    }
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
    97
40249
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
    98
    static int checkContentTypes(String[] extensions, String[][] expectedTypes)
31450
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
    99
        throws IOException {
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   100
        if (extensions.length != expectedTypes.length) {
40207
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
   101
            System.err.println("Parameter array lengths differ");
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
   102
            return 1;
31450
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   103
        }
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   104
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   105
        int failures = 0;
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   106
        for (int i = 0; i < extensions.length; i++) {
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   107
            String extension = extensions[i];
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   108
            Path file = Files.createTempFile("foo", "." + extension);
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   109
            try {
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   110
                String type = Files.probeContentType(file);
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   111
                if (type == null) {
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   112
                    System.err.println("Content type of " + extension
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   113
                            + " cannot be determined");
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   114
                    failures++;
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   115
                } else {
40249
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   116
                    boolean isTypeFound = false;
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   117
                    for (String s : expectedTypes[i]) {
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   118
                        if (type.equals(s)) {
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   119
                            isTypeFound = true;
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   120
                            break;
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   121
                        }
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   122
                    }
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   123
                    if (!isTypeFound) {
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   124
                        System.err.printf("Content type: %s; expected: ", type);
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   125
                        int j = 0;
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   126
                        for (String s : expectedTypes[i]) {
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   127
                            if (j++ == 0) {
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   128
                                System.err.printf("%s", s);
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   129
                            } else {
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   130
                                System.err.printf(", or %s", s);
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   131
                            }
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   132
                        }
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   133
                        System.err.println();
31450
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   134
                        failures++;
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   135
                    }
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   136
                }
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   137
            } finally {
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   138
                Files.delete(file);
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   139
            }
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   140
        }
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   141
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
   142
        return failures;
31450
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   143
    }
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   144
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   145
    public static void main(String[] args) throws IOException {
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
   146
        int failures = 0;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   147
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   148
        // exercise default file type detector
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
   149
        Path file = createHtmlFile();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   150
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   151
            String type = Files.probeContentType(file);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   152
            if (type == null) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   153
                System.err.println("Content type cannot be determined - test skipped");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   154
            } else {
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
   155
                failures += checkContentTypes("text/html", type);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   156
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   157
        } finally {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   158
            Files.delete(file);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   159
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   160
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   161
        // exercise custom file type detector
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   162
        file = createGrapeFile();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   163
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   164
            String type = Files.probeContentType(file);
40207
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
   165
            if (type == null) {
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
   166
                System.err.println("Custom file type detector not installed?");
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
   167
                failures++;
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
   168
            } else {
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
   169
                failures += checkContentTypes("grape/unknown", type);
cdd28dfda879 8146215: (fs) java/nio/file/Files/probeContentType/Basic.java failed frequently on Solaris-sparc with Unexpected type: text/plain
bpb
parents: 39830
diff changeset
   170
            }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   171
        } finally {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   172
            Files.delete(file);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   173
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   174
40208
4554a9ae19ef 8162745: content-types.properties files are missing some modern types
bpb
parents: 40207
diff changeset
   175
        // Verify that certain media extensions are mapped to the correct type.
4554a9ae19ef 8162745: content-types.properties files are missing some modern types
bpb
parents: 40207
diff changeset
   176
        String[] extensions = new String[]{
40249
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   177
            "aac",
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   178
            "flac",
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   179
            "jpg",
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   180
            "mp3",
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   181
            "mp4",
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   182
            "pdf",
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   183
            "png",
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   184
            "webm"
40208
4554a9ae19ef 8162745: content-types.properties files are missing some modern types
bpb
parents: 40207
diff changeset
   185
        };
40249
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   186
        String[][] expectedTypes = new String[][] {
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   187
            {"audio/aac", "audio/x-aac"},
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   188
            {"audio/flac", "audio/x-flac"},
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   189
            {"image/jpeg"},
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   190
            {"audio/mpeg"},
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   191
            {"video/mp4"},
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   192
            {"application/pdf"},
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   193
            {"image/png"},
c164790b79f9 8163431: probeContentType/Basic.java fails after changes for JDK-8146215
bpb
parents: 40208
diff changeset
   194
            {"video/webm"}
40208
4554a9ae19ef 8162745: content-types.properties files are missing some modern types
bpb
parents: 40207
diff changeset
   195
        };
4554a9ae19ef 8162745: content-types.properties files are missing some modern types
bpb
parents: 40207
diff changeset
   196
        failures += checkContentTypes(extensions, expectedTypes);
36128
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
   197
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
   198
        if (failures > 0) {
e17994ab030a 8150204: (fs) Enhance java/nio/file/Files/probeContentType/Basic.java debugging output
bpb
parents: 32042
diff changeset
   199
            throw new RuntimeException("Test failed!");
31450
35fe554da3fb 8129632: (fs) Files.probeContentType returns null on Mac OS X
bpb
parents: 9035
diff changeset
   200
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   201
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   202
}