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 java.io.ByteArrayInputStream; |
---|
23 | import java.io.IOException; |
---|
24 | import java.io.InputStream; |
---|
25 | import java.nio.charset.Charset; |
---|
26 | |
---|
27 | import junit.framework.TestCase; |
---|
28 | |
---|
29 | import org.apache.commons.io.IOUtils; |
---|
30 | import org.apache.james.mime4j.MimeException; |
---|
31 | import org.apache.james.mime4j.util.CharsetUtil; |
---|
32 | |
---|
33 | public class MultipartStreamTest extends TestCase { |
---|
34 | |
---|
35 | private static final Charset US_ASCII = CharsetUtil.US_ASCII; |
---|
36 | |
---|
37 | private static final String BODY = "A Preamble\r\n" + |
---|
38 | "--1729\r\n\r\n" + |
---|
39 | "Simple plain text\r\n" + |
---|
40 | "--1729\r\n" + |
---|
41 | "Content-Type: text/plain; charset=US-ASCII\r\n\r\n" + |
---|
42 | "Some more text\r\n" + |
---|
43 | "--1729--\r\n"; |
---|
44 | public static final String MESSAGE = "To: Road Runner <runner@example.org>\r\n" + |
---|
45 | "From: Wile E. Cayote <wile@example.org>\r\n" + |
---|
46 | "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" + |
---|
47 | "Subject: Mail\r\n" + |
---|
48 | "Content-Type: multipart/mixed;boundary=1729\r\n\r\n" + |
---|
49 | BODY; |
---|
50 | |
---|
51 | public static final String COMPLEX_MESSAGE = "To: Wile E. Cayote <wile@example.org>\r\n" + |
---|
52 | "From: Road Runner <runner@example.org>\r\n" + |
---|
53 | "Date: Tue, 19 Feb 2008 17:34:09 +0000 (GMT)\r\n" + |
---|
54 | "Subject: Mail\r\n" + |
---|
55 | "Content-Type: multipart/mixed;boundary=42\r\n\r\n" + |
---|
56 | "A little preamble\r\n" + |
---|
57 | "--42\r\n" + |
---|
58 | "Content-Type: text/plain; charset=US-ASCII\r\n\r\n" + |
---|
59 | "Rhubard!\r\n" + |
---|
60 | "--42\r\n" + |
---|
61 | "Content-Type: message/rfc822\r\n\r\n" + |
---|
62 | MESSAGE + |
---|
63 | "\r\n" + |
---|
64 | "--42\r\n" + |
---|
65 | "Content-Type: text/plain; charset=US-ASCII\r\n\r\n" + |
---|
66 | "Custard!" + |
---|
67 | "\r\n" + |
---|
68 | "--42--\r\n"; |
---|
69 | |
---|
70 | MimeTokenStream parser; |
---|
71 | |
---|
72 | @Override |
---|
73 | protected void setUp() throws Exception { |
---|
74 | super.setUp(); |
---|
75 | parser = new MimeTokenStream(); |
---|
76 | } |
---|
77 | |
---|
78 | @Override |
---|
79 | protected void tearDown() throws Exception { |
---|
80 | super.tearDown(); |
---|
81 | } |
---|
82 | |
---|
83 | public void testShouldSupplyInputStreamForSimpleBody() throws Exception { |
---|
84 | parser.parse(new ByteArrayInputStream(US_ASCII.encode(MESSAGE).array())); |
---|
85 | checkState(EntityState.T_START_HEADER); |
---|
86 | checkState(EntityState.T_FIELD); |
---|
87 | checkState(EntityState.T_FIELD); |
---|
88 | checkState(EntityState.T_FIELD); |
---|
89 | checkState(EntityState.T_FIELD); |
---|
90 | checkState(EntityState.T_FIELD); |
---|
91 | checkState(EntityState.T_END_HEADER); |
---|
92 | checkState(EntityState.T_START_MULTIPART); |
---|
93 | InputStream out = parser.getInputStream(); |
---|
94 | assertEquals(BODY, IOUtils.toString(out, "us-ascii")); |
---|
95 | checkState(EntityState.T_END_MULTIPART); |
---|
96 | } |
---|
97 | |
---|
98 | public void testInputStreamShouldReadOnlyMessage() throws Exception { |
---|
99 | parser.parse(new ByteArrayInputStream(US_ASCII.encode(COMPLEX_MESSAGE).array())); |
---|
100 | checkState(EntityState.T_START_HEADER); |
---|
101 | checkState(EntityState.T_FIELD); |
---|
102 | checkState(EntityState.T_FIELD); |
---|
103 | checkState(EntityState.T_FIELD); |
---|
104 | checkState(EntityState.T_FIELD); |
---|
105 | checkState(EntityState.T_FIELD); |
---|
106 | checkState(EntityState.T_END_HEADER); |
---|
107 | checkState(EntityState.T_START_MULTIPART); |
---|
108 | checkState(EntityState.T_PREAMBLE); |
---|
109 | checkState(EntityState.T_START_BODYPART); |
---|
110 | checkState(EntityState.T_START_HEADER); |
---|
111 | checkState(EntityState.T_FIELD); |
---|
112 | checkState(EntityState.T_END_HEADER); |
---|
113 | checkState(EntityState.T_BODY); |
---|
114 | checkState(EntityState.T_END_BODYPART); |
---|
115 | checkState(EntityState.T_START_BODYPART); |
---|
116 | checkState(EntityState.T_START_HEADER); |
---|
117 | checkState(EntityState.T_FIELD); |
---|
118 | checkState(EntityState.T_END_HEADER); |
---|
119 | checkState(EntityState.T_START_MESSAGE); |
---|
120 | checkState(EntityState.T_START_HEADER); |
---|
121 | checkState(EntityState.T_FIELD); |
---|
122 | checkState(EntityState.T_FIELD); |
---|
123 | checkState(EntityState.T_FIELD); |
---|
124 | checkState(EntityState.T_FIELD); |
---|
125 | checkState(EntityState.T_FIELD); |
---|
126 | checkState(EntityState.T_END_HEADER); |
---|
127 | checkState(EntityState.T_START_MULTIPART); |
---|
128 | InputStream out = parser.getInputStream(); |
---|
129 | assertEquals(BODY, IOUtils.toString(out, "us-ascii")); |
---|
130 | checkState(EntityState.T_END_MULTIPART); |
---|
131 | checkState(EntityState.T_END_MESSAGE); |
---|
132 | checkState(EntityState.T_END_BODYPART); |
---|
133 | checkState(EntityState.T_START_BODYPART); |
---|
134 | checkState(EntityState.T_START_HEADER); |
---|
135 | checkState(EntityState.T_FIELD); |
---|
136 | checkState(EntityState.T_END_HEADER); |
---|
137 | checkState(EntityState.T_BODY); |
---|
138 | checkState(EntityState.T_END_BODYPART); |
---|
139 | checkState(EntityState.T_END_MULTIPART); |
---|
140 | checkState(EntityState.T_END_MESSAGE); |
---|
141 | checkState(EntityState.T_END_OF_STREAM); |
---|
142 | } |
---|
143 | |
---|
144 | private void checkState(final EntityState state) throws IOException, MimeException { |
---|
145 | assertEquals(MimeTokenStream.stateToString(state), MimeTokenStream.stateToString(parser.next())); |
---|
146 | } |
---|
147 | } |
---|