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

Revision 6785, 5.8 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 org.apache.james.mime4j.MimeException;
23import org.apache.james.mime4j.util.ByteArrayBuffer;
24import org.apache.james.mime4j.util.ByteSequence;
25
26import junit.framework.TestCase;
27
28public class DefaultFieldBuilderTest extends TestCase {
29
30    static ByteArrayBuffer line(final String line) throws Exception {
31        ByteArrayBuffer buf = new ByteArrayBuffer(line.length());
32        byte[] b = line.getBytes("US-ASCII");
33        buf.append(b, 0, b.length);
34        return buf;
35    }
36
37    public void testBasics() throws Exception {
38        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
39        builder.reset();
40        builder.append(line("raw:   stuff;\r\n"));
41        builder.append(line("   more stuff;\r\n"));
42        builder.append(line("   a lot more stuff\r\n"));
43        ByteArrayBuffer buf = builder.getRaw();
44        assertNotNull(buf);
45        assertEquals("raw:   stuff;\r\n   more stuff;\r\n   a lot more stuff\r\n",
46                new String(buf.toByteArray(), "US-ASCII"));
47        RawField field = builder.build();
48        assertNotNull(field);
49        assertEquals("raw", field.getName());
50        assertEquals("  stuff;   more stuff;   a lot more stuff", field.getBody());
51        ByteSequence raw = field.getRaw();
52        assertNotNull(raw);
53        assertEquals("raw:   stuff;\r\n   more stuff;\r\n   a lot more stuff",
54                new String(raw.toByteArray(), "US-ASCII"));
55    }
56
57    public void testObsoleteSyntax() throws Exception {
58        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
59        builder.reset();
60        builder.append(line("raw  : stuff;\r\n"));
61        builder.append(line("   more stuff;\r\n"));
62        builder.append(line("   a lot more stuff\r\n"));
63        ByteArrayBuffer buf = builder.getRaw();
64        assertNotNull(buf);
65        assertEquals("raw  : stuff;\r\n   more stuff;\r\n   a lot more stuff\r\n",
66                new String(buf.toByteArray(), "US-ASCII"));
67        RawField field = builder.build();
68        assertNotNull(field);
69        assertEquals("raw", field.getName());
70        assertEquals("stuff;   more stuff;   a lot more stuff", field.getBody());
71        ByteSequence raw = field.getRaw();
72        assertNotNull(raw);
73        assertEquals("raw  : stuff;\r\n   more stuff;\r\n   a lot more stuff",
74                new String(raw.toByteArray(), "US-ASCII"));
75    }
76
77    public void testNoTrailingCRLF() throws Exception {
78        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
79        builder.reset();
80        builder.append(line("raw:   stuff;\r\n"));
81        builder.append(line("   more stuff;\r\n"));
82        builder.append(line("   a lot more stuff"));
83        ByteArrayBuffer buf = builder.getRaw();
84        assertNotNull(buf);
85        assertEquals("raw:   stuff;\r\n   more stuff;\r\n   a lot more stuff",
86                new String(buf.toByteArray(), "US-ASCII"));
87        RawField field = builder.build();
88        assertNotNull(field);
89        assertEquals("raw", field.getName());
90        assertEquals("  stuff;   more stuff;   a lot more stuff", field.getBody());
91        ByteSequence raw = field.getRaw();
92        assertNotNull(raw);
93        assertEquals("raw:   stuff;\r\n   more stuff;\r\n   a lot more stuff",
94                new String(raw.toByteArray(), "US-ASCII"));
95    }
96
97    public void testIllegalCharsInName() throws Exception {
98        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
99        builder.reset();
100        builder.append(line("raw stuff: some stuff\r\n"));
101        try {
102            builder.build();
103            fail("MimeException should have been thrown");
104        } catch (MimeException expected) {
105        }
106    }
107
108    public void testReset() throws Exception {
109        DefaultFieldBuilder builder = new DefaultFieldBuilder(0);
110        builder.reset();
111        builder.append(line("raw: some stuff\r\n"));
112        ByteArrayBuffer buf = builder.getRaw();
113        assertNotNull(buf);
114        assertEquals("raw: some stuff\r\n", new String(buf.toByteArray(), "US-ASCII"));
115        builder.reset();
116        buf = builder.getRaw();
117        assertTrue(buf.isEmpty());
118        try {
119            builder.build();
120            fail("MimeException should have been thrown");
121        } catch (MimeException expected) {
122        }
123    }
124
125    public void testTooLong() throws Exception {
126        DefaultFieldBuilder builder = new DefaultFieldBuilder(20);
127        builder.reset();
128        builder.append(line("raw: some stuff\r\n"));
129        try {
130            builder.append(line("toooooooooooooooooooooooooooooooooooooons of stuff\r\n"));
131            fail("MimeException should have been thrown");
132        } catch (MimeException expected) {
133        }
134    }
135
136}
Note: See TracBrowser for help on using the repository browser.