1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.mail.internal.factory.template

File DefaultMailTemplateManagerTest.java

 

Code metrics

0
87
7
1
280
186
9
0.1
12.43
7
1.29

Classes

Class Line # Actions
DefaultMailTemplateManagerTest 70 87 0% 9 2
0.978723497.9%
 

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