~ubuntu-branches/ubuntu/utopic/apache-mime4j/utopic-proposed

« back to all changes in this revision

Viewing changes to dom/src/test/java/org/apache/james/mime4j/dom/MultipartFormTest.java

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2013-11-03 11:13:27 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20131103111327-09huep00ex05z113
Tags: 0.7.2-2
* Upload to unstable
* Fixed Vcs-* fields in debian/control
* Updated debian/watch
* Standards-Version bump to 3.9.5, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.dom;
 
21
 
 
22
import java.io.ByteArrayOutputStream;
 
23
 
 
24
import junit.framework.TestCase;
 
25
 
 
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;
 
35
 
 
36
public class MultipartFormTest extends TestCase {
 
37
 
 
38
    public void testMultipartFormContent() throws Exception {
 
39
        BasicBodyFactory bodyFactory = new BasicBodyFactory();
 
40
 
 
41
        MessageImpl message = new MessageImpl();
 
42
        Header header = new HeaderImpl();
 
43
        header.addField(
 
44
                DefaultFieldParser.parse("Content-Type: multipart/form-data; boundary=foo"));
 
45
        message.setHeader(header);
 
46
 
 
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"));
 
52
        p1.setHeader(h1);
 
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"));
 
57
        p2.setHeader(h2);
 
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"));
 
62
        p3.setHeader(h3);
 
63
        p3.setBody(bodyFactory.textBody("all kind of stuff"));
 
64
 
 
65
        multipart.addBodyPart(p1);
 
66
        multipart.addBodyPart(p2);
 
67
        multipart.addBodyPart(p3);
 
68
 
 
69
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
70
        DefaultMessageWriter writer = new DefaultMessageWriter();
 
71
        writer.writeMultipart(multipart, out);
 
72
        out.close();
 
73
 
 
74
        String expected = "--foo\r\n" +
 
75
            "Content-Type: text/plain\r\n" +
 
76
            "\r\n" +
 
77
            "this stuff\r\n" +
 
78
            "--foo\r\n" +
 
79
            "Content-Type: text/plain\r\n" +
 
80
            "\r\n" +
 
81
            "that stuff\r\n" +
 
82
            "--foo\r\n" +
 
83
            "Content-Type: text/plain\r\n" +
 
84
            "\r\n" +
 
85
            "all kind of stuff\r\n" +
 
86
            "--foo--\r\n";
 
87
        String s = out.toString("US-ASCII");
 
88
        assertEquals(expected, s);
 
89
    }
 
90
 
 
91
}