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

File DashboardMacroTest.java

 

Code metrics

0
27
2
1
104
65
3
0.11
13.5
2
1.5

Classes

Class Line # Actions
DashboardMacroTest 50 27 0% 3 1
0.965517296.6%
 

Contributing tests

This file is covered by 2 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.dashboard;
21   
22    import java.util.Collections;
23    import java.util.List;
24   
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.context.Execution;
28    import org.xwiki.context.ExecutionContext;
29    import org.xwiki.properties.BeanDescriptor;
30    import org.xwiki.properties.BeanManager;
31    import org.xwiki.rendering.block.Block;
32    import org.xwiki.rendering.macro.MacroExecutionException;
33    import org.xwiki.rendering.macro.dashboard.DashboardMacroParameters;
34    import org.xwiki.rendering.macro.dashboard.DashboardRenderer;
35    import org.xwiki.rendering.macro.dashboard.GadgetRenderer;
36    import org.xwiki.rendering.transformation.MacroTransformationContext;
37    import org.xwiki.test.mockito.MockitoComponentMockingRule;
38   
39    import static junit.framework.TestCase.fail;
40    import static org.junit.Assert.assertEquals;
41    import static org.mockito.ArgumentMatchers.any;
42    import static org.mockito.Mockito.mock;
43    import static org.mockito.Mockito.when;
44   
45    /**
46    * Unit tests for {@link DashboardMacro}.
47    *
48    * @version $Id: 28c5b21d1c8a182d191fb3434c211712d4170a7d $
49    */
 
50    public class DashboardMacroTest
51    {
52    @Rule
53    public MockitoComponentMockingRule<DashboardMacro> mocker =
54    new MockitoComponentMockingRule<>(DashboardMacro.class);
55   
 
56  1 toggle @Test
57    public void executeWhenInsideDashboardMacro() throws Exception
58    {
59  1 BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
60  1 BeanDescriptor descriptor = mock(BeanDescriptor.class);
61  1 when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
62  1 when(descriptor.getProperties()).thenReturn(Collections.emptyList());
63   
64  1 Execution execution = this.mocker.getInstance(Execution.class);
65  1 ExecutionContext ec = new ExecutionContext();
66  1 when(execution.getContext()).thenReturn(ec);
67  1 ec.setProperty("dashboardMacroCalls", 1);
68   
69  1 DashboardMacroParameters parameters = new DashboardMacroParameters();
70  1 MacroTransformationContext macroContext = new MacroTransformationContext();
71   
72  1 try {
73  1 this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
74  0 fail("Exception should have been raised here");
75    } catch (MacroExecutionException expected) {
76  1 assertEquals("Dashboard macro recursion detected. Don't call the Dashboard macro inside of itself...",
77    expected.getMessage());
78    }
79    }
80   
 
81  1 toggle @Test
82    public void executeWhenNotInsideDashboardMacro() throws Exception
83    {
84  1 BeanManager beanManager = this.mocker.getInstance(BeanManager.class);
85  1 BeanDescriptor descriptor = mock(BeanDescriptor.class);
86  1 when(beanManager.getBeanDescriptor(any())).thenReturn(descriptor);
87  1 when(descriptor.getProperties()).thenReturn(Collections.emptyList());
88   
89  1 DashboardRenderer renderer = this.mocker.registerMockComponent(DashboardRenderer.class, "columns");
90   
91  1 GadgetRenderer gadgetRenderer = this.mocker.registerMockComponent(GadgetRenderer.class);
92   
93  1 Execution execution = this.mocker.getInstance(Execution.class);
94  1 ExecutionContext ec = new ExecutionContext();
95  1 when(execution.getContext()).thenReturn(ec);
96   
97  1 DashboardMacroParameters parameters = new DashboardMacroParameters();
98  1 MacroTransformationContext macroContext = new MacroTransformationContext();
99  1 this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
100   
101    // We verify that the counter ends up at 0 so that calls to subsequent dashboard macros can succeed.
102  1 assertEquals(0, ec.getProperty("dashboardMacroCalls"));
103    }
104    }