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

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/james/mime4j/message/CopyConstructorTest.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.message;
21
 
 
22
 
import java.util.Arrays;
23
 
import java.util.List;
24
 
 
25
 
import org.apache.james.mime4j.field.AbstractField;
26
 
import org.apache.james.mime4j.parser.Field;
27
 
 
28
 
import junit.framework.TestCase;
29
 
 
30
 
public class CopyConstructorTest extends TestCase {
31
 
 
32
 
    public void testCopyEmptyMessage() throws Exception {
33
 
        Message original = new Message();
34
 
 
35
 
        Message copy = new Message(original);
36
 
 
37
 
        assertNull(copy.getHeader());
38
 
        assertNull(copy.getBody());
39
 
        assertNull(copy.getParent());
40
 
    }
41
 
 
42
 
    public void testCopyMessage() throws Exception {
43
 
        Message parent = new Message();
44
 
        Header header = new Header();
45
 
        Body body = new BodyFactory().textBody("test");
46
 
 
47
 
        Message original = new Message();
48
 
        original.setHeader(header);
49
 
        original.setBody(body);
50
 
        original.setParent(parent);
51
 
 
52
 
        Message copy = new Message(original);
53
 
 
54
 
        assertNotNull(copy.getHeader());
55
 
        assertNotSame(header, copy.getHeader());
56
 
 
57
 
        assertNotNull(copy.getBody());
58
 
        assertNotSame(body, copy.getBody());
59
 
 
60
 
        assertSame(copy, copy.getBody().getParent());
61
 
 
62
 
        assertNull(copy.getParent());
63
 
    }
64
 
 
65
 
    public void testCopyEmptyBodyPart() throws Exception {
66
 
        BodyPart original = new BodyPart();
67
 
 
68
 
        BodyPart copy = new BodyPart(original);
69
 
 
70
 
        assertNull(copy.getHeader());
71
 
        assertNull(copy.getBody());
72
 
        assertNull(copy.getParent());
73
 
    }
74
 
 
75
 
    public void testCopyBodyPart() throws Exception {
76
 
        Message parent = new Message();
77
 
        Header header = new Header();
78
 
        Body body = new BodyFactory().textBody("test");
79
 
 
80
 
        BodyPart original = new BodyPart();
81
 
        original.setHeader(header);
82
 
        original.setBody(body);
83
 
        original.setParent(parent);
84
 
 
85
 
        BodyPart copy = new BodyPart(original);
86
 
 
87
 
        assertNotNull(copy.getHeader());
88
 
        assertNotSame(header, copy.getHeader());
89
 
 
90
 
        assertNotNull(copy.getBody());
91
 
        assertNotSame(body, copy.getBody());
92
 
 
93
 
        assertSame(copy, copy.getBody().getParent());
94
 
 
95
 
        assertNull(copy.getParent());
96
 
    }
97
 
 
98
 
    public void testCopyEmptyMultipart() throws Exception {
99
 
        Multipart original = new Multipart("mixed");
100
 
 
101
 
        Multipart copy = new Multipart(original);
102
 
 
103
 
        assertSame(original.getPreamble(), copy.getPreamble());
104
 
        assertSame(original.getEpilogue(), copy.getEpilogue());
105
 
        assertSame(original.getSubType(), copy.getSubType());
106
 
        assertTrue(copy.getBodyParts().isEmpty());
107
 
        assertNull(copy.getParent());
108
 
    }
109
 
 
110
 
    public void testCopyMultipart() throws Exception {
111
 
        Message parent = new Message();
112
 
        BodyPart bodyPart = new BodyPart();
113
 
 
114
 
        Multipart original = new Multipart("mixed");
115
 
        original.setPreamble("preamble");
116
 
        original.setEpilogue("epilogue");
117
 
        original.setParent(parent);
118
 
        original.addBodyPart(bodyPart);
119
 
 
120
 
        Multipart copy = new Multipart(original);
121
 
 
122
 
        assertSame(original.getPreamble(), copy.getPreamble());
123
 
        assertSame(original.getEpilogue(), copy.getEpilogue());
124
 
        assertSame(original.getSubType(), copy.getSubType());
125
 
        assertEquals(1, copy.getBodyParts().size());
126
 
        assertNull(copy.getParent());
127
 
 
128
 
        BodyPart bodyPartCopy = copy.getBodyParts().iterator().next();
129
 
        assertNotSame(bodyPart, bodyPartCopy);
130
 
 
131
 
        assertSame(parent, bodyPart.getParent());
132
 
        assertNull(bodyPartCopy.getParent());
133
 
    }
134
 
 
135
 
    public void testCopyMultipartMessage() throws Exception {
136
 
        BodyPart bodyPart1 = new BodyPart();
137
 
        BodyPart bodyPart2 = new BodyPart();
138
 
 
139
 
        Multipart multipart = new Multipart("mixed");
140
 
        multipart.addBodyPart(bodyPart1);
141
 
        multipart.addBodyPart(bodyPart2);
142
 
 
143
 
        Message original = new Message();
144
 
        original.setHeader(new Header());
145
 
        original.setBody(multipart);
146
 
 
147
 
        Message copy = new Message(original);
148
 
 
149
 
        Multipart multipartCopy = (Multipart) copy.getBody();
150
 
        List<BodyPart> bodyParts = multipartCopy.getBodyParts();
151
 
        BodyPart bodyPartCopy1 = bodyParts.get(0);
152
 
        BodyPart bodyPartCopy2 = bodyParts.get(1);
153
 
 
154
 
        assertNotSame(bodyPart1, bodyPartCopy1);
155
 
        assertEquals(original, bodyPart1.getParent());
156
 
        assertEquals(copy, bodyPartCopy1.getParent());
157
 
 
158
 
        assertNotSame(bodyPart2, bodyPartCopy2);
159
 
        assertEquals(original, bodyPart2.getParent());
160
 
        assertEquals(copy, bodyPartCopy2.getParent());
161
 
    }
162
 
 
163
 
    public void testCopyHeader() throws Exception {
164
 
        Field f1 = AbstractField.parse("name1: value1");
165
 
        Field f2 = AbstractField.parse("name2: value");
166
 
        Field f3 = AbstractField.parse("name1: value2");
167
 
 
168
 
        Header original = new Header();
169
 
        original.addField(f1);
170
 
        original.addField(f2);
171
 
        original.addField(f3);
172
 
 
173
 
        Header copy = new Header(original);
174
 
 
175
 
        // copy must have same fields as original
176
 
        assertEquals(Arrays.asList(f1, f2, f3), copy.getFields());
177
 
        assertEquals(Arrays.asList(f1, f3), copy.getFields("name1"));
178
 
 
179
 
        // modify original
180
 
        original.removeFields("name1");
181
 
        assertEquals(Arrays.asList(f2), original.getFields());
182
 
 
183
 
        // copy may not be affected
184
 
        assertEquals(Arrays.asList(f1, f2, f3), copy.getFields());
185
 
        assertEquals(Arrays.asList(f1, f3), copy.getFields("name1"));
186
 
    }
187
 
 
188
 
}