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

File DefaultMailTemplateManagerTest.java

 

Code metrics

0
93
11
1
295
208
13
0.14
8.45
11
1.18

Classes

Class Line # Actions
DefaultMailTemplateManagerTest 66 93 0% 13 2
0.980769298.1%
 

Contributing tests

This file is covered by 6 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.template;
21   
22    import java.io.Writer;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.List;
26    import java.util.Locale;
27   
28    import javax.inject.Provider;
29    import javax.mail.MessagingException;
30   
31    import org.apache.velocity.VelocityContext;
32    import org.junit.Before;
33    import org.junit.Rule;
34    import org.junit.Test;
35    import org.mockito.invocation.InvocationOnMock;
36    import org.mockito.stubbing.Answer;
37    import org.xwiki.bridge.DocumentAccessBridge;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.test.mockito.MockitoComponentMockingRule;
40    import org.xwiki.velocity.VelocityEngine;
41    import org.xwiki.velocity.VelocityManager;
42    import org.xwiki.velocity.XWikiVelocityException;
43   
44    import com.xpn.xwiki.XWiki;
45    import com.xpn.xwiki.XWikiContext;
46    import com.xpn.xwiki.doc.XWikiDocument;
47    import com.xpn.xwiki.objects.BaseObject;
48   
49    import static org.junit.Assert.assertEquals;
50    import static org.junit.Assert.fail;
51    import static org.mockito.ArgumentMatchers.any;
52    import static org.mockito.ArgumentMatchers.anyInt;
53    import static org.mockito.ArgumentMatchers.eq;
54    import static org.mockito.ArgumentMatchers.same;
55    import static org.mockito.Mockito.doAnswer;
56    import static org.mockito.Mockito.mock;
57    import static org.mockito.Mockito.verify;
58    import static org.mockito.Mockito.when;
59   
60    /**
61    * Unit tests for {@link org.xwiki.mail.internal.factory.template.DefaultMailTemplateManager}.
62    *
63    * @version $Id: ff920c13f462658f504604d79861d4b10242210b $
64    * @since 6.1RC1
65    */
 
66    public class DefaultMailTemplateManagerTest
67    {
68    @Rule
69    public MockitoComponentMockingRule<DefaultMailTemplateManager> mocker =
70    new MockitoComponentMockingRule<>(DefaultMailTemplateManager.class);
71   
72    private XWiki xwiki;
73   
 
74  6 toggle @Before
75    public void setUp() throws Exception
76    {
77  6 XWikiContext xwikiContext = mock(XWikiContext.class);
78  6 Provider<XWikiContext> contextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
79  6 when(contextProvider.get()).thenReturn(xwikiContext);
80   
81  6 this.xwiki = mock(XWiki.class);
82  6 when(xwikiContext.getWiki()).thenReturn(this.xwiki);
83  6 when(this.xwiki.getDefaultLocale(xwikiContext)).thenReturn(Locale.ENGLISH);
84   
85  6 XWikiDocument document = mock(XWikiDocument.class);
86  6 when(this.xwiki.getDocument(any(DocumentReference.class), eq(xwikiContext))).thenReturn(document);
87   
88  6 BaseObject object = mock(BaseObject.class);
89   
90  6 when(document.getXObjects(any())).thenReturn(Collections.singletonList(object));
91    }
92   
 
93  1 toggle @Test
94    public void evaluate() throws Exception
95    {
96  1 DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
97  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
98   
99  1 when(documentBridge.getProperty(same(documentReference), any(), anyInt(), eq("html")))
100    .thenReturn("Hello <b>${name}</b> <br />${email}");
101   
102  1 VelocityEngine velocityEngine = mock(VelocityEngine.class);
103  1 VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
104  1 when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
105   
106  1 doAnswer(new Answer<Void>()
107    {
 
108  1 toggle @Override
109    public Void answer(InvocationOnMock invocation) throws Throwable
110    {
111  1 Object[] args = invocation.getArguments();
112  1 ((Writer) args[1]).write("Hello <b>John Doe</b> <br />john@doe.com");
113  1 return null;
114    }
115    }).when(velocityEngine).evaluate(any(VelocityContext.class), any(Writer.class),
116    any(), eq("Hello <b>${name}</b> <br />${email}"));
117   
118  1 String result =
119    this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap());
120   
121  1 assertEquals(result, "Hello <b>John Doe</b> <br />john@doe.com");
122    }
123   
 
124  1 toggle @Test
125    public void evaluateWithLanguage() throws Exception
126    {
127  1 DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
128  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
129   
130  1 when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(1);
131  1 when(documentBridge.getProperty(any(DocumentReference.class), any(), eq(1), eq("html")))
132    .thenReturn("Salut <b>${name}</b> <br />${email}");
133   
134  1 VelocityEngine velocityEngine = mock(VelocityEngine.class);
135  1 VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
136  1 when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
137   
138  1 doAnswer(new Answer<Void>()
139    {
 
140  1 toggle @Override
141    public Void answer(InvocationOnMock invocation) throws Throwable
142    {
143  1 Object[] args = invocation.getArguments();
144  1 ((Writer) args[1]).write("Salut <b>John Doe</b> <br />john@doe.com");
145  1 return null;
146    }
147    }).when(velocityEngine).evaluate(any(VelocityContext.class), any(Writer.class),
148    any(), eq("Salut <b>${name}</b> <br />${email}"));
149   
150  1 String result = this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap(),
151    Locale.FRENCH);
152   
153  1 verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("fr"));
154   
155  1 assertEquals(result, "Salut <b>John Doe</b> <br />john@doe.com");
156    }
157   
 
