View Javadoc

1   /*
2    * $Id: ImapFunctionalWithAttachmentsTestCase.java 22518 2011-07-22 07:00:22Z claude.mamo $
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 static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  import org.mule.tck.probe.PollingProber;
17  import org.mule.tck.probe.Probe;
18  import org.mule.tck.probe.Prober;
19  import org.mule.util.FileUtils;
20  import org.mule.util.IOUtils;
21  import org.mule.util.SystemUtils;
22  
23  import com.icegreen.greenmail.util.GreenMailUtil;
24  
25  import java.io.File;
26  import java.io.InputStream;
27  import java.util.Arrays;
28  import java.util.Collection;
29  
30  import javax.mail.Message;
31  
32  import org.junit.Test;
33  import org.junit.runners.Parameterized.Parameters;
34  
35  public class ImapFunctionalWithAttachmentsTestCase extends AbstractEmailFunctionalTestCase
36  {
37      private static final String CONFIG_FILE = "email-attachment-save.xml";
38      private File saveDir;
39  
40      public ImapFunctionalWithAttachmentsTestCase(ConfigVariant variant, String configResources)
41      {
42          super(variant, true, "imap", configResources, true);
43          setStartContext(false);
44          
45          // set up email properties for mule config
46          System.setProperty("mail.user", DEFAULT_USER);
47          System.setProperty("mail.password", DEFAULT_PASSWORD);
48          System.setProperty("mail.host", "localhost");
49          
50          saveDir = new File(SystemUtils.JAVA_IO_TMPDIR, getClass().getName());
51          System.setProperty("mail.save.dir", saveDir.getAbsolutePath());
52      }
53  
54      @Parameters
55      public static Collection<Object[]> parameters()
56      {
57          return Arrays.asList(new Object[][]{            
58              {ConfigVariant.FLOW, CONFIG_FILE}
59          });
60      }      
61      
62      @Override
63      protected void doSetUp() throws Exception
64      {
65          super.doSetUp();
66          
67          // clear out destination directory
68          FileUtils.deleteTree(new File(System.getProperty("mail.save.dir")));
69      }
70          
71      @Test
72      public void testRequest() throws Exception
73      {
74          // we do this since we need to set system mail properties before starting
75          // mule since they are referenced in there
76          muleContext.start();
77          
78          // a test email gets sent by default
79          assertEquals(1, server.getReceivedMessages().length);
80  
81          InputStream inputstream = IOUtils.getResourceAsStream(CONFIG_FILE, getClass(), true, false);
82          assertNotNull(inputstream);
83          byte[] byteArray = IOUtils.toByteArray(inputstream);
84  
85          // send a file attachment email
86          GreenMailUtil.sendAttachmentEmail(DEFAULT_EMAIL, "joe", "email subject with attachments", "",
87              byteArray, "text/xml", CONFIG_FILE, "description", smtpSetup);
88          Message[] messages = server.getReceivedMessages();
89          assertEquals(2, messages.length);
90  
91          assertAttachmentWasSaved();
92      }
93      
94      private void assertAttachmentWasSaved()
95      {
96          Prober prober = new PollingProber(10000, 100);
97          prober.check(new Probe()
98          {
99              public boolean isSatisfied()
100             {
101                 // FIXME DZ: don't know why these are empty, so just compare the saved email
102                 // file to the expected content
103                 // assertEquals(messages[1].ATTACHMENT, messages[1].getDisposition());
104                 // assertEquals("email-attachment-save.xml", messages[1].getFileName());
105 
106                 File savedFile = new File(saveDir, CONFIG_FILE);
107                 return savedFile.exists();
108 
109                 // TODO DZ: compare the source and target files and makes sure they are the
110                 // same
111                 // FIXME DF: when you send an attachment email with text in the body, this
112                 // mule config saves the body in it's own file; how do you just save the file
113                 // attachment?
114             }
115 
116             public String describeFailure()
117             {
118                 return "No attachments were saved";
119             }
120         });
121     }
122 }