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.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 |
|
@link |
46 |
|
|
47 |
|
@version |
48 |
|
|
49 |
|
@AllComponents |
|
|
| 100% |
Uncovered Elements: 0 (78) |
Complexity: 10 |
Complexity Density: 0.15 |
|
50 |
|
public class MacroTransformationTest |
51 |
|
{ |
52 |
|
@Rule |
53 |
|
public final ComponentManagerRule componentManager = new ComponentManagerRule(); |
54 |
|
|
55 |
|
private MacroTransformation transformation; |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
57 |
7 |
@Before... |
58 |
|
public void setUp() throws Exception |
59 |
|
{ |
60 |
7 |
this.transformation = this.componentManager.getInstance(Transformation.class, "macro"); |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
66 |
1 |
@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 |
|
|
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
92 |
1 |
@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 |
|
|
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
120 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 3 |
Complexity Density: 0.21 |
1PASS
|
|
144 |
1 |
@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 |
|
|
164 |
|
|
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 |
|
|
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
179 |
1 |
@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 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
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 |
|
|
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
216 |
1 |
@Test... |
217 |
|
public void macroWithSamePriorityExecuteOnPageOrder() throws Exception |
218 |
|
{ |
219 |
|
|
220 |
|
|
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 |
|
|
246 |
|
|
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 |
|
|
272 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
273 |
1 |
@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 |
|
} |