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

File MethodArgumentUberspectorTest.java

 

Code metrics

6
57
20
3
246
190
24
0.42
2.85
6.67
1.2

Classes

Class Line # Actions
MethodArgumentUberspectorTest 57 45 0% 18 2
0.9682539796.8%
MethodArgumentUberspectorTest.InnerClass 77 1 0% 1 0
1.0100%
MethodArgumentUberspectorTest.ExtendingClass 85 11 0% 5 4
0.777777877.8%
 

Contributing tests

This file is covered by 13 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.velocity.introspection;
21   
22    import java.io.StringReader;
23    import java.io.StringWriter;
24    import java.util.Arrays;
25    import java.util.List;
26    import java.util.Locale;
27    import java.util.Properties;
28   
29    import org.apache.commons.lang.exception.ExceptionUtils;
30    import org.apache.velocity.VelocityContext;
31    import org.junit.After;
32    import org.junit.Before;
33    import org.junit.Rule;
34    import org.junit.Test;
35    import org.xwiki.component.util.DefaultParameterizedType;
36    import org.xwiki.properties.ConverterManager;
37    import org.xwiki.test.annotation.BeforeComponent;
38    import org.xwiki.test.annotation.ComponentList;
39    import org.xwiki.test.mockito.MockitoComponentManagerRule;
40    import org.xwiki.velocity.VelocityEngine;
41    import org.xwiki.velocity.XWikiVelocityException;
42    import org.xwiki.velocity.internal.DefaultVelocityConfiguration;
43    import org.xwiki.velocity.internal.DefaultVelocityContextFactory;
44    import org.xwiki.velocity.internal.DefaultVelocityEngine;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.junit.Assert.fail;
48    import static org.mockito.Mockito.when;
49   
50    /**
51    * Unit tests for {@link org.xwiki.velocity.introspection.MethodArgumentsUberspector}.
52    *
53    * @version $Id: 538194fd815c3458e424857afba710eb5ef2f477 $
54    * @since 6.4M3
55    */
56    @ComponentList({ DefaultVelocityEngine.class, DefaultVelocityConfiguration.class, DefaultVelocityContextFactory.class })
 
