~ubuntu-branches/ubuntu/saucy/apache-mime4j/saucy

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/james/mime4j/descriptor/BaseTestForBodyDescriptors.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-07-13 09:28:28 UTC
  • Revision ID: james.westby@ubuntu.com-20100713092828-g6wafdtidgmtx7su
Tags: upstream-0.6
ImportĀ upstreamĀ versionĀ 0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.descriptor;
 
21
 
 
22
import junit.framework.TestCase;
 
23
 
 
24
import org.apache.james.mime4j.parser.Field;
 
25
import org.apache.james.mime4j.util.ByteSequence;
 
26
 
 
27
public abstract class BaseTestForBodyDescriptors extends TestCase {
 
28
 
 
29
    protected abstract MutableBodyDescriptor newBodyDescriptor();
 
30
 
 
31
    protected abstract MutableBodyDescriptor newBodyDescriptor(BodyDescriptor parent);
 
32
    
 
33
    public void testGetParameters() {
 
34
        MutableBodyDescriptor bd = null;
 
35
        
 
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"));
 
43
        
 
44
        bd = newBodyDescriptor();
 
45
        bd.addField(new TestField("Content-Type ", "text/plain; param1=value1; param2=value2;"
 
46
                     + " param3=value3"));
 
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"));
 
51
        
 
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"));
 
59
        
 
60
        /*
 
61
         * Make sure escaped characters (except ") are still escaped.
 
62
         * The parameter value should be \n\"
 
63
         */
 
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"));
 
68
    }
 
69
    
 
70
    public void testAddField() {
 
71
        MutableBodyDescriptor bd = null;
 
72
        
 
73
        /*
 
74
         * Make sure that only the first Content-Type header added is used.
 
75
         */
 
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());
 
83
    }
 
84
    
 
85
    public void testGetMimeType() {
 
86
        MutableBodyDescriptor bd = null;
 
87
        
 
88
        bd = newBodyDescriptor();
 
89
        bd.addField(new TestField("Content-Type ", "text/PLAIN"));
 
90
        assertEquals("text/plain", bd.getMimeType());
 
91
        
 
92
        bd = newBodyDescriptor();
 
93
        bd.addField(new TestField("Content-Type ", "text/PLAIN;"));
 
94
        assertEquals("text/plain", bd.getMimeType());
 
95
        
 
96
        bd = newBodyDescriptor();
 
97
        bd.addField(new TestField("content-type", "   TeXt / html   "));
 
98
        assertEquals("text/html", bd.getMimeType());
 
99
        
 
100
        bd = newBodyDescriptor();
 
101
        bd.addField(new TestField("CONTENT-TYPE", "   x-app/yada ;  param = yada"));
 
102
        assertEquals("x-app/yada", bd.getMimeType());
 
103
        
 
104
        bd = newBodyDescriptor();
 
105
        bd.addField(new TestField("CONTENT-TYPE", "   yada"));
 
106
        assertEquals("text/plain", bd.getMimeType());
 
107
        
 
108
        /*
 
109
         * Make sure that only the first Content-Type header added is used.
 
110
         */
 
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());
 
116
        
 
117
        /*
 
118
         * Implicit mime types.
 
119
         */
 
120
        MutableBodyDescriptor child = null;
 
121
        MutableBodyDescriptor parent = null;
 
122
        
 
123
        parent = newBodyDescriptor();
 
124
        parent.addField(new TestField("Content-Type", "mutlipart/alternative; boundary=foo"));
 
125
        
 
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());
 
130
        
 
131
        parent = newBodyDescriptor();
 
132
        parent.addField(new TestField("Content-Type", "multipart/digest; boundary=foo"));
 
133
        
 
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());
 
138
        
 
139
    }
 
140
    
 
141
    public void testParameters() {
 
142
        MutableBodyDescriptor bd = null;
 
143
 
 
144
        /*
 
145
         * Test charset.
 
146
         */
 
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());
 
151
        
 
152
        bd = newBodyDescriptor();
 
153
        assertEquals("us-ascii", bd.getCharset());
 
154
        bd.addField(new TestField("Content-Type ", "text/type"));
 
155
        assertEquals("us-ascii", bd.getCharset());
 
156
        
 
157
        /*
 
158
         * Test boundary.
 
159
         */
 
160
        bd = newBodyDescriptor();
 
161
        bd.addField(new TestField("Content-Type", "text/html; boundary=yada yada"));
 
162
        assertNull(bd.getBoundary());
 
163
 
 
164
        bd = newBodyDescriptor();
 
165
        bd.addField(new TestField("Content-Type", "multipart/yada; boundary=yada"));
 
166
        assertEquals("yada", bd.getBoundary());
 
167
 
 
168
        /*
 
169
         * Test some weird parameters.
 
170
         */
 
171
        bd = newBodyDescriptor();
 
172
        bd.addField(new TestField("Content-Type", "multipart/yada; boundary=yada yada"));
 
173
        assertEquals("yada", bd.getBoundary());
 
174
        
 
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());
 
179
        
 
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());
 
185
        
 
186
    }
 
187
    
 
188
    public void testGetContentLength() throws Exception {
 
189
        MutableBodyDescriptor bd = null;
 
190
 
 
191
        bd = newBodyDescriptor();
 
192
        assertEquals(-1, bd.getContentLength());
 
193
 
 
194
        bd.addField(new TestField("Content-Length", "9901"));
 
195
        assertEquals(9901, bd.getContentLength());
 
196
 
 
197
        // only the first content-length counts
 
198
        bd.addField(new TestField("Content-Length", "1239901"));
 
199
        assertEquals(9901, bd.getContentLength());
 
200
    }
 
201
    
 
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());
 
206
    }
 
207
 
 
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());
 
212
    }
 
213
    
 
214
    private static final class TestField implements Field {
 
215
 
 
216
        private final String name;
 
217
        private final String body;
 
218
 
 
219
        public TestField(String name, String body){
 
220
            this.name = name;
 
221
            this.body = body;
 
222
        }
 
223
        
 
224
        public String getName() {
 
225
            return name;
 
226
        }
 
227
 
 
228
        public String getBody() {
 
229
            return body;
 
230
        }
 
231
 
 
232
        public ByteSequence getRaw() {
 
233
            throw new UnsupportedOperationException();
 
234
        }
 
235
        
 
236
    }
 
237
}