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.util.HashMap;
20  import java.util.Map;
21  
22  import junit.framework.TestCase;
23  
24  public class MediaTypeTest extends TestCase {
25  
26      public void testBasics() {
27          assertEquals(
28                  "application/octet-stream",
29                  new MediaType("application", "octet-stream").toString());
30  
31          assertEquals(
32                  "text/plain",
33                  new MediaType("text", "plain").toString());
34  
35          Map<String, String> parameters = new HashMap<String, String>();
36          assertEquals(
37                  "text/plain",
38                  new MediaType("text", "plain", parameters).toString());
39  
40          parameters.put("charset", "UTF-8");
41          assertEquals(
42                  "text/plain; charset=UTF-8",
43                  new MediaType("text", "plain", parameters).toString());
44  
45          parameters.put("x-eol-style", "crlf");
46          assertEquals(
47                  "text/plain; charset=UTF-8; x-eol-style=crlf",
48                  new MediaType("text", "plain", parameters).toString());
49      }
50  
51      public void testLowerCase() {
52          assertEquals(
53                  "text/plain",
54                  new MediaType("TEXT", "PLAIN").toString());
55          assertEquals(
56                  "text/plain",
57                  new MediaType("Text", "Plain").toString());
58  
59          Map<String, String> parameters = new HashMap<String, String>();
60          assertEquals(
61                  "text/plain",
62                  new MediaType("text", "PLAIN", parameters).toString());
63  
64          parameters.put("CHARSET", "UTF-8");
65          assertEquals(
66                  "text/plain; charset=UTF-8",
67                  new MediaType("TEXT", "plain", parameters).toString());
68  
69          parameters.put("X-Eol-Style", "crlf");
70          assertEquals(
71                  "text/plain; charset=UTF-8; x-eol-style=crlf",
72                  new MediaType("TeXt", "PlAiN", parameters).toString());
73      }
74  
75      public void testTrim() {
76          assertEquals(
77                  "text/plain",
78                  new MediaType(" text ", " plain ").toString());
79          assertEquals(
80                  "text/plain",
81                  new MediaType("\ttext", "plain\t").toString());
82  
83          Map<String, String> parameters = new HashMap<String, String>();
84          assertEquals(
85                  "text/plain",
86                  new MediaType("text\r\n", " \tplain", parameters).toString());
87  
88          parameters.put(" charset", "UTF-8");
89          assertEquals(
90                  "text/plain; charset=UTF-8",
91                  new MediaType("\n\ntext", "plain \r", parameters).toString());
92  
93          parameters.put("\r\n\tx-eol-style  \t", "crlf");
94          assertEquals(
95                  "text/plain; charset=UTF-8; x-eol-style=crlf",
96              new MediaType("    text", "\tplain ", parameters).toString());
97      }
98  
99      public void testQuote() {
100         Map<String, String> parameters = new HashMap<String, String>();
101         parameters.put("a", " value with spaces ");
102         parameters.put("b", "text/plain");
103         parameters.put("c", "()<>@,;:\\\"/[]?=");
104         assertEquals(
105                 "text/plain; a=\" value with spaces \"; b=\"text\\/plain\""
106                 + "; c=\"\\(\\)\\<\\>\\@\\,\\;\\:\\\\\\\"\\/\\[\\]\\?\\=\"",
107                 new MediaType("text", "plain", parameters).toString());
108     }
109     
110     /**
111      * @since TIKA-121
112      */
113     public void testParseWithParams() {
114         String mimeStringWithParams = "text/html;charset=UTF-8;foo=bar;foo2=bar2";
115 
116         MediaType type = MediaType.parse(mimeStringWithParams);
117         assertNotNull(type);
118         assertNotNull(type.getParameters());
119         assertNotNull(type.getParameters().keySet());
120         assertEquals(3, type.getParameters().keySet().size());
121         boolean gotCharset = false, gotFoo = false, gotFoo2 = false;
122         for (String param : type.getParameters().keySet()) {
123             if (param.equals("charset")) {
124                 gotCharset = true;
125             } else if (param.equals("foo")) {
126                 gotFoo = true;
127             } else if (param.equals("foo2")) {
128                 gotFoo2 = true;
129             }
130         }
131         assertTrue(gotCharset && gotFoo && gotFoo2);
132     }
133 
134     /**
135      * @since TIKA-121
136      */
137     public void testParseNoParams() {
138         String mimeStringNoParams = "text/html";
139 
140         MediaType type = MediaType.parse(mimeStringNoParams);
141         assertNotNull(type);
142         assertNotNull(type.getParameters());
143         assertNotNull(type.getParameters().keySet());
144         assertEquals(0, type.getParameters().keySet().size());
145     }
146 
147     /**
148      * @since TIKA-121
149      */
150     public void testParseNoParamsWithSemi() {
151         String mimeStringNoParamsWithSemi = "text/html;";
152         MediaType type = MediaType.parse(mimeStringNoParamsWithSemi);
153         assertNotNull(type);
154         assertNotNull(type.getParameters());
155         assertNotNull(type.getParameters().keySet());
156         assertEquals(0, type.getParameters().keySet().size());
157     }
158 
159     
160 }