| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.plugin.mailsender; |
| 21 |
|
|
| 22 |
|
import java.io.IOException; |
| 23 |
|
import java.util.List; |
| 24 |
|
import java.util.Properties; |
| 25 |
|
|
| 26 |
|
import javax.mail.Message; |
| 27 |
|
import javax.mail.MessagingException; |
| 28 |
|
import javax.mail.internet.InternetAddress; |
| 29 |
|
|
| 30 |
|
import org.jmock.Expectations; |
| 31 |
|
import org.jmock.Mock; |
| 32 |
|
import org.jmock.Mockery; |
| 33 |
|
import org.jmock.integration.junit4.JUnit4Mockery; |
| 34 |
|
import org.jvnet.mock_javamail.Mailbox; |
| 35 |
|
import org.xwiki.mail.MailSenderConfiguration; |
| 36 |
|
|
| 37 |
|
import com.xpn.xwiki.XWiki; |
| 38 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 39 |
|
import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase; |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
@link |
| 43 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (63) |
Complexity: 5 |
Complexity Density: 0.09 |
|
| 44 |
|
public class MailSenderApiTest extends AbstractBridgedXWikiComponentTestCase |
| 45 |
|
{ |
| 46 |
|
private Mock mockXWiki; |
| 47 |
|
|
| 48 |
|
private XWiki xwiki; |
| 49 |
|
|
| 50 |
|
private MailSenderPluginApi api; |
| 51 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 52 |
3 |
@Override... |
| 53 |
|
protected void setUp() throws Exception |
| 54 |
|
{ |
| 55 |
3 |
super.setUp(); |
| 56 |
|
|
| 57 |
3 |
this.mockXWiki = mock(XWiki.class); |
| 58 |
3 |
this.xwiki = (XWiki) this.mockXWiki.proxy(); |
| 59 |
3 |
getContext().setWiki(this.xwiki); |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
3 |
this.mockXWiki.stubs().method("getDocument").with(eq("XWiki.Mail"), ANYTHING).will( |
| 64 |
|
returnValue(new XWikiDocument())); |
| 65 |
3 |
this.mockXWiki.stubs().method("saveDocument"); |
| 66 |
|
|
| 67 |
|
|
| 68 |
3 |
Mockery mockery = new JUnit4Mockery(); |
| 69 |
3 |
final MailSenderConfiguration mockConfiguration = |
| 70 |
|
getComponentManager().registerMockComponent(mockery, MailSenderConfiguration.class); |
| 71 |
3 |
mockery.checking(new Expectations() |
| 72 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 73 |
3 |
{... |
| 74 |
3 |
allowing(mockConfiguration).getHost(); |
| 75 |
3 |
will(returnValue("myserver")); |
| 76 |
3 |
allowing(mockConfiguration).getPort(); |
| 77 |
3 |
will(returnValue(25)); |
| 78 |
3 |
allowing(mockConfiguration).getFromAddress(); |
| 79 |
3 |
will(returnValue(null)); |
| 80 |
3 |
allowing(mockConfiguration).getUsername(); |
| 81 |
3 |
will(returnValue(null)); |
| 82 |
3 |
allowing(mockConfiguration).getPassword(); |
| 83 |
3 |
will(returnValue(null)); |
| 84 |
3 |
allowing(mockConfiguration).getAdditionalProperties(); |
| 85 |
3 |
will(returnValue(new Properties())); |
| 86 |
|
} |
| 87 |
|
}); |
| 88 |
|
|
| 89 |
3 |
MailSenderPlugin plugin = new MailSenderPlugin("dummy", "dummy", getContext()); |
| 90 |
3 |
this.api = new MailSenderPluginApi(plugin, getContext()); |
| 91 |
|
|
| 92 |
|
|
| 93 |
3 |
Mailbox.clearAll(); |
| 94 |
|
} |
| 95 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 96 |
1 |
public void testSendMail() throws Exception... |
| 97 |
|
{ |
| 98 |
1 |
Mail mail = this.api.createMail(); |
| 99 |
1 |
mail.setFrom("john@acme.org"); |
| 100 |
1 |
mail.setTo("peter@acme.org"); |
| 101 |
1 |
mail.setSubject("Test subject"); |
| 102 |
1 |
mail.setTextPart("Text content"); |
| 103 |
1 |
mail.setHeader("header", "value"); |
| 104 |
|
|
| 105 |
1 |
assertEquals(0, this.api.sendMail(mail)); |
| 106 |
|
|
| 107 |
|
|
| 108 |
1 |
List<Message> inbox = Mailbox.get("peter@acme.org"); |
| 109 |
1 |
assertEquals(1, inbox.size()); |
| 110 |
1 |
Message message = inbox.get(0); |
| 111 |
1 |
assertEquals("Test subject", message.getSubject()); |
| 112 |
1 |
assertEquals("john@acme.org", ((InternetAddress) message.getFrom()[0]).getAddress()); |
| 113 |
1 |
assertEquals("value", message.getHeader("header")[0]); |
| 114 |
|
} |
| 115 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
| 116 |
1 |
public void testSendMailWithCustomConfiguration() throws Exception... |
| 117 |
|
{ |
| 118 |
1 |
Mail mail = this.api.createMail(); |
| 119 |
1 |
mail.setFrom("john@acme.org"); |
| 120 |
1 |
mail.setTo("peter@acme.org"); |
| 121 |
1 |
mail.setSubject("Test subject"); |
| 122 |
1 |
mail.setTextPart("Text content"); |
| 123 |
|
|
| 124 |
1 |
MailConfiguration config = |
| 125 |
|
this.api.createMailConfiguration(new com.xpn.xwiki.api.XWiki(this.xwiki, getContext())); |
| 126 |
1 |
assertEquals(25, config.getPort()); |
| 127 |
1 |
assertEquals("myserver", config.getHost()); |
| 128 |
1 |
assertNull(config.getFrom()); |
| 129 |
|
|
| 130 |
|
|
| 131 |
1 |
config.setFrom("jason@acme.org"); |
| 132 |
|
|
| 133 |
1 |
assertEquals(0, this.api.sendMail(mail, config)); |
| 134 |
|
|
| 135 |
|
|
| 136 |
|
} |
| 137 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
| 138 |
1 |
public void testSendRawMessage() throws MessagingException, IOException... |
| 139 |
|
{ |
| 140 |
1 |
assertEquals(0, this.api.sendRawMessage("john@acme.org", "peter@acme.org", |
| 141 |
|
"Subject:Test subject\nFrom:steve@acme.org\nCc:adam@acme.org\nheader:value\n\nTest content")); |
| 142 |
1 |
List<Message> inbox = Mailbox.get("peter@acme.org"); |
| 143 |
1 |
assertEquals(1, inbox.size()); |
| 144 |
1 |
Message message = inbox.get(0); |
| 145 |
1 |
assertEquals("Test subject", message.getSubject()); |
| 146 |
1 |
assertEquals("steve@acme.org", message.getFrom()[0].toString()); |
| 147 |
1 |
assertEquals("Test content\r\n", message.getContent()); |
| 148 |
1 |
assertEquals("value", message.getHeader("header")[0]); |
| 149 |
|
|
| 150 |
1 |
inbox = Mailbox.get("adam@acme.org"); |
| 151 |
1 |
assertEquals(1, inbox.size()); |
| 152 |
|
} |
| 153 |
|
} |