1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro

File IncludeMacroTest.java

 
testIncludeMacroWhenSectionSpecified: Failed to lookup default document displayer.
testIncludeMacroWithCurrentContextShowsVelocityMacrosAreShared: Failed to lookup component [org.xwiki.test.rendering.velo...
testIncludeMacroWhenInvalidSectionSpecified: expected:<[Cannot find section [unknown] in document [wik...
testIncludeMacroWithNewContextShowsVelocityMacrosAreIsolated: Failed to lookup component [org.xwiki.test.rendering.velo...
testIncludeMacroWithNewContextShowsPassingOnRestrictedFlag: Failed to lookup default document displayer.
testIncludeMacroWhenIncludingDocumentWithRelativeReferences: Failed to lookup default document displayer.
 

Code metrics

8
196
27
2
528
417
36
0.18
7.26
13.5
1.33

Classes

Class Line # Actions
IncludeMacroTest 72 196 0% 36 165
0.285714328.6%
IncludeMacroTest.ExpectedRecursiveInclusionException 259 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 10 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.rendering.internal.macro;
21   
22    import java.io.StringReader;
23    import java.io.StringWriter;
24    import java.util.Collections;
25    import java.util.HashMap;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.hamcrest.collection.IsArray;
30    import org.jmock.Expectations;
31    import org.jmock.api.Invocation;
32    import org.jmock.lib.action.CustomAction;
33    import org.junit.Assert;
34    import org.junit.Test;
35    import org.xwiki.bridge.DocumentModelBridge;
36    import org.xwiki.context.Execution;
37    import org.xwiki.display.internal.DocumentDisplayer;
38    import org.xwiki.display.internal.DocumentDisplayerParameters;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.model.reference.DocumentReferenceResolver;
41    import org.xwiki.rendering.block.Block;
42    import org.xwiki.rendering.block.MacroBlock;
43    import org.xwiki.rendering.block.MacroMarkerBlock;
44    import org.xwiki.rendering.block.MetaDataBlock;
45    import org.xwiki.rendering.block.XDOM;
46    import org.xwiki.rendering.internal.macro.include.IncludeMacro;
47    import org.xwiki.rendering.internal.transformation.macro.MacroTransformation;
48    import org.xwiki.rendering.listener.MetaData;
49    import org.xwiki.rendering.macro.Macro;
50    import org.xwiki.rendering.macro.MacroExecutionException;
51    import org.xwiki.rendering.macro.include.IncludeMacroParameters;
52    import org.xwiki.rendering.macro.include.IncludeMacroParameters.Context;
53    import org.xwiki.rendering.macro.script.ScriptMockSetup;
54    import org.xwiki.rendering.parser.Parser;
55    import org.xwiki.rendering.renderer.PrintRendererFactory;
56    import org.xwiki.rendering.syntax.Syntax;
57    import org.xwiki.rendering.transformation.MacroTransformationContext;
58    import org.xwiki.rendering.transformation.Transformation;
59    import org.xwiki.security.authorization.AuthorizationManager;
60    import org.xwiki.test.jmock.AbstractComponentTestCase;
61    import org.xwiki.velocity.VelocityManager;
62   
63    import static org.xwiki.rendering.test.BlockAssert.assertBlocks;
64    import static org.xwiki.rendering.test.BlockAssert.assertBlocksStartsWith;
65   
66    /**
67    * Unit tests for {@link IncludeMacro}.
68    *
69    * @version $Id: 63dcf2033d56e3fdcdc6b1ecd9203c6afec3bfa2 $
70    * @since 1.5M2
71    */
 
72    public class IncludeMacroTest extends AbstractComponentTestCase
73    {
74    private ScriptMockSetup mockSetup;
75   
76    private IncludeMacro includeMacro;
77   
78    private PrintRendererFactory rendererFactory;
79   
80    /**
81    * Mocks the component that is used to resolve the 'reference' parameter.
82    */
83    private DocumentReferenceResolver<String> mockDocumentReferenceResolver;
84   
85    private AuthorizationManager mockAuthorization;
86   
87    private DocumentModelBridge mockDocument;
88   
 
89  10 toggle @Override
90    public void setUp() throws Exception
91    {
92  10 super.setUp();
93   
94    // Put a fake XWiki context on the execution context.
95  10 Execution execution = getComponentManager().getInstance(Execution.class);
96  10 execution.getContext().setProperty("xwikicontext", new HashMap<Object, Object>());
97    }
98   
 
99  10 toggle @Override
100    protected void registerComponents() throws Exception
101    {
102  10 super.registerComponents();
103   
104  10 this.mockSetup = new ScriptMockSetup(getMockery(), getComponentManager());
105   
106  10 this.mockDocumentReferenceResolver =
107    registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "macro", "macroDocumentReferenceResolver");
108  10 this.mockAuthorization = registerMockComponent(AuthorizationManager.class);
109   
110  10 this.includeMacro = getComponentManager().getInstance(Macro.class, "include");
111  10 this.rendererFactory = getComponentManager().getInstance(PrintRendererFactory.class, "event/1.0");
112    }
113   
 
114  0 toggle @Test
115    public void testIncludeMacroWithNewContextShowsVelocityMacrosAreIsolated() throws Exception
116    {
117  0 String expected = "beginDocument\n"
118    + "beginMetaData [[base]=[wiki:Space.IncludedPage][source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
119    + "beginMacroMarkerStandalone [velocity] [] [#testmacro]\n" + "beginParagraph\n" + "onSpecialSymbol [#]\n"
120    + "onWord [testmacro]\n" + "endParagraph\n" + "endMacroMarkerStandalone [velocity] [] [#testmacro]\n"
121    + "endMetaData [[base]=[wiki:Space.IncludedPage][source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
122    + "endDocument";
123   
124    // We verify that a Velocity macro set in the including page is not seen in the included page.
125  0 List<Block> blocks =
126    Test failure here runIncludeMacroWithPreVelocity(Context.NEW, "#macro(testmacro)#end", "{{velocity}}#testmacro{{/velocity}}");
127   
128  0 assertBlocks(expected, blocks, this.rendererFactory);
129    }
130   
 
131  0 toggle @Test
132    public void testIncludeMacroWithNewContextShowsPassingOnRestrictedFlag() throws Exception
133    {
134  0 String expected = "beginDocument\n" + "beginMetaData "
135    + "[[base]=[wiki:Space.IncludedPage][source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
136    + "beginMacroMarkerStandalone [velocity] [] [$foo]\n" + "beginGroup [[class]=[xwikirenderingerror]]\n"
137    + "onWord [Failed to execute the [velocity] macro. Cause: [The execution of the [velocity] script "
138    + "macro is not allowed. Check the rights of its last author or the parameters if it's rendered "
139    + "from another script.]. Click on this message for details.]\n"
140    + "endGroup [[class]=[xwikirenderingerror]]\n"
141    + "beginGroup [[class]=[xwikirenderingerrordescription hidden]]\n"
142    + "onVerbatim [org.xwiki.rendering.macro.MacroExecutionException: "
143    + "The execution of the [velocity] script macro is not allowed.";
144   
145    // We verify that a Velocity macro set in the including page is not seen in the included page.
146  0 Test failure here List<Block> blocks = runIncludeMacro(Context.NEW, "{{velocity}}$foo{{/velocity}}", true);
147   
148  0 assertBlocksStartsWith(expected, blocks, this.rendererFactory);
149   
150    }
151   
 
152  0 toggle @Test
153    public void testIncludeMacroWithCurrentContextShowsVelocityMacrosAreShared() throws Exception
154    {
155  0 String expected = "beginDocument\n" + "beginMetaData [[source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n"
156    + "onMacroStandalone [velocity] [] [#testmacro]\n"
157    + "endMetaData [[source]=[wiki:Space.IncludedPage][syntax]=[XWiki 2.0]]\n" + "endDocument";
158   
159    // We verify that a Velocity macro set in the including page is seen in the included page.
160  0 Test failure here List<Block> blocks = runIncludeMacroWithPreVelocity(Context.CURRENT, "#macro(testmacro)#end",
161    "{{velocity}}#testmacro{{/velocity}}");
162   
163  0 assertBlocks(expected, blocks, this.rendererFactory);
164    }
165   
 
166  1 toggle @Test
167    public void testIncludeMacroWithNoDocumentSpecified() throws Exception
168    {
169  1 IncludeMacroParameters parameters = new IncludeMacroParameters();
170   
171  1 try {
172  1 this.includeMacro.execute(parameters, null, createMacroTransformationContext("whatever", false));
173  0 Assert.fail("An exception should have been thrown");
174    } catch (MacroExecutionException expected) {
175  1 Assert.assertEquals("You must specify a 'reference' parameter pointing to the entity to include.",
176    expected.getMessage());
177    }
178    }
179   
180    /**
181    * Verify that relative links returned by the Include macro as wrapped with a MetaDataBlock.
182    */
 
183  0 toggle @Test
184    public void testIncludeMacroWhenIncludingDocumentWithRelativeReferences() throws Exception
185    {
186  0 String expected = "beginDocument\n" + "beginMetaData [[base]=[includedWiki:includedSpace.includedPage]"
187    + "[source]=[includedWiki:includedSpace.includedPage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n"
188    + "beginLink [Typed = [false] Type = [doc] Reference = [page]] [false]\n"
189    + "endLink [Typed = [false] Type = [doc] Reference = [page]] [false]\n" + "onSpace\n"
190    + "beginLink [Typed = [true] Type = [attach] Reference = [test.png]] [false]\n"
191    + "endLink [Typed = [true] Type = [attach] Reference = [test.png]] [false]\n" + "onSpace\n"
192    + "onImage [Typed = [false] Type = [attach] Reference = [test.png]] [true]\n" + "endParagraph\n"
193    + "endMetaData [[base]=[includedWiki:includedSpace.includedPage]"
194    + "[source]=[includedWiki:includedSpace.includedPage][syntax]=[XWiki 2.0]]\n" + "endDocument";
195   
196  0 final DocumentReference includedDocumentReference =
197    new DocumentReference("includedWiki", "includedSpace", "includedPage");
198  0 setUpDocumentMock("includedWiki:includedSpace.includedPage", includedDocumentReference,
199    "[[page]] [[attach:test.png]] image:test.png");
200  0 getMockery().checking(new Expectations()
201    {
 
202  0 toggle {
203  0 oneOf(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
204  0 will(returnValue(true));
205  0 oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)),
206    with(any(DocumentModelBridge.class)));
207  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
208  0 will(returnValue(includedDocumentReference));
209  0 oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
210    }
211    });
212   
213  0 IncludeMacroParameters parameters = new IncludeMacroParameters();
214  0 parameters.setReference("includedWiki:includedSpace.includedPage");
215  0 parameters.setContext(Context.NEW);
216   
217  0 List<Block> blocks =
218    Test failure here this.includeMacro.execute(parameters, null, createMacroTransformationContext("whatever", false));
219   
220  0 assertBlocks(expected, blocks, this.rendererFactory);
221    }
222   
 
223  1 toggle @Test
224    public void testIncludeMacroWithRecursiveIncludeContextCurrent() throws Exception
225    {
226  1 this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
227   
228  1 final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
229    // Add an Include Macro MarkerBlock as a parent of the include Macro block since this is what would have
230    // happened if an Include macro is included in another Include macro.
231  1 final MacroMarkerBlock includeMacroMarker =
232    new MacroMarkerBlock("include", Collections.singletonMap("reference", "space.page"),
233    Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), false);
234   
235  1 getMockery().checking(new Expectations()
236    {
 
237  1 toggle {
238  1 allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
239  1 will(returnValue(new DocumentReference("wiki", "space", "page")));
240  1 allowing(mockDocumentReferenceResolver).resolve("space.page", includeMacroMarker);
241  1 will(returnValue(new DocumentReference("wiki", "space", "page")));
242    }
243    });
244   
245  1 IncludeMacroParameters parameters = new IncludeMacroParameters();
246  1 parameters.setReference("wiki:space.page");
247  1 parameters.setContext(Context.CURRENT);
248   
249  1 try {
250  1 this.includeMacro.execute(parameters, null, macroContext);
251  0 Assert.fail("The include macro hasn't checked the recursive inclusion");
252    } catch (MacroExecutionException expected) {
253  1 if (!expected.getMessage().startsWith("Found recursive inclusion")) {
254  0 throw expected;
255    }
256    }
257    }
258   
 
259    private static class ExpectedRecursiveInclusionException extends RuntimeException
260    {
261    }
262   
 
263  1 toggle @Test
264    public void testIncludeMacroWithRecursiveIncludeContextNew() throws Exception
265    {
266  1 final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);
267   
268  1 this.includeMacro.setDocumentAccessBridge(mockSetup.bridge);
269  1 this.includeMacro.setDocumentDisplayer(mockDocumentDisplayer);
270   
271  1 final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
272   
273  1 final IncludeMacroParameters parameters = new IncludeMacroParameters();
274  1 parameters.setReference("wiki:space.page");
275  1 parameters.setContext(Context.NEW);
276   
277  1 getMockery().checking(new Expectations()
278    {
 
279  1 toggle {
280  1 allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
281  1 will(returnValue(new DocumentReference("wiki", "space", "page")));
282   
283  1 allowing(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
284  1 will(returnValue(true));
285  1 allowing(mockSetup.bridge).getDocument(with(any(DocumentReference.class)));
286  1 will(returnValue(null));
287   
288  1 allowing(mockDocumentDisplayer).display(with(same((DocumentModelBridge) null)),
289    with(any(DocumentDisplayerParameters.class)));
290  1 will(new CustomAction("recursively call the include macro again")
291    {
 
292  1 toggle @Override
293    public Object invoke(Invocation invocation) throws Throwable
294    {
295  1 try {
296  1 includeMacro.execute(parameters, null, macroContext);
297    } catch (Exception expected) {
298  1 if (expected.getMessage().contains("Found recursive inclusion")) {
299  1 throw new ExpectedRecursiveInclusionException();
300    }
301    }
302  0 return true;
303    }
304    });
305    }
306    });
307   
308  1 try {
309  1 this.includeMacro.execute(parameters, null, macroContext);
310  0 Assert.fail("The include macro hasn't checked the recursive inclusion");
311    } catch (MacroExecutionException expected) {
312  1 if (!(expected.getCause() instanceof ExpectedRecursiveInclusionException)) {
313  0 throw expected;
314    }
315    }
316    }
317   
 
318  0 toggle @Test
319    public void testIncludeMacroInsideSourceMetaDataBlockAndWithRelativeDocumentReferencePassed() throws Exception
320    {
321  0 String expected = "beginDocument\n" + "beginMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n"
322    + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n"
323    + "endMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "endDocument";
324   
325  0 IncludeMacroParameters parameters = new IncludeMacroParameters();
326  0 parameters.setReference("relativePage");
327   
328  0 final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
329    // Add a Source MetaData Block as a parent of the include Macro block.
330  0 new MetaDataBlock(Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()),
331    new MetaData(Collections.<String, Object>singletonMap(MetaData.BASE, "wiki:space.page")));
332   
333  0 final DocumentReference sourceReference = new DocumentReference("wiki", "space", "page");
334  0 final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "relativePage");
335  0 final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
336  0 getMockery().checking(new Expectations()
337    {
 
338  0 toggle {
339  0 oneOf(mockDocumentReferenceResolver).resolve("relativePage", macroContext.getCurrentMacroBlock());
340  0 will(returnValue(resolvedReference));
341  0 oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
342  0 will(returnValue(true));
343  0 oneOf(mockSetup.bridge).getDocument(resolvedReference);
344  0 will(returnValue(mockDocument));
345  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
346  0 will(returnValue(sourceReference));
347  0 oneOf(mockDocument).getXDOM();
348  0 will(returnValue(getXDOM("content")));
349  0 oneOf(mockDocument).getSyntax();
350  0 will(returnValue(Syntax.XWIKI_2_0));
351    }
352    });
353   
354  0 Test failure here List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext);
355   
356  0 assertBlocks(expected, blocks, this.rendererFactory);
357    }
358   
 
