|
40 | 40 |
|
41 | 41 | import static org.assertj.core.api.Assertions.assertThat;
|
42 | 42 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 43 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 44 | +import static org.assertj.core.api.InstanceOfAssertFactories.array; |
43 | 45 | import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.CLOSE;
|
44 | 46 | import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.EXACT;
|
45 | 47 | import static org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMatchKind.REQUIRES_CONVERSION;
|
@@ -252,14 +254,75 @@ void convertAllArguments() throws Exception {
|
252 | 254 |
|
253 | 255 | @Test
|
254 | 256 | void setupArgumentsForVarargsInvocation() {
|
255 |
| - Object[] newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
256 |
| - new Class<?>[] {String[].class}, "a", "b", "c"); |
| 257 | + Object[] newArray; |
257 | 258 |
|
258 |
| - assertThat(newArray).hasSize(1); |
259 |
| - Object firstParam = newArray[0]; |
260 |
| - assertThat(firstParam.getClass().componentType()).isEqualTo(String.class); |
261 |
| - Object[] firstParamArray = (Object[]) firstParam; |
262 |
| - assertThat(firstParamArray).containsExactly("a", "b", "c"); |
| 259 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 260 | + new Class<?>[] {String[].class}, "a", "b", "c"); |
| 261 | + assertThat(newArray) |
| 262 | + .singleElement() |
| 263 | + .asInstanceOf(array(String[].class)) |
| 264 | + .containsExactly("a", "b", "c"); |
| 265 | + |
| 266 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 267 | + new Class<?>[] { Object[].class }, "a", "b", "c"); |
| 268 | + assertThat(newArray) |
| 269 | + .singleElement() |
| 270 | + .asInstanceOf(array(Object[].class)) |
| 271 | + .containsExactly("a", "b", "c"); |
| 272 | + |
| 273 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 274 | + new Class<?>[] { Integer.class, Integer.class, String[].class }, 123, 456, "a", "b", "c"); |
| 275 | + assertThat(newArray) |
| 276 | + .satisfiesExactly( |
| 277 | + i -> assertThat(i).isEqualTo(123), |
| 278 | + i -> assertThat(i).isEqualTo(456), |
| 279 | + i -> assertThat(i).asInstanceOf(array(String[].class)).containsExactly("a", "b", "c")); |
| 280 | + |
| 281 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 282 | + new Class<?>[] { String[].class }); |
| 283 | + assertThat(newArray) |
| 284 | + .singleElement() |
| 285 | + .asInstanceOf(array(String[].class)) |
| 286 | + .isEmpty(); |
| 287 | + |
| 288 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 289 | + new Class<?>[] { String[].class }, new Object[] { new String[] { "a", "b", "c" } }); |
| 290 | + assertThat(newArray) |
| 291 | + .singleElement() |
| 292 | + .asInstanceOf(array(String[].class)) |
| 293 | + .containsExactly("a", "b", "c"); |
| 294 | + |
| 295 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 296 | + new Class<?>[] { Object[].class }, new Object[] { new String[] { "a", "b", "c" } }); |
| 297 | + assertThat(newArray) |
| 298 | + .singleElement() |
| 299 | + .asInstanceOf(array(Object[].class)) |
| 300 | + .containsExactly("a", "b", "c"); |
| 301 | + |
| 302 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 303 | + new Class<?>[] { String[].class }, "a"); |
| 304 | + assertThat(newArray) |
| 305 | + .singleElement() |
| 306 | + .asInstanceOf(array(String[].class)) |
| 307 | + .containsExactly("a"); |
| 308 | + |
| 309 | + |
| 310 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 311 | + new Class<?>[] { String[].class }, new Object[]{null}); |
| 312 | + assertThat(newArray) |
| 313 | + .singleElement() |
| 314 | + .asInstanceOf(array(String[].class)) |
| 315 | + .singleElement() |
| 316 | + .isNull(); |
| 317 | + |
| 318 | + assertThatThrownBy(() -> ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 319 | + new Class<?>[] { Integer.class, Integer.class }, 123, 456)) |
| 320 | + .isInstanceOf(IllegalArgumentException.class) |
| 321 | + .hasMessage("Method must be varargs"); |
| 322 | + |
| 323 | + assertThatThrownBy(() -> ReflectionHelper.setupArgumentsForVarargsInvocation(new Class[] {}, "a", "b", "c")) |
| 324 | + .isInstanceOf(IllegalArgumentException.class) |
| 325 | + .hasMessage("Required parameter types must not be empty"); |
263 | 326 | }
|
264 | 327 |
|
265 | 328 | @Test
|
|
0 commit comments