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 }
|
1 /*
2 * Copyright (c) 2008, 2015, 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
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, custom file type, and minimal
37 * set of file extension to content type mappings.
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 static void checkContentTypes(String[] extensions, String[] expectedTypes)
55 throws IOException {
56 if (extensions.length != expectedTypes.length) {
57 throw new IllegalArgumentException("Parameter array lengths differ");
58 }
59
60 int failures = 0;
61 for (int i = 0; i < extensions.length; i++) {
62 String extension = extensions[i];
63 Path file = Files.createTempFile("foo", "." + extension);
64 try {
65 String type = Files.probeContentType(file);
66 if (type == null) {
67 System.err.println("Content type of " + extension
68 + " cannot be determined");
69 failures++;
70 } else {
71 if (!type.equals(expectedTypes[i])) {
72 System.err.println("Content type: " + type
73 + "; expected: " + expectedTypes[i]);
74 failures++;
75 }
76 }
77 } finally {
78 file.toFile().delete();
79 }
80 }
81
82 if (failures > 0) {
83 throw new RuntimeException("Test failed!");
84 }
85 }
86
87 public static void main(String[] args) throws IOException {
88
89 // exercise default file type detector
90 Path file = createHtmlFile();
91 try {
92 String type = Files.probeContentType(file);
93 if (type == null) {
94 System.err.println("Content type cannot be determined - test skipped");
95 } else {
96 if (!type.equals("text/html"))
97 throw new RuntimeException("Unexpected type: " + type);
98 }
99 } finally {
100 Files.delete(file);
101 }
102
103 // exercise custom file type detector
104 file = createGrapeFile();
105 try {
106 String type = Files.probeContentType(file);
107 if (type == null)
108 throw new RuntimeException("Custom file type detector not installed?");
109 if (!type.equals("grape/unknown"))
110 throw new RuntimeException("Unexpected type: " + type);
111 } finally {
112 Files.delete(file);
113 }
114
115 // Verify that common file extensions are mapped to the correct content
116 // types taking into account OS-dependent minimum state.
117 String[] extensions;
118 String[] expectedTypes;
119 if (System.getProperty("os.name").toLowerCase().contains("windows")) {
120 extensions = new String[] {
121 "jpg", "png"
122 };
123 expectedTypes = new String[] {
124 "image/jpeg", "image/png"
125 };
126 } else {
127 extensions = new String[] {
128 "jpg", "mp3", "mp4", "pdf", "png"
129 };
130 expectedTypes = new String[] {
131 "image/jpeg", "audio/mpeg", "video/mp4", "application/pdf",
132 "image/png"
133 };
134 }
135 checkContentTypes (extensions, expectedTypes);
136 }
137 }
|