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 *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
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
****************************************************************/
20
package org.apache.james.mime4j.dom;
22
import java.io.ByteArrayOutputStream;
24
import junit.framework.TestCase;
26
import org.apache.james.mime4j.dom.Header;
27
import org.apache.james.mime4j.dom.Multipart;
28
import org.apache.james.mime4j.field.DefaultFieldParser;
29
import org.apache.james.mime4j.message.BasicBodyFactory;
30
import org.apache.james.mime4j.message.BodyPart;
31
import org.apache.james.mime4j.message.HeaderImpl;
32
import org.apache.james.mime4j.message.MessageImpl;
33
import org.apache.james.mime4j.message.DefaultMessageWriter;
34
import org.apache.james.mime4j.message.MultipartImpl;
36
public class MultipartFormTest extends TestCase {
38
public void testMultipartFormContent() throws Exception {
39
BasicBodyFactory bodyFactory = new BasicBodyFactory();
41
MessageImpl message = new MessageImpl();
42
Header header = new HeaderImpl();
44
DefaultFieldParser.parse("Content-Type: multipart/form-data; boundary=foo"));
45
message.setHeader(header);
47
Multipart multipart = new MultipartImpl("alternative");
48
multipart.setParent(message);
49
BodyPart p1 = new BodyPart();
50
Header h1 = new HeaderImpl();
51
h1.addField(DefaultFieldParser.parse("Content-Type: text/plain"));
53
p1.setBody(bodyFactory.textBody("this stuff"));
54
BodyPart p2 = new BodyPart();
55
Header h2 = new HeaderImpl();
56
h2.addField(DefaultFieldParser.parse("Content-Type: text/plain"));
58
p2.setBody(bodyFactory.textBody("that stuff"));
59
BodyPart p3 = new BodyPart();
60
Header h3 = new HeaderImpl();
61
h3.addField(DefaultFieldParser.parse("Content-Type: text/plain"));
63
p3.setBody(bodyFactory.textBody("all kind of stuff"));
65
multipart.addBodyPart(p1);
66
multipart.addBodyPart(p2);
67
multipart.addBodyPart(p3);
69
ByteArrayOutputStream out = new ByteArrayOutputStream();
70
DefaultMessageWriter writer = new DefaultMessageWriter();
71
writer.writeMultipart(multipart, out);
74
String expected = "--foo\r\n" +
75
"Content-Type: text/plain\r\n" +
79
"Content-Type: text/plain\r\n" +
83
"Content-Type: text/plain\r\n" +
85
"all kind of stuff\r\n" +
87
String s = out.toString("US-ASCII");
88
assertEquals(expected, s);