jdk/test/java/nio/file/Files/ContentType.java
changeset 3065 452aaa2899fc
parent 2057 3acf8e5e2ca0
child 3424 8f6c0145dc8f
equal deleted inserted replaced
3063:a3fd491f7754 3065:452aaa2899fc
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    21  * have any questions.
    22  */
    22  */
    23 
    23 
       
    24 /* @test
       
    25  * @bug 4313887
       
    26  * @summary Unit test for probeContentType method
       
    27  * @library ..
       
    28  * @build ContentType SimpleFileTypeDetector
       
    29  */
       
    30 
    24 import java.nio.file.*;
    31 import java.nio.file.*;
    25 import java.io.*;
    32 import java.io.*;
    26 
    33 
    27 /**
    34 /**
    28  * Uses Files.probeContentType to probe html file and custom file type.
    35  * Uses Files.probeContentType to probe html file and custom file type.
    29  */
    36  */
    30 
    37 
    31 public class ContentType {
    38 public class ContentType {
    32 
    39 
    33     static FileRef createHtmlFile() throws IOException {
    40     static Path createHtmlFile() throws IOException {
    34         Path file = File.createTempFile("foo", ".html").toPath();
    41         Path file = File.createTempFile("foo", ".html").toPath();
    35         OutputStream out = file.newOutputStream();
    42         OutputStream out = file.newOutputStream();
    36         try {
    43         try {
    37             out.write("<html><body>foo</body></html>".getBytes());
    44             out.write("<html><body>foo</body></html>".getBytes());
    38         } finally {
    45         } finally {
    40         }
    47         }
    41 
    48 
    42         return file;
    49         return file;
    43     }
    50     }
    44 
    51 
    45     static FileRef createUnknownFile() throws IOException {
    52     static Path createGrapeFile() throws IOException {
    46         return File.createTempFile("unknown", "unknown-file-type-789").toPath();
       
    47     }
       
    48 
       
    49     static FileRef createGrapeFile() throws IOException {
       
    50         return File.createTempFile("red", ".grape").toPath();
    53         return File.createTempFile("red", ".grape").toPath();
    51     }
    54     }
    52 
    55 
    53     public static void main(String[] args) throws IOException {
    56     public static void main(String[] args) throws IOException {
    54 
    57 
    55         // exercise default file type detector
    58         // exercise default file type detector
    56         FileRef file = createHtmlFile();
    59         Path file = createHtmlFile();
    57         try {
    60         try {
    58             String type = Files.probeContentType(file);
    61             String type = Files.probeContentType(file);
    59             if (type == null) {
    62             if (type == null) {
    60                 System.err.println("Content type cannot be determined - test skipped");
    63                 System.err.println("Content type cannot be determined - test skipped");
    61             } else {
    64             } else {
    62                 if (!type.equals("text/html"))
    65                 if (!type.equals("text/html"))
    63                     throw new RuntimeException("Unexpected type: " + type);
    66                     throw new RuntimeException("Unexpected type: " + type);
    64             }
    67             }
    65         } finally {
    68         } finally {
    66             TestUtil.deleteUnchecked(file);
    69             file.delete();
    67         }
       
    68         file = createUnknownFile();
       
    69         try {
       
    70             String type = Files.probeContentType(file);
       
    71             if (type != null)
       
    72                  throw new RuntimeException(file + " should not be recognized as:" +
       
    73                      type);
       
    74         } finally {
       
    75             TestUtil.deleteUnchecked(file);
       
    76         }
    70         }
    77 
    71 
    78         // exercise custom file type detector
    72         // exercise custom file type detector
    79         file = createGrapeFile();
    73         file = createGrapeFile();
    80         try {
    74         try {
    82             if (type == null)
    76             if (type == null)
    83                 throw new RuntimeException("Custom file type detector not installed?");
    77                 throw new RuntimeException("Custom file type detector not installed?");
    84             if (!type.equals("grape/unknown"))
    78             if (!type.equals("grape/unknown"))
    85                 throw new RuntimeException("Unexpected type: " + type);
    79                 throw new RuntimeException("Unexpected type: " + type);
    86         } finally {
    80         } finally {
    87             TestUtil.deleteUnchecked(file);
    81             file.delete();
    88         }
    82         }
    89 
    83 
    90     }
    84     }
    91 }
    85 }