1
2
3
4
5
6
7 package org.mule.transport.email.functional;
8
9 import org.mule.tck.probe.PollingProber;
10 import org.mule.tck.probe.Probe;
11 import org.mule.tck.probe.Prober;
12 import org.mule.util.FileUtils;
13 import org.mule.util.IOUtils;
14 import org.mule.util.SystemUtils;
15
16 import com.icegreen.greenmail.util.GreenMailUtil;
17
18 import java.io.File;
19 import java.io.InputStream;
20
21 import javax.mail.Message;
22
23 import org.junit.Test;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 public class ImapFunctionalWithAttachmentsTestCase extends AbstractEmailFunctionalTestCase
29 {
30 private static final String CONFIG_FILE = "email-attachment-save.xml";
31 private File saveDir;
32
33 public ImapFunctionalWithAttachmentsTestCase()
34 {
35 super(true, "imap", CONFIG_FILE, true);
36 setStartContext(false);
37
38
39 System.setProperty("mail.user", DEFAULT_USER);
40 System.setProperty("mail.password", DEFAULT_PASSWORD);
41 System.setProperty("mail.host", "localhost");
42
43 saveDir = new File(SystemUtils.JAVA_IO_TMPDIR, getClass().getName());
44 System.setProperty("mail.save.dir", saveDir.getAbsolutePath());
45 }
46
47 @Override
48 protected void doSetUp() throws Exception
49 {
50 super.doSetUp();
51
52
53 FileUtils.deleteTree(new File(System.getProperty("mail.save.dir")));
54 }
55
56 @Test
57 public void testRequest() throws Exception
58 {
59
60
61 muleContext.start();
62
63
64 assertEquals(1, server.getReceivedMessages().length);
65
66 InputStream inputstream = IOUtils.getResourceAsStream(CONFIG_FILE, getClass(), true, false);
67 assertNotNull(inputstream);
68 byte[] byteArray = IOUtils.toByteArray(inputstream);
69
70
71 GreenMailUtil.sendAttachmentEmail(DEFAULT_EMAIL, "joe", "email subject with attachments", "",
72 byteArray, "text/xml", CONFIG_FILE, "description", smtpSetup);
73 Message[] messages = server.getReceivedMessages();
74 assertEquals(2, messages.length);
75
76 assertAttachmentWasSaved();
77 }
78
79 private void assertAttachmentWasSaved()
80 {
81 Prober prober = new PollingProber(10000, 100);
82 prober.check(new Probe()
83 {
84 public boolean isSatisfied()
85 {
86
87
88
89
90
91 File savedFile = new File(saveDir, CONFIG_FILE);
92 return savedFile.exists();
93
94
95
96
97
98
99 }
100
101 public String describeFailure()
102 {
103 return "No attachments were saved";
104 }
105 });
106 }
107 }