[6785] | 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.BufferedReader; |
---|
| 23 | import java.io.ByteArrayInputStream; |
---|
| 24 | import java.io.ByteArrayOutputStream; |
---|
| 25 | import java.io.IOException; |
---|
| 26 | import java.io.InputStream; |
---|
| 27 | import java.io.InputStreamReader; |
---|
| 28 | import java.util.Arrays; |
---|
| 29 | import java.util.Date; |
---|
| 30 | import java.util.List; |
---|
| 31 | import java.util.TimeZone; |
---|
| 32 | |
---|
| 33 | import junit.framework.TestCase; |
---|
| 34 | |
---|
| 35 | import org.apache.commons.io.IOUtils; |
---|
| 36 | import org.apache.james.mime4j.dom.Header; |
---|
| 37 | import org.apache.james.mime4j.dom.Message; |
---|
| 38 | import org.apache.james.mime4j.dom.Multipart; |
---|
| 39 | import org.apache.james.mime4j.dom.SingleBody; |
---|
| 40 | import org.apache.james.mime4j.dom.address.Group; |
---|
| 41 | import org.apache.james.mime4j.dom.address.Mailbox; |
---|
| 42 | import org.apache.james.mime4j.field.DefaultFieldParser; |
---|
| 43 | import org.apache.james.mime4j.field.address.AddressBuilder; |
---|
| 44 | import org.apache.james.mime4j.message.BodyPart; |
---|
| 45 | import org.apache.james.mime4j.message.HeaderImpl; |
---|
| 46 | import org.apache.james.mime4j.message.MessageImpl; |
---|
| 47 | import org.apache.james.mime4j.message.MimeBuilder; |
---|
| 48 | import org.apache.james.mime4j.message.MimeWriter; |
---|
| 49 | import org.apache.james.mime4j.message.MultipartImpl; |
---|
| 50 | |
---|
| 51 | public class MessageTest extends TestCase { |
---|
| 52 | private Header headerTextPlain = null; |
---|
| 53 | private Header headerMessageRFC822 = null; |
---|
| 54 | private Header headerEmpty = null; |
---|
| 55 | private Header headerMultipartMixed = null; |
---|
| 56 | private Header headerMultipartDigest = null; |
---|
| 57 | |
---|
| 58 | @Override |
---|
| 59 | public void setUp() throws Exception { |
---|
| 60 | headerTextPlain = new HeaderImpl(); |
---|
| 61 | headerMessageRFC822 = new HeaderImpl(); |
---|
| 62 | headerEmpty = new HeaderImpl(); |
---|
| 63 | headerMultipartMixed = new HeaderImpl(); |
---|
| 64 | headerMultipartDigest = new HeaderImpl(); |
---|
| 65 | |
---|
| 66 | headerTextPlain.addField( |
---|
| 67 | DefaultFieldParser.parse("Content-Type: text/plain")); |
---|
| 68 | headerMessageRFC822.addField( |
---|
| 69 | DefaultFieldParser.parse("Content-Type: message/RFC822")); |
---|
| 70 | headerMultipartMixed.addField( |
---|
| 71 | DefaultFieldParser.parse("Content-Type: multipart/mixed; boundary=foo")); |
---|
| 72 | headerMultipartDigest.addField( |
---|
| 73 | DefaultFieldParser.parse("Content-Type: multipart/digest; boundary=foo")); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public void testGetMimeType() { |
---|
| 77 | MessageImpl parent = null; |
---|
| 78 | MessageImpl child = null; |
---|
| 79 | |
---|
| 80 | parent = new MessageImpl(); |
---|
| 81 | child = new MessageImpl(); |
---|
| 82 | child.setParent(parent); |
---|
| 83 | parent.setHeader(headerMultipartDigest); |
---|
| 84 | child.setHeader(headerEmpty); |
---|
| 85 | assertEquals("multipart/digest, empty", "message/rfc822", |
---|
| 86 | child.getMimeType()); |
---|
| 87 | child.setHeader(headerTextPlain); |
---|
| 88 | assertEquals("multipart/digest, text/plain", "text/plain", |
---|
| 89 | child.getMimeType()); |
---|
| 90 | child.setHeader(headerMessageRFC822); |
---|
| 91 | assertEquals("multipart/digest, message/rfc822", "message/rfc822", |
---|
| 92 | child.getMimeType()); |
---|
| 93 | |
---|
| 94 | parent = new MessageImpl(); |
---|
| 95 | child = new MessageImpl(); |
---|
| 96 | child.setParent(parent); |
---|
| 97 | parent.setHeader(headerMultipartMixed); |
---|
| 98 | child.setHeader(headerEmpty); |
---|
| 99 | assertEquals("multipart/mixed, empty", "text/plain", |
---|
| 100 | child.getMimeType()); |
---|
| 101 | child.setHeader(headerTextPlain); |
---|
| 102 | assertEquals("multipart/mixed, text/plain", "text/plain", |
---|
| 103 | child.getMimeType()); |
---|
| 104 | child.setHeader(headerMessageRFC822); |
---|
| 105 | assertEquals("multipart/mixed, message/rfc822", "message/rfc822", |
---|
| 106 | child.getMimeType()); |
---|
| 107 | |
---|
| 108 | child = new MessageImpl(); |
---|
| 109 | child.setHeader(headerEmpty); |
---|
| 110 | assertEquals("null, empty", "text/plain", child.getMimeType()); |
---|
| 111 | child.setHeader(headerTextPlain); |
---|
| 112 | assertEquals("null, text/plain", "text/plain", child.getMimeType()); |
---|
| 113 | child.setHeader(headerMessageRFC822); |
---|
| 114 | assertEquals("null, message/rfc822", "message/rfc822", |
---|
| 115 | child.getMimeType()); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | public void testIsMultipart() { |
---|
| 119 | MessageImpl m = new MessageImpl(); |
---|
| 120 | |
---|
| 121 | m.setHeader(headerEmpty); |
---|
| 122 | assertTrue("empty", !m.isMultipart()); |
---|
| 123 | |
---|
| 124 | m.setHeader(headerTextPlain); |
---|
| 125 | assertTrue("text/plain", !m.isMultipart()); |
---|
| 126 | |
---|
| 127 | m.setHeader(headerMultipartDigest); |
---|
| 128 | assertTrue("multipart/digest", m.isMultipart()); |
---|
| 129 | |
---|
| 130 | m.setHeader(headerMultipartMixed); |
---|
| 131 | assertTrue("multipart/mixed", m.isMultipart()); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | public void testWriteTo() throws Exception { |
---|
| 135 | byte[] inputByte = getRawMessageAsByteArray(); |
---|
| 136 | |
---|
| 137 | Message m = MimeBuilder.DEFAULT.parse(new ByteArrayInputStream(inputByte)); |
---|
| 138 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
| 139 | |
---|
| 140 | MimeWriter.DEFAULT.writeMessage(m, out); |
---|
| 141 | |
---|
| 142 | InputStream output = new ByteArrayInputStream(out.toByteArray()); |
---|
| 143 | |
---|
| 144 | int b = -1; |
---|
| 145 | int i = 0; |
---|
| 146 | while ((b = output.read()) != -1) { |
---|
| 147 | assertEquals("same byte", b, inputByte[i]); |
---|
| 148 | i++; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | public void testAddHeaderWriteTo() throws Exception { |
---|
| 153 | String headerName = "testheader"; |
---|
| 154 | String headerValue = "testvalue"; |
---|
| 155 | String testheader = headerName + ": " + headerValue; |
---|
| 156 | |
---|
| 157 | byte[] inputByte = getRawMessageAsByteArray(); |
---|
| 158 | |
---|
| 159 | Message m = MimeBuilder.DEFAULT.parse(new ByteArrayInputStream(inputByte)); |
---|
| 160 | m.getHeader().addField(DefaultFieldParser.parse(testheader)); |
---|
| 161 | |
---|
| 162 | assertEquals("header added", m.getHeader().getField(headerName) |
---|
| 163 | .getBody(), headerValue); |
---|
| 164 | |
---|
| 165 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
| 166 | MimeWriter.DEFAULT.writeMessage(m, out); |
---|
| 167 | List<?> lines = IOUtils.readLines((new BufferedReader( |
---|
| 168 | new InputStreamReader(new ByteArrayInputStream(out |
---|
| 169 | .toByteArray()))))); |
---|
| 170 | |
---|
| 171 | assertTrue("header added", lines.contains(testheader)); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | public void testGetMessageId() throws Exception { |
---|
| 175 | MessageImpl m = new MessageImpl(); |
---|
| 176 | assertNull(m.getMessageId()); |
---|
| 177 | |
---|
| 178 | String id = "<msg17@localhost>"; |
---|
| 179 | Header header = new HeaderImpl(); |
---|
| 180 | header.setField(DefaultFieldParser.parse("Message-ID: " + id)); |
---|
| 181 | m.setHeader(header); |
---|
| 182 | assertEquals(id, m.getMessageId()); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | public void testCreateMessageId() throws Exception { |
---|
| 186 | MessageImpl m = new MessageImpl(); |
---|
| 187 | m.createMessageId("hostname"); |
---|
| 188 | |
---|
| 189 | String id = m.getMessageId(); |
---|
| 190 | assertNotNull(id); |
---|
| 191 | assertTrue(id.startsWith("<Mime4j.")); |
---|
| 192 | assertTrue(id.endsWith("@hostname>")); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | public void testGetSubject() throws Exception { |
---|
| 196 | MessageImpl m = new MessageImpl(); |
---|
| 197 | assertNull(m.getSubject()); |
---|
| 198 | |
---|
| 199 | String subject = "testing 1 2"; |
---|
| 200 | Header header = new HeaderImpl(); |
---|
| 201 | header.setField(DefaultFieldParser.parse("Subject: " + subject)); |
---|
| 202 | m.setHeader(header); |
---|
| 203 | assertEquals(subject, m.getSubject()); |
---|
| 204 | |
---|
| 205 | header.setField(DefaultFieldParser.parse("Subject: =?windows-1252?Q?99_=80?=")); |
---|
| 206 | assertEquals("99 \u20ac", m.getSubject()); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | public void testSetSubject() throws Exception { |
---|
| 210 | MessageImpl m = new MessageImpl(); |
---|
| 211 | |
---|
| 212 | m.setSubject("Semmelbr\366sel"); |
---|
| 213 | assertEquals("Semmelbr\366sel", m.getSubject()); |
---|
| 214 | assertEquals("=?ISO-8859-1?Q?Semmelbr=F6sel?=", m.getHeader().getField( |
---|
| 215 | "Subject").getBody()); |
---|
| 216 | |
---|
| 217 | m.setSubject(null); |
---|
| 218 | assertNull(m.getHeader().getField("Subject")); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | public void testGetDate() throws Exception { |
---|
| 222 | MessageImpl m = new MessageImpl(); |
---|
| 223 | assertNull(m.getDate()); |
---|
| 224 | |
---|
| 225 | Header header = new HeaderImpl(); |
---|
| 226 | header.setField(DefaultFieldParser.parse("Date: Thu, 1 Jan 1970 05:30:00 +0530")); |
---|
| 227 | m.setHeader(header); |
---|
| 228 | |
---|
| 229 | assertEquals(new Date(0), m.getDate()); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | public void testSetDate() throws Exception { |
---|
| 233 | MessageImpl m = new MessageImpl(); |
---|
| 234 | |
---|
| 235 | m.setDate(new Date(86400000), TimeZone.getTimeZone("GMT")); |
---|
| 236 | assertEquals(new Date(86400000), m.getDate()); |
---|
| 237 | assertEquals("Fri, 2 Jan 1970 00:00:00 +0000", m.getHeader().getField( |
---|
| 238 | "Date").getBody()); |
---|
| 239 | |
---|
| 240 | m.setDate(null); |
---|
| 241 | assertNull(m.getHeader().getField("Date")); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | public void testGetSender() throws Exception { |
---|
| 245 | MessageImpl m = new MessageImpl(); |
---|
| 246 | assertNull(m.getSender()); |
---|
| 247 | |
---|
| 248 | Header header = new HeaderImpl(); |
---|
| 249 | header.setField(DefaultFieldParser.parse("Sender: john.doe@example.net")); |
---|
| 250 | m.setHeader(header); |
---|
| 251 | |
---|
| 252 | assertEquals("john.doe@example.net", m.getSender().getAddress()); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | public void testSetSender() throws Exception { |
---|
| 256 | MessageImpl m = new MessageImpl(); |
---|
| 257 | |
---|
| 258 | m.setSender(AddressBuilder.DEFAULT.parseMailbox("john.doe@example.net")); |
---|
| 259 | assertEquals("john.doe@example.net", m.getHeader().getField("Sender") |
---|
| 260 | .getBody()); |
---|
| 261 | |
---|
| 262 | m.setSender(null); |
---|
| 263 | assertNull(m.getHeader().getField("Sender")); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | public void testGetFrom() throws Exception { |
---|
| 267 | MessageImpl m = new MessageImpl(); |
---|
| 268 | assertNull(m.getFrom()); |
---|
| 269 | |
---|
| 270 | Header header = new HeaderImpl(); |
---|
| 271 | header.setField(DefaultFieldParser.parse("From: john.doe@example.net")); |
---|
| 272 | m.setHeader(header); |
---|
| 273 | |
---|
| 274 | assertEquals("john.doe@example.net", m.getFrom().get(0).getAddress()); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | public void testSetFrom() throws Exception { |
---|
| 278 | MessageImpl m = new MessageImpl(); |
---|
| 279 | |
---|
| 280 | Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("john.doe@example.net"); |
---|
| 281 | Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("jane.doe@example.net"); |
---|
| 282 | |
---|
| 283 | m.setFrom(mailbox1); |
---|
| 284 | assertEquals("john.doe@example.net", m.getHeader().getField("From") |
---|
| 285 | .getBody()); |
---|
| 286 | |
---|
| 287 | m.setFrom(mailbox1, mailbox2); |
---|
| 288 | assertEquals("john.doe@example.net, jane.doe@example.net", m |
---|
| 289 | .getHeader().getField("From").getBody()); |
---|
| 290 | |
---|
| 291 | m.setFrom(Arrays.asList(mailbox1, mailbox2)); |
---|
| 292 | assertEquals("john.doe@example.net, jane.doe@example.net", m |
---|
| 293 | .getHeader().getField("From").getBody()); |
---|
| 294 | |
---|
| 295 | m.setFrom((Mailbox) null); |
---|
| 296 | assertNull(m.getHeader().getField("From")); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | public void testGetTo() throws Exception { |
---|
| 300 | MessageImpl m = new MessageImpl(); |
---|
| 301 | assertNull(m.getTo()); |
---|
| 302 | |
---|
| 303 | Header header = new HeaderImpl(); |
---|
| 304 | header.setField(DefaultFieldParser.parse("To: john.doe@example.net")); |
---|
| 305 | m.setHeader(header); |
---|
| 306 | |
---|
| 307 | assertEquals("john.doe@example.net", ((Mailbox) m.getTo().get(0)) |
---|
| 308 | .getAddress()); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | public void testSetTo() throws Exception { |
---|
| 312 | MessageImpl m = new MessageImpl(); |
---|
| 313 | |
---|
| 314 | Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("john.doe@example.net"); |
---|
| 315 | Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("jane.doe@example.net"); |
---|
| 316 | Group group = new Group("Does", mailbox1, mailbox2); |
---|
| 317 | Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>"); |
---|
| 318 | |
---|
| 319 | m.setTo(group); |
---|
| 320 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;", m |
---|
| 321 | .getHeader().getField("To").getBody()); |
---|
| 322 | |
---|
| 323 | m.setTo(group, mailbox3); |
---|
| 324 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;, " |
---|
| 325 | + "Mary Smith <mary@example.net>", m.getHeader().getField("To") |
---|
| 326 | .getBody()); |
---|
| 327 | |
---|
| 328 | m.setTo(Arrays.asList(group, mailbox3)); |
---|
| 329 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;, " |
---|
| 330 | + "Mary Smith <mary@example.net>", m.getHeader().getField("To") |
---|
| 331 | .getBody()); |
---|
| 332 | |
---|
| 333 | m.setTo((Mailbox) null); |
---|
| 334 | assertNull(m.getHeader().getField("To")); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | public void testGetCc() throws Exception { |
---|
| 338 | MessageImpl m = new MessageImpl(); |
---|
| 339 | assertNull(m.getCc()); |
---|
| 340 | |
---|
| 341 | Header header = new HeaderImpl(); |
---|
| 342 | header.setField(DefaultFieldParser.parse("Cc: john.doe@example.net")); |
---|
| 343 | m.setHeader(header); |
---|
| 344 | |
---|
| 345 | assertEquals("john.doe@example.net", ((Mailbox) m.getCc().get(0)) |
---|
| 346 | .getAddress()); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | public void testSetCc() throws Exception { |
---|
| 350 | MessageImpl m = new MessageImpl(); |
---|
| 351 | |
---|
| 352 | Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("john.doe@example.net"); |
---|
| 353 | Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("jane.doe@example.net"); |
---|
| 354 | Group group = new Group("Does", mailbox1, mailbox2); |
---|
| 355 | Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>"); |
---|
| 356 | |
---|
| 357 | m.setCc(group); |
---|
| 358 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;", m |
---|
| 359 | .getHeader().getField("Cc").getBody()); |
---|
| 360 | |
---|
| 361 | m.setCc(group, mailbox3); |
---|
| 362 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;, " |
---|
| 363 | + "Mary Smith <mary@example.net>", m.getHeader().getField("Cc") |
---|
| 364 | .getBody()); |
---|
| 365 | |
---|
| 366 | m.setCc(Arrays.asList(group, mailbox3)); |
---|
| 367 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;, " |
---|
| 368 | + "Mary Smith <mary@example.net>", m.getHeader().getField("Cc") |
---|
| 369 | .getBody()); |
---|
| 370 | |
---|
| 371 | m.setCc((Mailbox) null); |
---|
| 372 | assertNull(m.getHeader().getField("Cc")); |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | public void testGetBcc() throws Exception { |
---|
| 376 | MessageImpl m = new MessageImpl(); |
---|
| 377 | assertNull(m.getBcc()); |
---|
| 378 | |
---|
| 379 | Header header = new HeaderImpl(); |
---|
| 380 | header.setField(DefaultFieldParser.parse("Bcc: john.doe@example.net")); |
---|
| 381 | m.setHeader(header); |
---|
| 382 | |
---|
| 383 | assertEquals("john.doe@example.net", ((Mailbox) m.getBcc().get(0)) |
---|
| 384 | .getAddress()); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | public void testSetBcc() throws Exception { |
---|
| 388 | MessageImpl m = new MessageImpl(); |
---|
| 389 | |
---|
| 390 | Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("john.doe@example.net"); |
---|
| 391 | Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("jane.doe@example.net"); |
---|
| 392 | Group group = new Group("Does", mailbox1, mailbox2); |
---|
| 393 | Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>"); |
---|
| 394 | |
---|
| 395 | m.setBcc(group); |
---|
| 396 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;", m |
---|
| 397 | .getHeader().getField("Bcc").getBody()); |
---|
| 398 | |
---|
| 399 | m.setBcc(group, mailbox3); |
---|
| 400 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;, " |
---|
| 401 | + "Mary Smith <mary@example.net>", m.getHeader() |
---|
| 402 | .getField("Bcc").getBody()); |
---|
| 403 | |
---|
| 404 | m.setBcc(Arrays.asList(group, mailbox3)); |
---|
| 405 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;, " |
---|
| 406 | + "Mary Smith <mary@example.net>", m.getHeader() |
---|
| 407 | .getField("Bcc").getBody()); |
---|
| 408 | |
---|
| 409 | m.setBcc((Mailbox) null); |
---|
| 410 | assertNull(m.getHeader().getField("Bcc")); |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | public void testGetReplyTo() throws Exception { |
---|
| 414 | MessageImpl m = new MessageImpl(); |
---|
| 415 | assertNull(m.getReplyTo()); |
---|
| 416 | |
---|
| 417 | Header header = new HeaderImpl(); |
---|
| 418 | header.setField(DefaultFieldParser.parse("Reply-To: john.doe@example.net")); |
---|
| 419 | m.setHeader(header); |
---|
| 420 | |
---|
| 421 | assertEquals("john.doe@example.net", ((Mailbox) m.getReplyTo().get(0)) |
---|
| 422 | .getAddress()); |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | public void testSetReplyTo() throws Exception { |
---|
| 426 | MessageImpl m = new MessageImpl(); |
---|
| 427 | |
---|
| 428 | Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("john.doe@example.net"); |
---|
| 429 | Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("jane.doe@example.net"); |
---|
| 430 | Group group = new Group("Does", mailbox1, mailbox2); |
---|
| 431 | Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>"); |
---|
| 432 | |
---|
| 433 | m.setReplyTo(group); |
---|
| 434 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;", m |
---|
| 435 | .getHeader().getField("Reply-To").getBody()); |
---|
| 436 | |
---|
| 437 | m.setReplyTo(group, mailbox3); |
---|
| 438 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;, " |
---|
| 439 | + "Mary Smith <mary@example.net>", m.getHeader().getField( |
---|
| 440 | "Reply-To").getBody()); |
---|
| 441 | |
---|
| 442 | m.setReplyTo(Arrays.asList(group, mailbox3)); |
---|
| 443 | assertEquals("Does: john.doe@example.net, jane.doe@example.net;, " |
---|
| 444 | + "Mary Smith <mary@example.net>", m.getHeader().getField( |
---|
| 445 | "Reply-To").getBody()); |
---|
| 446 | |
---|
| 447 | m.setReplyTo((Mailbox) null); |
---|
| 448 | assertNull(m.getHeader().getField("Reply-To")); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | public void testDisposeGetsPropagatedToBody() throws Exception { |
---|
| 452 | DummyBody body1 = new DummyBody(); |
---|
| 453 | BodyPart part1 = new BodyPart(); |
---|
| 454 | part1.setHeader(headerEmpty); |
---|
| 455 | part1.setBody(body1); |
---|
| 456 | |
---|
| 457 | DummyBody body2 = new DummyBody(); |
---|
| 458 | BodyPart part2 = new BodyPart(); |
---|
| 459 | part2.setHeader(headerEmpty); |
---|
| 460 | part2.setBody(body2); |
---|
| 461 | |
---|
| 462 | Multipart mp = new MultipartImpl("mixed"); |
---|
| 463 | mp.addBodyPart(part1); |
---|
| 464 | mp.addBodyPart(part2); |
---|
| 465 | |
---|
| 466 | MessageImpl m = new MessageImpl(); |
---|
| 467 | m.setHeader(headerMultipartMixed); |
---|
| 468 | m.setBody(mp); |
---|
| 469 | |
---|
| 470 | assertFalse(body1.disposed); |
---|
| 471 | assertFalse(body2.disposed); |
---|
| 472 | |
---|
| 473 | m.dispose(); |
---|
| 474 | |
---|
| 475 | assertTrue(body1.disposed); |
---|
| 476 | assertTrue(body2.disposed); |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | private byte[] getRawMessageAsByteArray() { |
---|
| 480 | StringBuilder header = new StringBuilder(); |
---|
| 481 | StringBuilder body = new StringBuilder(); |
---|
| 482 | StringBuilder complete = new StringBuilder(); |
---|
| 483 | |
---|
| 484 | header.append("Date: Wed, 21 Feb 2007 11:09:27 +0100\r\n"); |
---|
| 485 | header.append("From: Test <test@test>\r\n"); |
---|
| 486 | header.append("To: Norman Maurer <nm@byteaction.de>\r\n"); |
---|
| 487 | header.append("Subject: Testmail\r\n"); |
---|
| 488 | header |
---|
| 489 | .append("Content-Type: text/plain; charset=ISO-8859-15; format=flowed\r\n"); |
---|
| 490 | header.append("Content-Transfer-Encoding: 8bit\r\n\r\n"); |
---|
| 491 | body.append("testbody\r\n"); |
---|
| 492 | complete.append(header); |
---|
| 493 | complete.append(body); |
---|
| 494 | |
---|
| 495 | return complete.toString().getBytes(); |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | private static final class DummyBody extends SingleBody { |
---|
| 499 | |
---|
| 500 | public boolean disposed = false; |
---|
| 501 | |
---|
| 502 | @Override |
---|
| 503 | public InputStream getInputStream() throws IOException { |
---|
| 504 | return new ByteArrayInputStream("dummy".getBytes("US-ASCII")); |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | @Override |
---|
| 508 | public void dispose() { |
---|
| 509 | disposed = true; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | } |
---|