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 *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
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.descriptor;
22
import junit.framework.TestCase;
24
import org.apache.james.mime4j.parser.Field;
25
import org.apache.james.mime4j.util.ByteSequence;
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(new TestField("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", bd.getContentTypeParameters().get("param1"));
41
assertEquals("value2", bd.getContentTypeParameters().get("param2"));
42
assertEquals("value3", bd.getContentTypeParameters().get("param3"));
44
bd = newBodyDescriptor();
45
bd.addField(new TestField("Content-Type ", "text/plain; param1=value1; param2=value2;"
47
assertEquals(3, bd.getContentTypeParameters().size());
48
assertEquals("value1", bd.getContentTypeParameters().get("param1"));
49
assertEquals("value2", bd.getContentTypeParameters().get("param2"));
50
assertEquals("value3", bd.getContentTypeParameters().get("param3"));
52
bd = newBodyDescriptor();
53
bd.addField(new TestField("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 ", bd.getContentTypeParameters().get("param1"));
58
assertEquals("\"value4 with escaped \" \"", 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(new TestField("Content-Type ", "text/plain; param=\"\\n\\\\\\\"\""));
66
assertEquals(1, bd.getContentTypeParameters().size());
67
assertEquals("\\n\\\"", 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(new TestField("Content-Type ", "text/plain; charset=ISO-8859-1"));
78
assertEquals("text/plain", bd.getMimeType());
79
assertEquals("iso-8859-1", bd.getCharset());
80
bd.addField(new TestField("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(new TestField("Content-Type ", "text/PLAIN"));
90
assertEquals("text/plain", bd.getMimeType());
92
bd = newBodyDescriptor();
93
bd.addField(new TestField("Content-Type ", "text/PLAIN;"));
94
assertEquals("text/plain", bd.getMimeType());
96
bd = newBodyDescriptor();
97
bd.addField(new TestField("content-type", " TeXt / html "));
98
assertEquals("text/html", bd.getMimeType());
100
bd = newBodyDescriptor();
101
bd.addField(new TestField("CONTENT-TYPE", " x-app/yada ; param = yada"));
102
assertEquals("x-app/yada", bd.getMimeType());
104
bd = newBodyDescriptor();
105
bd.addField(new TestField("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(new TestField("Content-Type ", "text/plain"));
113
assertEquals("text/plain", bd.getMimeType());
114
bd.addField(new TestField("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(new TestField("Content-Type", "mutlipart/alternative; boundary=foo"));
126
child = newBodyDescriptor(parent);
127
assertEquals("text/plain", child.getMimeType());
128
child.addField(new TestField("Content-Type", " child/type"));
129
assertEquals("child/type", child.getMimeType());
131
parent = newBodyDescriptor();
132
parent.addField(new TestField("Content-Type", "multipart/digest; boundary=foo"));
134
child = newBodyDescriptor(parent);
135
assertEquals("message/rfc822", child.getMimeType());
136
child.addField(new TestField("Content-Type", " child/type"));
137
assertEquals("child/type", child.getMimeType());
141
public void testParameters() {
142
MutableBodyDescriptor bd = null;
147
bd = newBodyDescriptor();
148
assertEquals("us-ascii", bd.getCharset());
149
bd.addField(new TestField("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(new TestField("Content-Type ", "text/type"));
155
assertEquals("us-ascii", bd.getCharset());
160
bd = newBodyDescriptor();
161
bd.addField(new TestField("Content-Type", "text/html; boundary=yada yada"));
162
assertNull(bd.getBoundary());
164
bd = newBodyDescriptor();
165
bd.addField(new TestField("Content-Type", "multipart/yada; boundary=yada"));
166
assertEquals("yada", bd.getBoundary());
169
* Test some weird parameters.
171
bd = newBodyDescriptor();
172
bd.addField(new TestField("Content-Type", "multipart/yada; boundary=yada yada"));
173
assertEquals("yada", bd.getBoundary());
175
bd = newBodyDescriptor();
176
bd.addField(new TestField("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(new TestField("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 testGetContentLength() throws Exception {
189
MutableBodyDescriptor bd = null;
191
bd = newBodyDescriptor();
192
assertEquals(-1, bd.getContentLength());
194
bd.addField(new TestField("Content-Length", "9901"));
195
assertEquals(9901, bd.getContentLength());
197
// only the first content-length counts
198
bd.addField(new TestField("Content-Length", "1239901"));
199
assertEquals(9901, bd.getContentLength());
202
public void testDoDefaultToUsAsciiWhenUntyped() throws Exception {
203
MutableBodyDescriptor descriptor = newBodyDescriptor();
204
descriptor.addField(new TestField("To", "me@example.org"));
205
assertEquals("us-ascii", descriptor.getCharset());
208
public void testDoNotDefaultToUsAsciiForNonTextTypes() throws Exception {
209
MutableBodyDescriptor descriptor = newBodyDescriptor();
210
descriptor.addField(new TestField("Content-Type", "image/png; name=blob.png"));
211
assertNull(descriptor.getCharset());
214
private static final class TestField implements Field {
216
private final String name;
217
private final String body;
219
public TestField(String name, String body){
224
public String getName() {
228
public String getBody() {
232
public ByteSequence getRaw() {
233
throw new UnsupportedOperationException();