1 | /**************************************************************** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
3 | * or more contributor license agreements. See the NOTICE file * |
---|
4 | * distributed with this work for additional information * |
---|
5 | * regarding copyright ownership. The ASF licenses this file * |
---|
6 | * to you under the Apache License, Version 2.0 (the * |
---|
7 | * "License"); you may not use this file except in compliance * |
---|
8 | * with the License. You may obtain a copy of the License at * |
---|
9 | * * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
11 | * * |
---|
12 | * Unless required by applicable law or agreed to in writing, * |
---|
13 | * software distributed under the License is distributed on an * |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
15 | * KIND, either express or implied. See the License for the * |
---|
16 | * specific language governing permissions and limitations * |
---|
17 | * under the License. * |
---|
18 | ****************************************************************/ |
---|
19 | |
---|
20 | package org.apache.james.mime4j.field; |
---|
21 | |
---|
22 | import org.apache.james.mime4j.dom.field.ContentTypeField; |
---|
23 | import org.apache.james.mime4j.field.ContentTypeFieldImpl; |
---|
24 | import org.apache.james.mime4j.field.DefaultFieldParser; |
---|
25 | |
---|
26 | import junit.framework.TestCase; |
---|
27 | |
---|
28 | public class ContentTypeFieldTest extends TestCase { |
---|
29 | |
---|
30 | public void testMimeTypeWithSemiColonNoParams() throws Exception { |
---|
31 | ContentTypeField f = null; |
---|
32 | |
---|
33 | f = (ContentTypeField) DefaultFieldParser.parse("Content-Type: text/html;"); |
---|
34 | assertEquals("text/html", f.getMimeType()); |
---|
35 | } |
---|
36 | |
---|
37 | public void testGetMimeType() throws Exception { |
---|
38 | ContentTypeField f = null; |
---|
39 | |
---|
40 | f = (ContentTypeField) DefaultFieldParser.parse("Content-Type: text/PLAIN"); |
---|
41 | assertEquals("text/plain", f.getMimeType()); |
---|
42 | |
---|
43 | f = (ContentTypeField) DefaultFieldParser.parse("content-type: TeXt / html "); |
---|
44 | assertEquals("text/html", f.getMimeType()); |
---|
45 | |
---|
46 | f = (ContentTypeField) DefaultFieldParser.parse("CONTENT-TYPE: x-app/yada ;" |
---|
47 | + " param = yada"); |
---|
48 | assertEquals("x-app/yada", f.getMimeType()); |
---|
49 | |
---|
50 | f = (ContentTypeField) DefaultFieldParser.parse("CONTENT-TYPE: yada"); |
---|
51 | assertEquals("", f.getMimeType()); |
---|
52 | } |
---|
53 | |
---|
54 | public void testGetMimeTypeStatic() throws Exception { |
---|
55 | ContentTypeField child = null; |
---|
56 | ContentTypeField parent = null; |
---|
57 | |
---|
58 | child = (ContentTypeField) DefaultFieldParser.parse("Content-Type: child/type"); |
---|
59 | parent = (ContentTypeField) DefaultFieldParser.parse("Content-Type: parent/type"); |
---|
60 | assertEquals("child/type", ContentTypeFieldImpl.getMimeType(child, parent)); |
---|
61 | |
---|
62 | child = null; |
---|
63 | parent = (ContentTypeField) DefaultFieldParser.parse("Content-Type: parent/type"); |
---|
64 | assertEquals("text/plain", ContentTypeFieldImpl.getMimeType(child, parent)); |
---|
65 | parent = (ContentTypeField) DefaultFieldParser.parse("Content-Type: multipart/digest"); |
---|
66 | assertEquals("message/rfc822", ContentTypeFieldImpl.getMimeType(child, parent)); |
---|
67 | |
---|
68 | child = (ContentTypeField) DefaultFieldParser.parse("Content-Type:"); |
---|
69 | parent = (ContentTypeField) DefaultFieldParser.parse("Content-Type: parent/type"); |
---|
70 | assertEquals("text/plain", ContentTypeFieldImpl.getMimeType(child, parent)); |
---|
71 | parent = (ContentTypeField) DefaultFieldParser.parse("Content-Type: multipart/digest"); |
---|
72 | assertEquals("message/rfc822", ContentTypeFieldImpl.getMimeType(child, parent)); |
---|
73 | } |
---|
74 | |
---|
75 | public void testGetCharsetStatic() throws Exception { |
---|
76 | ContentTypeField f = null; |
---|
77 | |
---|
78 | f = (ContentTypeField) DefaultFieldParser.parse("Content-Type: some/type; charset=iso8859-1"); |
---|
79 | assertEquals("iso8859-1", ContentTypeFieldImpl.getCharset(f)); |
---|
80 | |
---|
81 | f = (ContentTypeField) DefaultFieldParser.parse("Content-Type: some/type;"); |
---|
82 | assertEquals("us-ascii", ContentTypeFieldImpl.getCharset(f)); |
---|
83 | } |
---|
84 | |
---|
85 | public void testGetParameter() throws Exception { |
---|
86 | ContentTypeField f = null; |
---|
87 | |
---|
88 | f = (ContentTypeField) DefaultFieldParser.parse("CONTENT-TYPE: text / html ;" |
---|
89 | + " boundary=yada yada"); |
---|
90 | assertEquals("yada", f.getParameter("boundary")); |
---|
91 | |
---|
92 | f = (ContentTypeField) DefaultFieldParser.parse("Content-Type: x-app/yada;" |
---|
93 | + " boUNdarY= \"ya:\\\"*da\"; " |
---|
94 | + "\tcharset\t = us-ascii"); |
---|
95 | assertEquals("ya:\"*da", f.getParameter("boundary")); |
---|
96 | assertEquals("us-ascii", f.getParameter("charset")); |
---|
97 | |
---|
98 | f = (ContentTypeField) DefaultFieldParser.parse("Content-Type: x-app/yada; " |
---|
99 | + "boUNdarY= \"ya \\\"\\\"\tda \\\"\"; " |
---|
100 | + "\tcharset\t = \"\\\"hepp\\\" =us\t-ascii\""); |
---|
101 | assertEquals("ya \"\"\tda \"", f.getParameter("boundary")); |
---|
102 | assertEquals("\"hepp\" =us\t-ascii", f.getParameter("charset")); |
---|
103 | } |
---|
104 | |
---|
105 | } |
---|