1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
56 |
|
@ComponentList({ DefaultVelocityEngine.class, DefaultVelocityConfiguration.class, DefaultVelocityContextFactory.class }) |
|
|
| 96.8% |
Uncovered Elements: 2 (63) |
Complexity: 18 |
Complexity Density: 0.4 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
70 |
13 |
@BeforeComponent... |
71 |
|
public void setUpComponents() throws Exception |
72 |
|
{ |
73 |
13 |
this.componentManager.registerMemoryConfigurationSource(); |
74 |
13 |
this.converterManager = this.componentManager.registerMockComponent(ConverterManager.class); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
77 |
|
public class InnerClass |
78 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
1 |
public String method()... |
80 |
|
{ |
81 |
1 |
return "inner"; |
82 |
|
} |
83 |
|
} |
84 |
|
|
|
|
| 77.8% |
Uncovered Elements: 4 (18) |
Complexity: 5 |
Complexity Density: 0.45 |
|
85 |
|
public class ExtendingClass extends InnerClass |
86 |
|
{ |
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
87 |
2 |
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 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
96 |
1 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
105 |
7 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
116 |
13 |
@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 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
126 |
13 |
@After... |
127 |
|
public void tearDown() throws Exception |
128 |
|
{ |
129 |
13 |
if (this.writer != null) { |
130 |
13 |
this.writer.close(); |
131 |
|
} |
132 |
|
} |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
134 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
141 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
149 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
157 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
165 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
174 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
183 |
1 |
@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 |
|
|
195 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
196 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
204 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
211 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
218 |
1 |
@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 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
225 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
238 |
1 |
@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 |
|
} |