1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.macro.script

File NestedScriptMacroValidatorTest.java

 

Code metrics

4
41
7
1
142
95
10
0.24
5.86
7
1.43

Classes

Class Line # Actions
NestedScriptMacroValidatorTest 54 41 0% 10 3
0.942307794.2%
 

Contributing tests

This file is covered by 4 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.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    * Unit tests for {@link NestedScriptMacroValidatorListener}.
49    *
50    * @version $Id: f86478ccf788c6a271f87fe2b2486d4e4e1d2767 $
51    * @since 2.4M2
52    */
53    @MockingRequirement(NestedScriptMacroValidatorListener.class)
 
54    public class NestedScriptMacroValidatorTest extends AbstractMockingComponentTestCase<EventListener>
55    {
56    private EventListener validator;
57   
 
58  4 toggle @Before
59    public void configure() throws Exception
60    {
61    // Mock macro manager returns a script macro for "script" and null otherwise.
62  4 final MacroManager macroManager = getComponentManager().getInstance(MacroManager.class);
63  4 final ScriptMacro scriptMacro = new DefaultScriptMacro();
64  4 final TestNestedScriptMacroEnabled nestedScriptMacroEnabled = new TestNestedScriptMacroEnabled();
 
65  4 toggle 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   
 
77  1 toggle @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   
 
86  1 toggle @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   
 
95  1 toggle @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   
 
104  1 toggle @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    * Build a chain of nested macros ({@link MacroMarkerBlock} blocks) and put them into a macro transformation
115    * context. The chain will be the only child of a top level XDOM, the last child will be the current
116    * {@link MacroBlock}.
117    *
118    * @param chain list of nested macro names (starting with parent)
119    * @return an initialized macro transformation context
120    */
 
121  4 toggle 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