57    public class MethodArgumentUberspectorTest
58    {
59    @Rule
60    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
61   
62    private ConverterManager converterManager;
63   
64    private StringWriter writer;
65   
66    private VelocityEngine engine;
67   
68    private VelocityContext context;
69   
 
70  13 toggle @BeforeComponent
71    public void setUpComponents() throws Exception
72    {
73  13 this.componentManager.registerMemoryConfigurationSource();
74  13 this.converterManager = this.componentManager.registerMockComponent(ConverterManager.class);
75    }
76   
 
77    public class InnerClass
78    {
 
79  1 toggle public String method()
80    {
81  1 return "inner";
82    }
83    }
84   
 
85    public class ExtendingClass extends InnerClass
86    {
 
87  2 toggle public String method(List parameter)
88    {
89  2 if (parameter.get(0).equals("converted")) {
90  2 return "success";
91    } else {
92  0 return "failure";
93    }
94    }
95   
 
96  1 toggle public String methodWithGeneric(List<Locale> genericParameter)
97    {
98  1 if (genericParameter.get(0) instanceof Locale) {
99  1 return "success";
100    } else {
101  0 return "failure";
102    }
103    }
104   
 
105  7 toggle public String methodWithVararg(Integer param1, Double... params)
106    {
107  7 StringBuilder builder = new StringBuilder("success");
108  7 for (Double param : params) {
109  8 builder.append(' ');
110  8 builder.append(param);
111    }
112  7 return builder.toString();
113    }
114    }
115   
 
116  13 toggle @Before
117    public void setUp() throws Exception
118    {
119  13 this.engine = this.componentManager.getInstance(VelocityEngine.class);
120  13 this.engine.initialize(new Properties());
121  13 this.writer = new StringWriter();
122  13 this.context = new VelocityContext();
123  13 this.context.put("var", new ExtendingClass());
124    }
125   
 
126  13 toggle @After
127    public void tearDown() throws Exception
128    {
129  13 if (this.writer != null) {
130  13 this.writer.close();
131    }
132    }
133   
 
134  1 toggle @Test
135    public void getMethodWhenVarargsWithNoConversionAndNoVarargParamPassed() throws Exception
136    {
137  1 this.engine.evaluate(this.context, this.writer, "template", new StringReader("$var.methodWithVararg(10)"));
138  1 assertEquals("success", writer.toString());
139    }
140   
 
141  1 toggle @Test
142    public void getMethodWhenVarargsWithNoConversionAndOneVarargParamPassed() throws Exception
143    {
144  1 this.engine.evaluate(this.context, this.writer, "template",
145    new StringReader("$var.methodWithVararg(10, 10.0)"));
146  1 assertEquals("success 10.0", this.writer.toString());
147    }
148   
 
149  1 toggle @Test
150    public void getMethodWhenVarargsWithNoConversionAndTwoVarargParamsPassed() throws Exception
151    {
152  1 this.engine.evaluate(this.context, this.writer, "template",
153    new StringReader("$var.methodWithVararg(10, 10.0, 10.0)"));
154  1 assertEquals("success 10.0 10.0", this.writer.toString());
155    }
156   
 
157  1 toggle @Test
158    public void getMethodWhenVarargsWithConversionAndNoVarargParamPassed() throws Exception
159    {
160  1 when(this.converterManager.convert(Integer.class, "10")).thenReturn(10);
161  1 this.engine.evaluate(this.context, this.writer, "template", new StringReader("$var.methodWithVararg('10')"));
162  1 assertEquals("success", writer.toString());
163    }
164   
 
165  1 toggle @Test
166    public void getMethodWhenVarargsWithConversionAndOneVarargParamPassed() throws Exception
167    {
168  1 when(this.converterManager.convert(Integer.class, "10")).thenReturn(10);
169  1 this.engine.evaluate(this.context, this.writer, "template",
170    new StringReader("$var.methodWithVararg('10', 10.0)"));
171  1 assertEquals("success 10.0", writer.toString());
172    }
173   
 
174  1 toggle @Test
175    public void getMethodWhenVarargsWithConversionAndTwoVarargParamsPassed() throws Exception
176    {
177  1 when(this.converterManager.convert(Integer.class, "10")).thenReturn(10);
178  1 this.engine.evaluate(this.context, this.writer, "template",
179    new StringReader("$var.methodWithVararg('10', 10.0, 10.0)"));
180  1 assertEquals("success 10.0 10.0", writer.toString());
181    }
182   
 
183  1 toggle @Test
184    public void getMethodWhenVarargsWithConversionAndVarargParamPassedNeedingConversion() throws Exception
185    {
186  1 when(this.converterManager.convert(Integer.class, "10")).thenReturn(10);
187  1 when(this.converterManager.convert(Double.class, "10.0")).thenReturn(10.0);
188  1 this.engine.evaluate(this.context, this.writer, "template",
189    new StringReader("$var.methodWithVararg('10', 10.0, '10.0')"));
190  1 assertEquals("success 10.0 10.0", writer.toString());
191    }
192   
193    /**
194    * This used to fail, see <a href="http://jira.xwiki.org/browse/XCOMMONS-710">XCOMMONS-710</a>.
195    */
 
196  1 toggle @Test
197    public void getMethodWhenAddingSameMethodNameToExtendingClassAndConversion() throws Exception
198    {
199  1 when(this.converterManager.convert(List.class, "test")).thenReturn(Arrays.asList("converted"));
200  1 this.engine.evaluate(this.context, this.writer, "template", new StringReader("$var.method('test')"));
201  1 assertEquals("success", this.writer.toString());
202    }
203   
 
204  1 toggle @Test
205    public void getMethodWhenInnerMethodAndNoConversion() throws Exception
206    {
207  1 this.engine.evaluate(this.context, this.writer, "template", new StringReader("$var.method()"));
208  1 assertEquals("inner", this.writer.toString());
209    }
210   
 
211  1 toggle @Test
212    public void getMethodWhenNoConversion() throws Exception
213    {
214  1 this.engine.evaluate(this.context, this.writer, "template", new StringReader("$var.method(['converted'])"));
215  1 assertEquals("success", this.writer.toString());
216    }
217   
 
218  1 toggle @Test
219    public void getMethodWhenNoMatchingMethod() throws Exception
220    {
221  1 this.engine.evaluate(this.context, this.writer, "template", new StringReader("$var.notexisting()"));
222  1 assertEquals("$var.notexisting()", this.writer.toString());
223    }
224   
 
225  1 toggle @Test
226    public void getMethodWhenExistingMethodNameButInvalidSignature() throws Exception
227    {
228  1 try {
229  1 this.engine.evaluate(this.context, this.writer, "template", new StringReader("$var.method('a', 'b')"));
230  0 fail("Should have raised an exception");
231    } catch (XWikiVelocityException expected) {
232  1 assertEquals("Failed to evaluate content with id [template]", expected.getMessage());
233  1 assertEquals("IllegalArgumentException: wrong number of arguments",
234    ExceptionUtils.getRootCauseMessage(expected));
235    }
236    }
237   
 
238  1 toggle @Test
239    public void getMethodWithGeneric() throws Exception
240    {
241  1 when(this.converterManager.convert(new DefaultParameterizedType(null, List.class, Locale.class), "en, fr"))
242    .thenReturn(Arrays.asList(Locale.ENGLISH, Locale.FRENCH));
243  1 this.engine.evaluate(this.context, this.writer, "template", new StringReader("$var.methodWithGeneric('en, fr')"));
244  1 assertEquals("success", this.writer.toString());
245    }
246    }