1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.beep4j.internal.message.contenttype;
17  
18  import java.io.StringReader;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import junit.framework.TestCase;
24  
25  public class ContentTypeParserTest extends TestCase {
26  	
27  	public void testContentType() throws Exception {
28  		test("one/two", "one", "two", new HashMap<String,String>());
29  	}
30  	
31      public void testContentTypeWithParameter() throws ParseException {
32          test("one/two; three          =  four", 
33          		"one", "two", 
34          		Collections.singletonMap("three", "four"));
35      }
36      
37      public void testContentTypeWithQuotedParameter() throws Exception {
38          test("one/(foo)two; three          =  \"four\"", 
39          		"one", "two", 
40          		Collections.singletonMap("three", "four"));
41      }
42      
43      public void testContentTypeWithComments() throws Exception {
44          test("one(foo)/two; three          =  (foo) four", 
45          		"one", "two", 
46          		Collections.singletonMap("three", "four"));
47  	}
48  
49      private void test(String val, 
50      		String expectedType, String expectedSubtype, 
51      		Map<String,String> parameters) throws ParseException {
52      	
53          ContentTypeParser parser = new ContentTypeParser(new StringReader(val));
54          parser.parseAll();
55  
56          String type = parser.getType();
57          String subtype = parser.getSubType();
58  
59          assertEquals(parameters, parser.getParameters());
60          
61          assertEquals(expectedType, type);
62          assertEquals(expectedSubtype, subtype);
63      }
64  
65  }