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.cache; |
21 |
|
|
22 |
|
import java.io.StringWriter; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import org.junit.Test; |
26 |
|
import org.xwiki.rendering.block.Block; |
27 |
|
import org.xwiki.rendering.internal.transformation.macro.MacroTransformation; |
28 |
|
import org.xwiki.rendering.macro.Macro; |
29 |
|
import org.xwiki.rendering.macro.cache.CacheMacroParameters; |
30 |
|
import org.xwiki.rendering.macro.script.ScriptMockSetup; |
31 |
|
import org.xwiki.rendering.renderer.PrintRendererFactory; |
32 |
|
import org.xwiki.rendering.syntax.Syntax; |
33 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
34 |
|
import org.xwiki.rendering.transformation.Transformation; |
35 |
|
import org.xwiki.test.jmock.AbstractComponentTestCase; |
36 |
|
import org.xwiki.velocity.VelocityManager; |
37 |
|
|
38 |
|
import static org.xwiki.rendering.test.BlockAssert.assertBlocks; |
39 |
|
import static org.junit.Assert.*; |
40 |
|
|
41 |
|
|
42 |
|
@link |
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
|
|
| 39.2% |
Uncovered Elements: 48 (79) |
Complexity: 8 |
Complexity Density: 0.11 |
|
47 |
|
public class CacheMacroTest extends AbstractComponentTestCase |
48 |
|
{ |
49 |
|
private ScriptMockSetup mockSetup; |
50 |
|
|
51 |
|
private CacheMacro cacheMacro; |
52 |
|
|
53 |
|
private PrintRendererFactory rendererFactory; |
54 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
55 |
6 |
@Override... |
56 |
|
protected void registerComponents() throws Exception |
57 |
|
{ |
58 |
6 |
super.registerComponents(); |
59 |
|
|
60 |
6 |
this.mockSetup = new ScriptMockSetup(getMockery(), getComponentManager()); |
61 |
6 |
this.cacheMacro = getComponentManager().getInstance(Macro.class, "cache"); |
62 |
6 |
this.rendererFactory = getComponentManager().getInstance(PrintRendererFactory.class, "event/1.0"); |
63 |
|
} |
64 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
3FAIL
|
|
65 |
0 |
@Test... |
66 |
|
public void executeWhenNoIdAndSameContent() throws Exception |
67 |
|
{ |
68 |
0 |
String expected = "beginDocument\n" |
69 |
|
+ "beginMacroMarkerStandalone [velocity] [] [$var]\n" |
70 |
|
+ "beginParagraph\n" |
71 |
|
+ "onWord [content]\n" |
72 |
|
+ "endParagraph\n" |
73 |
|
+ "endMacroMarkerStandalone [velocity] [] [$var]\n" |
74 |
|
+ "endDocument"; |
75 |
|
|
76 |
0 |
CacheMacroParameters params = new CacheMacroParameters(); |
77 |
0 |
MacroTransformationContext context = createMacroTransformationContext(); |
78 |
|
|
79 |
0 |
VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class); |
80 |
0 |
StringWriter writer = new StringWriter(); |
81 |
0 |
velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", |
82 |
|
"#set ($var = 'content')"); |
83 |
|
|
84 |
0 |
List<Block> result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context); |
85 |
0 |
assertBlocks(expected, result, this.rendererFactory); |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
0 |
velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", |
90 |
|
"#set ($var = 'newcontent')"); |
91 |
0 |
result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context); |
92 |
0 |
assertBlocks(expected, result, this.rendererFactory); |
93 |
|
} |
94 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
3FAIL
|
|
95 |
0 |
@Test... |
96 |
|
public void executeWhenNoIdAndDifferentContent() throws Exception |
97 |
|
{ |
98 |
0 |
String expected1 = "beginDocument\n" |
99 |
|
+ "beginMacroMarkerStandalone [velocity] [] [$var]\n" |
100 |
|
+ "beginParagraph\n" |
101 |
|
+ "onWord [content]\n" |
102 |
|
+ "endParagraph\n" |
103 |
|
+ "endMacroMarkerStandalone [velocity] [] [$var]\n" |
104 |
|
+ "endDocument"; |
105 |
|
|
106 |
0 |
String expected2 = "beginDocument\n" |
107 |
|
+ "beginMacroMarkerStandalone [velocity] [] [$var##]\n" |
108 |
|
+ "beginParagraph\n" |
109 |
|
+ "onWord [newcontent]\n" |
110 |
|
+ "endParagraph\n" |
111 |
|
+ "endMacroMarkerStandalone [velocity] [] [$var##]\n" |
112 |
|
+ "endDocument"; |
113 |
|
|
114 |
0 |
CacheMacroParameters params = new CacheMacroParameters(); |
115 |
0 |
MacroTransformationContext context = createMacroTransformationContext(); |
116 |
|
|
117 |
0 |
VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class); |
118 |
0 |
StringWriter writer = new StringWriter(); |
119 |
0 |
velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", |
120 |
|
"#set ($var = 'content')"); |
121 |
|
|
122 |
0 |
List<Block> result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context); |
123 |
0 |
assertBlocks(expected1, result, this.rendererFactory); |
124 |
|
|
125 |
|
|
126 |
0 |
velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", |
127 |
|
"#set ($var = 'newcontent')"); |
128 |
0 |
result = this.cacheMacro.execute(params, "{{velocity}}$var##{{/velocity}}", context); |
129 |
0 |
assertBlocks(expected2, result, this.rendererFactory); |
130 |
|
} |
131 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
3FAIL
|
|
132 |
0 |
@Test... |
133 |
|
public void executeWhenSameIdAndDifferentContent() throws Exception |
134 |
|
{ |
135 |
0 |
String expected = "beginDocument\n" |
136 |
|
+ "beginMacroMarkerStandalone [velocity] [] [$var]\n" |
137 |
|
+ "beginParagraph\n" |
138 |
|
+ "onWord [content]\n" |
139 |
|
+ "endParagraph\n" |
140 |
|
+ "endMacroMarkerStandalone [velocity] [] [$var]\n" |
141 |
|
+ "endDocument"; |
142 |
|
|
143 |
0 |
CacheMacroParameters params = new CacheMacroParameters(); |
144 |
0 |
params.setId("uniqueid"); |
145 |
0 |
MacroTransformationContext context = createMacroTransformationContext(); |
146 |
|
|
147 |
0 |
VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class); |
148 |
0 |
StringWriter writer = new StringWriter(); |
149 |
0 |
velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", |
150 |
|
"#set ($var = 'content')"); |
151 |
|
|
152 |
0 |
List<Block> result = this.cacheMacro.execute(params, "{{velocity}}$var{{/velocity}}", context); |
153 |
0 |
assertBlocks(expected, result, this.rendererFactory); |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
0 |
result = this.cacheMacro.execute(params, "whatever here...", context); |
158 |
0 |
assertBlocks(expected, result, this.rendererFactory); |
159 |
|
} |
160 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
3FAIL
|
|
161 |
0 |
@Test... |
162 |
|
public void executeWithIdGeneratedByVelocityMacro() throws Exception |
163 |
|
{ |
164 |
0 |
VelocityManager velocityManager = getComponentManager().getInstance(VelocityManager.class); |
165 |
0 |
StringWriter writer = new StringWriter(); |
166 |
0 |
velocityManager.getVelocityEngine().evaluate(velocityManager.getVelocityContext(), writer, "template", |
167 |
|
"#set ($var = 'generatedid')"); |
168 |
|
|
169 |
0 |
CacheMacroParameters params = new CacheMacroParameters(); |
170 |
0 |
params.setId("{{velocity}}$var{{/velocity}}"); |
171 |
0 |
MacroTransformationContext context = createMacroTransformationContext(); |
172 |
|
|
173 |
0 |
List<Block> result1 = this.cacheMacro.execute(params, "whatever", context); |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
0 |
params.setId("generatedid"); |
178 |
0 |
List<Block> result2 = this.cacheMacro.execute(params, "something else", context); |
179 |
0 |
assertEquals(result1, result2); |
180 |
|
} |
181 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
182 |
1 |
@Test... |
183 |
|
public void executeWithDifferentTimeToLive() throws Exception |
184 |
|
{ |
185 |
1 |
CacheMacroParameters params = new CacheMacroParameters(); |
186 |
1 |
MacroTransformationContext context = createMacroTransformationContext(); |
187 |
|
|
188 |
1 |
params.setId("id"); |
189 |
1 |
params.setMaxEntries(10); |
190 |
1 |
params.setTimeToLive(100); |
191 |
1 |
List<Block> result1 = this.cacheMacro.execute(params, "content1", context); |
192 |
|
|
193 |
|
|
194 |
|
|
195 |
1 |
params.setTimeToLive(200); |
196 |
1 |
List<Block> result2 = this.cacheMacro.execute(params, "content2", context); |
197 |
1 |
assertFalse(result2.equals(result1)); |
198 |
|
} |
199 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
200 |
1 |
@Test... |
201 |
|
public void executeWithDifferentMaxEntries() throws Exception |
202 |
|
{ |
203 |
1 |
CacheMacroParameters params = new CacheMacroParameters(); |
204 |
1 |
MacroTransformationContext context = createMacroTransformationContext(); |
205 |
|
|
206 |
1 |
params.setId("id"); |
207 |
1 |
params.setMaxEntries(10); |
208 |
1 |
params.setTimeToLive(100); |
209 |
1 |
List<Block> result1 = this.cacheMacro.execute(params, "content1", context); |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
1 |
params.setMaxEntries(11); |
214 |
1 |
List<Block> result2 = this.cacheMacro.execute(params, "content2", context); |
215 |
1 |
assertFalse(result2.equals(result1)); |
216 |
|
} |
217 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
218 |
5 |
private MacroTransformationContext createMacroTransformationContext() throws Exception... |
219 |
|
{ |
220 |
5 |
MacroTransformation macroTransformation = getComponentManager().getInstance(Transformation.class, "macro"); |
221 |
5 |
MacroTransformationContext context = new MacroTransformationContext(); |
222 |
5 |
context.setTransformation(macroTransformation); |
223 |
5 |
context.setSyntax(Syntax.XWIKI_2_0); |
224 |
5 |
return context; |
225 |
|
} |
226 |
|
} |