| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.mail.internal.factory.usersandgroups; |
| 21 |
|
|
| 22 |
|
import java.util.Collections; |
| 23 |
|
import java.util.HashMap; |
| 24 |
|
import java.util.Iterator; |
| 25 |
|
import java.util.Map; |
| 26 |
|
import java.util.Properties; |
| 27 |
|
|
| 28 |
|
import javax.inject.Provider; |
| 29 |
|
import javax.mail.MessagingException; |
| 30 |
|
import javax.mail.Session; |
| 31 |
|
import javax.mail.internet.MimeMessage; |
| 32 |
|
|
| 33 |
|
import org.junit.Rule; |
| 34 |
|
import org.junit.Test; |
| 35 |
|
import org.xwiki.component.manager.ComponentManager; |
| 36 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
| 37 |
|
import org.xwiki.context.Execution; |
| 38 |
|
import org.xwiki.context.ExecutionContext; |
| 39 |
|
import org.xwiki.mail.MimeMessageFactory; |
| 40 |
|
import org.xwiki.model.reference.DocumentReference; |
| 41 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 42 |
|
|
| 43 |
|
import com.xpn.xwiki.XWikiContext; |
| 44 |
|
|
| 45 |
|
import static org.junit.Assert.*; |
| 46 |
|
import static org.mockito.Mockito.*; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
@link |
| 50 |
|
|
| 51 |
|
@version |
| 52 |
|
@since |
| 53 |
|
@since |
| 54 |
|
|
| |
|
| 90.5% |
Uncovered Elements: 4 (42) |
Complexity: 9 |
Complexity Density: 0.24 |
|
| 55 |
|
public class UsersAndGroupsMimeMessageFactoryTest |
| 56 |
|
{ |
| 57 |
|
@Rule |
| 58 |
|
public MockitoComponentMockingRule<UsersAndGroupsMimeMessageFactory> mocker = |
| 59 |
|
new MockitoComponentMockingRule<>(UsersAndGroupsMimeMessageFactory.class); |
| 60 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
| 61 |
1 |
@Test... |
| 62 |
|
public void createMessageWhenNullParametersPassed() throws Exception |
| 63 |
|
{ |
| 64 |
1 |
try { |
| 65 |
1 |
this.mocker.getComponentUnderTest().createMessage(Collections.<String, Object>emptyMap(), null); |
| 66 |
0 |
fail("Should have thrown an exception"); |
| 67 |
|
} catch (MessagingException expected) { |
| 68 |
1 |
assertEquals("You must pass parameters for this Mime Message Factory to work!", expected.getMessage()); |
| 69 |
|
} |
| 70 |
|
} |
| 71 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
| 72 |
1 |
@Test... |
| 73 |
|
public void createMessageWhenNoHintParameterPassed() throws Exception |
| 74 |
|
{ |
| 75 |
1 |
try { |
| 76 |
1 |
this.mocker.getComponentUnderTest().createMessage( |
| 77 |
|
Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap()); |
| 78 |
0 |
fail("Should have thrown an exception"); |
| 79 |
|
} catch (MessagingException expected) { |
| 80 |
1 |
assertEquals("The parameter [hint] is mandatory.", expected.getMessage()); |
| 81 |
|
} |
| 82 |
|
} |
| 83 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
| 84 |
1 |
@Test... |
| 85 |
|
public void createMessageWhenNoSourceParameterPassed() throws Exception |
| 86 |
|
{ |
| 87 |
1 |
try { |
| 88 |
1 |
this.mocker.getComponentUnderTest().createMessage(Collections.<String, Object>emptyMap(), |
| 89 |
|
Collections.<String, Object>singletonMap("hint", "factoryHint")); |
| 90 |
0 |
fail("Should have thrown an exception"); |
| 91 |
|
} catch (MessagingException expected) { |
| 92 |
1 |
assertEquals("The parameter [source] is mandatory.", expected.getMessage()); |
| 93 |
|
} |
| 94 |
|
} |
| 95 |
|
|
| |
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
1PASS
|
|
| 96 |
1 |
@Test... |
| 97 |
|
public void createMessageWhenNotExistingMimeMessageFactory() throws Exception |
| 98 |
|
{ |
| 99 |
1 |
Session session = Session.getInstance(new Properties()); |
| 100 |
1 |
Map<String, Object> parameters = new HashMap<>(); |
| 101 |
1 |
parameters.put("hint", "factoryHint"); |
| 102 |
1 |
parameters.put("source", "factoryHint"); |
| 103 |
|
|
| 104 |
1 |
Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent( |
| 105 |
|
new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context"); |
| 106 |
1 |
when(componentManagerProvider.get()).thenReturn(this.mocker); |
| 107 |
|
|
| 108 |
1 |
try { |
| 109 |
1 |
this.mocker.getComponentUnderTest().createMessage(Collections.<String, Object>emptyMap(), |
| 110 |
|
parameters); |
| 111 |
0 |
fail("Should have thrown an exception"); |
| 112 |
|
} catch (MessagingException expected) { |
| 113 |
1 |
assertEquals("Failed to find a [MimeMessageFactory<MimeMessage>] for hint [factoryHint]", |
| 114 |
|
expected.getMessage()); |
| 115 |
|
} |
| 116 |
|
} |
| 117 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
| 118 |
1 |
@Test... |
| 119 |
|
public void createMessage() throws Exception |
| 120 |
|
{ |
| 121 |
1 |
DocumentReference userReference = new DocumentReference("userwiki", "userspace", "userpage"); |
| 122 |
1 |
Map<String, Object> source = Collections.<String, Object>singletonMap("users", |
| 123 |
|
Collections.singletonList(userReference)); |
| 124 |
1 |
Map<String, Object> parameters = new HashMap<>(); |
| 125 |
1 |
parameters.put("hint", "template"); |
| 126 |
1 |
parameters.put("source", new DocumentReference("templatewiki", "templatespace", "templatepage")); |
| 127 |
|
|
| 128 |
1 |
Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent( |
| 129 |
|
new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context"); |
| 130 |
1 |
when(componentManagerProvider.get()).thenReturn(this.mocker); |
| 131 |
1 |
this.mocker.registerMockComponent(new DefaultParameterizedType(null, MimeMessageFactory.class, |
| 132 |
|
MimeMessage.class), "template"); |
| 133 |
|
|
| 134 |
|
|
| 135 |
1 |
Execution execution = this.mocker.registerMockComponent(Execution.class); |
| 136 |
1 |
XWikiContext xwikiContext = mock(XWikiContext.class); |
| 137 |
1 |
ExecutionContext executionContext = new ExecutionContext(); |
| 138 |
1 |
executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xwikiContext); |
| 139 |
1 |
when(execution.getContext()).thenReturn(executionContext); |
| 140 |
|
|
| 141 |
1 |
Iterator<MimeMessage> iterator = this.mocker.getComponentUnderTest().createMessage(source, parameters); |
| 142 |
1 |
assertNotNull(iterator); |
| 143 |
|
} |
| 144 |
|
} |