1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal.factory.usersandgroups

File UsersAndGroupsMimeMessageFactoryTest.java

 

Code metrics

0
37
5
1
144
101
9
0.24
7.4
5
1.8

Classes

Class Line # Actions
UsersAndGroupsMimeMessageFactoryTest 55 37 0% 9 4
0.904761990.5%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Unit tests for {@link UsersAndGroupsMimeMessageFactory}.
50    *
51    * @version $Id: 7d2f97d286e21d2f0a159d7512dd7b5f955af9a8 $
52    * @since 6.4.2
53    * @since 7.0M2
54    */
 
55    public class UsersAndGroupsMimeMessageFactoryTest
56    {
57    @Rule
58    public MockitoComponentMockingRule<UsersAndGroupsMimeMessageFactory> mocker =
59    new MockitoComponentMockingRule<>(UsersAndGroupsMimeMessageFactory.class);
60   
 
61  1 toggle @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   
 
72  1 toggle @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   
 
84  1 toggle @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   
 
96  1 toggle @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   
 
118  1 toggle @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    // Setup XWikiContext since this is required internally by the iterator constructor
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    }