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 junit.framework.TestCase;
20  
21  public class MimeTypeTest extends TestCase {
22  
23      private MimeTypes types;
24      private MimeType text;
25  
26      protected void setUp() throws MimeTypeException {
27          types = new MimeTypes();
28          text = types.forName("text/plain");
29      }
30  
31      /** Test MimeType constructor */
32      public void testConstrctor() {
33  
34          // Missing registry
35          try {
36              new MimeType(null, "text/plain");
37              fail("Expected IllegalArgumentException");
38          } catch (IllegalArgumentException e) {
39              // expected result
40          }
41  
42          // Missing name
43          try {
44              new MimeType(types, null);
45              fail("Expected IllegalArgumentException");
46          } catch (IllegalArgumentException e) {
47              // expected result
48          }
49  
50          // Invalid name (no slash)
51          try {
52              new MimeType(types, "application");
53              fail("Expected IllegalArgumentException");
54          } catch (IllegalArgumentException e) {
55              // expected result
56          }
57  
58          // Invalid name (not lower case)
59          try {
60              new MimeType(types, "TEXT/PLAIN");
61              fail("Expected IllegalArgumentException");
62          } catch (IllegalArgumentException e) {
63              // expected result
64          }
65      }
66  
67      public void testIsValidName() {
68          assertTrue(MimeType.isValid("application/octet-stream"));
69          assertTrue(MimeType.isValid("text/plain"));
70          assertTrue(MimeType.isValid("foo/bar"));
71          assertTrue(MimeType.isValid("a/b"));
72  
73          assertFalse(MimeType.isValid("application"));
74          assertFalse(MimeType.isValid("application/"));
75          assertFalse(MimeType.isValid("/"));
76          assertFalse(MimeType.isValid("/octet-stream"));
77          assertFalse(MimeType.isValid("application//octet-stream"));
78          assertFalse(MimeType.isValid("application/octet=stream"));
79          assertFalse(MimeType.isValid("application/\u00f6ctet-stream"));
80          assertFalse(MimeType.isValid("text/plain;"));
81          assertFalse(MimeType.isValid("text/plain; charset=UTF-8"));
82          try {
83              MimeType.isValid(null);
84              fail("Expected IllegalArgumentException");
85          } catch (IllegalArgumentException e) {
86              // expected result
87          }
88      }
89  
90      /** Test MimeType setDescription() */
91      public void testSetDescription() {
92          try {
93              text.setDescription(null);
94              fail("Expected IllegalArgumentException");
95          } catch (IllegalArgumentException e) {
96              // expected result
97          }
98      }
99  
100     /** Test MimeType setSuperType() */
101     public void testSetSuperType() throws MimeTypeException {
102         try {
103             text.setSuperType(null);
104             fail("Expected IllegalArgumentException");
105         } catch (IllegalArgumentException e) {
106             // expected result
107         }
108     }
109 
110 }