| 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; |
| 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 |
|
@link |
| 68 |
|
|
| 69 |
|
@version |
| 70 |
|
@since |
| 71 |
|
|
| |
|
| 28.6% |
Uncovered Elements: 165 (231) |
Complexity: 36 |
Complexity Density: 0.18 |
|
| 72 |
|
public class IncludeMacroTest extends AbstractComponentTestCase |
| 73 |
|
{ |
| 74 |
|
private ScriptMockSetup mockSetup; |
| 75 |
|
|
| 76 |
|
private IncludeMacro includeMacro; |
| 77 |
|
|
| 78 |
|
private PrintRendererFactory rendererFactory; |
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
private DocumentReferenceResolver<String> mockDocumentReferenceResolver; |
| 84 |
|
|
| 85 |
|
private AuthorizationManager mockAuthorization; |
| 86 |
|
|
| 87 |
|
private DocumentModelBridge mockDocument; |
| 88 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 89 |
10 |
@Override... |
| 90 |
|
public void setUp() throws Exception |
| 91 |
|
{ |
| 92 |
10 |
super.setUp(); |
| 93 |
|
|
| 94 |
|
|
| 95 |
10 |
Execution execution = getComponentManager().getInstance(Execution.class); |
| 96 |
10 |
execution.getContext().setProperty("xwikicontext", new HashMap<Object, Object>()); |
| 97 |
|
} |
| 98 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 99 |
10 |
@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 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
3FAIL
|
|
| 114 |
0 |
@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 |
|
|
| 125 |
0 |
List<Block> blocks = |
| 126 |
|
runIncludeMacroWithPreVelocity(Context.NEW, "#macro(testmacro)#end", "{{velocity}}#testmacro{{/velocity}}"); |
| 127 |
|
|
| 128 |
0 |
assertBlocks(expected, blocks, this.rendererFactory); |
| 129 |
|
} |
| 130 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
3FAIL
|
|
| 131 |
0 |
@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 |
|
|
| 146 |
0 |
List<Block> blocks = runIncludeMacro(Context.NEW, "{{velocity}}$foo{{/velocity}}", true); |
| 147 |
|
|
| 148 |
0 |
assertBlocksStartsWith(expected, blocks, this.rendererFactory); |
| 149 |
|
|
| 150 |
|
} |
| 151 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
3FAIL
|
|
| 152 |
0 |
@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 |
|
|
| 160 |
0 |
List<Block> blocks = runIncludeMacroWithPreVelocity(Context.CURRENT, "#macro(testmacro)#end", |
| 161 |
|
"{{velocity}}#testmacro{{/velocity}}"); |
| 162 |
|
|
| 163 |
0 |
assertBlocks(expected, blocks, this.rendererFactory); |
| 164 |
|
} |
| 165 |
|
|
| |
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
| 166 |
1 |
@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 |
|
|
| 182 |
|
|
| |
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
3FAIL
|
|
| 183 |
0 |
@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 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 202 |
0 |
{... |
| 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 |
|
this.includeMacro.execute(parameters, null, createMacroTransformationContext("whatever", false)); |
| 219 |
|
|
| 220 |
0 |
assertBlocks(expected, blocks, this.rendererFactory); |
| 221 |
|
} |
| 222 |
|
|
| |
|
| 78.6% |
Uncovered Elements: 3 (14) |
Complexity: 3 |
Complexity Density: 0.25 |
1PASS
|
|
| 223 |
1 |
@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 |
|
|
| 230 |
|
|
| 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 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 237 |
1 |
{... |
| 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 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 259 |
|
private static class ExpectedRecursiveInclusionException extends RuntimeException |
| 260 |
|
{ |
| 261 |
|
} |
| 262 |
|
|
| |
|
| 80% |
Uncovered Elements: 3 (15) |
Complexity: 3 |
Complexity Density: 0.23 |
1PASS
|
|
| 263 |
1 |
@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 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
| 279 |
1 |
{... |
| 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 |
|
{ |
| |
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 292 |
1 |
@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 |
|
|
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
3FAIL
|
|
| 318 |
0 |
@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 |
|
|
| 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 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 338 |
0 |
{... |
| 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 |
List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext); |
| 355 |
|
|
| 356 |
0 |
assertBlocks(expected, blocks, this.rendererFactory); |
| 357 |
|
} |
| 358 |
|
|
| |
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
3FAIL
|
|
| 359 |
0 |
@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 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 376 |
0 |
{... |
| 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 |
List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext); |
| 393 |
|
|
| 394 |
0 |
assertBlocks(expected, blocks, this.rendererFactory); |
| 395 |
|
} |
| 396 |
|
|
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
3FAIL
|
|
| 397 |
0 |
@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 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
|
| 409 |
0 |
{... |
| 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 |
Assert.assertEquals("Cannot find section [unknown] in document [wiki:space.document]", |
| 432 |
|
expected.getMessage()); |
| 433 |
|
} |
| 434 |
|
} |
| 435 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 436 |
8 |
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 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 445 |
0 |
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 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
| 451 |
0 |
{... |
| 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 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 467 |
0 |
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 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 473 |
0 |
private List<Block> runIncludeMacroWithPreVelocity(Context context, String velocity, String includedContent)... |
| 474 |
|
throws Exception |
| 475 |
|
{ |
| 476 |
0 |
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 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 484 |
0 |
private List<Block> runIncludeMacro(final Context context, String includedContent) throws Exception... |
| 485 |
|
{ |
| 486 |
0 |
return runIncludeMacro(context, includedContent, false); |
| 487 |
|
} |
| 488 |
|
|
| |
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
|
| 489 |
0 |
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 |
|
{ |
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 497 |
0 |
{... |
| 498 |
0 |
allowing(mockSetup.bridge).isDocumentViewable(with(same(includedDocumentReference))); |
| 499 |
0 |
will(returnValue(true)); |
| 500 |
|
|
| 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 |
|
|
| 519 |
|
|
| 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 |
return this.includeMacro.execute(parameters, null, macroContext); |
| 527 |
|
} |
| 528 |
|
} |