| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.mail.integration; |
| 21 |
|
|
| 22 |
|
import java.security.Security; |
| 23 |
|
import java.util.Arrays; |
| 24 |
|
import java.util.Collections; |
| 25 |
|
import java.util.Properties; |
| 26 |
|
|
| 27 |
|
import javax.inject.Provider; |
| 28 |
|
import javax.mail.BodyPart; |
| 29 |
|
import javax.mail.Message.RecipientType; |
| 30 |
|
import javax.mail.Multipart; |
| 31 |
|
import javax.mail.Session; |
| 32 |
|
import javax.mail.internet.InternetAddress; |
| 33 |
|
import javax.mail.internet.MimeMessage; |
| 34 |
|
import javax.mail.internet.MimeMultipart; |
| 35 |
|
|
| 36 |
|
import org.junit.After; |
| 37 |
|
import org.junit.Before; |
| 38 |
|
import org.junit.Rule; |
| 39 |
|
import org.junit.Test; |
| 40 |
|
import org.mockito.Mockito; |
| 41 |
|
import org.xwiki.bridge.event.ApplicationReadyEvent; |
| 42 |
|
import org.xwiki.component.phase.Disposable; |
| 43 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
| 44 |
|
import org.xwiki.context.Execution; |
| 45 |
|
import org.xwiki.context.ExecutionContext; |
| 46 |
|
import org.xwiki.context.ExecutionContextManager; |
| 47 |
|
import org.xwiki.environment.internal.EnvironmentConfiguration; |
| 48 |
|
import org.xwiki.environment.internal.StandardEnvironment; |
| 49 |
|
import org.xwiki.mail.MailSender; |
| 50 |
|
import org.xwiki.mail.MailSenderConfiguration; |
| 51 |
|
import org.xwiki.mail.MimeBodyPartFactory; |
| 52 |
|
import org.xwiki.mail.XWikiAuthenticator; |
| 53 |
|
import org.xwiki.mail.internal.DefaultMailSender; |
| 54 |
|
import org.xwiki.mail.internal.FileSystemMailContentStore; |
| 55 |
|
import org.xwiki.mail.internal.MemoryMailListener; |
| 56 |
|
import org.xwiki.mail.internal.configuration.DefaultMailSenderConfiguration; |
| 57 |
|
import org.xwiki.mail.internal.factory.attachment.AttachmentMimeBodyPartFactory; |
| 58 |
|
import org.xwiki.mail.internal.factory.text.TextMimeBodyPartFactory; |
| 59 |
|
import org.xwiki.mail.internal.thread.MailSenderInitializerListener; |
| 60 |
|
import org.xwiki.mail.internal.thread.PrepareMailQueueManager; |
| 61 |
|
import org.xwiki.mail.internal.thread.PrepareMailRunnable; |
| 62 |
|
import org.xwiki.mail.internal.thread.SendMailQueueManager; |
| 63 |
|
import org.xwiki.mail.internal.thread.SendMailRunnable; |
| 64 |
|
import org.xwiki.mail.internal.thread.context.Copier; |
| 65 |
|
import org.xwiki.model.ModelContext; |
| 66 |
|
import org.xwiki.model.reference.WikiReference; |
| 67 |
|
import org.xwiki.observation.EventListener; |
| 68 |
|
import org.xwiki.test.annotation.BeforeComponent; |
| 69 |
|
import org.xwiki.test.annotation.ComponentList; |
| 70 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
| 71 |
|
|
| 72 |
|
import com.icegreen.greenmail.junit.GreenMailRule; |
| 73 |
|
import com.icegreen.greenmail.util.DummySSLSocketFactory; |
| 74 |
|
import com.icegreen.greenmail.util.ServerSetupTest; |
| 75 |
|
import com.xpn.xwiki.XWikiContext; |
| 76 |
|
|
| 77 |
|
import static org.junit.Assert.assertEquals; |
| 78 |
|
import static org.mockito.Mockito.when; |
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
@version |
| 85 |
|
@since |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
@ComponentList({ |
| 89 |
|
MailSenderInitializerListener.class, |
| 90 |
|
TextMimeBodyPartFactory.class, |
| 91 |
|
AttachmentMimeBodyPartFactory.class, |
| 92 |
|
StandardEnvironment.class, |
| 93 |
|
DefaultMailSender.class, |
| 94 |
|
MemoryMailListener.class, |
| 95 |
|
SendMailRunnable.class, |
| 96 |
|
PrepareMailRunnable.class, |
| 97 |
|
PrepareMailQueueManager.class, |
| 98 |
|
SendMailQueueManager.class, |
| 99 |
|
FileSystemMailContentStore.class |
| 100 |
|
}) |
| 101 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (54) |
Complexity: 5 |
Complexity Density: 0.1 |
|
| 102 |
|
public class AuthenticatingIntegrationTest |
| 103 |
|
{ |
| 104 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 105 |
1 |
static {... |
| 106 |
1 |
Security.setProperty("ssl.SocketFactory.provider", DummySSLSocketFactory.class.getName()); |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
@Rule |
| 110 |
|
public GreenMailRule mail = new GreenMailRule(ServerSetupTest.SMTPS); |
| 111 |
|
|
| 112 |
|
@Rule |
| 113 |
|
public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule(); |
| 114 |
|
|
| 115 |
|
private MailSenderConfiguration configuration; |
| 116 |
|
|
| 117 |
|
private MimeBodyPartFactory<String> defaultBodyPartFactory; |
| 118 |
|
|
| 119 |
|
private MailSender sender; |
| 120 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
|
| 121 |
1 |
@BeforeComponent... |
| 122 |
|
public void registerConfiguration() throws Exception |
| 123 |
|
{ |
| 124 |
1 |
Properties properties = new Properties(); |
| 125 |
1 |
properties.setProperty("mail.smtp.starttls.enable", "true"); |
| 126 |
|
|
| 127 |
|
|
| 128 |
1 |
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); |
| 129 |
|
|
| 130 |
1 |
this.configuration = |
| 131 |
|
new TestMailSenderConfiguration(this.mail.getSmtps().getPort(), "peter", "password", properties); |
| 132 |
1 |
this.componentManager.registerComponent(MailSenderConfiguration.class, this.configuration); |
| 133 |
|
|
| 134 |
|
|
| 135 |
1 |
ModelContext modelContext = this.componentManager.registerMockComponent(ModelContext.class); |
| 136 |
1 |
when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki")); |
| 137 |
|
|
| 138 |
1 |
Provider<XWikiContext> xwikiContextProvider = |
| 139 |
|
this.componentManager.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
| 140 |
1 |
when(xwikiContextProvider.get()).thenReturn(Mockito.mock(XWikiContext.class)); |
| 141 |
|
|
| 142 |
1 |
this.componentManager.registerMockComponent(ExecutionContextManager.class); |
| 143 |
1 |
this.componentManager.registerMockComponent(Execution.class); |
| 144 |
|
|
| 145 |
1 |
this.componentManager.registerMockComponent(new DefaultParameterizedType(null, Copier.class, |
| 146 |
|
ExecutionContext.class)); |
| 147 |
|
|
| 148 |
1 |
EnvironmentConfiguration environmentConfiguration = |
| 149 |
|
this.componentManager.registerMockComponent(EnvironmentConfiguration.class); |
| 150 |
1 |
when(environmentConfiguration.getPermanentDirectoryPath()).thenReturn(System.getProperty("java.io.tmpdir")); |
| 151 |
|
} |
| 152 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 153 |
1 |
@Before... |
| 154 |
|
public void initialize() throws Exception |
| 155 |
|
{ |
| 156 |
|
|
| 157 |
1 |
this.mail.setUser("peter@doe.com", "peter", "password"); |
| 158 |
|
|
| 159 |
1 |
this.defaultBodyPartFactory = |
| 160 |
|
this.componentManager.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, |
| 161 |
|
String.class)); |
| 162 |
1 |
this.sender = this.componentManager.getInstance(MailSender.class); |
| 163 |
|
|
| 164 |
|
|
| 165 |
1 |
MailSenderInitializerListener listener = |
| 166 |
|
this.componentManager.getInstance(EventListener.class, MailSenderInitializerListener.LISTENER_NAME); |
| 167 |
1 |
listener.onEvent(new ApplicationReadyEvent(), null, null); |
| 168 |
|
} |
| 169 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 170 |
1 |
@After... |
| 171 |
|
public void cleanUp() throws Exception |
| 172 |
|
{ |
| 173 |
|
|
| 174 |
|
|
| 175 |
1 |
Disposable listener = |
| 176 |
|
this.componentManager.getInstance(EventListener.class, MailSenderInitializerListener.LISTENER_NAME); |
| 177 |
1 |
listener.dispose(); |
| 178 |
|
} |
| 179 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
| 180 |
1 |
@Test... |
| 181 |
|
public void sendTextMail() throws Exception |
| 182 |
|
{ |
| 183 |
|
|
| 184 |
1 |
Execution execution = this.componentManager.getInstance(Execution.class); |
| 185 |
1 |
ExecutionContext executionContext = new ExecutionContext(); |
| 186 |
1 |
XWikiContext xContext = new XWikiContext(); |
| 187 |
1 |
xContext.setWikiId("wiki"); |
| 188 |
1 |
executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext); |
| 189 |
1 |
when(execution.getContext()).thenReturn(executionContext); |
| 190 |
|
|
| 191 |
1 |
Copier<ExecutionContext> executionContextCloner = |
| 192 |
|
this.componentManager.getInstance(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class)); |
| 193 |
|
|
| 194 |
1 |
when(executionContextCloner.copy(executionContext)).thenReturn(executionContext); |
| 195 |
|
|
| 196 |
|
|
| 197 |
1 |
Properties properties = this.configuration.getAllProperties(); |
| 198 |
1 |
assertEquals("true", properties.getProperty(DefaultMailSenderConfiguration.JAVAMAIL_SMTP_AUTH)); |
| 199 |
1 |
Session session = Session.getInstance(properties, new XWikiAuthenticator(this.configuration)); |
| 200 |
|
|
| 201 |
|
|
| 202 |
1 |
MimeMessage message = new MimeMessage(session); |
| 203 |
1 |
message.setSubject("subject"); |
| 204 |
1 |
message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com")); |
| 205 |
|
|
| 206 |
|
|
| 207 |
1 |
Multipart multipart = new MimeMultipart("mixed"); |
| 208 |
|
|
| 209 |
1 |
multipart.addBodyPart(this.defaultBodyPartFactory.create("some text here", |
| 210 |
|
Collections.<String, Object>singletonMap("mimetype", "text/plain"))); |
| 211 |
1 |
message.setContent(multipart); |
| 212 |
|
|
| 213 |
|
|
| 214 |
1 |
this.sender.sendAsynchronously(Arrays.asList(message), session, null); |
| 215 |
|
|
| 216 |
|
|
| 217 |
1 |
this.mail.waitForIncomingEmail(10000L, 1); |
| 218 |
1 |
MimeMessage[] messages = this.mail.getReceivedMessages(); |
| 219 |
|
|
| 220 |
1 |
assertEquals(1, messages.length); |
| 221 |
1 |
assertEquals("subject", messages[0].getHeader("Subject", null)); |
| 222 |
1 |
assertEquals("john@doe.com", messages[0].getHeader("To", null)); |
| 223 |
|
|
| 224 |
1 |
assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount()); |
| 225 |
|
|
| 226 |
1 |
BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0); |
| 227 |
1 |
assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]); |
| 228 |
1 |
assertEquals("some text here", textBodyPart.getContent()); |
| 229 |
|
} |
| 230 |
|
} |