359  0 toggle @Test
360    public void testIncludeMacroWhenSectionSpecified() throws Exception
361    {
362  0 String expected = "beginDocument\n" + "beginMetaData [[source]=[wiki:space.document][syntax]=[XWiki 2.0]]\n"
363    + "beginHeader [1, Hsection]\n" + "onWord [section]\n" + "endHeader [1, Hsection]\n" + "beginParagraph\n"
364    + "onWord [content2]\n" + "endParagraph\n"
365    + "endMetaData [[source]=[wiki:space.document][syntax]=[XWiki 2.0]]\n" + "endDocument";
366   
367  0 IncludeMacroParameters parameters = new IncludeMacroParameters();
368  0 parameters.setReference("document");
369  0 parameters.setSection("Hsection");
370   
371  0 final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
372  0 final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "document");
373  0 final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
374  0 getMockery().checking(new Expectations()
375    {
 
376  0 toggle {
377  0 oneOf(mockDocumentReferenceResolver).resolve("document", macroContext.getCurrentMacroBlock());
378  0 will(returnValue(resolvedReference));
379  0 oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
380  0 will(returnValue(true));
381  0 oneOf(mockSetup.bridge).getDocument(resolvedReference);
382  0 will(returnValue(mockDocument));
383  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
384  0 will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
385  0 oneOf(mockDocument).getSyntax();
386  0 will(returnValue(Syntax.XWIKI_2_0));
387  0 oneOf(mockDocument).getXDOM();
388  0 will(returnValue(getXDOM("content1\n\n= section =\ncontent2")));
389    }
390    });
391   
392  0 Test failure here List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext);
393   
394  0 assertBlocks(expected, blocks, this.rendererFactory);
395    }
396   
 
