1
2
3
4
5
6
7
8
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
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
68 FileUtils.deleteTree(new File(System.getProperty("mail.save.dir")));
69 }
70
71 @Test
72 public void testRequest() throws Exception
73 {
74
75
76 muleContext.start();
77
78
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
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
102
103
104
105
106 File savedFile = new File(saveDir, CONFIG_FILE);
107 return savedFile.exists();
108
109
110
111
112
113
114 }
115
116 public String describeFailure()
117 {
118 return "No attachments were saved";
119 }
120 });
121 }
122 }