source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/dom/src/test/java/org/apache/james/mime4j/dom/MimeBuilderCopyTest.java @ 6785

Revision 6785, 7.1 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

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