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

File MacroTransformationTest.java

 

Code metrics

4
66
8
1
299
206
10
0.15
8.25
8
1.25

Classes

Class Line # Actions
MacroTransformationTest 50 66 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 7 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.transformation.macro;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.List;
26   
27    import org.apache.commons.lang3.StringUtils;
28    import org.junit.Assert;
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.xwiki.rendering.block.Block;
33    import org.xwiki.rendering.block.MacroBlock;
34    import org.xwiki.rendering.block.XDOM;
35    import org.xwiki.rendering.renderer.BlockRenderer;
36    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
37    import org.xwiki.rendering.renderer.printer.WikiPrinter;
38    import org.xwiki.rendering.syntax.Syntax;
39    import org.xwiki.rendering.transformation.Transformation;
40    import org.xwiki.rendering.transformation.TransformationContext;
41    import org.xwiki.test.ComponentManagerRule;
42    import org.xwiki.test.annotation.AllComponents;
43   
44    /**
45    * Unit tests for {@link MacroTransformation}.
46    *
47    * @version $Id: e9e25eafa8e39649fffc1cfeb2c0c96a241877e1 $
48    */
49    @AllComponents
 
50    public class MacroTransformationTest
51    {
52    @Rule
53    public final ComponentManagerRule componentManager = new ComponentManagerRule();
54   
55    private MacroTransformation transformation;
56   
 
57  7 toggle @Before
58    public void setUp() throws Exception
59    {
60  7 this.transformation = this.componentManager.getInstance(Transformation.class, "macro");
61    }
62   
63    /**
64    * Test that a simple macro is correctly evaluated.
65    */
 
66  1 toggle @Test
67    public void transformSimpleMacro() throws Exception
68    {
69  1 String expected = "beginDocument\n"
70    + "beginMacroMarkerStandalone [testsimplemacro] []\n"
71    + "beginParagraph\n"
72    + "onWord [simplemacro0]\n"
73    + "endParagraph\n"
74    + "endMacroMarkerStandalone [testsimplemacro] []\n"
75    + "endDocument";
76   
77  1 XDOM dom = new XDOM(Arrays.asList((Block) new MacroBlock("testsimplemacro",
78    Collections.<String, String>emptyMap(), false)));
79   
80  1 this.transformation.transform(dom, new TransformationContext(dom, Syntax.XWIKI_2_0));
81   
82  1 WikiPrinter printer = new DefaultWikiPrinter();
83  1 BlockRenderer eventBlockRenderer =
84    this.componentManager.getInstance(BlockRenderer.class, Syntax.EVENT_1_0.toIdString());
85  1 eventBlockRenderer.render(dom, printer);
86  1 Assert.assertEquals(expected, printer.toString());
87    }
88   
89    /**
90    * Test that a macro can generate another macro.
91    */
 
92  1 toggle @Test
93    public void transformNestedMacro() throws Exception
94    {
95  1 String expected = "beginDocument\n"
96    + "beginMacroMarkerStandalone [testnestedmacro] []\n"
97    + "beginMacroMarkerStandalone [testsimplemacro] []\n"
98    + "beginParagraph\n"
99    + "onWord [simplemacro0]\n"
100    + "endParagraph\n"
101    + "endMacroMarkerStandalone [testsimplemacro] []\n"
102    + "endMacroMarkerStandalone [testnestedmacro] []\n"
103    + "endDocument";
104   
105  1 XDOM dom = new XDOM(Arrays.asList((Block) new MacroBlock("testnestedmacro",
106    Collections.<String, String>emptyMap(), false)));
107   
108  1 this.transformation.transform(dom, new TransformationContext(dom, Syntax.XWIKI_2_0));
109   
110  1 WikiPrinter printer = new DefaultWikiPrinter();
111  1 BlockRenderer eventBlockRenderer =
112    this.componentManager.getInstance(BlockRenderer.class, Syntax.EVENT_1_0.toIdString());
113  1 eventBlockRenderer.render(dom, printer);
114  1 Assert.assertEquals(expected, printer.toString());
115    }
116   
117    /**
118    * Test that we have a safeguard against infinite recursive macros.
119    */
 
120  1 toggle @Test
121    public void transformMacroWithInfiniteRecursion() throws Exception
122    {
123  1 String expected = "beginDocument\n"
124    + StringUtils.repeat("beginMacroMarkerStandalone [testrecursivemacro] []\n", 5)
125    + "onMacroStandalone [testrecursivemacro] []\n"
126    + StringUtils.repeat("endMacroMarkerStandalone [testrecursivemacro] []\n", 5)
127    + "endDocument";
128   
129  1 XDOM dom = new XDOM(Arrays.asList((Block) new MacroBlock("testrecursivemacro",
130    Collections.<String, String>emptyMap(), false)));
131   
132    // In order to have a fast test, set the max macro execution to 5 macros.
133  1 this.transformation.setMaxRecursions(4);
134   
135  1 this.transformation.transform(dom, new TransformationContext(dom, Syntax.XWIKI_2_0));
136   
137  1 WikiPrinter printer = new DefaultWikiPrinter();
138  1 BlockRenderer eventBlockRenderer =
139    this.componentManager.getInstance(BlockRenderer.class, Syntax.EVENT_1_0.toIdString());
140  1 eventBlockRenderer.render(dom, printer);
141  1 Assert.assertEquals(expected, printer.toString());
142    }
143   
 
144  1 toggle @Test
145    public void transformWhenLotsOfMacrosButNoInfiniteRecursion() throws Exception
146    {
147  1 String expected = "beginDocument\n";
148  11 for (int i = 0; i < 10; i++) {
149  10 expected += "beginMacroMarkerStandalone [testsimplemacro] []\n"
150    + "beginParagraph\n"
151    + "onWord [simplemacro" + i + "]\n"
152    + "endParagraph\n"
153    + "endMacroMarkerStandalone [testsimplemacro] []\n";
154    }
155  1 expected += "endDocument";
156   
157  1 List<Block> macroBlocks = new ArrayList<Block>();
158  11 for (int i = 0; i < 10; i++) {
159  10 macroBlocks.add(new MacroBlock("testsimplemacro", Collections.<String, String>emptyMap(), false));
160    }
161  1 XDOM dom = new XDOM(macroBlocks);
162   
163    // Make sure that the max macro execution is less than the # of macros we have in the content to prove that
164    // we only stop on real recursion.
165  1 this.transformation.setMaxRecursions(5);
166   
167  1 this.transformation.transform(dom, new TransformationContext(dom, Syntax.XWIKI_2_0));
168   
169  1 WikiPrinter printer = new DefaultWikiPrinter();
170  1 BlockRenderer eventBlockRenderer =
171    this.componentManager.getInstance(BlockRenderer.class, Syntax.EVENT_1_0.toIdString());
172  1 eventBlockRenderer.render(dom, printer);
173  1 Assert.assertEquals(expected, printer.toString());
174    }
175   
176    /**
177    * Test that macro priorities are working.
178    */
 
179  1 toggle @Test
180    public void transformMacrosWithPriorities() throws Exception
181    {
182  1 String expected = "beginDocument\n"
183    + "beginMacroMarkerStandalone [testsimplemacro] []\n"
184    + "beginParagraph\n"
185    + "onWord [simplemacro1]\n"
186    + "endParagraph\n"
187    + "endMacroMarkerStandalone [testsimplemacro] []\n"
188    + "beginMacroMarkerStandalone [testprioritymacro] []\n"
189    + "beginParagraph\n"
190    + "onWord [word]\n"
191    + "endParagraph\n"
192    + "endMacroMarkerStandalone [testprioritymacro] []\n"
193    + "endDocument";
194   
195    // "testprioritymacro" has a highest priority than "testsimplemacro" and will be executed first.
196    // This is verified as follows:
197    // - "testprioritymacro" generates a WordBlock
198    // - "testsimplemacro" outputs "simplemacro" followed by the number of WordBlocks that exist in the document
199    // Thus if "testsimplemacro" is executed before "testprioritymacro" it would print "simplemacro0"
200  1 XDOM dom = new XDOM(Arrays.<Block>asList(
201    new MacroBlock("testsimplemacro", Collections.<String, String>emptyMap(), false),
202    new MacroBlock("testprioritymacro", Collections.<String, String>emptyMap(), false)));
203   
204  1 this.transformation.transform(dom, new TransformationContext(dom, Syntax.XWIKI_2_0));
205   
206  1 WikiPrinter printer = new DefaultWikiPrinter();
207  1 BlockRenderer eventBlockRenderer =
208    this.componentManager.getInstance(BlockRenderer.class, Syntax.EVENT_1_0.toIdString());
209  1 eventBlockRenderer.render(dom, printer);
210  1 Assert.assertEquals(expected, printer.toString());
211    }
212   
213    /**
214    * Test that macro with same priorities execute in the order in which they are defined.
215    */
 
216  1 toggle @Test
217    public void macroWithSamePriorityExecuteOnPageOrder() throws Exception
218    {
219    // Both macros have the same priorities and thus "testsimplemacro" should be executed first and generate
220    // "simplemacro0".
221  1 XDOM dom = new XDOM(Arrays.<Block>asList(
222    new MacroBlock("testsimplemacro", Collections.<String, String>emptyMap(), false),
223    new MacroBlock("testcontentmacro", Collections.<String, String>emptyMap(), "content", false)));
224   
225  1 TransformationContext context = new TransformationContext(dom, Syntax.XWIKI_2_0);
226  1 this.transformation.transform(dom, context);
227   
228  1 WikiPrinter printer = new DefaultWikiPrinter();
229  1 BlockRenderer eventBlockRenderer =
230    this.componentManager.getInstance(BlockRenderer.class, Syntax.EVENT_1_0.toIdString());
231  1 eventBlockRenderer.render(dom, printer);
232   
233  1 String expected = "beginDocument\n"
234    + "beginMacroMarkerStandalone [testsimplemacro] []\n"
235    + "beginParagraph\n"
236    + "onWord [simplemacro0]\n"
237    + "endParagraph\n"
238    + "endMacroMarkerStandalone [testsimplemacro] []\n"
239    + "beginMacroMarkerStandalone [testcontentmacro] [] [content]\n"
240    + "onWord [content]\n"
241    + "endMacroMarkerStandalone [testcontentmacro] [] [content]\n"
242    + "endDocument";
243  1 Assert.assertEquals(expected, printer.toString());
244   
245    // We must also test the other order ("testcontentmacro" before "testsimplemacro") to ensure for example that
246    // there's no lexical order on Macro class names for example.
247  1 dom = new XDOM(Arrays.<Block>asList(
248    new MacroBlock("testcontentmacro", Collections.<String, String>emptyMap(), "content", false),
249    new MacroBlock("testsimplemacro", Collections.<String, String>emptyMap(), false)));
250   
251  1 context.setXDOM(dom);
252  1 this.transformation.transform(dom, context);
253   
254  1 printer = new DefaultWikiPrinter();
255  1 eventBlockRenderer.render(dom, printer);
256   
257  1 expected = "beginDocument\n"
258    + "beginMacroMarkerStandalone [testcontentmacro] [] [content]\n"
259    + "onWord [content]\n"
260    + "endMacroMarkerStandalone [testcontentmacro] [] [content]\n"
261    + "beginMacroMarkerStandalone [testsimplemacro] []\n"
262    + "beginParagraph\n"
263    + "onWord [simplemacro1]\n"
264    + "endParagraph\n"
265    + "endMacroMarkerStandalone [testsimplemacro] []\n"
266    + "endDocument";
267  1 Assert.assertEquals(expected, printer.toString());
268    }
269   
270    /**
271    * Test that a not existing macro generate an error in the XDOM.
272    */
 
273  1 toggle @Test
274    public void transformNotExistingMacro() throws Exception
275    {
276  1 String expected = "beginDocument\n"
277    + "beginMacroMarkerStandalone [notexisting] []\n"
278    + "beginGroup [[class]=[xwikirenderingerror]]\n"
279    + "onWord [Unknown macro: notexisting.]\n"
280    + "endGroup [[class]=[xwikirenderingerror]]\n"
281    + "beginGroup [[class]=[xwikirenderingerrordescription hidden]]\n"
282    + "onVerbatim [The \"notexisting\" macro is not in the list of registered macros. "
283    + "Verify the spelling or contact your administrator.] [false]\n"
284    + "endGroup [[class]=[xwikirenderingerrordescription hidden]]\n"
285    + "endMacroMarkerStandalone [notexisting] []\n"
286    + "endDocument";
287   
288  1 XDOM dom = new XDOM(Arrays.asList((Block) new MacroBlock("notexisting",
289    Collections.<String, String>emptyMap(), false)));
290   
291  1 this.transformation.transform(dom, new TransformationContext(dom, Syntax.XWIKI_2_0));
292   
293  1 WikiPrinter printer = new DefaultWikiPrinter();
294  1 BlockRenderer eventBlockRenderer =
295    this.componentManager.getInstance(BlockRenderer.class, Syntax.EVENT_1_0.toIdString());
296  1 eventBlockRenderer.render(dom, printer);
297  1 Assert.assertEquals(expected, printer.toString());
298    }
299    }