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