397  0 toggle @Test
398    public void testIncludeMacroWhenInvalidSectionSpecified() throws Exception
399    {
400  0 IncludeMacroParameters parameters = new IncludeMacroParameters();
401  0 parameters.setReference("document");
402  0 parameters.setSection("unknown");
403   
404  0 final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
405  0 final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "document");
406  0 final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
407  0 getMockery().checking(new Expectations()
408    {
 
409  0 toggle {
410  0 oneOf(mockDocumentReferenceResolver).resolve("document", macroContext.getCurrentMacroBlock());
411  0 will(returnValue(resolvedReference));
412  0 oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
413  0 will(returnValue(true));
414  0 oneOf(mockSetup.bridge).getDocument(resolvedReference);
415  0 will(returnValue(mockDocument));
416  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
417  0 will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
418  0 oneOf(mockDocument).getSyntax();
419  0 will(returnValue(Syntax.XWIKI_2_0));
420  0 oneOf(mockDocument).getXDOM();
421  0 will(returnValue(getXDOM("content")));
422  0 oneOf(mockDocument).getDocumentReference();
423  0 will(returnValue(resolvedReference));
424    }
425    });
426   
427  0 try {
428  0 this.includeMacro.execute(parameters, null, macroContext);
429  0 Assert.fail("Should have raised an exception");
430    } catch (MacroExecutionException expected) {
431  0 Test failure here Assert.assertEquals("Cannot find section [unknown] in document [wiki:space.document]",
432    expected.getMessage());
433    }
434    }
435   
 
