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  public class MimeTypesTest extends TestCase {
25  
26      private MimeTypes types;
27  
28      private MimeType binary;
29  
30      private MimeType text;
31  
32      private MimeType html;
33  
34      protected void setUp() throws MimeTypeException {
35          types = new MimeTypes();
36          binary = types.forName("application/octet-stream");
37          text = types.forName("text/plain");
38          text.addAlias("text/x-plain");
39          html = types.forName("text/html");
40          html.setSuperType(text);
41      }
42  
43      public void testForName() throws MimeTypeException {
44          assertEquals(text, types.forName("text/plain"));
45          assertEquals(text, types.forName("TEXT/PLAIN"));
46  
47          try {
48              types.forName("invalid");
49              fail("MimeTypeException not thrown on invalid type name");
50          } catch (MimeTypeException e) {
51              // expected
52          }
53      }
54  
55      public void testAddAlias() throws MimeTypeException {
56          assertEquals(text, types.forName("text/x-plain"));
57          try {
58              text.addAlias("invalid");
59              fail("MimeTypeException not thrown on invalid alias name");
60          } catch (MimeTypeException e) {
61              // expected
62          }
63      }
64  
65      public void testSuperType() throws MimeTypeException {
66          assertNull(binary.getSuperType());
67          assertEquals(binary, text.getSuperType());
68          assertEquals(text, html.getSuperType());
69     }
70  
71      public void testIsDescendantOf() {
72          assertFalse(binary.isDescendantOf(binary));
73          assertFalse(text.isDescendantOf(text));
74          assertFalse(html.isDescendantOf(html));
75  
76          assertTrue(text.isDescendantOf(binary));
77          assertFalse(binary.isDescendantOf(text));
78          
79          assertTrue(html.isDescendantOf(binary));
80          assertFalse(binary.isDescendantOf(html));
81  
82          assertTrue(html.isDescendantOf(text));
83          assertFalse(text.isDescendantOf(html));
84  
85          try {
86              binary.isDescendantOf(null);
87              fail("Expected IllegalArgumentException");
88          } catch (IllegalArgumentException e) {
89              // expected result
90          }
91      }
92  
93      public void testCompareTo() {
94          assertTrue(binary.compareTo(binary) == 0);
95          assertTrue(binary.compareTo(text) < 0);
96          assertTrue(binary.compareTo(html) < 0);
97  
98          assertTrue(text.compareTo(binary) > 0);
99          assertTrue(text.compareTo(text) == 0);
100         assertTrue(text.compareTo(html) < 0);
101 
102         assertTrue(html.compareTo(binary) > 0);
103         assertTrue(html.compareTo(text) > 0);
104         assertTrue(html.compareTo(html) == 0);
105 
106         try {
107             binary.compareTo(null);
108             fail("Expected IllegalArgumentException");
109         } catch (IllegalArgumentException e) {
110             // expected result
111         }
112     }
113 
114     /** Test getMimeType(byte[]) */
115     public void testGetMimeType_byteArray() {
116         try {
117             types.getMimeType((byte[])null);
118             fail("Expected IllegalArgumentException");
119         } catch (IllegalArgumentException e) {
120             // expected result
121         }
122 
123         // Plain text detection
124         assertText(new byte[] { (byte) 0xFF, (byte) 0xFE });
125         assertText(new byte[] { (byte) 0xFF, (byte) 0xFE });
126         assertText(new byte[] { (byte) 0xEF, (byte) 0xFB, (byte) 0xBF });
127         assertText(new byte[] { 'a', 'b', 'c' });
128         assertText(new byte[] { '\t', '\r', '\n', 0x0C, 0x1B });
129         assertNotText(new byte[] { '\t', '\r', '\n', 0x0E, 0x1C });
130     }
131 
132     private void assertText(byte[] prefix) {
133         assertMagic("text/plain", prefix);
134     }
135 
136     private void assertNotText(byte[] prefix) {
137         assertMagic("application/octet-stream", prefix);
138     }
139 
140     private void assertMagic(String expected, byte[] prefix) {
141         MimeType type = types.getMimeType(prefix);
142         assertNotNull(type);
143         assertEquals(expected, type.getName());
144     }
145 
146     /** Test getMimeType(InputStream) */
147     public void testGetMimeType_InputStream() throws IOException {
148         try {
149             types.getMimeType((InputStream)null);
150             fail("Expected IllegalArgumentException");
151         } catch (IllegalArgumentException e) {
152             // expected result
153         }
154     }
155 
156 }