|
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.assertThatIllegalArgumentException; |
| 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;
|
@@ -250,16 +252,73 @@ void convertAllArguments() throws Exception {
|
250 | 252 | checkArguments(args, "3", null, "3.0");
|
251 | 253 | }
|
252 | 254 |
|
| 255 | + @Test |
| 256 | + void setupArgumentsForVarargsInvocationPreconditions() { |
| 257 | + assertThatIllegalArgumentException() |
| 258 | + .isThrownBy(() -> ReflectionHelper.setupArgumentsForVarargsInvocation(new Class[] {}, "a")) |
| 259 | + .withMessage("Required parameter types array must not be empty"); |
| 260 | + |
| 261 | + assertThatIllegalArgumentException() |
| 262 | + .isThrownBy(() -> ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 263 | + new Class<?>[] { Integer.class, Integer.class }, 123)) |
| 264 | + .withMessage("The last required parameter type must be an array to support varargs invocation"); |
| 265 | + } |
| 266 | + |
253 | 267 | @Test
|
254 | 268 | void setupArgumentsForVarargsInvocation() {
|
255 |
| - Object[] newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
256 |
| - new Class<?>[] {String[].class}, "a", "b", "c"); |
257 |
| - |
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"); |
| 269 | + Object[] newArray; |
| 270 | + |
| 271 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class<?>[] { String[].class }, "a", "b", "c"); |
| 272 | + assertThat(newArray) |
| 273 | + .singleElement() |
| 274 | + .asInstanceOf(array(String[].class)) |
| 275 | + .containsExactly("a", "b", "c"); |
| 276 | + |
| 277 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class<?>[] { Object[].class }, "a", "b", "c"); |
| 278 | + assertThat(newArray) |
| 279 | + .singleElement() |
| 280 | + .asInstanceOf(array(Object[].class)) |
| 281 | + .containsExactly("a", "b", "c"); |
| 282 | + |
| 283 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 284 | + new Class<?>[] { Integer.class, Integer.class, String[].class }, 123, 456, "a", "b", "c"); |
| 285 | + assertThat(newArray).satisfiesExactly( |
| 286 | + one -> assertThat(one).isEqualTo(123), |
| 287 | + two -> assertThat(two).isEqualTo(456), |
| 288 | + three -> assertThat(three).asInstanceOf(array(String[].class)).containsExactly("a", "b", "c")); |
| 289 | + |
| 290 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class<?>[] { String[].class }); |
| 291 | + assertThat(newArray) |
| 292 | + .singleElement() |
| 293 | + .asInstanceOf(array(String[].class)) |
| 294 | + .isEmpty(); |
| 295 | + |
| 296 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 297 | + new Class<?>[] { String[].class }, new Object[] { new String[] { "a", "b", "c" } }); |
| 298 | + assertThat(newArray) |
| 299 | + .singleElement() |
| 300 | + .asInstanceOf(array(String[].class)) |
| 301 | + .containsExactly("a", "b", "c"); |
| 302 | + |
| 303 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation( |
| 304 | + new Class<?>[] { Object[].class }, new Object[] { new String[] { "a", "b", "c" } }); |
| 305 | + assertThat(newArray) |
| 306 | + .singleElement() |
| 307 | + .asInstanceOf(array(Object[].class)) |
| 308 | + .containsExactly("a", "b", "c"); |
| 309 | + |
| 310 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class<?>[] { String[].class }, "a"); |
| 311 | + assertThat(newArray) |
| 312 | + .singleElement() |
| 313 | + .asInstanceOf(array(String[].class)) |
| 314 | + .containsExactly("a"); |
| 315 | + |
| 316 | + newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(new Class<?>[] { String[].class }, new Object[] { null }); |
| 317 | + assertThat(newArray) |
| 318 | + .singleElement() |
| 319 | + .asInstanceOf(array(String[].class)) |
| 320 | + .singleElement() |
| 321 | + .isNull(); |
263 | 322 | }
|
264 | 323 |
|
265 | 324 | @Test
|
|
0 commit comments