| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.rendering.macro.script; |
| 21 |
|
|
| 22 |
|
import java.util.Collections; |
| 23 |
|
import java.util.HashMap; |
| 24 |
|
import java.util.LinkedList; |
| 25 |
|
import java.util.Map; |
| 26 |
|
|
| 27 |
|
import org.junit.Assert; |
| 28 |
|
|
| 29 |
|
import org.jmock.Expectations; |
| 30 |
|
import org.junit.Before; |
| 31 |
|
import org.junit.Test; |
| 32 |
|
import org.xwiki.observation.EventListener; |
| 33 |
|
import org.xwiki.observation.event.CancelableEvent; |
| 34 |
|
import org.xwiki.script.event.ScriptEvaluatingEvent; |
| 35 |
|
import org.xwiki.rendering.block.Block; |
| 36 |
|
import org.xwiki.rendering.block.MacroBlock; |
| 37 |
|
import org.xwiki.rendering.block.MacroMarkerBlock; |
| 38 |
|
import org.xwiki.rendering.block.XDOM; |
| 39 |
|
import org.xwiki.rendering.internal.macro.script.DefaultScriptMacro; |
| 40 |
|
import org.xwiki.rendering.internal.macro.script.NestedScriptMacroValidatorListener; |
| 41 |
|
import org.xwiki.rendering.macro.MacroId; |
| 42 |
|
import org.xwiki.rendering.macro.MacroManager; |
| 43 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
| 44 |
|
import org.xwiki.test.jmock.AbstractMockingComponentTestCase; |
| 45 |
|
import org.xwiki.test.jmock.annotation.MockingRequirement; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@link |
| 49 |
|
|
| 50 |
|
@version |
| 51 |
|
@since |
| 52 |
|
|
| 53 |
|
@MockingRequirement(NestedScriptMacroValidatorListener.class) |
| |
|
| 94.2% |
Uncovered Elements: 3 (52) |
Complexity: 10 |
Complexity Density: 0.24 |
|
| 54 |
|
public class NestedScriptMacroValidatorTest extends AbstractMockingComponentTestCase<EventListener> |
| 55 |
|
{ |
| 56 |
|
private EventListener validator; |
| 57 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 58 |
4 |
@Before... |
| 59 |
|
public void configure() throws Exception |
| 60 |
|
{ |
| 61 |
|
|
| 62 |
4 |
final MacroManager macroManager = getComponentManager().getInstance(MacroManager.class); |
| 63 |
4 |
final ScriptMacro scriptMacro = new DefaultScriptMacro(); |
| 64 |
4 |
final TestNestedScriptMacroEnabled nestedScriptMacroEnabled = new TestNestedScriptMacroEnabled(); |
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 65 |
4 |
getMockery().checking(new Expectations() {{... |
| 66 |
4 |
allowing(macroManager).getMacro(with(new MacroId("script"))); |
| 67 |
4 |
will(returnValue(scriptMacro)); |
| 68 |
4 |
allowing(macroManager).getMacro(with(new MacroId("nestedscriptmacroenabled"))); |
| 69 |
4 |
will(returnValue(nestedScriptMacroEnabled)); |
| 70 |
4 |
allowing(macroManager).getMacro(with(any(MacroId.class))); |
| 71 |
4 |
will(returnValue(null)); |
| 72 |
|
}}); |
| 73 |
|
|
| 74 |
4 |
this.validator = getComponentManager().getInstance(EventListener.class, "nestedscriptmacrovalidator"); |
| 75 |
|
} |
| 76 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 77 |
1 |
@Test... |
| 78 |
|
public void testNoNestedScript() throws Exception |
| 79 |
|
{ |
| 80 |
1 |
MacroTransformationContext context = buildContext("script", "script"); |
| 81 |
1 |
CancelableEvent event = new ScriptEvaluatingEvent(); |
| 82 |
1 |
this.validator.onEvent(event, context, null); |
| 83 |
1 |
Assert.assertTrue(event.isCanceled()); |
| 84 |
|
} |
| 85 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 86 |
1 |
@Test... |
| 87 |
|
public void testNoNestedScriptInHtml() throws Exception |
| 88 |
|
{ |
| 89 |
1 |
MacroTransformationContext context = buildContext("script", "html", "script"); |
| 90 |
1 |
CancelableEvent event = new ScriptEvaluatingEvent(); |
| 91 |
1 |
this.validator.onEvent(event, context, null); |
| 92 |
1 |
Assert.assertTrue(event.isCanceled()); |
| 93 |
|
} |
| 94 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 95 |
1 |
@Test... |
| 96 |
|
public void testIncludeInterceptsNestedChain() throws Exception |
| 97 |
|
{ |
| 98 |
1 |
MacroTransformationContext context = buildContext("script", "include", "script"); |
| 99 |
1 |
CancelableEvent event = new ScriptEvaluatingEvent(); |
| 100 |
1 |
this.validator.onEvent(event, context, null); |
| 101 |
1 |
Assert.assertFalse(event.isCanceled()); |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 104 |
1 |
@Test... |
| 105 |
|
public void testNestedScriptMacroEnabledInterceptsNestedChain() throws Exception |
| 106 |
|
{ |
| 107 |
1 |
MacroTransformationContext context = buildContext("script", "nestedscriptmacroenabled", "script"); |
| 108 |
1 |
CancelableEvent event = new ScriptEvaluatingEvent(); |
| 109 |
1 |
this.validator.onEvent(event, context, null); |
| 110 |
1 |
Assert.assertFalse(event.isCanceled()); |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
|
@link |
| 115 |
|
|
| 116 |
|
@link |
| 117 |
|
|
| 118 |
|
@param |
| 119 |
|
@return |
| 120 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 3 (18) |
Complexity: 4 |
Complexity Density: 0.29 |
|
| 121 |
4 |
private MacroTransformationContext buildContext(String... chain)... |
| 122 |
|
{ |
| 123 |
4 |
MacroTransformationContext context = new MacroTransformationContext(); |
| 124 |
4 |
if (chain == null || chain.length < 1) { |
| 125 |
0 |
context.setXDOM(new XDOM(new LinkedList<Block>())); |
| 126 |
0 |
return context; |
| 127 |
|
} |
| 128 |
|
|
| 129 |
4 |
Map<String, String> parameters = new HashMap<String, String>(); |
| 130 |
4 |
MacroBlock child = new MacroBlock(chain[chain.length-1], parameters, false); |
| 131 |
4 |
Block current = child; |
| 132 |
11 |
for (int i = chain.length-2; i >= 0; i--) { |
| 133 |
7 |
Block parent = new MacroMarkerBlock(chain[i], parameters, Collections.singletonList(current), false); |
| 134 |
7 |
current = parent; |
| 135 |
|
} |
| 136 |
4 |
XDOM root = new XDOM(Collections.singletonList(current)); |
| 137 |
4 |
context.setXDOM(root); |
| 138 |
4 |
context.setCurrentMacroBlock(child); |
| 139 |
4 |
return context; |
| 140 |
|
} |
| 141 |
|
} |
| 142 |
|
|