1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.wikibridge; |
21 |
|
|
22 |
|
import java.io.Reader; |
23 |
|
import java.io.StringReader; |
24 |
|
import java.io.Writer; |
25 |
|
import java.util.Arrays; |
26 |
|
import java.util.Collections; |
27 |
|
import java.util.HashMap; |
28 |
|
import java.util.List; |
29 |
|
import java.util.Map; |
30 |
|
import java.util.Properties; |
31 |
|
|
32 |
|
import javax.inject.Provider; |
33 |
|
import javax.script.ScriptContext; |
34 |
|
|
35 |
|
import org.apache.velocity.VelocityContext; |
36 |
|
import org.hamcrest.Description; |
37 |
|
import org.jmock.Expectations; |
38 |
|
import org.jmock.api.Action; |
39 |
|
import org.jmock.api.Invocation; |
40 |
|
import org.junit.Assert; |
41 |
|
import org.junit.Before; |
42 |
|
import org.junit.Test; |
43 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
44 |
|
import org.xwiki.component.descriptor.DefaultComponentDescriptor; |
45 |
|
import org.xwiki.context.Execution; |
46 |
|
import org.xwiki.model.reference.DocumentReference; |
47 |
|
import org.xwiki.model.reference.SpaceReference; |
48 |
|
import org.xwiki.model.reference.WikiReference; |
49 |
|
import org.xwiki.rendering.converter.Converter; |
50 |
|
import org.xwiki.rendering.listener.reference.DocumentResourceReference; |
51 |
|
import org.xwiki.rendering.macro.MacroId; |
52 |
|
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; |
53 |
|
import org.xwiki.rendering.macro.script.ScriptMockSetup; |
54 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor; |
55 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroFactory; |
56 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroManager; |
57 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor; |
58 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility; |
59 |
|
import org.xwiki.rendering.parser.Parser; |
60 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
61 |
|
import org.xwiki.rendering.syntax.Syntax; |
62 |
|
import org.xwiki.rendering.wiki.WikiModel; |
63 |
|
import org.xwiki.script.ScriptContextManager; |
64 |
|
import org.xwiki.test.jmock.AbstractComponentTestCase; |
65 |
|
import org.xwiki.velocity.VelocityEngine; |
66 |
|
import org.xwiki.velocity.VelocityManager; |
67 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
68 |
|
|
69 |
|
|
70 |
|
@link |
71 |
|
|
72 |
|
@version |
73 |
|
@since |
74 |
|
|
|
|
| 99.1% |
Uncovered Elements: 1 (115) |
Complexity: 16 |
Complexity Density: 0.16 |
|
75 |
|
public class DefaultWikiMacroTest extends AbstractComponentTestCase |
76 |
|
{ |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
private DocumentReference wikiMacroDocumentReference; |
81 |
|
|
82 |
|
|
83 |
|
@link |
84 |
|
|
85 |
|
private WikiMacroManager wikiMacroManager; |
86 |
|
|
87 |
|
private WikiModel mockWikiModel; |
88 |
|
|
89 |
|
private WikiMacroFactory mockWikiMacroFactory; |
90 |
|
|
91 |
|
private WikiDescriptorManager mockWikiDescriptorManager; |
92 |
|
|
93 |
|
private Provider<DocumentReference> mockCurrentDocumentReferenceProvider; |
94 |
|
|
95 |
|
private Provider<DocumentReference> mockCurrentSpaceReferenceProvider; |
96 |
|
|
97 |
|
private Map<String, Object> xcontext; |
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
99 |
7 |
@Override... |
100 |
|
@Before |
101 |
|
public void setUp() throws Exception |
102 |
|
{ |
103 |
7 |
super.setUp(); |
104 |
|
|
105 |
|
|
106 |
7 |
ScriptMockSetup scriptMockSetup = new ScriptMockSetup(getMockery(), getComponentManager()); |
107 |
7 |
final DocumentAccessBridge mockDocBridge = scriptMockSetup.bridge; |
108 |
7 |
this.mockWikiModel = scriptMockSetup.wikiModel; |
109 |
|
|
110 |
7 |
this.wikiMacroDocumentReference = new DocumentReference("wiki", "space", "macroPage"); |
111 |
7 |
this.wikiMacroManager = getComponentManager().getInstance(WikiMacroManager.class); |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
7 |
this.xcontext = new HashMap<String, Object>(); |
116 |
7 |
Execution execution = getComponentManager().getInstance(Execution.class); |
117 |
7 |
execution.getContext().setProperty("xwikicontext", this.xcontext); |
118 |
7 |
ScriptContextManager scm = getComponentManager().getInstance(ScriptContextManager.class); |
119 |
7 |
scm.getCurrentScriptContext().setAttribute("xcontext", this.xcontext, ScriptContext.ENGINE_SCOPE); |
120 |
|
|
121 |
7 |
getMockery().checking(new Expectations() |
122 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
|
123 |
7 |
{... |
124 |
7 |
allowing(mockWikiDescriptorManager).getCurrentWikiId(); |
125 |
7 |
will(returnValue("wiki")); |
126 |
7 |
allowing(mockCurrentDocumentReferenceProvider).get(); |
127 |
7 |
will(returnValue(new DocumentReference("wiki", "space", "document"))); |
128 |
7 |
allowing(mockCurrentSpaceReferenceProvider).get(); |
129 |
7 |
will(returnValue(new SpaceReference("space", new WikiReference("wiki")))); |
130 |
7 |
allowing(mockDocBridge).getCurrentUser(); |
131 |
7 |
will(returnValue("dummy")); |
132 |
7 |
allowing(mockDocBridge).setCurrentUser(with(any(String.class))); |
133 |
7 |
allowing(mockDocBridge).getCurrentUserReference(); |
134 |
7 |
will(returnValue(new DocumentReference("wiki", "XWiki", "dummy"))); |
135 |
7 |
allowing(mockWikiMacroFactory).isAllowed(with(any(DocumentReference.class)), |
136 |
|
with(any(WikiMacroVisibility.class))); |
137 |
7 |
will(returnValue(true)); |
138 |
|
|
139 |
|
|
140 |
|
|
141 |
7 |
allowing(mockDocBridge).getDocument(wikiMacroDocumentReference); |
142 |
7 |
will(returnValue(null)); |
143 |
|
} |
144 |
|
}); |
145 |
|
} |
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
147 |
7 |
@Override... |
148 |
|
protected void registerComponents() throws Exception |
149 |
|
{ |
150 |
7 |
super.registerComponents(); |
151 |
|
|
152 |
7 |
this.mockWikiDescriptorManager = registerMockComponent(WikiDescriptorManager.class); |
153 |
7 |
this.mockCurrentDocumentReferenceProvider = registerMockComponent(DocumentReference.TYPE_PROVIDER, "current"); |
154 |
7 |
this.mockCurrentSpaceReferenceProvider = registerMockComponent(SpaceReference.TYPE_PROVIDER, "current"); |
155 |
|
|
156 |
|
|
157 |
7 |
this.mockWikiMacroFactory = registerMockComponent(WikiMacroFactory.class); |
158 |
|
} |
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
160 |
6 |
private void registerWikiMacro(String macroId, String macroContent, Syntax syntax) throws Exception... |
161 |
|
{ |
162 |
6 |
List<WikiMacroParameterDescriptor> parameterDescriptors = |
163 |
|
Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", true), |
164 |
|
new WikiMacroParameterDescriptor("param2", "This is param2", true)); |
165 |
6 |
registerWikiMacro(macroId, macroContent, syntax, parameterDescriptors); |
166 |
|
} |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
168 |
8 |
private void registerWikiMacro(String macroId, String macroContent, Syntax syntax,... |
169 |
|
List<WikiMacroParameterDescriptor> parameterDescriptors) throws Exception |
170 |
|
{ |
171 |
8 |
WikiMacroDescriptor descriptor = new WikiMacroDescriptor(new MacroId(macroId), "Wiki Macro", "Description", |
172 |
|
"Test", WikiMacroVisibility.GLOBAL, new DefaultContentDescriptor(false), parameterDescriptors); |
173 |
|
|
174 |
8 |
Parser parser = getComponentManager().getInstance(Parser.class, syntax.toIdString()); |
175 |
|
|
176 |
8 |
DefaultWikiMacro wikiMacro = new DefaultWikiMacro(wikiMacroDocumentReference, null, true, descriptor, |
177 |
|
parser.parse(new StringReader(macroContent)), syntax, getComponentManager()); |
178 |
|
|
179 |
8 |
this.wikiMacroManager.registerWikiMacro(wikiMacroDocumentReference, wikiMacro); |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
187 |
1 |
@Test... |
188 |
|
public void testExecute() throws Exception |
189 |
|
{ |
190 |
1 |
registerWikiMacro("wikimacro1", "This is **bold**", Syntax.XWIKI_2_0); |
191 |
|
|
192 |
1 |
Converter converter = getComponentManager().getInstance(Converter.class); |
193 |
|
|
194 |
1 |
DefaultWikiPrinter printer = new DefaultWikiPrinter(); |
195 |
1 |
converter.convert(new StringReader("{{wikimacro1 param1=\"value1\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, |
196 |
|
Syntax.XHTML_1_0, printer); |
197 |
|
|
198 |
|
|
199 |
1 |
Assert.assertEquals("<p>This is <strong>bold</strong></p>", printer.toString()); |
200 |
|
|
201 |
1 |
Assert.assertFalse(this.xcontext.containsKey("macro")); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
208 |
1 |
@Test... |
209 |
|
public void testExecuteWhenInlineAndWithMacro() throws Exception |
210 |
|
{ |
211 |
1 |
registerWikiMacro("wikimacro1", "This is **bold**", Syntax.XWIKI_2_0); |
212 |
1 |
registerWikiMacro("wikimacro2", "{{wikimacro1 param1=\"v1\" param2=\"v2\"/}}", Syntax.XWIKI_2_0); |
213 |
|
|
214 |
1 |
Converter converter = getComponentManager().getInstance(Converter.class); |
215 |
|
|
216 |
1 |
DefaultWikiPrinter printer = new DefaultWikiPrinter(); |
217 |
|
|
218 |
1 |
converter.convert(new StringReader("Hello {{wikimacro2 param1=\"value1\" param2=\"value2\"/}}"), |
219 |
|
Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer); |
220 |
|
|
221 |
|
|
222 |
1 |
Assert.assertEquals("<p>Hello This is <strong>bold</strong></p>", printer.toString()); |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
228 |
1 |
@Test... |
229 |
|
public void testExecuteWhenInnerMacro() throws Exception |
230 |
|
{ |
231 |
1 |
registerWikiMacro("wikimacro1", "{{toc/}}", Syntax.XWIKI_2_0); |
232 |
|
|
233 |
1 |
getMockery().checking(new Expectations() |
234 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
235 |
1 |
{... |
236 |
1 |
DocumentResourceReference reference = new DocumentResourceReference(null); |
237 |
1 |
reference.setAnchor("Hheading"); |
238 |
1 |
allowing(mockWikiModel).getDocumentViewURL(reference); |
239 |
1 |
will(returnValue("url")); |
240 |
|
} |
241 |
|
}); |
242 |
|
|
243 |
1 |
Converter converter = getComponentManager().getInstance(Converter.class); |
244 |
|
|
245 |
1 |
DefaultWikiPrinter printer = new DefaultWikiPrinter(); |
246 |
1 |
converter.convert(new StringReader("= heading\n\n{{wikimacro1 param1=\"value1\" param2=\"value2\"/}}"), |
247 |
|
Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer); |
248 |
|
|
249 |
|
|
250 |
1 |
Assert.assertEquals( |
251 |
|
"<h1 id=\"Hheading\"><span>heading</span></h1>" |
252 |
|
+ "<ul><li><span class=\"wikilink\"><a href=\"#Hheading\">heading</a></span></li></ul>", |
253 |
|
printer.toString()); |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
259 |
1 |
@Test... |
260 |
|
public void testExecuteWhenWithDifferentMacroSyntax() throws Exception |
261 |
|
{ |
262 |
1 |
registerWikiMacro("wikimacro", "{{groovy}}println \"[[path:/some/path]]\"{{/groovy}}", Syntax.XWIKI_2_1); |
263 |
|
|
264 |
1 |
Converter converter = getComponentManager().getInstance(Converter.class); |
265 |
|
|
266 |
1 |
DefaultWikiPrinter printer = new DefaultWikiPrinter(); |
267 |
1 |
converter.convert(new StringReader("{{wikimacro param1=\"value1\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, |
268 |
|
Syntax.XHTML_1_0, printer); |
269 |
|
|
270 |
|
|
271 |
1 |
Assert.assertEquals("<p><span class=\"wikiinternallink\"><a href=\"/some/path\">" |
272 |
|
+ "<span class=\"wikigeneratedlinkcontent\">/some/path</span></a></span></p>", printer.toString()); |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
@link |
278 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
279 |
1 |
@Test... |
280 |
|
public void testExecuteWhenWikiMacroDirectlyProvideTheResult() throws Exception |
281 |
|
{ |
282 |
1 |
registerWikiMacro("wikimacrowithresult", |
283 |
|
"{{groovy}}" |
284 |
|
+ "xcontext.macro.result = java.util.Collections.singletonList(new org.xwiki.rendering.block.WordBlock(xcontext.macro.params.param1));" |
285 |
|
+ "{{/groovy}}", |
286 |
|
Syntax.XWIKI_2_0); |
287 |
|
|
288 |
1 |
Converter converter = getComponentManager().getInstance(Converter.class); |
289 |
|
|
290 |
1 |
DefaultWikiPrinter printer = new DefaultWikiPrinter(); |
291 |
|
|
292 |
1 |
converter.convert(new StringReader("Hello {{wikimacrowithresult param1=\"World\" param2=\"param2\"/}}"), |
293 |
|
Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer); |
294 |
|
|
295 |
|
|
296 |
1 |
Assert.assertEquals("<p>Hello World</p>", printer.toString()); |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
302 |
1 |
@Test... |
303 |
|
public void testDefaultParameterValues() throws Exception |
304 |
|
{ |
305 |
|
|
306 |
1 |
final VelocityManager mockVelocityManager = getMockery().mock(VelocityManager.class); |
307 |
1 |
DefaultComponentDescriptor<VelocityManager> descriptorVM = new DefaultComponentDescriptor<VelocityManager>(); |
308 |
1 |
descriptorVM.setRole(VelocityManager.class); |
309 |
1 |
getComponentManager().registerComponent(descriptorVM, mockVelocityManager); |
310 |
|
|
311 |
|
|
312 |
1 |
final VelocityEngine vEngine = getComponentManager().getInstance(VelocityEngine.class); |
313 |
1 |
Properties properties = new Properties(); |
314 |
1 |
properties.setProperty("resource.loader", "file"); |
315 |
1 |
vEngine.initialize(properties); |
316 |
|
|
317 |
|
|
318 |
1 |
Execution execution = getComponentManager().getInstance(Execution.class); |
319 |
1 |
Map<?, ?> xwikiContext = (Map<?, ?>) execution.getContext().getProperty("xwikicontext"); |
320 |
1 |
final VelocityContext vContext = new VelocityContext(); |
321 |
1 |
vContext.put("xcontext", xwikiContext); |
322 |
|
|
323 |
1 |
getMockery().checking(new Expectations() |
324 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
325 |
1 |
{... |
326 |
1 |
oneOf(mockVelocityManager).getCurrentVelocityContext(); |
327 |
1 |
will(returnValue(vContext)); |
328 |
1 |
oneOf(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), |
329 |
|
with(any(Reader.class))); |
330 |
1 |
will(new Action() |
331 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
332 |
1 |
@Override... |
333 |
|
public Object invoke(Invocation invocation) throws Throwable |
334 |
|
{ |
335 |
1 |
return vEngine.evaluate(vContext, (Writer) invocation.getParameter(0), |
336 |
|
(String) invocation.getParameter(1), (Reader) invocation.getParameter(2)); |
337 |
|
} |
338 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
339 |
0 |
@Override... |
340 |
|
public void describeTo(Description description) |
341 |
|
{ |
342 |
|
} |
343 |
|
}); |
344 |
|
} |
345 |
|
}); |
346 |
|
|
347 |
1 |
List<WikiMacroParameterDescriptor> parameterDescriptors = |
348 |
|
Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", false, "default_value")); |
349 |
|
|
350 |
1 |
registerWikiMacro("wikimacro1", "{{velocity}}$xcontext.macro.params.param1{{/velocity}}", Syntax.XWIKI_2_0, |
351 |
|
parameterDescriptors); |
352 |
|
|
353 |
1 |
Converter converter = getComponentManager().getInstance(Converter.class); |
354 |
|
|
355 |
1 |
DefaultWikiPrinter printer = new DefaultWikiPrinter(); |
356 |
1 |
converter.convert(new StringReader("{{wikimacro1/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer); |
357 |
|
|
358 |
|
|
359 |
1 |
Assert.assertEquals("<p>default_value</p>", printer.toString()); |
360 |
|
} |
361 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
362 |
1 |
@Test... |
363 |
|
public void testGetCurrentMacroBlock() throws Exception |
364 |
|
{ |
365 |
1 |
registerWikiMacro("wikimacro", |
366 |
|
"{{groovy}}" + "println xcontext.macro.context.getCurrentMacroBlock().id\n" |
367 |
|
+ "println xcontext.macro.context.getCurrentMacroBlock().parent.class\n" |
368 |
|
+ "println xcontext.macro.context.getCurrentMacroBlock().nextSibling.children[0].word\n" |
369 |
|
+ "println xcontext.macro.context.getCurrentMacroBlock().previousSibling.children[0].word\n" |
370 |
|
+ "{{/groovy}}", |
371 |
|
Syntax.XWIKI_2_0, Collections.<WikiMacroParameterDescriptor>emptyList()); |
372 |
|
|
373 |
1 |
Converter converter = getComponentManager().getInstance(Converter.class); |
374 |
|
|
375 |
1 |
DefaultWikiPrinter printer = new DefaultWikiPrinter(); |
376 |
1 |
converter.convert(new StringReader("before\n\n{{wikimacro/}}\n\nafter"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0, |
377 |
|
printer); |
378 |
|
|
379 |
1 |
Assert.assertEquals("before" + "\n\n" + "wikimacro\n" + "class org.xwiki.rendering.block.XDOM\n" + "after\n" |
380 |
|
+ "before" + "\n\n" + "after", printer.toString()); |
381 |
|
} |
382 |
|
} |