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.stream; |
---|
21 | |
---|
22 | import org.apache.james.mime4j.MimeException; |
---|
23 | import org.apache.james.mime4j.util.ByteArrayBuffer; |
---|
24 | import org.apache.james.mime4j.util.ByteSequence; |
---|
25 | |
---|
26 | import junit.framework.TestCase; |
---|
27 | |
---|
28 | public class LenientFieldBuilderTest 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 | LenientFieldBuilder builder = new LenientFieldBuilder(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 | assertNull(buf); |
---|
45 | RawField field = builder.build(); |
---|
46 | assertNotNull(field); |
---|
47 | assertEquals("raw", field.getName()); |
---|
48 | assertEquals(" stuff; more stuff; a lot more stuff", field.getBody()); |
---|
49 | ByteSequence raw = field.getRaw(); |
---|
50 | assertNull(raw); |
---|
51 | } |
---|
52 | |
---|
53 | public void testObsoleteSyntax() throws Exception { |
---|
54 | LenientFieldBuilder builder = new LenientFieldBuilder(0); |
---|
55 | builder.reset(); |
---|
56 | builder.append(line("raw : stuff;\r\n")); |
---|
57 | builder.append(line(" more stuff;\r\n")); |
---|
58 | builder.append(line(" a lot more stuff\r\n")); |
---|
59 | ByteArrayBuffer buf = builder.getRaw(); |
---|
60 | assertNull(buf); |
---|
61 | RawField field = builder.build(); |
---|
62 | assertNotNull(field); |
---|
63 | assertEquals("raw", field.getName()); |
---|
64 | assertEquals("stuff; more stuff; a lot more stuff", field.getBody()); |
---|
65 | ByteSequence raw = field.getRaw(); |
---|
66 | assertNull(raw); |
---|
67 | } |
---|
68 | |
---|
69 | public void testIllegalCharsInName() throws Exception { |
---|
70 | LenientFieldBuilder builder = new LenientFieldBuilder(0); |
---|
71 | builder.reset(); |
---|
72 | builder.append(line("raw stuff: some stuff\r\n")); |
---|
73 | RawField field = builder.build(); |
---|
74 | assertNotNull(field); |
---|
75 | assertEquals("raw stuff", field.getName()); |
---|
76 | assertEquals("some stuff", field.getBody()); |
---|
77 | } |
---|
78 | |
---|
79 | public void testReset() throws Exception { |
---|
80 | LenientFieldBuilder builder = new LenientFieldBuilder(0); |
---|
81 | builder.reset(); |
---|
82 | builder.append(line("raw: some stuff\r\n")); |
---|
83 | builder.reset(); |
---|
84 | try { |
---|
85 | builder.build(); |
---|
86 | fail("MimeException should have been thrown"); |
---|
87 | } catch (MimeException expected) { |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | public void testTooLong() throws Exception { |
---|
92 | LenientFieldBuilder builder = new LenientFieldBuilder(20); |
---|
93 | builder.reset(); |
---|
94 | builder.append(line("raw: some stuff\r\n")); |
---|
95 | builder.append(line("toooooooooooooooooooooooooooooooooooooons of stuff\r\n")); |
---|
96 | RawField field = builder.build(); |
---|
97 | assertNotNull(field); |
---|
98 | assertEquals("raw", field.getName()); |
---|
99 | assertEquals("some stuff", field.getBody()); |
---|
100 | } |
---|
101 | |
---|
102 | } |
---|