Methods Summary |
---|
public void | setUp()
try {
local = InetAddress.getLocalHost().getHostName();
} catch (java.net.UnknownHostException uhe) {
// ignore
}
|
public void | testAPIExample()Test an example that is similar to the one given in the API
If this testcase takes >90s to complete, it is very likely that
the two threads are blocked waiting for each other and Thread.join()
timed out.
ServerThread testMailServer = new ServerThread();
Thread server = new Thread(testMailServer);
server.start();
ClientThread testMailClient = new ClientThread();
testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>");
testMailClient.to("to@you.com");
testMailClient.cc("cc1@you.com");
testMailClient.cc("cc2@you.com");
testMailClient.bcc("bcc@you.com");
testMailClient.setSubject("Test subject");
testMailClient.setMessage( "test line 1\n" +
"test line 2" );
Thread client = new Thread(testMailClient);
client.start();
try {
server.join(60 * 1000); // 60s
client.join(30 * 1000); // a further 30s
} catch (InterruptedException ie ) {
fail( "InterruptedException: " + ie );
}
String result = testMailServer.getResult();
String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
"HELO " + local + "\r\n" +
"250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
"MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" +
"250\r\n" +
"RCPT TO: <to@you.com>\r\n" +
"250\r\n" +
"RCPT TO: <cc1@you.com>\r\n" +
"250\r\n" +
"RCPT TO: <cc2@you.com>\r\n" +
"250\r\n" +
"RCPT TO: <bcc@you.com>\r\n" +
"250\r\n" +
"DATA\r\n" +
"354\r\n" +
"Subject: Test subject\r\n" +
"From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" +
"To: to@you.com\r\n" +
"Cc: cc1@you.com, cc2@you.com\r\n" +
"X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
"\r\n" +
"test line 1\r\n" +
"test line 2\r\n" +
"\r\n" +
".\r\n" +
"250\r\n" +
"QUIT\r\n" +
"221\r\n";
for (int icounter = 0; icounter<expectedResult.length(); icounter++) {
if (icounter < result.length()) {
if (expectedResult.charAt(icounter) != result.charAt(icounter)) {
System.out.println("posit " + icounter + " expected "
+ expectedResult.charAt(icounter)
+ " result " + result.charAt(icounter));
}
}
}
if (expectedResult.length()>result.length()) {
System.out.println("excedent of expected result "
+ expectedResult.substring(result.length()));
}
if (expectedResult.length()<result.length()) {
System.out.println("excedent of result "
+ result.substring(expectedResult.length()));
}
assertEquals(expectedResult.length(), result.length());
assertEquals(expectedResult, result); // order of headers cannot be guaranteed
if (testMailClient.isFailed()) {
fail(testMailClient.getFailMessage());
}
|
public void | testAsciiCharset()Test a MailMessage with US-ASCII character set
The next four testcase can be kinda hard to debug as Ant will often
print the junit failure in US-ASCII.
ServerThread testMailServer = new ServerThread();
Thread server = new Thread(testMailServer);
server.start();
ClientThread testMailClient = new ClientThread();
testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>");
testMailClient.to("Ceki G\u00fclc\u00fc <abuse@mail-abuse.org>");
testMailClient.setSubject("Test subject");
testMailClient.setMessage("");
Thread client = new Thread(testMailClient);
client.start();
try {
server.join(60 * 1000); // 60s
client.join(30 * 1000); // a further 30s
} catch (InterruptedException ie ) {
fail("InterruptedException: " + ie);
}
String result = testMailServer.getResult();
String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
"HELO " + local + "\r\n" +
"250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
"MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" +
"250\r\n" +
"RCPT TO: <abuse@mail-abuse.org>\r\n" +
"250\r\n" +
"DATA\r\n" +
"354\r\n" +
"Subject: Test subject\r\n" +
"From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" +
"To: Ceki G\u00fclc\u00fc <abuse@mail-abuse.org>\r\n" +
"X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
".\r\n" +
"250\r\n" +
"QUIT\r\n" +
"221\r\n";
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
PrintStream bos1 = new PrintStream(baos1, true);
PrintStream bos2 = new PrintStream(baos2, true);
bos1.print(expectedResult);
bos2.print(result);
assertEquals( "expected message length != actual message length "
+ "in testAsciiCharset()", expectedResult.length(), result.length() );
assertEquals( "baos1 and baos2 should be the same in testAsciiCharset()",
baos1.toString(), baos2.toString() ); // order of headers cannot be guaranteed
if (testMailClient.isFailed()) {
fail(testMailClient.getFailMessage());
}
|
public void | testBccOnly()Test a MailMessage with no to or cc lines
ServerThread testMailServer = new ServerThread();
Thread server = new Thread(testMailServer);
server.start();
ClientThread testMailClient = new ClientThread();
testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>");
testMailClient.bcc("bcc@you.com");
testMailClient.setSubject("Test subject");
testMailClient.setMessage( "test line 1\n" +
"test line 2" );
Thread client = new Thread(testMailClient);
client.start();
try {
server.join(60 * 1000); // 60s
client.join(30 * 1000); // a further 30s
} catch (InterruptedException ie ) {
fail( "InterruptedException: " + ie );
}
String result = testMailServer.getResult();
String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
"HELO " + local + "\r\n" +
"250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
"MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" +
"250\r\n" +
"RCPT TO: <bcc@you.com>\r\n" +
"250\r\n" +
"DATA\r\n" +
"354\r\n" +
"Subject: Test subject\r\n" +
"From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" +
"X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
"\r\n" +
"test line 1\r\n" +
"test line 2\r\n" +
"\r\n" +
".\r\n" +
"250\r\n" +
"QUIT\r\n" +
"221\r\n";
assertEquals( expectedResult.length(), result.length() );
assertEquals( expectedResult, result );
if ( testMailClient.isFailed() ) {
fail( testMailClient.getFailMessage() );
}
|
public void | testCcOnly()Test a MailMessage with no to or bcc lines
ServerThread testMailServer = new ServerThread();
Thread server = new Thread(testMailServer);
server.start();
ClientThread testMailClient = new ClientThread();
testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>");
testMailClient.cc("cc@you.com");
testMailClient.setSubject("Test subject");
testMailClient.setMessage( "test line 1\n" +
"test line 2" );
Thread client = new Thread(testMailClient);
client.start();
try {
server.join(60 * 1000); // 60s
client.join(30 * 1000); // a further 30s
} catch (InterruptedException ie ) {
fail( "InterruptedException: " + ie );
}
String result = testMailServer.getResult();
String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
"HELO " + local + "\r\n" +
"250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
"MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" +
"250\r\n" +
"RCPT TO: <cc@you.com>\r\n" +
"250\r\n" +
"DATA\r\n" +
"354\r\n" +
"Subject: Test subject\r\n" +
"From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" +
"Cc: cc@you.com\r\n" +
"X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
"\r\n" +
"test line 1\r\n" +
"test line 2\r\n" +
"\r\n" +
".\r\n" +
"250\r\n" +
"QUIT\r\n" +
"221\r\n";
assertEquals(expectedResult.length(), result.length());
assertEquals(expectedResult, result);
if (testMailClient.isFailed()) {
fail(testMailClient.getFailMessage());
}
|
public void | testEmptyBody()Test a MailMessage with empty body message
ServerThread testMailServer = new ServerThread();
Thread server = new Thread(testMailServer);
server.start();
ClientThread testMailClient = new ClientThread();
testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>");
testMailClient.to("to@you.com");
testMailClient.setSubject("Test subject");
testMailClient.setMessage("");
Thread client = new Thread(testMailClient);
client.start();
try {
server.join(60 * 1000); // 60s
client.join(30 * 1000); // a further 30s
} catch (InterruptedException ie ) {
fail( "InterruptedException: " + ie );
}
String result = testMailServer.getResult();
String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
"HELO " + local + "\r\n" +
"250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
"MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" +
"250\r\n" +
"RCPT TO: <to@you.com>\r\n" +
"250\r\n" +
"DATA\r\n" +
"354\r\n" +
"Subject: Test subject\r\n" +
"From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" +
"To: to@you.com\r\n" +
"X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
".\r\n" +
"250\r\n" +
"QUIT\r\n" +
"221\r\n";
assertEquals(expectedResult.length(), result.length());
assertEquals(expectedResult, result);
if (testMailClient.isFailed()) {
fail(testMailClient.getFailMessage());
}
|
public void | testNoSubject()Test a MailMessage with no subject line
Subject is an optional field (RFC 822 s4.1)
ServerThread testMailServer = new ServerThread();
Thread server = new Thread(testMailServer);
server.start();
ClientThread testMailClient = new ClientThread();
testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>");
testMailClient.to("to@you.com");
testMailClient.setMessage( "test line 1\n" +
"test line 2" );
Thread client = new Thread(testMailClient);
client.start();
try {
server.join(60 * 1000); // 60s
client.join(30 * 1000); // a further 30s
} catch (InterruptedException ie ) {
fail( "InterruptedException: " + ie );
}
String result = testMailServer.getResult();
String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
"HELO " + local + "\r\n" +
"250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
"MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" +
"250\r\n" +
"RCPT TO: <to@you.com>\r\n" +
"250\r\n" +
"DATA\r\n" +
"354\r\n" +
"From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" +
"To: to@you.com\r\n" +
"X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
"\r\n" +
"test line 1\r\n" +
"test line 2\r\n" +
"\r\n" +
".\r\n" +
"250\r\n" +
"QUIT\r\n" +
"221\r\n";
assertEquals( expectedResult.length(), result.length() );
assertEquals( expectedResult, result );
if ( testMailClient.isFailed() ) {
fail( testMailClient.getFailMessage() );
}
|
public void | testToOnly()Test a MailMessage with no cc or bcc lines
ServerThread testMailServer = new ServerThread();
Thread server = new Thread(testMailServer);
server.start();
ClientThread testMailClient = new ClientThread();
testMailClient.from("Mail Message <EmailTaskTest@ant.apache.org>");
testMailClient.to("to@you.com");
testMailClient.setSubject("Test subject");
testMailClient.setMessage( "test line 1\n" +
"test line 2" );
Thread client = new Thread(testMailClient);
client.start();
try {
server.join(60 * 1000); // 60s
client.join(30 * 1000); // a further 30s
} catch (InterruptedException ie ) {
fail("InterruptedException: " + ie);
}
String result = testMailServer.getResult();
String expectedResult = "220 test SMTP EmailTaskTest\r\n" +
"HELO " + local + "\r\n" +
"250 " + local + " Hello " + local + " [127.0.0.1], pleased to meet you\r\n" +
"MAIL FROM: <EmailTaskTest@ant.apache.org>\r\n" +
"250\r\n" +
"RCPT TO: <to@you.com>\r\n" +
"250\r\n" +
"DATA\r\n" +
"354\r\n" +
"Subject: Test subject\r\n" +
"From: Mail Message <EmailTaskTest@ant.apache.org>\r\n" +
"To: to@you.com\r\n" +
"X-Mailer: org.apache.tools.mail.MailMessage (ant.apache.org)\r\n" +
"\r\n" +
"test line 1\r\n" +
"test line 2\r\n" +
"\r\n" +
".\r\n" +
"250\r\n" +
"QUIT\r\n" +
"221\r\n";
assertEquals(expectedResult.length(), result.length());
assertEquals(expectedResult, result); // order of headers cannot be guaranteed
if (testMailClient.isFailed()) {
fail(testMailClient.getFailMessage());
}
|