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 }
|
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 }
|