1 /* 2 * Copyright (c) 2008, 2011, 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.nio.file.*; 33 import java.io.*; 34 35 /** 36 * Uses Files.probeContentType to probe html file and custom file type. 37 */ 38 39 public class Basic { 40 41 static Path createHtmlFile() throws IOException { 42 Path file = Files.createTempFile("foo", ".html"); 43 try (OutputStream out = Files.newOutputStream(file)) { 44 out.write("<html><body>foo</body></html>".getBytes()); 45 } 46 47 return file; 48 } 49 50 static Path createGrapeFile() throws IOException { 51 return Files.createTempFile("red", ".grape"); 52 } 53 54 public static void main(String[] args) throws IOException { 55 56 // exercise default file type detector 57 Path file = createHtmlFile(); 58 try { 59 String type = Files.probeContentType(file); 60 if (type == null) { 61 System.err.println("Content type cannot be determined - test skipped"); 62 } else { 63 if (!type.equals("text/html")) 64 throw new RuntimeException("Unexpected type: " + type); 65 } 66 } finally { 67 Files.delete(file); 68 } 69 70 // exercise custom file type detector 71 file = createGrapeFile(); 72 try { 73 String type = Files.probeContentType(file); 74 if (type == null) 75 throw new RuntimeException("Custom file type detector not installed?"); 76 if (!type.equals("grape/unknown")) 77 throw new RuntimeException("Unexpected type: " + type); 78 } finally { 79 Files.delete(file); 80 } 81 82 } 83 }