1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.uiextension

File WikiUIExtensionParametersTest.java

 

Code metrics

0
44
8
1
174
115
8
0.18
5.5
8
1

Classes

Class Line # Actions
WikiUIExtensionParametersTest 54 44 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 6 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.uiextension;
21   
22    import static org.mockito.ArgumentMatchers.any;
23    import static org.mockito.ArgumentMatchers.eq;
24    import static org.mockito.Mockito.mock;
25    import static org.mockito.Mockito.times;
26    import static org.mockito.Mockito.verify;
27    import static org.mockito.Mockito.when;
28   
29    import java.io.StringWriter;
30   
31    import org.apache.commons.collections.MapUtils;
32    import org.apache.velocity.VelocityContext;
33    import org.junit.Assert;
34    import org.junit.Before;
35    import org.junit.Rule;
36    import org.junit.Test;
37    import org.xwiki.context.Execution;
38    import org.xwiki.context.ExecutionContext;
39    import org.xwiki.model.ModelContext;
40    import org.xwiki.model.reference.WikiReference;
41    import org.xwiki.test.LogRule;
42    import org.xwiki.test.mockito.MockitoComponentManagerRule;
43    import org.xwiki.uiextension.internal.WikiUIExtensionParameters;
44    import org.xwiki.velocity.VelocityEngine;
45    import org.xwiki.velocity.VelocityManager;
46    import org.xwiki.velocity.XWikiVelocityException;
47   
48    /**
49    * Unit tests for {@link WikiUIExtensionParametersTest}.
50    *
51    * @version $Id: fc73d8f7cc8239a67c8569f9a9b4257c71ef4d9a $
52    * @since 5.0M1
53    */
 
54    public class WikiUIExtensionParametersTest
55    {
56    private VelocityEngine velocityEngine;
57   
58    private VelocityContext velocityContext;
59   
60    private ModelContext modelContext;
61   
62    private Execution execution;
63   
64    @Rule
 
65  6 toggle public LogRule logRule = new LogRule() {{
66  6 record(LogLevel.WARN);
67  6 recordLoggingForType(WikiUIExtensionParameters.class);
68    }};
69   
70    @Rule
71    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
72   
 
73  6 toggle @Before
74    public void setUp() throws Exception
75    {
76  6 VelocityManager velocityManager = componentManager.registerMockComponent(VelocityManager.class);
77  6 execution = componentManager.registerMockComponent(Execution.class);
78  6 modelContext = componentManager.registerMockComponent(ModelContext.class);
79  6 velocityEngine = mock(VelocityEngine.class);
80  6 velocityContext = new VelocityContext();
81  6 ExecutionContext executionContext = mock(ExecutionContext.class);
82   
83  6 when(execution.getContext()).thenReturn(executionContext);
84  6 when(velocityManager.getVelocityContext()).thenReturn(velocityContext);
85  6 when(velocityManager.getVelocityEngine()).thenReturn(velocityEngine);
86    }
87   
 
88  1 toggle @Test
89    public void getParametersWithAnEmptyParametersProperty() throws Exception
90    {
91  1 when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("xwiki"));
92  1 WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "", componentManager);
93  1 Assert.assertEquals(MapUtils.EMPTY_MAP, parameters.get());
94    }
95   
 
96  1 toggle @Test
97    public void getParametersWithAnEqualSignInAValue() throws Exception
98    {
99  1 when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("xwiki"));
100  1 when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value")))
101    .thenReturn(true);
102  1 WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager);
103   
104    // Since the StringWriter is created within the method, the value is "" and not "value".
105  1 Assert.assertEquals("", parameters.get().get("key"));
106    }
107   
 
108  1 toggle @Test
109    public void getParametersWhenVelocityFails() throws Exception
110    {
111  1 when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("xwiki"));
112  1 when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value")))
113    .thenThrow(new XWikiVelocityException(""));
114  1 WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager);
115   
116    // It should fail and put a warn in the logs
117  1 Assert.assertEquals(null, parameters.get().get("key"));
118  1 Assert.assertTrue(
119    logRule.contains("Failed to evaluate UI extension data value, key [key], value [value]. Reason: []"));
120    }
121   
 
122  1 toggle @Test
123    public void getParametersFromTheSameRequestAndForTheSameWiki() throws Exception
124    {
125  1 when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("xwiki"));
126  1 when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value")))
127    .thenReturn(true);
128  1 WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager);
129   
130    // It should fail silently
131  1 Assert.assertEquals("", parameters.get().get("key"));
132  1 Assert.assertEquals("", parameters.get().get("key"));
133   
134    // Verify the evaluate is done only once
135  1 verify(velocityEngine).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"));
136    }
137   
 
138  1 toggle @Test
139    public void getParametersFromTheSameRequestButForDifferentWikis() throws Exception
140    {
141  1 when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki1"))
142    .thenReturn(new WikiReference("wiki2"));
143  1 when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value")))
144    .thenReturn(true);
145  1 WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager);
146   
147    // It should fail silently
148  1 Assert.assertEquals("", parameters.get().get("key"));
149  1 Assert.assertEquals("", parameters.get().get("key"));
150   
151    // Verify the velocity evaluation has been done for both wikis.
152  1 verify(velocityEngine, times(2)).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"));
153    }
154   
 
155  1 toggle @Test
156    public void getParametersFromDifferentRequests() throws Exception
157    {
158  1 when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki1"));
159  1 when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value")))
160    .thenReturn(true);
161  1 WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager);
162   
163  1 ExecutionContext ec1 = mock(ExecutionContext.class, "ec1");
164  1 ExecutionContext ec2 = mock(ExecutionContext.class, "ec2");
165  1 when(execution.getContext()).thenReturn(ec1).thenReturn(ec2);
166   
167    // It should fail silently
168  1 Assert.assertEquals("", parameters.get().get("key"));
169  1 Assert.assertEquals("", parameters.get().get("key"));
170   
171    // Verify the velocity evaluation has been done for both wikis.
172  1 verify(velocityEngine, times(2)).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"));
173    }
174    }