source: contrib/MailArchiver/sources/vendor/mime4j/custom/dom/src/test/java/org/apache/james/mime4j/dom/HeaderTest.java @ 6785

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

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

RevLine 
[6785]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.dom;
21
22import junit.framework.TestCase;
23
24import org.apache.commons.io.output.ByteArrayOutputStream;
25import org.apache.james.mime4j.dom.Header;
26import org.apache.james.mime4j.dom.field.Field;
27import org.apache.james.mime4j.field.DefaultFieldParser;
28import org.apache.james.mime4j.message.HeaderImpl;
29import org.apache.james.mime4j.message.MimeWriter;
30import org.apache.james.mime4j.util.ByteArrayBuffer;
31import org.apache.james.mime4j.util.ContentUtil;
32
33public class HeaderTest extends TestCase {
34
35    public static final String SUBJECT = "Subject: test";
36
37    public static final String TO = "To: anyuser <any@user>";
38
39    public void testHeader() throws Exception {
40        Header header = new HeaderImpl();
41        header.addField(DefaultFieldParser.parse(SUBJECT));
42        header.addField(DefaultFieldParser.parse(TO));
43
44        assertNotNull("Subject found", header.getField("Subject"));
45        assertNotNull("To found", header.getField("To"));
46
47        assertEquals("Headers equals", SUBJECT + "\r\n" + TO + "\r\n", header
48                .toString());
49    }
50   
51    private static final String SWISS_GERMAN_HELLO = "Gr\374ezi_z\344m\344";
52
53    public void testWriteSpecialCharacters() throws Exception {
54        String hello = SWISS_GERMAN_HELLO;
55        Header header = new HeaderImpl();
56        header.addField(DefaultFieldParser.parse("Hello: " + hello));
57       
58        Field field = header.getField("Hello");
59        assertNotNull(field);
60        // field.getBody is already a 7 bit ASCII string, after MIME4J-151
61        // assertEquals(hello, field.getBody());
62        assertEquals(SWISS_GERMAN_HELLO, field.getBody());
63       
64        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
65       
66        MimeWriter.DEFAULT.writeHeader(header, outstream);
67        byte[] b = outstream.toByteArray();
68        ByteArrayBuffer buf = new ByteArrayBuffer(b.length);
69        buf.append(b, 0, b.length);
70        String s = ContentUtil.decode(buf);
71       
72        assertEquals("Hello: " + SWISS_GERMAN_HELLO + "\r\n\r\n", s);
73    }
74
75    public void testRemoveFields() throws Exception {
76        Header header = new HeaderImpl();
77        header.addField(DefaultFieldParser.parse("Received: from foo by bar for james"));
78        header.addField(DefaultFieldParser.parse("Content-type: text/plain; charset=US-ASCII"));
79        header.addField(DefaultFieldParser.parse("ReCeIvEd: from bar by foo for james"));
80
81        assertEquals(3, header.getFields().size());
82        assertEquals(2, header.getFields("received").size());
83        assertEquals(1, header.getFields("Content-Type").size());
84
85        assertEquals(2, header.removeFields("rEcEiVeD"));
86
87        assertEquals(1, header.getFields().size());
88        assertEquals(0, header.getFields("received").size());
89        assertEquals(1, header.getFields("Content-Type").size());
90
91        assertEquals("Content-type", header.getFields().get(0).getName());
92    }
93
94    public void testRemoveNonExistantField() throws Exception {
95        Header header = new HeaderImpl();
96        header.addField(DefaultFieldParser.parse("Received: from foo by bar for james"));
97        header.addField(DefaultFieldParser.parse("Content-type: text/plain; charset=US-ASCII"));
98        header.addField(DefaultFieldParser.parse("ReCeIvEd: from bar by foo for james"));
99
100        assertEquals(0, header.removeFields("noSuchField"));
101
102        assertEquals(3, header.getFields().size());
103        assertEquals(2, header.getFields("received").size());
104        assertEquals(1, header.getFields("Content-Type").size());
105    }
106
107    public void testSetField() throws Exception {
108        Header header = new HeaderImpl();
109        header.addField(DefaultFieldParser.parse("From: mime4j@james.apache.org"));
110        header.addField(DefaultFieldParser.parse("Received: from foo by bar for james"));
111        header.addField(DefaultFieldParser.parse("Content-type: text/plain; charset=US-ASCII"));
112        header.addField(DefaultFieldParser.parse("ReCeIvEd: from bar by foo for james"));
113
114        header.setField(DefaultFieldParser.parse("received: from nobody by noone for james"));
115
116        assertEquals(3, header.getFields().size());
117        assertEquals(1, header.getFields("received").size());
118
119        assertEquals("From", header.getFields().get(0).getName());
120        assertEquals("received", header.getFields().get(1).getName());
121        assertEquals("Content-type", header.getFields().get(2).getName());
122    }
123
124    public void testSetNonExistantField() throws Exception {
125        Header header = new HeaderImpl();
126        header.addField(DefaultFieldParser.parse("Received: from foo by bar for james"));
127        header.addField(DefaultFieldParser.parse("Content-type: text/plain; charset=US-ASCII"));
128        header.addField(DefaultFieldParser.parse("ReCeIvEd: from bar by foo for james"));
129
130        header.setField(DefaultFieldParser.parse("Message-ID: <msg9901@apache.org>"));
131
132        assertEquals(4, header.getFields().size());
133        assertEquals(1, header.getFields("message-id").size());
134
135        assertEquals("Message-ID", header.getFields().get(3).getName());
136    }
137   
138}
Note: See TracBrowser for help on using the repository browser.