1 16 17 package org.springframework.mail; 18 19 import java.util.Arrays ; 20 import java.util.Date ; 21 import java.util.List ; 22 23 import junit.framework.TestCase; 24 25 import org.springframework.mail.cos.CosMailSenderImpl; 26 27 32 public class SimpleMailMessageTests extends TestCase { 33 34 public void testSimpleMessage() { 35 SimpleMailMessage message = new SimpleMailMessage(); 36 message.setFrom("me@mail.org"); 37 message.setTo("you@mail.org"); 38 39 SimpleMailMessage messageCopy = new SimpleMailMessage(message); 40 assertEquals("me@mail.org", messageCopy.getFrom()); 41 assertEquals("you@mail.org", messageCopy.getTo()[0]); 42 43 message.setReplyTo("reply@mail.org"); 44 message.setCc(new String [] {"he@mail.org", "she@mail.org"}); 45 message.setBcc(new String [] {"us@mail.org", "them@mail.org"}); 46 Date sentDate = new Date (); 47 message.setSentDate(sentDate); 48 message.setSubject("my subject"); 49 message.setText("my text"); 50 51 assertEquals("me@mail.org", message.getFrom()); 52 assertEquals("reply@mail.org", message.getReplyTo()); 53 assertEquals("you@mail.org", message.getTo()[0]); 54 List ccs = Arrays.asList(message.getCc()); 55 assertTrue(ccs.contains("he@mail.org")); 56 assertTrue(ccs.contains("she@mail.org")); 57 List bccs = Arrays.asList(message.getBcc()); 58 assertTrue(bccs.contains("us@mail.org")); 59 assertTrue(bccs.contains("them@mail.org")); 60 assertEquals(sentDate, message.getSentDate()); 61 assertEquals("my subject", message.getSubject()); 62 assertEquals("my text", message.getText()); 63 64 messageCopy = new SimpleMailMessage(message); 65 assertEquals("me@mail.org", messageCopy.getFrom()); 66 assertEquals("reply@mail.org", messageCopy.getReplyTo()); 67 assertEquals("you@mail.org", messageCopy.getTo()[0]); 68 ccs = Arrays.asList(messageCopy.getCc()); 69 assertTrue(ccs.contains("he@mail.org")); 70 assertTrue(ccs.contains("she@mail.org")); 71 bccs = Arrays.asList(message.getBcc()); 72 assertTrue(bccs.contains("us@mail.org")); 73 assertTrue(bccs.contains("them@mail.org")); 74 assertEquals(sentDate, messageCopy.getSentDate()); 75 assertEquals("my subject", messageCopy.getSubject()); 76 assertEquals("my text", messageCopy.getText()); 77 } 78 79 82 public final void testHashCode() { 83 SimpleMailMessage message1 = new SimpleMailMessage(); 84 message1.setFrom("from@somewhere"); 85 message1.setReplyTo("replyTo@somewhere"); 86 message1.setTo("to@somewhere"); 87 message1.setCc("cc@somewhere"); 88 message1.setBcc("bcc@somewhere"); 89 message1.setSentDate(new Date ()); 90 message1.setSubject("subject"); 91 message1.setText("text"); 92 93 SimpleMailMessage message2 = new SimpleMailMessage(message1); 95 96 assertEquals(message1, message2); 97 assertEquals(message1.hashCode(), message2.hashCode()); 98 } 99 100 103 public final void testEqualsObject() { 104 SimpleMailMessage message1; 105 SimpleMailMessage message2; 106 107 message1 = new SimpleMailMessage(); 109 message2 = message1; 110 assertTrue(message1.equals(message2)); 111 112 message1 = new SimpleMailMessage(); 114 message2 = null; 115 assertTrue(!(message1.equals(message2))); 116 117 assertTrue(!(message1.equals(new Object ()))); 119 120 message1 = new SimpleMailMessage(); 122 message2 = new SimpleMailMessage(); 123 assertTrue(message1.equals(message2)); 124 125 message1 = new SimpleMailMessage(); 126 message1.setFrom("from@somewhere"); 127 message1.setReplyTo("replyTo@somewhere"); 128 message1.setTo("to@somewhere"); 129 message1.setCc("cc@somewhere"); 130 message1.setBcc("bcc@somewhere"); 131 message1.setSentDate(new Date ()); 132 message1.setSubject("subject"); 133 message1.setText("text"); 134 message2 = new SimpleMailMessage(message1); 135 assertTrue(message1.equals(message2)); 136 } 137 138 public void testCosMailSenderImplWithSimpleMessageAndBadHostName() throws MailException { 139 SimpleMailMessage message = new SimpleMailMessage(); 140 message.setFrom("a@a.com"); 141 message.setTo("b@b.com"); 142 message.setSubject("test"); 143 message.setText("another test"); 144 145 CosMailSenderImpl sender = new CosMailSenderImpl(); 146 sender.setHost("hostxyzdoesnotexist"); 147 try { 148 sender.send(message); 149 fail("Should have thrown MailSendException"); 150 } 151 catch (Exception ex) { 152 assertTrue(ex instanceof MailSendException); 153 } 154 } 155 156 } 157 | Popular Tags |