436  8 toggle private MacroTransformationContext createMacroTransformationContext(String documentName, boolean isInline)
437    {
438  8 MacroTransformationContext context = new MacroTransformationContext();
439  8 MacroBlock includeMacro =
440    new MacroBlock("include", Collections.singletonMap("reference", documentName), isInline);
441  8 context.setCurrentMacroBlock(includeMacro);
442  8 return context;
443    }
444   
 
445  0 toggle private void setUpDocumentMock(final String resolve, final DocumentReference reference, final String content)
446    throws Exception
447    {
448  0 this.mockDocument = getMockery().mock(DocumentModelBridge.class, resolve);
449  0 getMockery().checking(new Expectations()
450    {
 
451  0 toggle {
452  0 allowing(mockDocumentReferenceResolver).resolve(with(resolve),
453    with(IsArray.array(any(MacroBlock.class))));
454  0 will(returnValue(reference));
455  0 allowing(mockSetup.bridge).getDocument(reference);
456  0 will(returnValue(mockDocument));
457  0 allowing(mockDocument).getSyntax();
458  0 will(returnValue(Syntax.XWIKI_2_0));
459  0 allowing(mockDocument).getXDOM();
460  0 will(returnValue(getXDOM(content)));
461  0 allowing(mockDocument).getDocumentReference();
462  0 will(returnValue(reference));
463    }
464    });
465    }
466   
 
