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

File MacroContentExecutorTest.java

 

Code metrics

0
27
3
1
109
64
4
0.15
9
3
1.33

Classes

Class Line # Actions
MacroContentExecutorTest 46 27 0% 4 1
0.9666666496.7%
 

Contributing tests

This file is covered by 3 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.executor;
21   
22    import java.util.Collections;
23   
24    import org.junit.Rule;
25    import org.junit.Test;
26    import org.xwiki.model.reference.DocumentReference;
27    import org.xwiki.rendering.block.XDOM;
28    import org.xwiki.rendering.executor.ContentExecutorException;
29    import org.xwiki.rendering.parser.ContentParser;
30    import org.xwiki.rendering.syntax.Syntax;
31    import org.xwiki.rendering.transformation.MacroTransformationContext;
32    import org.xwiki.rendering.transformation.Transformation;
33    import org.xwiki.rendering.transformation.TransformationContext;
34    import org.xwiki.rendering.transformation.TransformationException;
35    import org.xwiki.test.mockito.MockitoComponentMockingRule;
36   
37    import static org.mockito.Mockito.*;
38    import static org.junit.Assert.*;
39   
40    /**
41    * Unit tests for {@link MacroContentExecutor}.
42    *
43    * @version $Id: aa7be7e9462b341fffed4d0f2714967104be94a5 $
44    * @since 8.4RC1
45    */
 
46    public class MacroContentExecutorTest
47    {
48    @Rule
49    public final MockitoComponentMockingRule<MacroContentExecutor> mocker =
50    new MockitoComponentMockingRule<>(MacroContentExecutor.class);
51   
52    private static final DocumentReference DOCUMENT_REFERENCE = new DocumentReference("wiki", "space", "page");
53   
 
54  1 toggle @Test
55    public void executeWithNoSource() throws Exception
56    {
57  1 XDOM parsedBlocks = new XDOM(Collections.emptyList());
58  1 ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
59  1 when(contentParser.parse("", Syntax.PLAIN_1_0)).thenReturn(parsedBlocks);
60   
61  1 TransformationContext transformationContext = new TransformationContext();
62  1 MacroTransformationContext context = new MacroTransformationContext(transformationContext);
63   
64  1 this.mocker.getComponentUnderTest().execute("", Syntax.PLAIN_1_0, context);
65   
66    // The test is here: Verify that the Macro Transformation has been called
67  1 Transformation macroTransformation = this.mocker.getInstance(Transformation.class, "macro");
68  1 verify(macroTransformation).transform(parsedBlocks, transformationContext);
69    }
70   
 
71  1 toggle @Test
72    public void executeWithSource() throws Exception
73    {
74  1 XDOM parsedBlocks = new XDOM(Collections.emptyList());
75  1 ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
76  1 when(contentParser.parse("", Syntax.PLAIN_1_0, DOCUMENT_REFERENCE)).thenReturn(parsedBlocks);
77   
78  1 TransformationContext transformationContext = new TransformationContext();
79  1 MacroTransformationContext context = new MacroTransformationContext(transformationContext);
80   
81  1 this.mocker.getComponentUnderTest().execute("", Syntax.PLAIN_1_0, DOCUMENT_REFERENCE, context);
82   
83    // The test is here: Verify that the Macro Transformation has been called
84  1 Transformation macroTransformation = this.mocker.getInstance(Transformation.class, "macro");
85  1 verify(macroTransformation).transform(parsedBlocks, transformationContext);
86    }
87   
 
88  1 toggle @Test
89    public void executeWhenTransformationException() throws Exception
90    {
91  1 XDOM parsedBlocks = new XDOM(Collections.emptyList());
92  1 ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
93  1 when(contentParser.parse("", Syntax.PLAIN_1_0)).thenReturn(parsedBlocks);
94   
95  1 TransformationContext transformationContext = new TransformationContext();
96  1 MacroTransformationContext context = new MacroTransformationContext(transformationContext);
97   
98  1 Transformation macroTransformation = this.mocker.getInstance(Transformation.class, "macro");
99  1 doThrow(new TransformationException("error"))
100    .when(macroTransformation).transform(parsedBlocks, transformationContext);
101   
102  1 try {
103  1 this.mocker.getComponentUnderTest().execute("", Syntax.PLAIN_1_0, context);
104  0 fail("Should have raised a ContentExecutorException");
105    } catch (ContentExecutorException expected) {
106  1 assertEquals("Failed to execute content", expected.getMessage());
107    }
108    }
109    }