KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mail > SimpleMailMessageTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.mail;
18
19 import java.util.Arrays JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.List JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.mail.cos.CosMailSenderImpl;
26
27 /**
28  * @author Dmitriy Kopylenko
29  * @author Juergen Hoeller
30  * @since 10.09.2003
31  */

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 JavaDoc[] {"he@mail.org", "she@mail.org"});
45         message.setBcc(new String JavaDoc[] {"us@mail.org", "them@mail.org"});
46         Date JavaDoc sentDate = new Date JavaDoc();
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 JavaDoc ccs = Arrays.asList(message.getCc());
55         assertTrue(ccs.contains("he@mail.org"));
56         assertTrue(ccs.contains("she@mail.org"));
57         List JavaDoc 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     /**
80      * Tests that two equal SimpleMailMessages have equal hash codes.
81      */

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 JavaDoc());
90         message1.setSubject("subject");
91         message1.setText("text");
92
93         // Copy the message
94
SimpleMailMessage message2 = new SimpleMailMessage(message1);
95
96         assertEquals(message1, message2);
97         assertEquals(message1.hashCode(), message2.hashCode());
98     }
99
100     /**
101      * Tests {@link SimpleMailMessage#equals(Object).
102      */

103     public final void testEqualsObject() {
104         SimpleMailMessage message1;
105         SimpleMailMessage message2;
106
107         // Same object is equal
108
message1 = new SimpleMailMessage();
109         message2 = message1;
110         assertTrue(message1.equals(message2));
111
112         // Null object is not equal
113
message1 = new SimpleMailMessage();
114         message2 = null;
115         assertTrue(!(message1.equals(message2)));
116
117         // Different class is not equal
118
assertTrue(!(message1.equals(new Object JavaDoc())));
119
120         // Equal values are equal
121
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 JavaDoc());
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 JavaDoc ex) {
152             assertTrue(ex instanceof MailSendException);
153         }
154     }
155
156 }
157
Popular Tags