1 17 18 package org.apache.tools.mail; 19 20 import java.io.BufferedReader ; 21 import java.io.BufferedWriter ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.OutputStreamWriter ; 26 import java.io.PrintStream ; 27 import java.net.InetAddress ; 28 import java.net.Socket ; 29 import java.net.ServerSocket ; 30 import java.util.Enumeration ; 31 import java.util.Vector ; 32 33 import org.apache.tools.mail.MailMessage; 34 35 import junit.framework.TestCase; 36 37 42 public class MailMessageTest extends TestCase { 43 44 private static int TEST_PORT = 27224; 46 47 private String local = null; 48 49 public MailMessageTest(String name) { 50 super(name); 51 } 52 53 public void setUp() { 54 try { 55 local = InetAddress.getLocalHost().getHostName(); 56 } catch (java.net.UnknownHostException uhe) { 57 } 59 } 60 61 67 public void testAPIExample() { 68 69 ServerThread testMailServer = new ServerThread(); 70 Thread server = new Thread (testMailServer); 71 server.start(); 72 73 ClientThread testMailClient = new ClientThread(); 74 75 testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>"); 76 testMailClient.to("to@you.com"); 77 testMailClient.cc("cc1@you.com"); 78 testMailClient.cc("cc2@you.com"); 79 testMailClient.bcc("bcc@you.com"); 80 testMailClient.setSubject("Test subject"); 81 testMailClient.setMessage( "test line 1\n" + 82 "test line 2" ); 83 84 Thread client = new Thread (testMailClient); 85 client.start(); 86 87 try { 88 server.join(60 * 1000); client.join(30 * 1000); } catch (InterruptedException ie ) { 91 fail( "InterruptedException: " + ie ); 92 } 93 94 String result = testMailServer.getResult(); 95 String expectedResult = "220 test SMTP EmailTaskTest\r\n" + 96 "HELO " + local + "\r\n" + 97 "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" + 98 "MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" + 99 "250\r\n" + 100 "RCPT TO: <to@you.com>\r\n" + 101 "250\r\n" + 102 "RCPT TO: <cc1@you.com>\r\n" + 103 "250\r\n" + 104 "RCPT TO: <cc2@you.com>\r\n" + 105 "250\r\n" + 106 "RCPT TO: <bcc@you.com>\r\n" + 107 "250\r\n" + 108 "DATA\r\n" + 109 "354\r\n" + 110 "Subject: Test subject\r\n" + 111 "From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" + 112 "To: to@you.com\r\n" + 113 "Cc: cc1@you.com, cc2@you.com\r\n" + 114 "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" + 115 "\r\n" + 116 "test line 1\r\n" + 117 "test line 2\r\n" + 118 "\r\n" + 119 ".\r\n" + 120 "250\r\n" + 121 "QUIT\r\n" + 122 "221\r\n"; 123 for (int icounter = 0; icounter<expectedResult.length(); icounter++) { 124 if (icounter < result.length()) { 125 if (expectedResult.charAt(icounter) != result.charAt(icounter)) { 126 System.out.println("posit " + icounter + " expected " 127 + expectedResult.charAt(icounter) 128 + " result " + result.charAt(icounter)); 129 } 130 } 131 } 132 if (expectedResult.length()>result.length()) { 133 System.out.println("excedent of expected result " 134 + expectedResult.substring(result.length())); 135 } 136 if (expectedResult.length()<result.length()) { 137 System.out.println("excedent of result " 138 + result.substring(expectedResult.length())); 139 } 140 assertEquals(expectedResult.length(), result.length()); 141 assertEquals(expectedResult, result); if (testMailClient.isFailed()) { 143 fail(testMailClient.getFailMessage()); 144 } 145 } 146 147 150 public void testToOnly() { 151 ServerThread testMailServer = new ServerThread(); 152 Thread server = new Thread (testMailServer); 153 server.start(); 154 155 ClientThread testMailClient = new ClientThread(); 156 157 testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>"); 158 testMailClient.to("to@you.com"); 159 testMailClient.setSubject("Test subject"); 160 testMailClient.setMessage( "test line 1\n" + 161 "test line 2" ); 162 163 Thread client = new Thread (testMailClient); 164 client.start(); 165 166 try { 167 server.join(60 * 1000); client.join(30 * 1000); } catch (InterruptedException ie ) { 170 fail("InterruptedException: " + ie); 171 } 172 173 String result = testMailServer.getResult(); 174 String expectedResult = "220 test SMTP EmailTaskTest\r\n" + 175 "HELO " + local + "\r\n" + 176 "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" + 177 "MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" + 178 "250\r\n" + 179 "RCPT TO: <to@you.com>\r\n" + 180 "250\r\n" + 181 "DATA\r\n" + 182 "354\r\n" + 183 "Subject: Test subject\r\n" + 184 "From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" + 185 "To: to@you.com\r\n" + 186 "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" + 187 "\r\n" + 188 "test line 1\r\n" + 189 "test line 2\r\n" + 190 "\r\n" + 191 ".\r\n" + 192 "250\r\n" + 193 "QUIT\r\n" + 194 "221\r\n"; 195 assertEquals(expectedResult.length(), result.length()); 196 assertEquals(expectedResult, result); if (testMailClient.isFailed()) { 198 fail(testMailClient.getFailMessage()); 199 } 200 } 201 202 203 206 public void testCcOnly() { 207 ServerThread testMailServer = new ServerThread(); 208 Thread server = new Thread (testMailServer); 209 server.start(); 210 211 ClientThread testMailClient = new ClientThread(); 212 213 testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>"); 214 testMailClient.cc("cc@you.com"); 215 testMailClient.setSubject("Test subject"); 216 testMailClient.setMessage( "test line 1\n" + 217 "test line 2" ); 218 219 Thread client = new Thread (testMailClient); 220 client.start(); 221 222 try { 223 server.join(60 * 1000); client.join(30 * 1000); } catch (InterruptedException ie ) { 226 fail( "InterruptedException: " + ie ); 227 } 228 229 String result = testMailServer.getResult(); 230 String expectedResult = "220 test SMTP EmailTaskTest\r\n" + 231 "HELO " + local + "\r\n" + 232 "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" + 233 "MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" + 234 "250\r\n" + 235 "RCPT TO: <cc@you.com>\r\n" + 236 "250\r\n" + 237 "DATA\r\n" + 238 "354\r\n" + 239 "Subject: Test subject\r\n" + 240 "From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" + 241 "Cc: cc@you.com\r\n" + 242 "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" + 243 "\r\n" + 244 "test line 1\r\n" + 245 "test line 2\r\n" + 246 "\r\n" + 247 ".\r\n" + 248 "250\r\n" + 249 "QUIT\r\n" + 250 "221\r\n"; 251 assertEquals(expectedResult.length(), result.length()); 252 assertEquals(expectedResult, result); 253 if (testMailClient.isFailed()) { 254 fail(testMailClient.getFailMessage()); 255 } 256 } 257 258 259 262 public void testBccOnly() { 263 ServerThread testMailServer = new ServerThread(); 264 Thread server = new Thread (testMailServer); 265 server.start(); 266 267 ClientThread testMailClient = new ClientThread(); 268 269 testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>"); 270 testMailClient.bcc("bcc@you.com"); 271 testMailClient.setSubject("Test subject"); 272 testMailClient.setMessage( "test line 1\n" + 273 "test line 2" ); 274 275 Thread client = new Thread (testMailClient); 276 client.start(); 277 278 try { 279 server.join(60 * 1000); client.join(30 * 1000); } catch (InterruptedException ie ) { 282 fail( "InterruptedException: " + ie ); 283 } 284 285 String result = testMailServer.getResult(); 286 String expectedResult = "220 test SMTP EmailTaskTest\r\n" + 287 "HELO " + local + "\r\n" + 288 "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" + 289 "MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" + 290 "250\r\n" + 291 "RCPT TO: <bcc@you.com>\r\n" + 292 "250\r\n" + 293 "DATA\r\n" + 294 "354\r\n" + 295 "Subject: Test subject\r\n" + 296 "From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" + 297 "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" + 298 "\r\n" + 299 "test line 1\r\n" + 300 "test line 2\r\n" + 301 "\r\n" + 302 ".\r\n" + 303 "250\r\n" + 304 "QUIT\r\n" + 305 "221\r\n"; 306 assertEquals( expectedResult.length(), result.length() ); 307 assertEquals( expectedResult, result ); 308 if ( testMailClient.isFailed() ) { 309 fail( testMailClient.getFailMessage() ); 310 } 311 } 312 313 314 318 public void testNoSubject() { 319 ServerThread testMailServer = new ServerThread(); 320 Thread server = new Thread (testMailServer); 321 server.start(); 322 323 ClientThread testMailClient = new ClientThread(); 324 325 testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>"); 326 testMailClient.to("to@you.com"); 327 testMailClient.setMessage( "test line 1\n" + 328 "test line 2" ); 329 330 Thread client = new Thread (testMailClient); 331 client.start(); 332 333 try { 334 server.join(60 * 1000); client.join(30 * 1000); } catch (InterruptedException ie ) { 337 fail( "InterruptedException: " + ie ); 338 } 339 340 String result = testMailServer.getResult(); 341 String expectedResult = "220 test SMTP EmailTaskTest\r\n" + 342 "HELO " + local + "\r\n" + 343 "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" + 344 "MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" + 345 "250\r\n" + 346 "RCPT TO: <to@you.com>\r\n" + 347 "250\r\n" + 348 "DATA\r\n" + 349 "354\r\n" + 350 "From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" + 351 "To: to@you.com\r\n" + 352 "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" + 353 "\r\n" + 354 "test line 1\r\n" + 355 "test line 2\r\n" + 356 "\r\n" + 357 ".\r\n" + 358 "250\r\n" + 359 "QUIT\r\n" + 360 "221\r\n"; 361 assertEquals( expectedResult.length(), result.length() ); 362 assertEquals( expectedResult, result ); 363 if ( testMailClient.isFailed() ) { 364 fail( testMailClient.getFailMessage() ); 365 } 366 } 367 368 369 372 public void testEmptyBody() { 373 ServerThread testMailServer = new ServerThread(); 374 Thread server = new Thread (testMailServer); 375 server.start(); 376 377 ClientThread testMailClient = new ClientThread(); 378 379 testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>"); 380 testMailClient.to("to@you.com"); 381 testMailClient.setSubject("Test subject"); 382 testMailClient.setMessage(""); 383 384 Thread client = new Thread (testMailClient); 385 client.start(); 386 387 try { 388 server.join(60 * 1000); client.join(30 * 1000); } catch (InterruptedException ie ) { 391 fail( "InterruptedException: " + ie ); 392 } 393 394 String result = testMailServer.getResult(); 395 String expectedResult = "220 test SMTP EmailTaskTest\r\n" + 396 "HELO " + local + "\r\n" + 397 "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" + 398 "MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" + 399 "250\r\n" + 400 "RCPT TO: <to@you.com>\r\n" + 401 "250\r\n" + 402 "DATA\r\n" + 403 "354\r\n" + 404 "Subject: Test subject\r\n" + 405 "From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" + 406 "To: to@you.com\r\n" + 407 "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" + 408 "\r\n" + 409 "\r\n" + 410 "\r\n" + 411 ".\r\n" + 412 "250\r\n" + 413 "QUIT\r\n" + 414 "221\r\n"; 415 assertEquals(expectedResult.length(), result.length()); 416 assertEquals(expectedResult, result); 417 if (testMailClient.isFailed()) { 418 fail(testMailClient.getFailMessage()); 419 } 420 } 421 422 423 428 public void testAsciiCharset() { 429 430 ServerThread testMailServer = new ServerThread(); 431 Thread server = new Thread (testMailServer); 432 server.start(); 433 434 ClientThread testMailClient = new ClientThread(); 435 436 testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>"); 437 testMailClient.to("Ceki G\u00fclc\u00fc <abuse@mail-abuse.org>"); 438 testMailClient.setSubject("Test subject"); 439 testMailClient.setMessage(""); 440 441 Thread client = new Thread (testMailClient); 442 client.start(); 443 444 try { 445 server.join(60 * 1000); client.join(30 * 1000); } catch (InterruptedException ie ) { 448 fail("InterruptedException: " + ie); 449 } 450 451 String result = testMailServer.getResult(); 452 String expectedResult = "220 test SMTP EmailTaskTest\r\n" + 453 "HELO " + local + "\r\n" + 454 "250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" + 455 "MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" + 456 "250\r\n" + 457 "RCPT TO: <abuse@mail-abuse.org>\r\n" + 458 "250\r\n" + 459 "DATA\r\n" + 460 "354\r\n" + 461 "Subject: Test subject\r\n" + 462 "From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" + 463 "To: Ceki G\u00fclc\u00fc <abuse@mail-abuse.org>\r\n" + 464 "X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" + 465 "\r\n" + 466 "\r\n" + 467 "\r\n" + 468 ".\r\n" + 469 "250\r\n" + 470 "QUIT\r\n" + 471 "221\r\n"; 472 ByteArrayOutputStream baos1 = new ByteArrayOutputStream (); 473 ByteArrayOutputStream baos2 = new ByteArrayOutputStream (); 474 PrintStream bos1 = new PrintStream (baos1, true); 475 PrintStream bos2 = new PrintStream (baos2, true); 476 477 bos1.print(expectedResult); 478 bos2.print(result); 479 480 assertEquals( "expected message length != actual message length " 481 + "in testAsciiCharset()", expectedResult.length(), result.length() ); 482 assertEquals( "baos1 and baos2 should be the same in testAsciiCharset()", 483 baos1.toString(), baos2.toString() ); if (testMailClient.isFailed()) { 485 fail(testMailClient.getFailMessage()); 486 } 487 } 488 489 490 491 492 495 private class ServerThread implements Runnable { 496 497 private StringBuffer sb = null; 498 private boolean loop = false; 499 ServerSocket ssock = null; 500 Socket sock = null; 501 BufferedWriter out = null; 502 BufferedReader in = null; 503 private boolean data = false; 505 public void run() { 506 507 try { 508 ssock = new ServerSocket (TEST_PORT); 509 sock = ssock.accept(); in = new BufferedReader ( new InputStreamReader ( 511 sock.getInputStream()) ); 512 out = new BufferedWriter ( new OutputStreamWriter ( 513 sock.getOutputStream() ) ); 514 sb = new StringBuffer (); 515 send( "220 test SMTP EmailTaskTest\r\n" ); 516 loop = true; 517 while ( loop ) { 518 String response = in.readLine(); 519 if ( response == null ) { 520 loop = false; 521 break; 522 } 523 sb.append( response + "\r\n" ); 524 525 if ( !data && response.startsWith( "HELO" ) ) { 526 send( "250 " + local + " Hello " + local + " " + 527 "[127.0.0.1], pleased to meet you\r\n" ); 528 } else if ( !data && response.startsWith("MAIL") ) { 529 send( "250\r\n" ); 530 } else if ( !data && response.startsWith("RCPT")) { 531 send( "250\r\n" ); 532 } else if (!data && response.startsWith("DATA")) { 533 send( "354\r\n" ); 534 data = true; 535 } else if (data && response.equals(".") ) { 536 send( "250\r\n" ); 537 data = false; 538 } else if (!data && response.startsWith("QUIT")) { 539 send( "221\r\n" ); 540 loop = false; 541 } else if (!data) { 542 send( "500 5.5.1 Command unrecognized: \"" + 545 response + "\"\r\n" ); 546 loop = false; 547 } else { 548 } 550 551 } } catch (IOException ioe) { 553 fail(); 554 } finally { 555 disconnect(); 556 } 557 } 558 559 private void send(String retmsg) throws IOException { 560 out.write( retmsg ); 561 out.flush(); 562 sb.append( retmsg ); 563 } 564 565 private void disconnect() { 566 if (out != null) { 567 try { 568 out.flush(); 569 out.close(); 570 out = null; 571 } catch (IOException e) { 572 } 574 } 575 if (in != null) { 576 try { 577 in.close(); 578 in = null; 579 } catch (IOException e) { 580 } 582 } 583 if (sock != null) { 584 try { 585 sock.close(); 586 sock = null; 587 } catch (IOException e) { 588 } 590 } 591 if (ssock != null) { 592 try { 593 ssock.close(); 594 ssock = null; 595 } catch (IOException e) { 596 } 598 } 599 } 600 601 public synchronized String getResult() { 602 loop = false; 603 return sb.toString(); 604 } 605 606 } 607 608 611 private class ClientThread implements Runnable { 612 613 private MailMessage msg; 614 private boolean fail = false; 615 private String failMessage = null; 616 617 protected String from = null; 618 protected String subject = null; 619 protected String message = null; 620 621 protected Vector replyToList = new Vector (); 622 protected Vector toList = new Vector (); 623 protected Vector ccList = new Vector (); 624 protected Vector bccList = new Vector (); 625 626 627 public void run() { 628 for (int i = 9; i > 0; i--) { 629 try { 630 msg = new MailMessage("localhost", TEST_PORT); 631 } catch (java.net.ConnectException ce) { 632 try { 633 Thread.sleep(10 * 1000); 634 } catch (InterruptedException ie) { 635 } 637 } catch (IOException ioe) { 638 fail = true; 639 failMessage = "IOException: " + ioe; 640 return; 641 } 642 if (msg != null) { 643 break; 644 } 645 } 646 647 if (msg == null) { 648 fail = true; 649 failMessage = "java.net.ConnectException: Connection refused"; 650 return; 651 } 652 653 try { 654 msg.from(from); 655 656 Enumeration e; 657 658 e = replyToList.elements(); 659 while (e.hasMoreElements()) { 660 msg.replyto(e.nextElement().toString()); 661 } 662 663 e = toList.elements(); 664 while (e.hasMoreElements()) { 665 msg.to(e.nextElement().toString()); 666 } 667 668 e = ccList.elements(); 669 while (e.hasMoreElements()) { 670 msg.cc(e.nextElement().toString()); 671 } 672 673 e = bccList.elements(); 674 while (e.hasMoreElements()) { 675 msg.bcc(e.nextElement().toString()); 676 } 677 678 if (subject != null) { 679 msg.setSubject(subject); 680 } 681 682 if (message != null ) { 683 PrintStream out = msg.getPrintStream(); 684 out.println( message ); 685 } 686 687 msg.sendAndClose(); 688 } catch (IOException ioe) { 689 fail = true; 690 failMessage = "IOException: " + ioe; 691 return; 692 } 693 } 694 695 public boolean isFailed() { 696 return fail; 697 } 698 699 public String getFailMessage() { 700 return failMessage; 701 } 702 703 public void replyTo(String replyTo) { 704 replyToList.add(replyTo); 705 } 706 707 public void to(String to) { 708 toList.add(to); 709 } 710 711 public void cc(String cc) { 712 ccList.add(cc); 713 } 714 715 public void bcc(String bcc) { 716 bccList.add(bcc); 717 } 718 719 public void setSubject(String subject) { 720 this.subject = subject; 721 } 722 723 public void from(String from) { 724 this.from = from; 725 } 726 727 public void setMessage(String message) { 728 this.message = message; 729 } 730 731 } 732 733 } 734 | Popular Tags |