View Javadoc

1   /*
2    * $Id: ImapCustomSeenHandlingTestCase.java 20799 2010-12-19 05:56:37Z 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.tck.functional.CountdownCallback;
14  import org.mule.tck.functional.FunctionalTestComponent;
15  import org.mule.transport.email.GreenMailUtilities;
16  import org.mule.transport.email.ImapConnector;
17  
18  import com.icegreen.greenmail.store.StoredMessage;
19  import com.icegreen.greenmail.user.UserManager;
20  
21  import java.util.List;
22  
23  import javax.mail.Flags;
24  import javax.mail.internet.MimeMessage;
25  
26  public class ImapCustomSeenHandlingTestCase extends AbstractEmailFunctionalTestCase
27  {
28      private CountdownCallback messageReceived = new CountdownCallback(1);
29  
30      public ImapCustomSeenHandlingTestCase()
31      {
32          super(false, ImapConnector.IMAP);
33  
34          // do not start Mule the IMAP server must be filled with appropriate test data first
35          setStartContext(false);
36      }
37  
38      @Override
39      protected String getConfigResources()
40      {
41          return "imap-custom-seen-flag.xml";
42      }
43  
44      public void testMessagesMatchingFilterGetCustomFlagSet() throws Exception
45      {
46          putMailMessageMatchingFilterIntoImapServer();
47          setupTestComponentListener();
48  
49          // now that everything is set up, start the context and let the polling begin
50          muleContext.start();
51  
52          // wait for the message to be processed
53          assertTrue(messageReceived.await(RECEIVE_TIMEOUT));
54          assertMessageIsFlagged();
55      }
56  
57      private void putMailMessageMatchingFilterIntoImapServer() throws Exception
58      {
59          String email = "barney@mule.org";
60  
61          UserManager userManager = server.getManagers().getUserManager();
62          MimeMessage message = GreenMailUtilities.toMessage(TEST_MESSAGE, email, null);
63          GreenMailUtilities.storeEmail(userManager, email, DEFAULT_USER, DEFAULT_PASSWORD, message);
64      }
65  
66      private void setupTestComponentListener() throws Exception
67      {
68          FunctionalTestComponent component = getFunctionalTestComponent("custom-flags");
69          assertNotNull(component);
70  
71          component.setEventCallback(messageReceived);
72      }
73  
74      private void assertMessageIsFlagged() throws Exception
75      {
76          // our superclass puts one message per default so we definitely have more than one message
77          // here. Just check if one is there with the required flag
78          boolean flaggedMessageFound = anyMessageIsFlagged();
79          assertTrue("no FLAGGED message found", flaggedMessageFound);
80      }
81  
82      private boolean anyMessageIsFlagged()
83      {
84          for (StoredMessage message : allImapMessages())
85          {
86              if (message.getFlags().contains(Flags.Flag.FLAGGED))
87              {
88                  return true;
89              }
90          }
91          return false;
92      }
93  
94      /**
95       * We cannot use <code>server.getReceivedMessages()</code> to obtain all messages from the
96       * IMAP server, the {@link MimeMessage} instances that are returned by that method do not
97       * have all the flags set.
98       */
99      @SuppressWarnings("unchecked")
100     private List<StoredMessage> allImapMessages()
101     {
102         return server.getManagers().getImapHostManager().getAllMessages();
103     }
104 }