View Javadoc

1   /*
2    * $Id: ImapMessageRequesterTestCase.java 22450 2011-07-19 08:20:41Z dirk.olmes $
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.functional;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.junit4.FunctionalTestCase;
16  import org.mule.tck.junit4.rule.DynamicPort;
17  import org.mule.transport.email.GreenMailUtilities;
18  import org.mule.transport.email.ImapConnector;
19  
20  import com.icegreen.greenmail.util.GreenMail;
21  import com.icegreen.greenmail.util.ServerSetup;
22  
23  import org.junit.Rule;
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  
29  public class ImapMessageRequesterTestCase extends FunctionalTestCase
30  {
31      private static final String EMAIL = "bob@example.com";
32      private static final String MESSAGE = "Test email message";
33      private static final String PASSWORD = "password";
34      private static int PORT = -1;
35      private static final String USER = "bob";
36  
37      private GreenMail server;
38  
39      @Rule
40      public DynamicPort dynamicPort = new DynamicPort("port1");
41  
42  
43      @Override
44      protected String getConfigResources()
45      {
46          return "imap-message-requester.xml";
47      }
48  
49      @Override
50      protected void doSetUp() throws Exception
51      {
52          super.doSetUp();
53          PORT = dynamicPort.getNumber();
54          startGreenmailServer();
55      }
56  
57      private void startGreenmailServer() throws Exception
58      {
59          ServerSetup setup = new ServerSetup(PORT, null, ImapConnector.IMAP);
60          server = new GreenMail(setup);
61          server.start();
62          GreenMailUtilities.storeEmail(server.getManagers().getUserManager(), EMAIL, USER, PASSWORD,
63              GreenMailUtilities.toMessage(MESSAGE, EMAIL, null));
64      }
65  
66      @Override
67      protected void doTearDown() throws Exception
68      {
69          server.stop();
70          super.doTearDown();
71      }
72  
73      @Test
74      public void testMessageRequester() throws Exception
75      {
76          String imapUri = String.format("imap://%1s:%2s@localhost:%3d/INBOX", USER, PASSWORD, PORT);
77          
78          MuleClient client = new MuleClient(muleContext);
79          MuleMessage message = client.request(imapUri, RECEIVE_TIMEOUT);
80          
81          assertNotNull(message);
82          assertEquals(MESSAGE, message.getPayload());
83      }
84  
85  }
86  
87