467  0 toggle private XDOM getXDOM(String content) throws Exception
468    {
469  0 Parser parser = getComponentManager().getInstance(Parser.class, "xwiki/2.0");
470  0 return parser.parse(new StringReader(content));
471    }
472   
 
473  0 toggle private List<Block> runIncludeMacroWithPreVelocity(Context context, String velocity, String includedContent)
474    throws Exception
475    {
476  0 Test failure here VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class);
477  0 StringWriter writer = new StringWriter();
478  0 velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer,
479    "wiki:Space.IncludingPage", velocity);
480   
481  0 return runIncludeMacro(context, includedContent);
482    }
483   
 
484  0 toggle private List<Block> runIncludeMacro(final Context context, String includedContent) throws Exception
485    {
486  0 return runIncludeMacro(context, includedContent, false);
487    }
488   
 
489  0 toggle private List<Block> runIncludeMacro(final Context context, String includedContent, boolean restricted)
490    throws Exception
491    {
492  0 final DocumentReference includedDocumentReference = new DocumentReference("wiki", "Space", "IncludedPage");
493  0 String includedDocStringRef = "wiki:space.page";
494  0 setUpDocumentMock(includedDocStringRef, includedDocumentReference, includedContent);
495  0 getMockery().checking(new Expectations()
496    {
 
497  0 toggle {
498  0 allowing(mockSetup.bridge).isDocumentViewable(with(same(includedDocumentReference)));
499  0 will(returnValue(true));
500    // Verify that push/pop are called when context is NEW
501  0 if (context == Context.NEW) {
502  0 oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(same(mockDocument)));
503  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
504  0 will(returnValue(includedDocumentReference));
505  0 oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
506    } else {
507  0 oneOf(mockSetup.bridge).getCurrentDocumentReference();
508  0 will(returnValue(new DocumentReference("wiki", "Space", "IncludingPage")));
509    }
510    }
511    });
512  0 this.includeMacro.setDocumentAccessBridge(this.mockSetup.bridge);
513   
514  0 IncludeMacroParameters parameters = new IncludeMacroParameters();
515  0 parameters.setReference(includedDocStringRef);
516  0 parameters.setContext(context);
517   
518    // Create a Macro transformation context with the Macro transformation object defined so that the include
519    // macro can transform included page which is using a new context.
520  0 MacroTransformation macroTransformation = getComponentManager().getInstance(Transformation.class, "macro");
521  0 MacroTransformationContext macroContext = createMacroTransformationContext(includedDocStringRef, false);
522  0 macroContext.setId("wiki:Space.IncludingPage");
523  0 macroContext.setTransformation(macroTransformation);
524  0 macroContext.getTransformationContext().setRestricted(restricted);
525   
526  0 Test failure here return this.includeMacro.execute(parameters, null, macroContext);
527    }
528    }