158  1 toggle @Test
159    public void evaluateWithObjectNotFoundWithLanguagePassed() throws Exception
160    {
161  1 DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
162  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
163   
164    // First call with the passed language, return -1 (No XWiki.Mail xobject found)
165  1 when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(-1);
166   
167    // Second call with the default language (en), return one (Only XWiki.Mail xobject is found)
168  1 when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("en"))).thenReturn(1);
169   
170  1 when(documentBridge.getProperty(any(DocumentReference.class), any(), eq(1), eq("html")))
171    .thenReturn("Salut <b>${name}</b> <br />${email}");
172   
173  1 VelocityEngine velocityEngine = mock(VelocityEngine.class);
174  1 VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
175  1 when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
176   
177  1 doAnswer(new Answer<Void>()
178    {
 
179  1 toggle @Override
180    public Void answer(InvocationOnMock invocation) throws Throwable
181    {
182  1 Object[] args = invocation.getArguments();
183  1 ((Writer) args[1]).write("Salut <b>John Doe</b> <br />john@doe.com");
184  1 return null;
185    }
186    }).when(velocityEngine).evaluate(any(), any(), any(), eq("Salut <b>${name}</b> <br />${email}"));
187   
188  1 String result = this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap(),
189    Locale.FRENCH);
190   
191  1 verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("fr"));
192  1 verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("en"));
193   
194  1 assertEquals(result, "Salut <b>John Doe</b> <br />john@doe.com");
195    }
196   
 
197  1 toggle @Test
198    public void evaluateWithObjectNotFoundWithDefaultLanguage() throws Exception
199    {
200  1 DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
201  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
202   
203    // First call with the passed language, return -1 (No XWiki.Mail xobject found)
204  1 when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(-1);
205   
206    // Second call with the default language, return -1 (No XWiki.Mail xobject found)
207  1 when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("en"))).thenReturn(-1);
208   
209  1 when(documentBridge.getProperty(any(DocumentReference.class), any(), eq(0), eq("html")))
210    .thenReturn("Salut <b>${name}</b> <br />${email}");
211   
212  1 VelocityEngine velocityEngine = mock(VelocityEngine.class);
213  1 VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
214  1 when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
215   
216  1 doAnswer(new Answer<Void>()
217    {
 
218  1 toggle @Override
219    public Void answer(InvocationOnMock invocation) throws Throwable
220    {
221  1 Object[] args = invocation.getArguments();
222  1 ((Writer) args[1]).write("Salut <b>John Doe</b> <br />john@doe.com");
223  1 return null;
224    }
225    }).when(velocityEngine).evaluate(any(), any(), any(), eq("Salut <b>${name}</b> <br />${email}"));
226   
227  1 String result = this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap(),
228    Locale.FRENCH);
229   
230  1 verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("fr"));
231  1 verify(documentBridge).getObjectNumber(any(), any(), eq("language"), eq("en"));
232   
233  1 assertEquals(result, "Salut <b>John Doe</b> <br />john@doe.com");
234    }
235   
 
236  1 toggle @Test
237    public void evaluateWithErrorNoObjectMatches() throws Exception
238    {
239  1 XWikiDocument document = mock(XWikiDocument.class);
240  1 when(this.xwiki.getDocument(any(DocumentReference.class), any())).thenReturn(document);
241   
242  1 BaseObject object1 = mock(BaseObject.class);
243  1 BaseObject object2 = mock(BaseObject.class);
244   
245  1 List<BaseObject> xobjects = Arrays.asList(object1, object2);
246  1 when(document.getXObjects(any())).thenReturn(xobjects);
247   
248  1 DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
249  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
250   
251    // First call with the passed language, return -1 (No XWiki.Mail xobject found)
252  1 when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(-1);
253   
254    // Second call with the default language, return -1 (No XWiki.Mail xobject found)
255  1 when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("en"))).thenReturn(-1);
256   
257  1 try {
258  1 this.mocker.getComponentUnderTest().evaluate(documentReference, "html", Collections.emptyMap(),
259    Locale.FRENCH);
260   
261  0 fail("Should have thrown an exception here!");
262    } catch (MessagingException expected) {
263  1 assertEquals(
264    "No [Document XWiki.Mail] object matches the locale [fr] or the default locale [en] "
265    +"in the Document [wiki:space.page]",
266    expected.getMessage());
267    }
268    }
269   
 
270  1 toggle @Test
271    public void evaluateWhenVelocityError() throws Exception
272    {
273  1 DocumentAccessBridge documentBridge = this.mocker.getInstance(DocumentAccessBridge.class);
274  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
275   
276  1 when(documentBridge.getProperty(same(documentReference), any(), anyInt(), eq("html")))
277    .thenReturn("Hello <b>${name}</b> <br />${email}");
278   
279  1 VelocityEngine velocityEngine = mock(VelocityEngine.class);
280  1 VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
281  1 when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
282   
283  1 when(velocityEngine.evaluate(any(VelocityContext.class), any(Writer.class),
284    any(), eq("Hello <b>${name}</b> <br />${email}"))).thenThrow(new XWikiVelocityException("Error"));
285   
286  1 try {
287  1 this.mocker.getComponentUnderTest().evaluate(documentReference, "html",
288    Collections.<String, Object>emptyMap());
289  0 fail("Should have thrown an exception here!");
290    } catch (MessagingException expected) {
291  1 assertEquals("Failed to evaluate property [html] for Document [wiki:space.page] and locale [null]",
292    expected.getMessage());
293    }
294    }
295    }