source: contrib/MailArchiver/sources/vendor/mime4j/custom/core/src/test/java/org/apache/james/mime4j/stream/BaseTestForBodyDescriptors.java @ 6785

Revision 6785, 9.0 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j.stream;
21
22import junit.framework.TestCase;
23
24public abstract class BaseTestForBodyDescriptors extends TestCase {
25
26    protected abstract MutableBodyDescriptor newBodyDescriptor();
27
28    protected abstract MutableBodyDescriptor newBodyDescriptor(BodyDescriptor parent);
29   
30    public void testGetParameters() throws Exception {
31        MutableBodyDescriptor bd = null;
32       
33        bd = newBodyDescriptor();
34        bd.addField(new RawField("Content-Type ", "text/plain; charset=ISO-8859-1; "
35                + "boundary=foo; param1=value1; param2=value2; param3=value3"));
36        assertEquals(3, bd.getContentTypeParameters().size());
37        assertEquals("value1", bd.getContentTypeParameters().get("param1"));
38        assertEquals("value2", bd.getContentTypeParameters().get("param2"));
39        assertEquals("value3", bd.getContentTypeParameters().get("param3"));
40       
41        bd = newBodyDescriptor();
42        bd.addField(new RawField("Content-Type ", "text/plain; param1=value1; param2=value2;"
43                     + " param3=value3"));
44        assertEquals(3, bd.getContentTypeParameters().size());
45        assertEquals("value1", bd.getContentTypeParameters().get("param1"));
46        assertEquals("value2", bd.getContentTypeParameters().get("param2"));
47        assertEquals("value3", bd.getContentTypeParameters().get("param3"));
48       
49        bd = newBodyDescriptor();
50        bd.addField(new RawField("Content-Type ", "text/plain; "
51                + "param1= \" value with\tspaces \" ; "
52                + "param2=\"\\\"value4 with escaped \\\" \\\"\";"));
53        assertEquals(2, bd.getContentTypeParameters().size());
54        assertEquals(" value with\tspaces ", bd.getContentTypeParameters().get("param1"));
55        assertEquals("\"value4 with escaped \" \"", bd.getContentTypeParameters().get("param2"));
56       
57        /*
58         * Make sure escaped characters (except ") are still escaped.
59         * The parameter value should be \n\"
60         */
61        bd = newBodyDescriptor();
62        bd.addField(new RawField("Content-Type ", "text/plain; param=\"\\n\\\\\\\"\""));
63        assertEquals(1, bd.getContentTypeParameters().size());
64        assertEquals("\\n\\\"", bd.getContentTypeParameters().get("param"));
65    }
66   
67    public void testAddField() throws Exception {
68        MutableBodyDescriptor bd = null;
69       
70        /*
71         * Make sure that only the first Content-Type header added is used.
72         */
73        bd = newBodyDescriptor();
74        bd.addField(new RawField("Content-Type ", "text/plain; charset=ISO-8859-1"));
75        assertEquals("text/plain", bd.getMimeType());
76        assertEquals("iso-8859-1", bd.getCharset());
77        bd.addField(new RawField("Content-Type ", "text/html; charset=us-ascii"));
78        assertEquals("text/plain", bd.getMimeType());
79        assertEquals("iso-8859-1", bd.getCharset());
80    }
81   
82    public void testGetMimeType() throws Exception {
83        MutableBodyDescriptor bd = null;
84       
85        bd = newBodyDescriptor();
86        bd.addField(new RawField("Content-Type ", "text/PLAIN"));
87        assertEquals("text/plain", bd.getMimeType());
88       
89        bd = newBodyDescriptor();
90        bd.addField(new RawField("Content-Type ", "text/PLAIN;"));
91        assertEquals("text/plain", bd.getMimeType());
92       
93        bd = newBodyDescriptor();
94        bd.addField(new RawField("content-type", "   TeXt / html   "));
95        assertEquals("text/html", bd.getMimeType());
96       
97        bd = newBodyDescriptor();
98        bd.addField(new RawField("CONTENT-TYPE", "   x-app/yada ;  param = yada"));
99        assertEquals("x-app/yada", bd.getMimeType());
100       
101        bd = newBodyDescriptor();
102        bd.addField(new RawField("CONTENT-TYPE", "   yada"));
103        assertEquals("text/plain", bd.getMimeType());
104       
105        /*
106         * Make sure that only the first Content-Type header added is used.
107         */
108        bd = newBodyDescriptor();
109        bd.addField(new RawField("Content-Type ", "text/plain"));
110        assertEquals("text/plain", bd.getMimeType());
111        bd.addField(new RawField("Content-Type ", "text/html"));
112        assertEquals("text/plain", bd.getMimeType());
113       
114        /*
115         * Implicit mime types.
116         */
117        MutableBodyDescriptor child = null;
118        MutableBodyDescriptor parent = null;
119       
120        parent = newBodyDescriptor();
121        parent.addField(new RawField("Content-Type", "mutlipart/alternative; boundary=foo"));
122       
123        child = newBodyDescriptor(parent);
124        assertEquals("text/plain", child.getMimeType());
125        child.addField(new RawField("Content-Type", " child/type"));
126        assertEquals("child/type", child.getMimeType());
127       
128        parent = newBodyDescriptor();
129        parent.addField(new RawField("Content-Type", "multipart/digest; boundary=foo"));
130       
131        child = newBodyDescriptor(parent);
132        assertEquals("message/rfc822", child.getMimeType());
133        child.addField(new RawField("Content-Type", " child/type"));
134        assertEquals("child/type", child.getMimeType());
135       
136    }
137   
138    public void testParameters() throws Exception {
139        MutableBodyDescriptor bd = null;
140
141        /*
142         * Test charset.
143         */
144        bd = newBodyDescriptor();
145        assertEquals("us-ascii", bd.getCharset());
146        bd.addField(new RawField("Content-Type ", "text/type; charset=ISO-8859-1"));
147        assertEquals("iso-8859-1", bd.getCharset());
148       
149        bd = newBodyDescriptor();
150        assertEquals("us-ascii", bd.getCharset());
151        bd.addField(new RawField("Content-Type ", "text/type"));
152        assertEquals("us-ascii", bd.getCharset());
153       
154        /*
155         * Test boundary.
156         */
157        bd = newBodyDescriptor();
158        bd.addField(new RawField("Content-Type", "text/html; boundary=yada yada"));
159        assertNull(bd.getBoundary());
160
161        bd = newBodyDescriptor();
162        bd.addField(new RawField("Content-Type", "multipart/yada; boundary=yada"));
163        assertEquals("yada", bd.getBoundary());
164
165        /*
166         * Test some weird parameters.
167         */
168        bd = newBodyDescriptor();
169        bd.addField(new RawField("Content-Type", "multipart/yada; boundary=yada yada"));
170        assertEquals("yada yada", bd.getBoundary());
171       
172        bd = newBodyDescriptor();
173        bd.addField(new RawField("Content-Type", "multipart/yada; boUNdarY= ya:*da; \tcharset\t =  big5"));
174        assertEquals("ya:*da", bd.getBoundary());
175        assertEquals("big5", bd.getCharset());
176       
177        bd = newBodyDescriptor();
178        bd.addField(new RawField("Content-Type", "multipart/yada; boUNdarY= \"ya \\\"\\\"\tda \\\"\"; "
179                            + "\tcharset\t =  \"\\\"hepp\\\"  =us\t-ascii\""));
180        assertEquals("ya \"\"\tda \"", bd.getBoundary());
181        assertEquals("\"hepp\"  =us\t-ascii", bd.getCharset());
182       
183    }
184   
185    public void testGetContentLength() throws Exception {
186        MutableBodyDescriptor bd = null;
187
188        bd = newBodyDescriptor();
189        assertEquals(-1, bd.getContentLength());
190
191        bd.addField(new RawField("Content-Length", "9901"));
192        assertEquals(9901, bd.getContentLength());
193
194        // only the first content-length counts
195        bd.addField(new RawField("Content-Length", "1239901"));
196        assertEquals(9901, bd.getContentLength());
197    }
198   
199    public void testDoDefaultToUsAsciiWhenUntyped() throws Exception {
200        MutableBodyDescriptor descriptor = newBodyDescriptor();
201        descriptor.addField(new RawField("To", "me@example.org"));
202        assertEquals("us-ascii", descriptor.getCharset());
203    }
204
205    public void testDoNotDefaultToUsAsciiForNonTextTypes() throws Exception {
206        MutableBodyDescriptor descriptor = newBodyDescriptor();
207        descriptor.addField(new RawField("Content-Type", "image/png; name=blob.png"));
208        assertNull(descriptor.getCharset());
209    }
210   
211}
Note: See TracBrowser for help on using the repository browser.