1   /*
2    * $Id: RoundTripTestCase.java 7823 2007-08-10 15:26:59Z dirk.olmes $
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 MuleSource MPL
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.email;
12  
13  import org.mule.extras.client.MuleClient;
14  import org.mule.tck.FunctionalTestCase;
15  import org.mule.umo.UMOMessage;
16  import org.mule.umo.UMOException;
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 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 RoundTripTestCase 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 "round-trip-test.xml";
40      }
41  
42      public void testRoundTrip() throws UMOException, 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          UMOMessage message = client.receive("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      protected void doPreFunctionalSetUp() throws Exception
62      {
63          greenMailSupport.startServers();
64          greenMailSupport.createBobAndStoreEmail(greenMailSupport.getValidMessage(AbstractGreenMailSupport.ALICE_EMAIL));
65      }
66  
67      /**
68       * Stop the servers when the test ends
69       * @throws Exception
70       */
71      protected void doFunctionalTearDown() throws Exception
72      {
73          greenMailSupport.stopServers();
74      }
75  
76  }