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

File ContextMacroTest.java

 

Code metrics

0
69
6
1
199
141
8
0.12
11.5
6
1.33

Classes

Class Line # Actions
ContextMacroTest 59 69 0% 8 2
0.9733333697.3%
 

Contributing tests

This file is covered by 5 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.internal.macro.context;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24   
25    import org.junit.Assert;
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.xwiki.bridge.DocumentAccessBridge;
30    import org.xwiki.bridge.DocumentModelBridge;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.DocumentReferenceResolver;
33    import org.xwiki.properties.BeanDescriptor;
34    import org.xwiki.properties.BeanManager;
35    import org.xwiki.rendering.block.Block;
36    import org.xwiki.rendering.block.LinkBlock;
37    import org.xwiki.rendering.block.MacroBlock;
38    import org.xwiki.rendering.block.ParagraphBlock;
39    import org.xwiki.rendering.block.XDOM;
40    import org.xwiki.rendering.listener.MetaData;
41    import org.xwiki.rendering.listener.reference.ResourceReference;
42    import org.xwiki.rendering.listener.reference.ResourceType;
43    import org.xwiki.rendering.macro.MacroContentParser;
44    import org.xwiki.rendering.macro.MacroExecutionException;
45    import org.xwiki.rendering.macro.context.ContextMacroParameters;
46    import org.xwiki.rendering.syntax.Syntax;
47    import org.xwiki.rendering.transformation.MacroTransformationContext;
48    import org.xwiki.test.mockito.MockitoComponentMockingRule;
49   
50    import static org.junit.Assert.*;
51    import static org.mockito.Mockito.*;
52   
53    /**
54    * Unit tests for {@link ContextMacro}.
55    *
56    * @version $Id: 887777d369139cbede7ba6fe5ff71ca931774f0e $
57    * @since 8.3RC1
58    */
 
59    public class ContextMacroTest
60    {
61    @Rule
62    public MockitoComponentMockingRule<ContextMacro> mocker = new MockitoComponentMockingRule<>(ContextMacro.class);
63   
 
64  5 toggle @Before
65    public void setUp() throws Exception
66    {
67    // Macro Descriptor set up
68  5 BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
69  5 BeanDescriptor descriptor = mock(BeanDescriptor.class);
70  5 when(descriptor.getProperties()).thenReturn(Collections.emptyList());
71  5 when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
72    }
73   
 
74  1 toggle @Test
75    public void executeWhenNoDocumentSpecified() throws Exception
76    {
77  1 ContextMacroParameters parameters = new ContextMacroParameters();
78   
79  1 try {
80  1 this.mocker.getComponentUnderTest().execute(parameters, "", new MacroTransformationContext());
81  0 fail("Should have thrown an exception");
82    } catch (MacroExecutionException expected) {
83  1 Assert.assertEquals("You must specify a 'document' parameter pointing to the document to set in the "
84    + "context as the current document.", expected.getMessage());
85    }
86    }
87   
 
88  1 toggle @Test
89    public void executeWithReferencedDocumentHavingProgrammingRightsButNotTheCallingDocument() throws Exception
90    {
91  1 MacroTransformationContext macroContext = new MacroTransformationContext();
92  1 MacroBlock macroBlock = new MacroBlock("context", Collections.emptyMap(), false);
93  1 macroContext.setCurrentMacroBlock(macroBlock);
94   
95  1 DocumentReferenceResolver<String> resolver =
96    this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
97  1 when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(
98    new DocumentReference("wiki", "space", "page"));
99   
100  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
101  1 when(dab.hasProgrammingRights()).thenReturn(false).thenReturn(true);
102   
103  1 ContextMacroParameters parameters = new ContextMacroParameters();
104  1 parameters.setDocument("wiki:space.page");
105   
106  1 try {
107  1 this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
108  0 fail("Should have thrown an exception");
109    } catch (MacroExecutionException expected) {
110  1 assertEquals("Current document must have programming rights since the context document provided ["
111    + "wiki:space.page] has programming rights.", expected.getMessage());
112    }
113    }
114   
 
115  1 toggle @Test
116    public void executeWithReferencedDocumentHavingProgrammingRightsAndCallingDocumentToo() throws Exception
117    {
118  1 MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
119  1 MacroTransformationContext macroContext = new MacroTransformationContext();
120  1 macroContext.setSyntax(Syntax.XWIKI_2_0);
121  1 macroContext.setCurrentMacroBlock(macroBlock);
122   
123  1 DocumentReferenceResolver<String> resolver =
124    this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
125  1 DocumentReference referencedDocumentReference = new DocumentReference("wiki", "space", "page");
126  1 when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(referencedDocumentReference);
127   
128  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
129  1 when(dab.hasProgrammingRights()).thenReturn(true).thenReturn(true);
130  1 DocumentModelBridge dmb = mock(DocumentModelBridge.class);
131  1 when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
132   
133  1 MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
134  1 when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(
135    new XDOM(Collections.emptyList()));
136   
137  1 ContextMacroParameters parameters = new ContextMacroParameters();
138  1 parameters.setDocument("wiki:space.page");
139   
140  1 this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
141    }
142   
 
143  1 toggle @Test
144    public void executeOk() throws Exception
145    {
146  1 MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
147  1 MacroTransformationContext macroContext = new MacroTransformationContext();
148  1 macroContext.setSyntax(Syntax.XWIKI_2_0);
149  1 macroContext.setCurrentMacroBlock(macroBlock);
150   
151  1 DocumentReferenceResolver<String> resolver =
152    this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
153  1 DocumentReference referencedDocumentReference = new DocumentReference("wiki", "space", "page");
154  1 when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(referencedDocumentReference);
155   
156  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
157  1 DocumentModelBridge dmb = mock(DocumentModelBridge.class);
158  1 when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
159   
160  1 MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
161  1 XDOM xdom = new XDOM(Arrays.asList((Block) new ParagraphBlock(Arrays.asList((Block) new LinkBlock(
162    Collections.emptyList(), new ResourceReference("", ResourceType.DOCUMENT), false)))));
163  1 when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(xdom);
164   
165  1 ContextMacroParameters parameters = new ContextMacroParameters();
166  1 parameters.setDocument("wiki:space.page");
167   
168    // Note: we're not testing the returned value here since this is done in integation tests.
169  1 this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
170    }
171   
 
172  1 toggle @Test
173    public void executeWithRelativeDocumentReferenceParameter() throws Exception
174    {
175  1 MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
176   
177  1 MacroTransformationContext macroContext = new MacroTransformationContext();
178  1 macroContext.setSyntax(Syntax.XWIKI_2_0);
179  1 macroContext.setCurrentMacroBlock(macroBlock);
180   
181  1 DocumentReferenceResolver<String> resolver =
182    this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
183  1 DocumentReference referencedDocumentReference = new DocumentReference("basewiki", "basespace", "page");
184  1 when(resolver.resolve("page", macroBlock)).thenReturn(referencedDocumentReference);
185   
186  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
187  1 DocumentModelBridge dmb = mock(DocumentModelBridge.class);
188  1 when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
189   
190  1 MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
191  1 when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(
192    new XDOM(Collections.emptyList()));
193   
194  1 ContextMacroParameters parameters = new ContextMacroParameters();
195  1 parameters.setDocument("page");
196   
197  1 this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
198    }
199    }