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.message;
22
import java.util.Arrays;
23
import java.util.List;
25
import org.apache.james.mime4j.field.AbstractField;
26
import org.apache.james.mime4j.parser.Field;
28
import junit.framework.TestCase;
30
public class CopyConstructorTest extends TestCase {
32
public void testCopyEmptyMessage() throws Exception {
33
Message original = new Message();
35
Message copy = new Message(original);
37
assertNull(copy.getHeader());
38
assertNull(copy.getBody());
39
assertNull(copy.getParent());
42
public void testCopyMessage() throws Exception {
43
Message parent = new Message();
44
Header header = new Header();
45
Body body = new BodyFactory().textBody("test");
47
Message original = new Message();
48
original.setHeader(header);
49
original.setBody(body);
50
original.setParent(parent);
52
Message copy = new Message(original);
54
assertNotNull(copy.getHeader());
55
assertNotSame(header, copy.getHeader());
57
assertNotNull(copy.getBody());
58
assertNotSame(body, copy.getBody());
60
assertSame(copy, copy.getBody().getParent());
62
assertNull(copy.getParent());
65
public void testCopyEmptyBodyPart() throws Exception {
66
BodyPart original = new BodyPart();
68
BodyPart copy = new BodyPart(original);
70
assertNull(copy.getHeader());
71
assertNull(copy.getBody());
72
assertNull(copy.getParent());
75
public void testCopyBodyPart() throws Exception {
76
Message parent = new Message();
77
Header header = new Header();
78
Body body = new BodyFactory().textBody("test");
80
BodyPart original = new BodyPart();
81
original.setHeader(header);
82
original.setBody(body);
83
original.setParent(parent);
85
BodyPart copy = new BodyPart(original);
87
assertNotNull(copy.getHeader());
88
assertNotSame(header, copy.getHeader());
90
assertNotNull(copy.getBody());
91
assertNotSame(body, copy.getBody());
93
assertSame(copy, copy.getBody().getParent());
95
assertNull(copy.getParent());
98
public void testCopyEmptyMultipart() throws Exception {
99
Multipart original = new Multipart("mixed");
101
Multipart copy = new Multipart(original);
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());
110
public void testCopyMultipart() throws Exception {
111
Message parent = new Message();
112
BodyPart bodyPart = new BodyPart();
114
Multipart original = new Multipart("mixed");
115
original.setPreamble("preamble");
116
original.setEpilogue("epilogue");
117
original.setParent(parent);
118
original.addBodyPart(bodyPart);
120
Multipart copy = new Multipart(original);
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());
128
BodyPart bodyPartCopy = copy.getBodyParts().iterator().next();
129
assertNotSame(bodyPart, bodyPartCopy);
131
assertSame(parent, bodyPart.getParent());
132
assertNull(bodyPartCopy.getParent());
135
public void testCopyMultipartMessage() throws Exception {
136
BodyPart bodyPart1 = new BodyPart();
137
BodyPart bodyPart2 = new BodyPart();
139
Multipart multipart = new Multipart("mixed");
140
multipart.addBodyPart(bodyPart1);
141
multipart.addBodyPart(bodyPart2);
143
Message original = new Message();
144
original.setHeader(new Header());
145
original.setBody(multipart);
147
Message copy = new Message(original);
149
Multipart multipartCopy = (Multipart) copy.getBody();
150
List<BodyPart> bodyParts = multipartCopy.getBodyParts();
151
BodyPart bodyPartCopy1 = bodyParts.get(0);
152
BodyPart bodyPartCopy2 = bodyParts.get(1);
154
assertNotSame(bodyPart1, bodyPartCopy1);
155
assertEquals(original, bodyPart1.getParent());
156
assertEquals(copy, bodyPartCopy1.getParent());
158
assertNotSame(bodyPart2, bodyPartCopy2);
159
assertEquals(original, bodyPart2.getParent());
160
assertEquals(copy, bodyPartCopy2.getParent());
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");
168
Header original = new Header();
169
original.addField(f1);
170
original.addField(f2);
171
original.addField(f3);
173
Header copy = new Header(original);
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"));
180
original.removeFields("name1");
181
assertEquals(Arrays.asList(f2), original.getFields());
183
// copy may not be affected
184
assertEquals(Arrays.asList(f1, f2, f3), copy.getFields());
185
assertEquals(Arrays.asList(f1, f3), copy.getFields("name1"));