KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > mail > MailTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.mail;
22
23 import java.util.*;
24 import java.io.*;
25 import java.sql.*;
26 import junit.framework.*;
27 import org.apache.log4j.*;
28 import com.methodhead.persistable.*;
29 import com.methodhead.test.*;
30 import com.methodhead.*;
31 import javax.mail.internet.*;
32 import javax.mail.*;
33 import org.apache.commons.mail.*;
34
35 public class MailTest extends TestCase {
36
37   String JavaDoc testRecipient1_ = "test1@methodhead.com";
38   String JavaDoc testRecipient2_ = "test2@methodhead.com";
39   Properties props = null;
40
41   static {
42     TestUtils.initLogger();
43     //TestUtils.setLogLevel( Level.DEBUG );
44
//TestUtils.initDb();
45
}
46
47   public MailTest( String JavaDoc name ) {
48     super( name );
49   }
50
51   protected void setUp() {
52     try {
53       props = new Properties();
54       props.setProperty( "mail.host", "localhost" );
55       props.setProperty( "mail.from", "test1@methodhead.com" );
56       Mail.init( props );
57     }
58     catch ( Exception JavaDoc e ) {
59       fail( e.getMessage() );
60     }
61   }
62
63   protected void tearDown() {
64   }
65
66   public void testInit() {
67     try {
68       props = new Properties();
69       props.setProperty( "mail.host", "localhost" );
70
71       //
72
// should be null by default
73
//
74
assertNotNull( Mail.mailProperties_ );
75
76       //
77
// should set mailProperties_
78
//
79
Mail.init( props );
80       assertEquals( props, Mail.mailProperties_ );
81     }
82     catch ( Exception JavaDoc e ) {
83       e.printStackTrace();
84       fail();
85     }
86   }
87
88   public void testIsValidEmailAddress() {
89     try {
90       //
91
// invalid from should throw an exception
92
//
93
assertEquals( false, Mail.isValidAddress( null ) );
94       assertEquals( false, Mail.isValidAddress( "" ) );
95       assertEquals( false, Mail.isValidAddress( "no reply" ) );
96       assertEquals( false, Mail.isValidAddress( "invalid" ) );
97       assertEquals( true, Mail.isValidAddress( "test1@methodhead.com" ) );
98     }
99     catch ( Exception JavaDoc e ) {
100       e.printStackTrace();
101       fail();
102     }
103   }
104
105   public void testSendErrors() {
106     try {
107       //
108
// invalid from should throw an exception
109
//
110
try {
111         Mail.send( new String JavaDoc[] { testRecipient1_ }, null, "test", "This is a test." );
112       }
113       catch ( MhfException e ) {
114         // success
115
}
116
117       try {
118         Mail.send( new String JavaDoc[] { testRecipient1_ }, "no reply", "test", "This is a test." );
119       }
120       catch ( MhfException e ) {
121         // success
122
}
123     }
124     catch ( Exception JavaDoc e ) {
125       e.printStackTrace();
126       fail();
127     }
128   }
129
130   public void testSend() {
131     try {
132       //
133
// send a simple message to one recipient
134
//
135
Mail.send( new String JavaDoc[] { testRecipient1_ }, "noreply@danhensgen.com", "testSend() - single recipient", "This is a test." );
136
137       //
138
// send a simple message to two recipients
139
//
140
Mail.send( new String JavaDoc[] { testRecipient1_, testRecipient2_ }, "noreply@danhensgen.com", "testSend() - multiple recipients", "This is a test." );
141
142       //
143
// send a simple message to two recipients, one of them invalid; should
144
// send successfully to valid recipient
145
//
146
Mail.send( new String JavaDoc[] { testRecipient1_, "invalid address" }, "noreply@danhensgen.com", "testSend() - multiple recipients, one invalid", "This is a test." );
147     }
148     catch ( Exception JavaDoc e ) {
149       e.printStackTrace();
150       fail();
151     }
152   }
153
154   public void testSendSingleAddress() {
155     try {
156       //
157
// send a simple message to one recipient
158
//
159
Mail.send( testRecipient1_, "noreply@danhensgen.com", "testSend() - single recipient (no array)", "This is a test." );
160     }
161     catch ( Exception JavaDoc e ) {
162       e.printStackTrace();
163       fail();
164     }
165   }
166
167   public void testSendHtml() {
168     try {
169       //
170
// send a simple message with both text and html to one recipient
171
//
172
Mail.send( new String JavaDoc[] { testRecipient1_ }, "noreply@danhensgen.com", "testSend() - html, single recipient", "This is a test.", "This is a <b>test</b>." );
173
174       //
175
// send a simple message to two recipients
176
//
177
Mail.send( new String JavaDoc[] { testRecipient1_, testRecipient2_ }, "noreply@danhensgen.com", "testSend() - html, multiple recipients", "This is a test.", "This is a <b>test</b>." );
178
179       //
180
// send a simple message to two recipients, one of them invalid; should
181
// send successfully to valid recipient
182
//
183
Mail.send( new String JavaDoc[] { testRecipient1_, "invalid address" }, "noreply@danhensgen.com", "testSend() - html, multiple recipients, one invalid", "This is a test.", "This is a <b>test</b>." );
184     }
185     catch ( Exception JavaDoc e ) {
186       e.printStackTrace();
187       fail();
188     }
189   }
190
191   public void testSendWithAttachmentsErrors() {
192     try {
193       //
194
// invalid from should throw an exception
195
//
196
try {
197         Mail.send( new String JavaDoc[] { testRecipient1_ }, null, "test", "This is a test." );
198       }
199       catch ( MhfException e ) {
200         // success
201
}
202
203       try {
204         Mail.send( new String JavaDoc[] { testRecipient1_ }, "no reply", "test", "This is a test." );
205       }
206       catch ( MhfException e ) {
207         // success
208
}
209
210       //
211
// null attachment list should throw an exception
212
//
213
try {
214         Mail.send( new String JavaDoc[] { testRecipient1_ }, "noreply@danhensgen.com", "test", "This is a test.", ( File[] )null );
215       }
216       catch ( MhfException e ) {
217         // success
218
}
219
220       //
221
// null attachment should throw an exception
222
//
223
try {
224         Mail.send( new String JavaDoc[] { testRecipient1_ }, "noreply@danhensgen.com", "test", "This is a test.", new File[] { null } );
225       }
226       catch ( MhfException e ) {
227         // success
228
}
229
230       //
231
// non-existant attachment should throw an exception
232
//
233
try {
234         Mail.send( new String JavaDoc[] { testRecipient1_ }, "noreply@danhensgen.com", "test", "This is a test.", new File[] { new File( "invalidFile.txt" ) } );
235       }
236       catch ( MhfException e ) {
237         // success
238
}
239     }
240     catch ( Exception JavaDoc e ) {
241       e.printStackTrace();
242       fail();
243     }
244   }
245
246   public void testSendWithAttachments() {
247     try {
248       //
249
// send a message to one recipient with one attachment
250
//
251
Mail.send( new String JavaDoc[] { testRecipient1_ }, "noreply@danhensgen.com", "testSendWithAttachments() - single recipient, single attachment", "This is a test.", new File[] { new File( "support/image1.jpg" ) } );
252
253       //
254
// send a message to one recipient with two attachments
255
//
256
Mail.send( new String JavaDoc[] { testRecipient1_ }, "noreply@danhensgen.com", "testSendWithAttachments() - single recipient, two attachments", "This is a test.", new File[] { new File( "support/image1.jpg" ), new File( "support/image2.jpg" ) } );
257
258       //
259
// send a message to two recipients with one attachment
260
//
261
Mail.send( new String JavaDoc[] { testRecipient1_, testRecipient2_ }, "noreply@danhensgen.com", "testSendWithAttachments() - two recipients, one attachment", "This is a test.", new File[] { new File( "support/image1.jpg" ) } );
262     }
263     catch ( Exception JavaDoc e ) {
264       e.printStackTrace();
265       fail();
266     }
267   }
268
269   public void testSendHtmlWithAttachments() {
270     try {
271       //
272
// send a message to one recipient with one attachment
273
//
274
Mail.send( new String JavaDoc[] { testRecipient1_ }, "noreply@danhensgen.com", "testSendWithAttachments() - html, single recipient, single attachment", "This is a test.", "This is a <b>test</b>.", new File[] { new File( "support/image1.jpg" ) } );
275
276       //
277
// send a message to one recipient with two attachments
278
//
279
Mail.send( new String JavaDoc[] { testRecipient1_ }, "noreply@danhensgen.com", "testSendWithAttachments() - single recipient, two attachments", "This is a test.", new File[] { new File( "support/image1.jpg" ), new File( "support/image2.jpg" ) } );
280
281       //
282
// send a message to two recipients with one attachment
283
//
284
Mail.send( new String JavaDoc[] { testRecipient1_, testRecipient2_ }, "noreply@danhensgen.com", "testSendWithAttachments() - two recipients, one attachment", "This is a test.", new File[] { new File( "support/image1.jpg" ) } );
285     }
286     catch ( Exception JavaDoc e ) {
287       e.printStackTrace();
288       fail();
289     }
290   }
291 }
292
Popular Tags