1 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 testRecipient1_ = "test1@methodhead.com"; 38 String testRecipient2_ = "test2@methodhead.com"; 39 Properties props = null; 40 41 static { 42 TestUtils.initLogger(); 43 } 46 47 public MailTest( String 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 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 assertNotNull( Mail.mailProperties_ ); 75 76 Mail.init( props ); 80 assertEquals( props, Mail.mailProperties_ ); 81 } 82 catch ( Exception e ) { 83 e.printStackTrace(); 84 fail(); 85 } 86 } 87 88 public void testIsValidEmailAddress() { 89 try { 90 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 e ) { 100 e.printStackTrace(); 101 fail(); 102 } 103 } 104 105 public void testSendErrors() { 106 try { 107 try { 111 Mail.send( new String [] { testRecipient1_ }, null, "test", "This is a test." ); 112 } 113 catch ( MhfException e ) { 114 } 116 117 try { 118 Mail.send( new String [] { testRecipient1_ }, "no reply", "test", "This is a test." ); 119 } 120 catch ( MhfException e ) { 121 } 123 } 124 catch ( Exception e ) { 125 e.printStackTrace(); 126 fail(); 127 } 128 } 129 130 public void testSend() { 131 try { 132 Mail.send( new String [] { testRecipient1_ }, "noreply@danhensgen.com", "testSend() - single recipient", "This is a test." ); 136 137 Mail.send( new String [] { testRecipient1_, testRecipient2_ }, "noreply@danhensgen.com", "testSend() - multiple recipients", "This is a test." ); 141 142 Mail.send( new String [] { testRecipient1_, "invalid address" }, "noreply@danhensgen.com", "testSend() - multiple recipients, one invalid", "This is a test." ); 147 } 148 catch ( Exception e ) { 149 e.printStackTrace(); 150 fail(); 151 } 152 } 153 154 public void testSendSingleAddress() { 155 try { 156 Mail.send( testRecipient1_, "noreply@danhensgen.com", "testSend() - single recipient (no array)", "This is a test." ); 160 } 161 catch ( Exception e ) { 162 e.printStackTrace(); 163 fail(); 164 } 165 } 166 167 public void testSendHtml() { 168 try { 169 Mail.send( new String [] { testRecipient1_ }, "noreply@danhensgen.com", "testSend() - html, single recipient", "This is a test.", "This is a <b>test</b>." ); 173 174 Mail.send( new String [] { testRecipient1_, testRecipient2_ }, "noreply@danhensgen.com", "testSend() - html, multiple recipients", "This is a test.", "This is a <b>test</b>." ); 178 179 Mail.send( new String [] { 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 e ) { 186 e.printStackTrace(); 187 fail(); 188 } 189 } 190 191 public void testSendWithAttachmentsErrors() { 192 try { 193 try { 197 Mail.send( new String [] { testRecipient1_ }, null, "test", "This is a test." ); 198 } 199 catch ( MhfException e ) { 200 } 202 203 try { 204 Mail.send( new String [] { testRecipient1_ }, "no reply", "test", "This is a test." ); 205 } 206 catch ( MhfException e ) { 207 } 209 210 try { 214 Mail.send( new String [] { testRecipient1_ }, "noreply@danhensgen.com", "test", "This is a test.", ( File[] )null ); 215 } 216 catch ( MhfException e ) { 217 } 219 220 try { 224 Mail.send( new String [] { testRecipient1_ }, "noreply@danhensgen.com", "test", "This is a test.", new File[] { null } ); 225 } 226 catch ( MhfException e ) { 227 } 229 230 try { 234 Mail.send( new String [] { testRecipient1_ }, "noreply@danhensgen.com", "test", "This is a test.", new File[] { new File( "invalidFile.txt" ) } ); 235 } 236 catch ( MhfException e ) { 237 } 239 } 240 catch ( Exception e ) { 241 e.printStackTrace(); 242 fail(); 243 } 244 } 245 246 public void testSendWithAttachments() { 247 try { 248 Mail.send( new String [] { testRecipient1_ }, "noreply@danhensgen.com", "testSendWithAttachments() - single recipient, single attachment", "This is a test.", new File[] { new File( "support/image1.jpg" ) } ); 252 253 Mail.send( new String [] { 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 Mail.send( new String [] { 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 e ) { 264 e.printStackTrace(); 265 fail(); 266 } 267 } 268 269 public void testSendHtmlWithAttachments() { 270 try { 271 Mail.send( new String [] { 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 Mail.send( new String [] { 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 Mail.send( new String [] { 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 e ) { 287 e.printStackTrace(); 288 fail(); 289 } 290 } 291 } 292 | Popular Tags |