source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/dom/src/test/java/org/apache/james/mime4j/field/FieldsTest.java @ 6785

Revision 6785, 17.2 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.field;
21
22import java.io.ByteArrayOutputStream;
23import java.io.IOException;
24import java.util.Arrays;
25import java.util.Date;
26import java.util.HashMap;
27import java.util.Map;
28import java.util.TimeZone;
29
30import junit.framework.TestCase;
31
32import org.apache.james.mime4j.dom.address.Group;
33import org.apache.james.mime4j.dom.address.Mailbox;
34import org.apache.james.mime4j.dom.field.AddressListField;
35import org.apache.james.mime4j.dom.field.ContentDispositionField;
36import org.apache.james.mime4j.dom.field.ContentTransferEncodingField;
37import org.apache.james.mime4j.dom.field.ContentTypeField;
38import org.apache.james.mime4j.dom.field.DateTimeField;
39import org.apache.james.mime4j.dom.field.Field;
40import org.apache.james.mime4j.dom.field.MailboxField;
41import org.apache.james.mime4j.dom.field.MailboxListField;
42import org.apache.james.mime4j.field.Fields;
43import org.apache.james.mime4j.field.address.AddressBuilder;
44import org.apache.james.mime4j.util.ByteArrayBuffer;
45import org.apache.james.mime4j.util.ContentUtil;
46import org.apache.james.mime4j.util.MimeUtil;
47
48public class FieldsTest extends TestCase {
49
50    public void testContentTypeString() throws Exception {
51        ContentTypeField field = Fields.contentType("multipart/mixed; "
52                + "boundary=\"-=Part.0.37877968dd4f6595.11eccf0271c"
53                + ".2dce5678cbc933d5=-\"");
54        assertTrue(field.isValidField());
55
56        String expectedRaw = "Content-Type: multipart/mixed;\r\n "
57                + "boundary=\"-=Part.0.37877968dd4f6595.11eccf0271c"
58                + ".2dce5678cbc933d5=-\"";
59        assertEquals(expectedRaw, decode(field));
60    }
61
62    public void testContentTypeStringParameters() throws Exception {
63        Map<String, String> parameters = new HashMap<String, String>();
64        parameters.put("boundary",
65                "-=Part.0.37877968dd4f6595.11eccf0271c.2dce5678cbc933d5=-");
66        ContentTypeField field = Fields.contentType("multipart/mixed",
67                parameters);
68        assertTrue(field.isValidField());
69
70        String expectedRaw = "Content-Type: multipart/mixed;\r\n "
71                + "boundary=\"-=Part.0.37877968dd4f6595.11eccf0271c"
72                + ".2dce5678cbc933d5=-\"";
73        assertEquals(expectedRaw, decode(field));
74    }
75
76    public void testContentTypeStringParametersWithSpaces() throws Exception {
77        Map<String, String> parameters = new HashMap<String, String>();
78        parameters.put("param", "value with space chars");
79        ContentTypeField field = Fields.contentType("multipart/mixed",
80                parameters);
81        assertTrue(field.isValidField());
82
83        String expectedRaw = "Content-Type: multipart/mixed; "
84                + "param=\"value with space chars\"";
85        assertEquals(expectedRaw, decode(field));
86    }
87
88    public void testContentTypeStringNullParameters() throws Exception {
89        ContentTypeField field = Fields.contentType("text/plain", null);
90        assertTrue(field.isValidField());
91
92        String expectedRaw = "Content-Type: text/plain";
93        assertEquals(expectedRaw, decode(field));
94    }
95
96    public void testInvalidContentType() throws Exception {
97        ContentTypeField field = Fields.contentType("multipart/mixed; "
98                + "boundary=-=Part.0.37877968dd4f6595.11eccf0271c"
99                + ".2dce5678cbc933d5=-");
100        assertFalse(field.isValidField());
101
102        assertEquals("multipart/mixed", field.getMimeType());
103    }
104
105    public void testContentTransferEncoding() throws Exception {
106        ContentTransferEncodingField field = Fields
107                .contentTransferEncoding("base64");
108        assertTrue(field.isValidField());
109
110        assertEquals("Content-Transfer-Encoding: base64",
111                decode(field));
112    }
113
114    public void testContentDispositionString() throws Exception {
115        ContentDispositionField field = Fields.contentDisposition("inline; "
116                + "filename=\"testing 1 2.dat\"; size=12345; "
117                + "creation-date=\"Thu, 1 Jan 1970 00:00:00 +0000\"");
118        assertTrue(field.isValidField());
119
120        String expectedRaw = "Content-Disposition: inline; filename="
121                + "\"testing 1 2.dat\"; size=12345;\r\n creation-date="
122                + "\"Thu, 1 Jan 1970 00:00:00 +0000\"";
123        assertEquals(expectedRaw, decode(field));
124    }
125
126    public void testContentDispositionStringParameters() throws Exception {
127        Map<String, String> parameters = new HashMap<String, String>();
128        parameters.put("creation-date", MimeUtil.formatDate(new Date(0),
129                TimeZone.getTimeZone("GMT")));
130        ContentDispositionField field = Fields.contentDisposition("attachment",
131                parameters);
132        assertTrue(field.isValidField());
133
134        String expectedRaw = "Content-Disposition: attachment; "
135                + "creation-date=\"Thu, 1 Jan 1970 00:00:00\r\n +0000\"";
136        assertEquals(expectedRaw, decode(field));
137
138        assertEquals(new Date(0), field.getCreationDate());
139    }
140
141    public void testContentDispositionStringNullParameters() throws Exception {
142        ContentDispositionField field = Fields.contentDisposition("inline",
143                (Map<String, String>) null);
144        assertTrue(field.isValidField());
145
146        String expectedRaw = "Content-Disposition: inline";
147        assertEquals(expectedRaw, decode(field));
148    }
149
150    public void testContentDispositionFilename() throws Exception {
151        ContentDispositionField field = Fields.contentDisposition("attachment",
152                "some file.dat");
153        assertTrue(field.isValidField());
154
155        assertEquals("attachment", field.getDispositionType());
156        assertEquals("some file.dat", field.getFilename());
157    }
158
159    public void testContentDispositionFilenameSize() throws Exception {
160        ContentDispositionField field = Fields.contentDisposition("attachment",
161                "some file.dat", 300);
162        assertTrue(field.isValidField());
163
164        assertEquals("attachment", field.getDispositionType());
165        assertEquals("some file.dat", field.getFilename());
166        assertEquals(300, field.getSize());
167    }
168
169    public void testContentDispositionFilenameSizeDate() throws Exception {
170        ContentDispositionField field = Fields.contentDisposition("attachment",
171                "some file.dat", 300, new Date(1000), new Date(2000), new Date(
172                        3000));
173        assertTrue(field.isValidField());
174
175        assertEquals("attachment", field.getDispositionType());
176        assertEquals("some file.dat", field.getFilename());
177        assertEquals(300, field.getSize());
178        assertEquals(new Date(1000), field.getCreationDate());
179        assertEquals(new Date(2000), field.getModificationDate());
180        assertEquals(new Date(3000), field.getReadDate());
181    }
182
183    public void testInvalidContentDisposition() throws Exception {
184        ContentDispositionField field = Fields.contentDisposition("inline; "
185                + "filename=some file.dat");
186        assertFalse(field.isValidField());
187
188        assertEquals("inline", field.getDispositionType());
189    }
190
191    public void testDateStringDateTimeZone() throws Exception {
192        DateTimeField field = Fields.date("Date", new Date(0), TimeZone
193                .getTimeZone("GMT"));
194        assertTrue(field.isValidField());
195
196        assertEquals("Date: Thu, 1 Jan 1970 00:00:00 +0000", decode(field
197                ));
198        assertEquals(new Date(0), field.getDate());
199
200        field = Fields.date("Resent-Date", new Date(0), TimeZone
201                .getTimeZone("GMT+1"));
202        assertTrue(field.isValidField());
203
204        assertEquals("Resent-Date: Thu, 1 Jan 1970 01:00:00 +0100",
205                decode(field));
206        assertEquals(new Date(0), field.getDate());
207    }
208
209    public void testDateDST() throws Exception {
210        long millis = 1216221153000l;
211        DateTimeField field = Fields.date("Date", new Date(millis), TimeZone
212                .getTimeZone("CET"));
213        assertTrue(field.isValidField());
214
215        assertEquals("Date: Wed, 16 Jul 2008 17:12:33 +0200", decode(field
216                ));
217        assertEquals(new Date(millis), field.getDate());
218    }
219
220    public void testMessageId() throws Exception {
221        Field messageId = Fields.messageId("acme.org");
222
223        String raw = decode(messageId);
224        assertTrue(raw.startsWith("Message-ID: <Mime4j."));
225        assertTrue(raw.endsWith("@acme.org>"));
226    }
227
228    public void testSubject() throws Exception {
229        assertEquals("Subject: ", decode(Fields.subject("")));
230        assertEquals("Subject: test", decode(Fields.subject("test")));
231        assertEquals("Subject: =?ISO-8859-1?Q?Sm=F8rebr=F8d?=", decode(Fields
232                .subject("Sm\370rebr\370d")));
233
234        String seventyEight = "12345678901234567890123456789012345678901234567890123456789012345678";
235        assertEquals("Subject:\r\n " + seventyEight, decode(Fields.subject(
236                seventyEight)));
237
238        String seventyNine = seventyEight + "9";
239        String expected = "Subject: =?US-ASCII?Q?1234567890123456789012345678901234?="
240                + "\r\n =?US-ASCII?Q?56789012345678901234567890123456789?=";
241        assertEquals(expected, decode(Fields.subject(seventyNine)));
242    }
243
244    public void testSender() throws Exception {
245        MailboxField field = Fields.sender(AddressBuilder.DEFAULT
246                .parseMailbox("JD <john.doe@acme.org>"));
247        assertEquals("Sender: JD <john.doe@acme.org>", decode(field));
248    }
249
250    public void testFrom() throws Exception {
251        Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("JD <john.doe@acme.org>");
252        Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>");
253
254        MailboxListField field = Fields.from(mailbox1);
255        assertEquals("From: JD <john.doe@acme.org>", decode(field));
256
257        field = Fields.from(mailbox1, mailbox2);
258        assertEquals("From: JD <john.doe@acme.org>, "
259                + "Mary Smith <mary@example.net>", decode(field));
260
261        field = Fields.from(Arrays.asList(mailbox1, mailbox2));
262        assertEquals("From: JD <john.doe@acme.org>, "
263                + "Mary Smith <mary@example.net>", decode(field));
264    }
265
266    public void testTo() throws Exception {
267        Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("JD <john.doe@acme.org>");
268        Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("jane.doe@example.org");
269        Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>");
270        Group group = new Group("The Does", mailbox1, mailbox2);
271
272        AddressListField field = Fields.to(group);
273        assertEquals("To: The Does: JD <john.doe@acme.org>, "
274                + "jane.doe@example.org;", decode(field));
275
276        field = Fields.to(group, mailbox3);
277        assertEquals("To: The Does: JD <john.doe@acme.org>, "
278                + "jane.doe@example.org;, Mary Smith\r\n <mary@example.net>",
279                decode(field));
280
281        field = Fields.to(Arrays.asList(group, mailbox3));
282        assertEquals("To: The Does: JD <john.doe@acme.org>, "
283                + "jane.doe@example.org;, Mary Smith\r\n <mary@example.net>",
284                decode(field));
285    }
286
287    public void testCc() throws Exception {
288        Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("JD <john.doe@acme.org>");
289        Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("jane.doe@example.org");
290        Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>");
291        Group group = new Group("The Does", mailbox1, mailbox2);
292
293        AddressListField field = Fields.cc(group);
294        assertEquals("Cc: The Does: JD <john.doe@acme.org>, "
295                + "jane.doe@example.org;", decode(field));
296
297        field = Fields.cc(group, mailbox3);
298        assertEquals("Cc: The Does: JD <john.doe@acme.org>, "
299                + "jane.doe@example.org;, Mary Smith\r\n <mary@example.net>",
300                decode(field));
301
302        field = Fields.cc(Arrays.asList(group, mailbox3));
303        assertEquals("Cc: The Does: JD <john.doe@acme.org>, "
304                + "jane.doe@example.org;, Mary Smith\r\n <mary@example.net>",
305                decode(field));
306    }
307
308    public void testBcc() throws Exception {
309        Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("JD <john.doe@acme.org>");
310        Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("jane.doe@example.org");
311        Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>");
312        Group group = new Group("The Does", mailbox1, mailbox2);
313
314        AddressListField field = Fields.bcc(group);
315        assertEquals("Bcc: The Does: JD <john.doe@acme.org>, "
316                + "jane.doe@example.org;", decode(field));
317
318        field = Fields.bcc(group, mailbox3);
319        assertEquals("Bcc: The Does: JD <john.doe@acme.org>, "
320                + "jane.doe@example.org;, Mary Smith\r\n <mary@example.net>",
321                decode(field));
322
323        field = Fields.bcc(Arrays.asList(group, mailbox3));
324        assertEquals("Bcc: The Does: JD <john.doe@acme.org>, "
325                + "jane.doe@example.org;, Mary Smith\r\n <mary@example.net>",
326                decode(field));
327    }
328
329    public void testReplyTo() throws Exception {
330        Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("JD <john.doe@acme.org>");
331        Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("jane.doe@example.org");
332        Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>");
333        Group group = new Group("The Does", mailbox1, mailbox2);
334
335        AddressListField field = Fields.replyTo(group);
336        assertEquals("Reply-To: The Does: JD <john.doe@acme.org>, "
337                + "jane.doe@example.org;", decode(field));
338
339        field = Fields.replyTo(group, mailbox3);
340        assertEquals("Reply-To: The Does: JD <john.doe@acme.org>, "
341                + "jane.doe@example.org;, Mary\r\n Smith <mary@example.net>",
342                decode(field));
343
344        field = Fields.replyTo(Arrays.asList(group, mailbox3));
345        assertEquals("Reply-To: The Does: JD <john.doe@acme.org>, "
346                + "jane.doe@example.org;, Mary\r\n Smith <mary@example.net>",
347                decode(field));
348    }
349
350    public void testMailbox() throws Exception {
351        MailboxField field = Fields.mailbox("Resent-Sender", AddressBuilder.DEFAULT
352                .parseMailbox("JD <john.doe@acme.org>"));
353        assertEquals("Resent-Sender: JD <john.doe@acme.org>", decode(field));
354    }
355
356    public void testMailboxList() throws Exception {
357        Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("JD <john.doe@acme.org>");
358        Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>");
359
360        MailboxListField field = Fields.mailboxList("Resent-From", Arrays
361                .asList(mailbox1, mailbox2));
362        assertEquals("Resent-From: JD <john.doe@acme.org>, "
363                + "Mary Smith <mary@example.net>", decode(field));
364    }
365
366    public void testAddressList() throws Exception {
367        Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("JD <john.doe@acme.org>");
368        Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("jane.doe@example.org");
369        Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>");
370        Group group = new Group("The Does", mailbox1, mailbox2);
371
372        AddressListField field = Fields.addressList("Resent-To", Arrays.asList(
373                group, mailbox3));
374        assertEquals("Resent-To: The Does: JD <john.doe@acme.org>, "
375                + "jane.doe@example.org;, Mary\r\n Smith <mary@example.net>",
376                decode(field));
377    }
378
379    public void testInvalidFieldName() throws Exception {
380        try {
381            Fields.date("invalid field name", new Date());
382            fail();
383        } catch (IllegalArgumentException expected) {
384        }
385    }
386
387    public static String decode(Field f) {
388        ByteArrayOutputStream bos = new ByteArrayOutputStream();
389        try {
390            f.writeTo(bos);
391        } catch (IOException e) {
392            throw new RuntimeException("bytearrayoutputstream doens't throw this exception");
393        }
394        ByteArrayBuffer bab = new ByteArrayBuffer(bos.toByteArray(), true);
395        return ContentUtil.decode(bab);
396    }
397
398}
Note: See TracBrowser for help on using the repository browser.