View Javadoc

1   /*
2    * $Id: EmailRoundTripTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.DynamicPortTestCase;
17  import org.mule.transport.email.functional.AbstractEmailFunctionalTestCase;
18  
19  import javax.mail.internet.MimeMessage;
20  
21  import org.junit.Test;
22  
23  /**
24   * This demonstrates "round trip" processing of email - an email is pulled from a POP
25   * server and then sent to an SMTP server.  While within Mule the message is serialized
26   * as RFC822 encoded bytes (this would let the message be transmitted over JMS etc).
27   *
28   * <p>The email servers for the test are managed by the greenMailSupport instance.
29   * The Mule services (defined in email-round-trip-test.xml) are started by the test framework.
30   * So all we need to do here is test that the message is handled correctly.</p>
31   */
32  public class EmailRoundTripTestCase extends DynamicPortTestCase
33  {
34      private AbstractGreenMailSupport greenMailSupport = null;
35  
36      protected String getConfigResources()
37      {
38          return "email-round-trip-test.xml";
39      }
40  
41      /**
42       * Start the servers when the test starts
43       * @throws Exception
44       */
45      @Override
46      protected void suitePreSetUp() throws Exception
47      {
48          // see AbstractGreenMailSupport for all the ports this test uses and their order 
49          greenMailSupport = new FixedPortGreenMailSupport(getPorts().get(1));
50          greenMailSupport.startServers(getPorts());
51          greenMailSupport.createBobAndStoreEmail(greenMailSupport.getValidMessage(AbstractGreenMailSupport.ALICE_EMAIL));
52      }
53  
54      /**
55       * Stop the servers when the test ends
56       * @throws Exception
57       */
58      @Override
59      protected void suitePostTearDown() throws Exception
60      {
61          greenMailSupport.stopServers();
62      }
63  
64      @Test
65      public void testRoundTrip() throws MuleException, InterruptedException
66      {
67          // first, check that the conversion happened - we should have a copy of
68          // the message as rfc822 encoded bytes on vm://rfc822
69          MuleClient client = new MuleClient(muleContext);
70          MuleMessage message = client.request("vm://rfc822", RECEIVE_TIMEOUT);
71          assertTrue(message.getPayload() instanceof byte[]);
72  
73          // next, check that the email is received in the server
74          greenMailSupport.getServers().waitForIncomingEmail(AbstractEmailFunctionalTestCase.DELIVERY_DELAY_MS, 1);
75          MimeMessage[] messages = greenMailSupport.getServers().getReceivedMessages();
76          assertNotNull("did not receive any messages", messages);
77          assertEquals("did not receive 1 mail", 1, messages.length);
78      }
79  
80      @Override
81      protected int getNumPortsToFind()
82      {
83          // see AbstractGreenMailSupport for all the services this test starts
84          return 6;
85      }
86  }