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.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 |
|
@link |
62 |
|
|
63 |
|
@version |
64 |
|
@since |
65 |
|
|
|
|
| 98.1% |
Uncovered Elements: 2 (104) |
Complexity: 13 |
Complexity Density: 0.14 |
|
66 |
|
public class DefaultMailTemplateManagerTest |
67 |
|
{ |
68 |
|
@Rule |
69 |
|
public MockitoComponentMockingRule<DefaultMailTemplateManager> mocker = |
70 |
|
new MockitoComponentMockingRule<>(DefaultMailTemplateManager.class); |
71 |
|
|
72 |
|
private XWiki xwiki; |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
74 |
6 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
93 |
1 |
@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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
108 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
124 |
1 |
@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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
140 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
158 |
1 |
@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 |
|
|
165 |
1 |
when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(-1); |
166 |
|
|
167 |
|
|
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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
179 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
197 |
1 |
@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 |
|
|
204 |
1 |
when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(-1); |
205 |
|
|
206 |
|
|
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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
218 |
1 |
@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 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 2 |
Complexity Density: 0.14 |
1PASS
|
|
236 |
1 |
@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 |
|
|
252 |
1 |
when(documentBridge.getObjectNumber(any(), any(), eq("language"), eq("fr"))).thenReturn(-1); |
253 |
|
|
254 |
|
|
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 |
|
|
|
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
1PASS
|
|
270 |
1 |
@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 |
|
} |