1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.tika.mime;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import junit.framework.TestCase;
23
24 import org.apache.tika.config.TikaConfig;
25 import org.apache.tika.metadata.Metadata;
26
27 public class MimeDetectionTest extends TestCase {
28
29 private MimeTypes mimeTypes;
30
31
32 @Override
33 protected void setUp() throws Exception {
34 super.setUp();
35 this.mimeTypes = TikaConfig.getDefaultConfig().getMimeRepository();
36
37 }
38
39 public void testDetection() throws Exception {
40 testFile("image/svg+xml", "circles.svg");
41 testFile("image/svg+xml", "circles-with-prefix.svg");
42 testFile("image/png", "datamatrix.png");
43 testFile("text/html", "test.html");
44 testFile("application/xml", "test-iso-8859-1.xml");
45 testFile("application/xml", "test-utf8.xml");
46 testFile("application/xml", "test-utf16le.xml");
47 testFile("application/xml", "test-utf16be.xml");
48 testFile("application/xml", "test-long-comment.xml");
49 testFile("application/xslt+xml", "stylesheet.xsl");
50 testUrl(
51 "application/rdf+xml",
52 "http://www.ai.sri.com/daml/services/owl-s/1.2/Process.owl",
53 "test-difficult-rdf1.xml");
54 testUrl(
55 "application/rdf+xml",
56 "http://www.w3.org/2002/07/owl#",
57 "test-difficult-rdf2.xml");
58
59 testFile("text/html", "evilhtml.html");
60
61 testFile("text/html", "testlargerbuffer.html");
62 }
63
64 public void testAutosetSupertype() throws MimeTypeException {
65 MimeTypes types = new MimeTypes();
66 MimeType type = types.forName("application/something+xml");
67 assertEquals("application/xml", type.getSuperType().getName());
68
69 type = types.forName("text/something");
70 assertEquals("text/plain", type.getSuperType().getName());
71 }
72
73 private void testUrl(String expected, String url, String file) throws IOException{
74 InputStream in = getClass().getResourceAsStream(file);
75 testStream(expected, url, in);
76 }
77
78 private void testFile(String expected, String filename) throws IOException {
79 InputStream in = getClass().getResourceAsStream(filename);
80 testStream(expected, filename, in);
81 }
82
83 private void testStream(String expected, String urlOrFileName, InputStream in) throws IOException{
84 assertNotNull("Test stream: ["+urlOrFileName+"] is null!", in);
85 if (!in.markSupported()) {
86 in = new java.io.BufferedInputStream(in);
87 }
88 try {
89 Metadata metadata = new Metadata();
90 String mime = this.mimeTypes.detect(in, metadata).toString();
91 assertEquals(urlOrFileName + " is not properly detected: detected.", expected, mime);
92
93
94 metadata.set(Metadata.RESOURCE_NAME_KEY, urlOrFileName);
95 mime = this.mimeTypes.detect(in, metadata).toString();
96 assertEquals(urlOrFileName + " is not properly detected after adding resource name.", expected, mime);
97 } finally {
98 in.close();
99 }
100 }
101
102 }