1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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      /** @inheritDoc */
32      @Override
33      protected void setUp() throws Exception {
34          super.setUp();
35          this.mimeTypes = TikaConfig.getDefaultConfig().getMimeRepository();
36          //this.mimeTypes = MimeTypesFactory.create("/org/apache/tika/mime/tika-mimetypes-minimal.xml");
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          // add evil test from TIKA-327
59          testFile("text/html", "evilhtml.html");
60          // add another evil html test from TIKA-357
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              //Add resource name and test again
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 }