1   /*
2    * $Id: EmailRoundTripTestCase.java 10789 2008-02-12 20:04:43Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.email;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.FunctionalTestCase;
17  
18  import javax.mail.internet.MimeMessage;
19  
20  /**
21   * This demonstrates "round trip" processing of email - an email is pulled from a POP
22   * server and then sent to an SMTP server.  While within Mule the message is serialized
23   * as RFC822 encoded bytes (this would let the message be transmitted over JMS etc).
24   *
25   * <p>The email servers for the test are managed by the greenMailSupport instance.
26   * The Mule services (defined in email-round-trip-test.xml) are started by the test framework.
27   * So all we need to do here is test that the message is handled correctly.</p>
28   */
29  public class EmailRoundTripTestCase extends FunctionalTestCase
30  {
31  
32      public static final long WAIT_MS = 3000L;
33  
34      // this places the SMTP server at 62000 and POP at 62002
35      private AbstractGreenMailSupport greenMailSupport = new FixedPortGreenMailSupport(62000);
36  
37      protected String getConfigResources()
38      {
39          return "email-round-trip-test.xml";
40      }
41  
42      public void testRoundTrip() throws MuleException, InterruptedException
43      {
44          // first, check that the conversion happened - we should have a copy of
45          // the message as rfc822 encoded bytes on vm://rfc822
46          MuleClient client = new MuleClient();
47          MuleMessage message = client.request("vm://rfc822?connector=queue", WAIT_MS);
48          assertTrue(message.getPayload() instanceof byte[]);
49  
50          // next, check that the email is received in the server
51          greenMailSupport.getServers().waitForIncomingEmail(WAIT_MS, 1);
52          MimeMessage[] messages = greenMailSupport.getServers().getReceivedMessages();
53          assertNotNull("did not receive any messages", messages);
54          assertEquals("did not receive 1 mail", 1, messages.length);
55      }
56  
57      /**
58       * Start the servers when the test starts
59       * @throws Exception
60       */
61      // @Override
62      protected void suitePreSetUp() throws Exception
63      {
64          greenMailSupport.startServers();
65          greenMailSupport.createBobAndStoreEmail(greenMailSupport.getValidMessage(AbstractGreenMailSupport.ALICE_EMAIL));
66      }
67  
68      /**
69       * Stop the servers when the test ends
70       * @throws Exception
71       */
72      // @Override
73      protected void suitePostTearDown() throws Exception
74      {
75          greenMailSupport.stopServers();
76      }
77  
78  }