FileDocCategorySizeDatePackage
MailMessageTest.javaAPI DocApache Ant 1.7024195Wed Dec 13 06:16:20 GMT 2006org.apache.tools.mail

MailMessageTest

public class MailMessageTest extends TestCase
JUnit 3 testcases for org.apache.tools.mail.MailMessage.
since
Ant 1.6

Fields Summary
private static int
TEST_PORT
private String
local
Constructors Summary
public MailMessageTest(String name)


       
        super(name);
    
Methods Summary
public voidsetUp()

        try {
            local = InetAddress.getLocalHost().getHostName();
        } catch (java.net.UnknownHostException uhe) {
            // ignore
        }
    
public voidtestAPIExample()
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 voidtestAsciiCharset()
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 voidtestBccOnly()
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 voidtestCcOnly()
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 voidtestEmptyBody()
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 voidtestNoSubject()
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 voidtestToOnly()
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());
        }