jdk/test/java/nio/file/Files/ContentType.java
changeset 2057 3acf8e5e2ca0
child 3065 452aaa2899fc
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    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
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 import java.nio.file.*;
       
    25 import java.io.*;
       
    26 
       
    27 /**
       
    28  * Uses Files.probeContentType to probe html file and custom file type.
       
    29  */
       
    30 
       
    31 public class ContentType {
       
    32 
       
    33     static FileRef createHtmlFile() throws IOException {
       
    34         Path file = File.createTempFile("foo", ".html").toPath();
       
    35         OutputStream out = file.newOutputStream();
       
    36         try {
       
    37             out.write("<html><body>foo</body></html>".getBytes());
       
    38         } finally {
       
    39             out.close();
       
    40         }
       
    41 
       
    42         return file;
       
    43     }
       
    44 
       
    45     static FileRef createUnknownFile() 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();
       
    51     }
       
    52 
       
    53     public static void main(String[] args) throws IOException {
       
    54 
       
    55         // exercise default file type detector
       
    56         FileRef file = createHtmlFile();
       
    57         try {
       
    58             String type = Files.probeContentType(file);
       
    59             if (type == null) {
       
    60                 System.err.println("Content type cannot be determined - test skipped");
       
    61             } else {
       
    62                 if (!type.equals("text/html"))
       
    63                     throw new RuntimeException("Unexpected type: " + type);
       
    64             }
       
    65         } finally {
       
    66             TestUtil.deleteUnchecked(file);
       
    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         }
       
    77 
       
    78         // exercise custom file type detector
       
    79         file = createGrapeFile();
       
    80         try {
       
    81             String type = Files.probeContentType(file);
       
    82             if (type == null)
       
    83                 throw new RuntimeException("Custom file type detector not installed?");
       
    84             if (!type.equals("grape/unknown"))
       
    85                 throw new RuntimeException("Unexpected type: " + type);
       
    86         } finally {
       
    87             TestUtil.deleteUnchecked(file);
       
    88         }
       
    89 
       
    90     }
       
    91 }