1 /*
   2  * Copyright (c) 2008, 2016, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4313887
  26  * @summary Unit test for probeContentType method
  27  * @library ../..
  28  * @build Basic SimpleFileTypeDetector
  29  * @run main/othervm Basic
  30  */
  31 

  32 import java.io.*;
  33 import java.nio.file.*;
  34 import java.util.stream.Stream;
  35 
  36 /**
  37  * Uses Files.probeContentType to probe html file, custom file type, and minimal
  38  * set of file extension to content type mappings.
  39  */

  40 public class Basic {
  41 
  42     private static final boolean IS_UNIX =
  43         ! System.getProperty("os.name").startsWith("Windows");
  44 
  45     static Path createHtmlFile() throws IOException {
  46         Path file = Files.createTempFile("foo", ".html");
  47         try (OutputStream out = Files.newOutputStream(file)) {
  48             out.write("<html><body>foo</body></html>".getBytes());
  49         }
  50 
  51         return file;
  52     }
  53 
  54     static Path createGrapeFile() throws IOException {
  55         return Files.createTempFile("red", ".grape");
  56     }
  57 
  58     private static int checkContentTypes(String expected, String actual) {
  59         assert expected != null;
  60         assert actual != null;
  61 
  62         if (!expected.equals(actual)) {
  63             if (IS_UNIX) {
  64                 Path userMimeTypes =
  65                     Paths.get(System.getProperty("user.home"), ".mime.types");
  66                 if (!Files.exists(userMimeTypes)) {
  67                     System.out.println(userMimeTypes + " does not exist");
  68                 } else if (!Files.isReadable(userMimeTypes)) {
  69                     System.out.println(userMimeTypes + " is not readable");
  70                 } else {
  71                     System.out.println(userMimeTypes + " contents:");
  72                     try (Stream<String> lines = Files.lines(userMimeTypes)) {
  73                         lines.forEach(System.out::println);
  74                         System.out.println("");
  75                     } catch (IOException ioe) {
  76                         System.err.println("Problem reading "
  77                                            + userMimeTypes);
  78                     }
  79                 }
  80 
  81                 Path etcMimeTypes = Paths.get("/etc/mime.types");
  82                 if (!Files.exists(etcMimeTypes)) {
  83                     System.out.println(etcMimeTypes + " does not exist");
  84                 } else if (!Files.isReadable(etcMimeTypes)) {
  85                     System.out.println(etcMimeTypes + " is not readable");
  86                 } else {
  87                     System.out.println(etcMimeTypes + " contents:");
  88                     try (Stream<String> lines = Files.lines(etcMimeTypes)) {
  89                         lines.forEach(System.out::println);
  90                         System.out.println("");
  91                     } catch (IOException ioe) {
  92                         System.err.println("Problem reading "
  93                                            + etcMimeTypes);
  94                     }
  95                 }
  96             }
  97 
  98             System.err.println("Expected \"" + expected
  99                                + "\" but obtained \""
 100                                + actual + "\"");
 101 
 102             return 1;
 103         }
 104 
 105         return 0;
 106     }
 107 
 108     public static void main(String[] args) throws IOException {
 109         int failures = 0;
 110 
 111         // exercise default file type detector
 112         Path file = createHtmlFile();
 113         try {
 114             String type = Files.probeContentType(file);
 115             if (type == null) {
 116                 System.err.println("Content type cannot be determined - test skipped");
 117             } else {
 118                 failures += checkContentTypes("text/html", type);

 119             }
 120         } finally {
 121             Files.delete(file);
 122         }
 123 
 124         // exercise custom file type detector
 125         file = createGrapeFile();
 126         try {
 127             String type = Files.probeContentType(file);
 128             if (type == null)
 129                 throw new RuntimeException("Custom file type detector not installed?");
 130             failures += checkContentTypes("grape/unknown", type);

 131         } finally {
 132             Files.delete(file);
 133         }
 134 
 135         if (failures > 0) {
 136             throw new RuntimeException("Test failed!");
 137         }
 138     }
 139 }
--- EOF ---