This change breaks backwards compatibility but I think is justified. I agree that...
[mime4j.git] / src / test / java / org / apache / james / mime4j / BaseTestForBodyDescriptors.java
blob405122df053ac9dae21c2edc2f2a2f6f3710ac0e
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 ****************************************************************/
20 package org.apache.james.mime4j;
22 import junit.framework.TestCase;
24 /**
25 * @version $Id: BodyDescriptorTest.java,v 1.2 2004/10/02 12:41:11 ntherning Exp $
27 public abstract class BaseTestForBodyDescriptors extends TestCase {
29 protected abstract MutableBodyDescriptor newBodyDescriptor();
31 protected abstract MutableBodyDescriptor newBodyDescriptor(BodyDescriptor parent);
33 public void testGetParameters() {
34 MutableBodyDescriptor bd = null;
36 bd = newBodyDescriptor();
37 bd.addField("Content-Type ", "text/plain; charset=ISO-8859-1; "
38 + "boundary=foo; param1=value1; param2=value2; param3=value3");
39 assertEquals(3, bd.getContentTypeParameters().size());
40 assertEquals("value1", (String) bd.getContentTypeParameters().get("param1"));
41 assertEquals("value2", (String) bd.getContentTypeParameters().get("param2"));
42 assertEquals("value3", (String) bd.getContentTypeParameters().get("param3"));
44 bd = newBodyDescriptor();
45 bd.addField("Content-Type ", "text/plain; param1=value1; param2=value2;"
46 + " param3=value3");
47 assertEquals(3, bd.getContentTypeParameters().size());
48 assertEquals("value1", (String) bd.getContentTypeParameters().get("param1"));
49 assertEquals("value2", (String) bd.getContentTypeParameters().get("param2"));
50 assertEquals("value3", (String) bd.getContentTypeParameters().get("param3"));
52 bd = newBodyDescriptor();
53 bd.addField("Content-Type ", "text/plain; "
54 + "param1= \" value with\tspaces \" ; "
55 + "param2=\"\\\"value4 with escaped \\\" \\\"\";");
56 assertEquals(2, bd.getContentTypeParameters().size());
57 assertEquals(" value with\tspaces ", (String) bd.getContentTypeParameters().get("param1"));
58 assertEquals("\"value4 with escaped \" \"", (String) bd.getContentTypeParameters().get("param2"));
61 * Make sure escaped characters (except ") are still escaped.
62 * The parameter value should be \n\"
64 bd = newBodyDescriptor();
65 bd.addField("Content-Type ", "text/plain; param=\"\\n\\\\\\\"\"");
66 assertEquals(1, bd.getContentTypeParameters().size());
67 assertEquals("\\n\\\"", (String) bd.getContentTypeParameters().get("param"));
70 public void testAddField() {
71 MutableBodyDescriptor bd = null;
74 * Make sure that only the first Content-Type header added is used.
76 bd = newBodyDescriptor();
77 bd.addField("Content-Type ", "text/plain; charset=ISO-8859-1");
78 assertEquals("text/plain", bd.getMimeType());
79 assertEquals("iso-8859-1", bd.getCharset());
80 bd.addField("Content-Type ", "text/html; charset=us-ascii");
81 assertEquals("text/plain", bd.getMimeType());
82 assertEquals("iso-8859-1", bd.getCharset());
85 public void testGetMimeType() {
86 MutableBodyDescriptor bd = null;
88 bd = newBodyDescriptor();
89 bd.addField("Content-Type ", "text/PLAIN");
90 assertEquals("text/plain", bd.getMimeType());
92 bd = newBodyDescriptor();
93 bd.addField("Content-Type ", "text/PLAIN;");
94 assertEquals("text/plain", bd.getMimeType());
96 bd = newBodyDescriptor();
97 bd.addField("content-type", " TeXt / html ");
98 assertEquals("text/html", bd.getMimeType());
100 bd = newBodyDescriptor();
101 bd.addField("CONTENT-TYPE", " x-app/yada ; param = yada");
102 assertEquals("x-app/yada", bd.getMimeType());
104 bd = newBodyDescriptor();
105 bd.addField("CONTENT-TYPE", " yada");
106 assertEquals("text/plain", bd.getMimeType());
109 * Make sure that only the first Content-Type header added is used.
111 bd = newBodyDescriptor();
112 bd.addField("Content-Type ", "text/plain");
113 assertEquals("text/plain", bd.getMimeType());
114 bd.addField("Content-Type ", "text/html");
115 assertEquals("text/plain", bd.getMimeType());
118 * Implicit mime types.
120 MutableBodyDescriptor child = null;
121 MutableBodyDescriptor parent = null;
123 parent = newBodyDescriptor();
124 parent.addField("Content-Type", "mutlipart/alternative; boundary=foo");
126 child = newBodyDescriptor(parent);
127 assertEquals("text/plain", child.getMimeType());
128 child.addField("Content-Type", " child/type");
129 assertEquals("child/type", child.getMimeType());
131 parent = newBodyDescriptor();
132 parent.addField("Content-Type", "multipart/digest; boundary=foo");
134 child = newBodyDescriptor(parent);
135 assertEquals("message/rfc822", child.getMimeType());
136 child.addField("Content-Type", " child/type");
137 assertEquals("child/type", child.getMimeType());
141 public void testParameters() {
142 MutableBodyDescriptor bd = null;
145 * Test charset.
147 bd = newBodyDescriptor();
148 assertEquals("us-ascii", bd.getCharset());
149 bd.addField("Content-Type ", "text/type; charset=ISO-8859-1");
150 assertEquals("iso-8859-1", bd.getCharset());
152 bd = newBodyDescriptor();
153 assertEquals("us-ascii", bd.getCharset());
154 bd.addField("Content-Type ", "text/type");
155 assertEquals("us-ascii", bd.getCharset());
158 * Test boundary.
160 bd = newBodyDescriptor();
161 bd.addField("Content-Type", "text/html; boundary=yada yada");
162 assertNull(bd.getBoundary());
164 bd = newBodyDescriptor();
165 bd.addField("Content-Type", "multipart/yada; boundary=yada");
166 assertEquals("yada", bd.getBoundary());
169 * Test some weird parameters.
171 bd = newBodyDescriptor();
172 bd.addField("Content-Type", "multipart/yada; boundary=yada yada");
173 assertEquals("yada", bd.getBoundary());
175 bd = newBodyDescriptor();
176 bd.addField("Content-Type", "multipart/yada; boUNdarY= ya:*da; \tcharset\t = big5");
177 assertEquals("ya:*da", bd.getBoundary());
178 assertEquals("big5", bd.getCharset());
180 bd = newBodyDescriptor();
181 bd.addField("Content-Type", "multipart/yada; boUNdarY= \"ya \\\"\\\"\tda \\\"\"; "
182 + "\tcharset\t = \"\\\"hepp\\\" =us\t-ascii\"");
183 assertEquals("ya \"\"\tda \"", bd.getBoundary());
184 assertEquals("\"hepp\" =us\t-ascii", bd.getCharset());
188 public void testDoDefaultToUsAsciiWhenUntyped() throws Exception {
189 MutableBodyDescriptor descriptor = newBodyDescriptor();
190 descriptor.addField("To", "me@example.org");
191 assertEquals("us-ascii", descriptor.getCharset());
194 public void testDoNotDefaultToUsAsciiForNonTextTypes() throws Exception {
195 MutableBodyDescriptor descriptor = newBodyDescriptor();
196 descriptor.addField("Content-Type", "image/png; name=blob.png");
197 assertNull(descriptor.getCharset());